wip: Implementing external function api

This commit is contained in:
Ilyong Cho
2015-04-03 18:11:21 +09:00
parent 7b5764c445
commit 23ade8f4ff
10 changed files with 183 additions and 22 deletions
@@ -25,6 +25,7 @@
#include "ecma-objects-general.h"
#include "ecma-try-catch-macro.h"
#include "jrt.h"
#include "jerry-api.h"
/** \addtogroup ecma ECMA
* @{
@@ -78,6 +79,51 @@ ecma_unpack_code_internal_property_value (uint32_t value, /**< packed value */
return opcode_idx;
} /* ecma_unpack_code_internal_property_value */
/**
* Pack the pointer to external handler to value
* that can be stored in an [[Code]] internal property.
*
* @return packed value
*/
static uint32_t
ecma_pack_external_code_internal_property_value (jerry_external_handler_t handler)
{
uint32_t value;
if (sizeof (jerry_external_handler_t) == sizeof (uint32_t))
{
value = static_cast <uint32_t> (reinterpret_cast <uintptr_t> (handler));
}
else
{
ecma_external_pointer_t *handler_p = ecma_alloc_external_pointer ();
*handler_p = reinterpret_cast <ecma_external_pointer_t> (handler);
ECMA_SET_POINTER (value, handler_p);
}
return value;
} /* ecma_pack_external_code_internal_property_value */
/**
* Unpack 'is_strict' flag and opcode index from value
* that can be stored in an [[Code]] internal property.
*
* @return opcode index
*/
static jerry_external_handler_t
ecma_unpack_external_code_internal_property_value (uint32_t value) /**< packed value */
{
jerry_external_handler_t handler;
if (sizeof (ecma_external_pointer_t) == sizeof (uint32_t))
{
handler = reinterpret_cast <jerry_external_handler_t> (static_cast <uintptr_t> (value));
}
else
{
ecma_external_pointer_t* handler_p = ECMA_GET_POINTER (ecma_external_pointer_t, value);
handler = reinterpret_cast <jerry_external_handler_t> (*handler_p);
}
return handler;
} /* ecma_unpack_code_internal_property_value */
/**
* IsCallable operation.
*
@@ -139,12 +185,21 @@ ecma_op_create_function_object (ecma_string_t* formal_parameter_list_p[], /**< f
ecma_length_t formal_parameters_number, /**< formal parameters list's length */
ecma_object_t *scope_p, /**< function's scope */
bool is_strict, /**< 'strict' flag */
opcode_counter_t first_opcode_idx) /**< index of first opcode of function's body */
bool is_external, /**< true if the function is an external */
opcode_counter_t first_opcode_idx, /**< index of first opcode of function's body */
jerry_external_handler_t external_handler) /**< external handler */
{
JERRY_ASSERT (!is_external || external_handler != NULL);
// 1., 4., 13.
ecma_object_t *prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_FUNCTION_PROTOTYPE);
ecma_object_t *f = ecma_create_object (prototype_obj_p, true, ECMA_OBJECT_TYPE_FUNCTION);
ecma_object_type_t object_type = ECMA_OBJECT_TYPE_FUNCTION;
if (is_external)
{
object_type = ECMA_OBJECT_TYPE_EXTERNAL_FUNCTION;
}
ecma_object_t *f = ecma_create_object (prototype_obj_p, true, object_type);
ecma_deref_object (prototype_obj_p);
@@ -179,8 +234,16 @@ ecma_op_create_function_object (ecma_string_t* formal_parameter_list_p[], /**< f
// 12.
ecma_property_t *code_prop_p = ecma_create_internal_property (f, ECMA_INTERNAL_PROPERTY_CODE);
code_prop_p->u.internal_property.value = ecma_pack_code_internal_property_value (is_strict,
first_opcode_idx);
if (is_external)
{
code_prop_p->u.internal_property.value = ecma_pack_external_code_internal_property_value (external_handler);
}
else
{
code_prop_p->u.internal_property.value = ecma_pack_code_internal_property_value (is_strict,
first_opcode_idx);
}
// 14.
ecma_number_t* len_p = ecma_alloc_number ();
@@ -467,7 +530,8 @@ ecma_op_function_call (ecma_object_t *func_obj_p, /**< Function object */
ecma_completion_value_t ret_value;
if (ecma_get_object_type (func_obj_p) == ECMA_OBJECT_TYPE_FUNCTION)
if (ecma_get_object_type (func_obj_p) == ECMA_OBJECT_TYPE_FUNCTION
|| ecma_get_object_type (func_obj_p) == ECMA_OBJECT_TYPE_EXTERNAL_FUNCTION)
{
if (unlikely (ecma_get_object_is_builtin (func_obj_p)))
{
@@ -480,15 +544,24 @@ ecma_op_function_call (ecma_object_t *func_obj_p, /**< Function object */
{
/* Entering Function Code (ECMA-262 v5, 10.4.3) */
ecma_property_t *scope_prop_p = ecma_get_internal_property (func_obj_p, ECMA_INTERNAL_PROPERTY_SCOPE);
ecma_property_t *code_prop_p = ecma_get_internal_property (func_obj_p, ECMA_INTERNAL_PROPERTY_CODE);
ecma_object_t *scope_p = ECMA_GET_NON_NULL_POINTER (ecma_object_t,
scope_prop_p->u.internal_property.value);
// 8.
bool is_strict;
opcode_counter_t code_first_opcode_idx = 0;
ecma_property_t *code_prop_p = ecma_get_internal_property (func_obj_p, ECMA_INTERNAL_PROPERTY_CODE);
uint32_t code_prop_value = code_prop_p->u.internal_property.value;
bool is_strict;
// 8.
opcode_counter_t code_first_opcode_idx = ecma_unpack_code_internal_property_value (code_prop_value, &is_strict);
if (ecma_get_object_type (func_obj_p) == ECMA_OBJECT_TYPE_FUNCTION)
{
code_first_opcode_idx = ecma_unpack_code_internal_property_value (code_prop_value, &is_strict);
}
else /* ecma_get_object_type (func_obj_p) == ECMA_OBJECT_TYPE_EXTERNAL_FUNCTION */
{
is_strict = false;
}
ecma_value_t this_binding;
// 1.
@@ -514,7 +587,7 @@ ecma_op_function_call (ecma_object_t *func_obj_p, /**< Function object */
// 5.
ecma_object_t *local_env_p = ecma_create_decl_lex_env (scope_p);
// 9.
ECMA_TRY_CATCH (args_var_declaration_ret,
ecma_function_call_setup_args_variables (func_obj_p,
local_env_p,
@@ -523,18 +596,29 @@ ecma_op_function_call (ecma_object_t *func_obj_p, /**< Function object */
is_strict),
ret_value);
ecma_completion_value_t completion = run_int_from_pos (code_first_opcode_idx,
this_binding,
local_env_p,
is_strict,
false);
if (ecma_is_completion_value_return (completion))
// 9.
if (ecma_get_object_type (func_obj_p) == ECMA_OBJECT_TYPE_FUNCTION)
{
ret_value = ecma_make_normal_completion_value (ecma_get_completion_value_value (completion));
ecma_completion_value_t completion = run_int_from_pos (code_first_opcode_idx,
this_binding,
local_env_p,
is_strict,
false);
if (ecma_is_completion_value_return (completion))
{
ret_value = ecma_make_normal_completion_value (ecma_get_completion_value_value (completion));
}
else
{
ret_value = completion;
}
}
else
{
ret_value = completion;
jerry_external_handler_t handler = ecma_unpack_external_code_internal_property_value (code_prop_value);
handler (NULL, NULL, 0, NULL);
ret_value = 0;
}
ECMA_FINALIZE (args_var_declaration_ret);
@@ -681,7 +765,9 @@ ecma_op_function_declaration (ecma_object_t *lex_env_p, /**< lexical environment
formal_parameter_list_length,
lex_env_p,
is_strict,
function_code_opcode_idx);
false,
function_code_opcode_idx,
0);
// c.
bool func_already_declared = ecma_op_has_binding (lex_env_p, function_name_p);