273 lines
7.1 KiB
C++

/* Copyright 2015 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "deserializer.h"
#include "ecma-extension.h"
#include "ecma-gc.h"
#include "ecma-helpers.h"
#include "ecma-init-finalize.h"
#include "jerry.h"
#include "jrt.h"
#include "parser.h"
#include "serializer.h"
#include "vm.h"
/**
* Jerry engine build date
*/
const char *jerry_build_date = JERRY_BUILD_DATE;
/**
* Jerry engine build commit hash
*/
const char *jerry_commit_hash = JERRY_COMMIT_HASH;
/**
* Jerry engine build branch name
*/
const char *jerry_branch_name = JERRY_BRANCH_NAME;
/**
* Jerry run-time configuration flags
*/
static jerry_flag_t jerry_flags;
/** \addtogroup jerry Jerry engine extension interface
* @{
*/
/**
* Buffer of character data (used for exchange between core and extensions' routines)
*/
char jerry_extension_characters_buffer [CONFIG_EXTENSION_CHAR_BUFFER_SIZE];
/**
* Extend Global scope with specified extension object
*
* After extension the object is accessible through non-configurable property
* with name equal to builtin_object_name converted to ecma chars.
*/
bool
jerry_extend_with (jerry_extension_descriptor_t *desc_p) /**< description of the extension object */
{
return ecma_extension_register (desc_p);
} /* jerry_extend_with */
/**
* Copy string characters to specified buffer, append zero character at end of the buffer.
*
* @return number of bytes, actually copied to the buffer - if string's content was copied successfully;
* otherwise (in case size of buffer is insuficcient) - negative number, which is calculated
* as negation of buffer size, that is required to hold the string's content.
*/
ssize_t
jerry_string_to_char_buffer (const jerry_string_t *string_p, /**< string descriptor */
char *buffer_p, /**< output characters buffer */
ssize_t buffer_size) /**< size of output buffer */
{
return ecma_string_to_zt_string (string_p, (ecma_char_t*) buffer_p, buffer_size);
} /* jerry_string_to_char_buffer */
/**
* Acquire string pointer for usage outside of the engine
*
* Warning:
* acquired pointer should be released with jerry_release_string
*
* @return pointer that may be used outside of the engine
*/
jerry_string_t*
jerry_acquire_string (jerry_string_t *string_p) /**< pointer passed to function */
{
return ecma_copy_or_ref_ecma_string (string_p);
} /* jerry_acquire_string */
/**
* Release string pointer acquired through jerry_acquire_string.
*/
void
jerry_release_string (jerry_string_t *string_p) /**< pointer acquired through jerry_acquire_string */
{
ecma_deref_ecma_string (string_p);
} /* jerry_release_string */
/**
* Acquire object pointer for usage outside of the engine
*
* Warning:
* acquired pointer should be released with jerry_release_object
*
* @return pointer that may be used outside of the engine
*/
jerry_object_t*
jerry_acquire_object (jerry_object_t *object_p) /**< pointer passed to function */
{
ecma_ref_object (object_p);
return object_p;
} /* jerry_acquire_object */
/**
* Release object pointer acquired through jerry_acquire_object.
*/
void
jerry_release_object (jerry_object_t *object_p) /**< pointer acquired through jerry_acquire_object */
{
ecma_deref_object (object_p);
} /* jerry_release_object */
/**
* @}
*/
/**
* Jerry engine initialization
*/
void
jerry_init (jerry_flag_t flags) /**< combination of Jerry flags */
{
jerry_flags = flags;
mem_init ();
deserializer_init ();
ecma_init ();
} /* jerry_init */
/**
* Terminate Jerry engine
*
* Warning:
* All contexts should be freed with jerry_cleanup_ctx
* before calling the cleanup routine.
*/
void
jerry_cleanup (void)
{
bool is_show_mem_stats = ((jerry_flags & JERRY_FLAG_MEM_STATS) != 0);
ecma_finalize ();
deserializer_free ();
mem_finalize (is_show_mem_stats);
} /* jerry_cleanup */
/**
* Get Jerry configured memory limits
*/
void
jerry_get_memory_limits (size_t *out_data_bss_brk_limit_p, /**< out: Jerry's maximum usage of
* data + bss + brk sections */
size_t *out_stack_limit_p) /**< out: Jerry's maximum usage of stack */
{
*out_data_bss_brk_limit_p = CONFIG_MEM_HEAP_AREA_SIZE + CONFIG_MEM_DATA_LIMIT_MINUS_HEAP_SIZE;
*out_stack_limit_p = CONFIG_MEM_STACK_LIMIT;
} /* jerry_get_memory_limits */
/**
* Register Jerry's fatal error callback
*/
void
jerry_reg_err_callback (jerry_error_callback_t callback) /**< pointer to callback function */
{
JERRY_UNIMPLEMENTED_REF_UNUSED_VARS ("Error callback is not implemented", callback);
} /* jerry_reg_err_callback */
/**
* Allocate new run context
*/
jerry_ctx_t*
jerry_new_ctx (void)
{
JERRY_UNIMPLEMENTED ("Run contexts are not implemented");
} /* jerry_new_ctx */
/**
* Cleanup resources associated with specified run context
*/
void
jerry_cleanup_ctx (jerry_ctx_t* ctx_p) /**< run context */
{
JERRY_UNIMPLEMENTED_REF_UNUSED_VARS ("Run contexts are not implemented", ctx_p);
} /* jerry_cleanup_ctx */
/**
* Parse script for specified context
*/
bool
jerry_parse (jerry_ctx_t* ctx_p, /**< run context */
const char* source_p, /**< script source */
size_t source_size) /**< script source size */
{
/* FIXME: Remove after implementation of run contexts */
(void) ctx_p;
bool is_show_opcodes = ((jerry_flags & JERRY_FLAG_SHOW_OPCODES) != 0);
parser_init (source_p, source_size, is_show_opcodes);
parser_parse_program ();
const opcode_t* opcodes = (const opcode_t*) deserialize_bytecode ();
serializer_print_opcodes ();
parser_free ();
bool is_show_mem_stats = ((jerry_flags & JERRY_FLAG_MEM_STATS) != 0);
init_int (opcodes, is_show_mem_stats);
return true;
} /* jerry_parse */
/**
* Run Jerry in specified run context
*/
jerry_completion_code_t
jerry_run (jerry_ctx_t* ctx_p) /**< run context */
{
/* FIXME: Remove after implementation of run contexts */
(void) ctx_p;
return run_int ();
} /* jerry_run */
/**
* Simple jerry runner
*/
jerry_completion_code_t
jerry_run_simple (const char *script_source, /**< script source */
size_t script_source_size, /**< script source size */
jerry_flag_t flags) /**< combination of Jerry flags */
{
jerry_init (flags);
jerry_completion_code_t ret_code = JERRY_COMPLETION_CODE_OK;
if (!jerry_parse (NULL, script_source, script_source_size))
{
/* unhandled SyntaxError */
ret_code = JERRY_COMPLETION_CODE_UNHANDLED_EXCEPTION;
}
else
{
if ((flags & JERRY_FLAG_PARSE_ONLY) == 0)
{
ret_code = jerry_run (NULL);
}
}
jerry_cleanup ();
return ret_code;
} /* jerry_run_simple */