diff --git a/jerry-core/ecma/operations/ecma-function-object.cpp b/jerry-core/ecma/operations/ecma-function-object.cpp index 034f4bfcc2..1a8918a371 100644 --- a/jerry-core/ecma/operations/ecma-function-object.cpp +++ b/jerry-core/ecma/operations/ecma-function-object.cpp @@ -23,6 +23,7 @@ #include "ecma-lex-env.h" #include "ecma-objects.h" #include "ecma-objects-general.h" +#include "ecma-objects-arguments.h" #include "ecma-try-catch-macro.h" #include "jrt.h" @@ -37,48 +38,60 @@ */ /** - * Pack 'is_strict' flag and opcode index to value + * Pack 'is_strict', 'do_instantiate_arguments_object' flags and opcode index to value * that can be stored in an [[Code]] internal property. * * @return packed value */ static uint32_t ecma_pack_code_internal_property_value (bool is_strict, /**< is code strict? */ + bool do_instantiate_args_obj, /**< should an Arguments object be + * instantiated for the code */ opcode_counter_t opcode_idx) /**< index of first opcode */ { uint32_t value = opcode_idx; const uint32_t is_strict_bit_offset = (uint32_t) (sizeof (value) * JERRY_BITSINBYTE - 1); + const uint32_t do_instantiate_arguments_object_bit_offset = (uint32_t) (sizeof (value) * JERRY_BITSINBYTE - 2); JERRY_ASSERT (((value) & (1u << is_strict_bit_offset)) == 0); + JERRY_ASSERT (((value) & (1u << do_instantiate_arguments_object_bit_offset)) == 0); if (is_strict) { value |= (1u << is_strict_bit_offset); } + if (do_instantiate_args_obj) + { + value |= (1u << do_instantiate_arguments_object_bit_offset); + } + return value; } /* ecma_pack_code_internal_property_value */ /** - * Unpack 'is_strict' flag and opcode index from value + * Unpack 'is_strict', 'do_instantiate_arguments_object' flags and opcode index from value * that can be stored in an [[Code]] internal property. * * @return opcode index */ static opcode_counter_t ecma_unpack_code_internal_property_value (uint32_t value, /**< packed value */ - bool* out_is_strict_p) /**< out: is code strict? */ + bool* out_is_strict_p, /**< out: is code strict? */ + bool* out_do_instantiate_args_obj_p) /**< should an Arguments object be + * instantiated for the code */ { JERRY_ASSERT (out_is_strict_p != NULL); + JERRY_ASSERT (out_do_instantiate_args_obj_p != NULL); const uint32_t is_strict_bit_offset = (uint32_t) (sizeof (value) * JERRY_BITSINBYTE - 1); + const uint32_t do_instantiate_arguments_object_bit_offset = (uint32_t) (sizeof (value) * JERRY_BITSINBYTE - 2); - bool is_strict = ((value & (1u << is_strict_bit_offset)) != 0); - *out_is_strict_p = is_strict; + *out_is_strict_p = ((value & (1u << is_strict_bit_offset)) != 0); + *out_do_instantiate_args_obj_p = ((value & (1u << do_instantiate_arguments_object_bit_offset)) != 0); + value &= ~((1u << is_strict_bit_offset) | (1u << do_instantiate_arguments_object_bit_offset)); - opcode_counter_t opcode_idx = (opcode_counter_t) (value & ~(1u << is_strict_bit_offset)); - - return opcode_idx; + return (opcode_counter_t) value; } /* ecma_unpack_code_internal_property_value */ /** @@ -144,6 +157,8 @@ 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 */ + bool do_instantiate_arguments_object, /**< should an Arguments object be instantiated + * for the function object upon call */ opcode_counter_t first_opcode_idx) /**< index of first opcode of function's body */ { // 1., 4., 13. @@ -185,6 +200,7 @@ 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, + do_instantiate_arguments_object, first_opcode_idx); // 14. @@ -339,10 +355,11 @@ ecma_op_create_external_function_object (ecma_external_pointer_t code_p) /**< po } /* ecma_op_create_external_function_object */ /** - * Setup variables for arguments listed in formal parameter list. + * Setup variables for arguments listed in formal parameter list, + * and, if necessary, Arguments object with 'arguments' binding. * * See also: - * Declaration binding instantiation (ECMA-262 v5, 10.5), block 4 + * Declaration binding instantiation (ECMA-262 v5, 10.5), block 4 and 7 * * @return completion value * Returned value must be freed with ecma_free_completion_value @@ -352,7 +369,10 @@ ecma_function_call_setup_args_variables (ecma_object_t *func_obj_p, /**< Functio ecma_object_t *env_p, /**< lexical environment */ const ecma_value_t *arguments_list_p, /**< arguments list */ ecma_length_t arguments_list_len, /**< length of argument list */ - bool is_strict) /**< flag indicating strict mode */ + bool is_strict, /**< flag indicating strict mode */ + bool do_instantiate_arguments_object) /**< flag indicating whether + * Arguments object should be + * instantiated */ { ecma_property_t *formal_parameters_prop_p = ecma_get_internal_property (func_obj_p, ECMA_INTERNAL_PROPERTY_FORMAL_PARAMETERS); @@ -360,61 +380,113 @@ ecma_function_call_setup_args_variables (ecma_object_t *func_obj_p, /**< Functio formal_parameters_p = ECMA_GET_POINTER (ecma_collection_header_t, formal_parameters_prop_p->u.internal_property.value); - if (formal_parameters_p == NULL) + if (formal_parameters_p != NULL) { - return ecma_make_empty_completion_value (); - } + ecma_length_t formal_parameters_count = formal_parameters_p->unit_number; - ecma_length_t formal_parameters_count = formal_parameters_p->unit_number; + ecma_collection_iterator_t formal_params_iterator; + ecma_collection_iterator_init (&formal_params_iterator, formal_parameters_p); - ecma_collection_iterator_t formal_params_iterator; - ecma_collection_iterator_init (&formal_params_iterator, formal_parameters_p); - - for (size_t n = 0; - n < formal_parameters_count; - n++) - { - ecma_value_t v; - if (n >= arguments_list_len) - { - v = ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED); - } - else + for (size_t n = 0; + n < formal_parameters_count; + n++) { - v = arguments_list_p[n]; - } + ecma_value_t v; + if (n >= arguments_list_len) + { + v = ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED); + } + else + { + v = arguments_list_p[n]; + } - bool is_moved = ecma_collection_iterator_next (&formal_params_iterator); - JERRY_ASSERT (is_moved); + bool is_moved = ecma_collection_iterator_next (&formal_params_iterator); + JERRY_ASSERT (is_moved); - ecma_value_t formal_parameter_name_value = *formal_params_iterator.current_value_p; - ecma_string_t *formal_parameter_name_string_p = ecma_get_string_from_value (formal_parameter_name_value); + ecma_value_t formal_parameter_name_value = *formal_params_iterator.current_value_p; + ecma_string_t *formal_parameter_name_string_p = ecma_get_string_from_value (formal_parameter_name_value); - bool arg_already_declared = ecma_op_has_binding (env_p, formal_parameter_name_string_p); - if (!arg_already_declared) - { - ecma_completion_value_t completion = ecma_op_create_mutable_binding (env_p, - formal_parameter_name_string_p, - false); - if (ecma_is_completion_value_throw (completion)) + bool arg_already_declared = ecma_op_has_binding (env_p, formal_parameter_name_string_p); + if (!arg_already_declared) { - return completion; - } + ecma_completion_value_t completion = ecma_op_create_mutable_binding (env_p, + formal_parameter_name_string_p, + false); + if (ecma_is_completion_value_throw (completion)) + { + return completion; + } + + JERRY_ASSERT (ecma_is_completion_value_empty (completion)); - JERRY_ASSERT (ecma_is_completion_value_empty (completion)); + completion = ecma_op_set_mutable_binding (env_p, + formal_parameter_name_string_p, + v, + is_strict); + + if (ecma_is_completion_value_throw (completion)) + { + return completion; + } + + JERRY_ASSERT (ecma_is_completion_value_empty (completion)); + } + } + } - completion = ecma_op_set_mutable_binding (env_p, - formal_parameter_name_string_p, - v, - is_strict); + if (do_instantiate_arguments_object) + { + /* + * According to ECMA-262 v5, 10.5, the Arguments object should be instantiated + * after instantiating declared functions, and only if there is no binding named 'arguments' + * by that time. + * + * However, we can setup Arguments object and 'arguments' binding here, because: + * - instantiation of Arguments object itself doesn't have any side effects; + * - if 'arguments' is name of a declared function in current scope, + * value of the binding would be overwritten, execution would proceed in correct state. + * - declaration of function, named 'arguments', is considered to be unrecommended (and so, rare) case, + * so instantiation of Arguments object here, in general, is supposed to not affect resource consumption + * significantly. + */ + + ecma_string_t *arguments_string_p = ecma_get_magic_string (ECMA_MAGIC_STRING_ARGUMENTS); + + bool binding_already_declared = ecma_op_has_binding (env_p, arguments_string_p); + + if (!binding_already_declared) + { + ecma_object_t *args_obj_p = ecma_op_create_arguments_object (func_obj_p, + env_p, + formal_parameters_p, + arguments_list_p, + arguments_list_len, + is_strict); - if (ecma_is_completion_value_throw (completion)) + if (is_strict) { - return completion; + ecma_op_create_immutable_binding (env_p, arguments_string_p); + ecma_op_initialize_immutable_binding (env_p, arguments_string_p, ecma_make_object_value (args_obj_p)); + } + else + { + ecma_completion_value_t completion = ecma_op_create_mutable_binding (env_p, + arguments_string_p, + false); + JERRY_ASSERT (ecma_is_completion_value_empty (completion)); + + completion = ecma_op_set_mutable_binding (env_p, + arguments_string_p, + ecma_make_object_value (args_obj_p), + false); + JERRY_ASSERT (ecma_is_completion_value_empty (completion)); } - JERRY_ASSERT (ecma_is_completion_value_empty (completion)); + ecma_deref_object (args_obj_p); } + + ecma_deref_ecma_string (arguments_string_p); } return ecma_make_empty_completion_value (); @@ -543,8 +615,11 @@ ecma_op_function_call (ecma_object_t *func_obj_p, /**< Function object */ uint32_t code_prop_value = code_prop_p->u.internal_property.value; bool is_strict; + bool do_instantiate_args_obj; // 8. - opcode_counter_t code_first_opcode_idx = ecma_unpack_code_internal_property_value (code_prop_value, &is_strict); + opcode_counter_t code_first_opcode_idx = ecma_unpack_code_internal_property_value (code_prop_value, + &is_strict, + &do_instantiate_args_obj); ecma_value_t this_binding; // 1. @@ -576,14 +651,16 @@ ecma_op_function_call (ecma_object_t *func_obj_p, /**< Function object */ local_env_p, arguments_list_p, arguments_list_len, - is_strict), + is_strict, + do_instantiate_args_obj), ret_value); - ecma_completion_value_t completion = run_int_from_pos (code_first_opcode_idx, - this_binding, - local_env_p, - is_strict, - false); + ecma_completion_value_t completion = vm_run_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)); @@ -768,14 +845,18 @@ ecma_op_function_declaration (ecma_object_t *lex_env_p, /**< lexical environment ecma_string_t* formal_parameter_list_p[], /**< formal parameters list */ ecma_length_t formal_parameter_list_length, /**< length of formal parameters list */ bool is_strict, /**< flag indicating if function is declared in strict mode code */ + bool do_instantiate_arguments_object, /**< flag, indicating whether an Arguments object + * should be instantiated for the function object + * upon call */ bool is_configurable_bindings) /**< flag indicating whether function - is declared in eval code */ + * is declared in eval code */ { // b. ecma_object_t *func_obj_p = ecma_op_create_function_object (formal_parameter_list_p, formal_parameter_list_length, lex_env_p, is_strict, + do_instantiate_arguments_object, function_code_opcode_idx); // c. diff --git a/jerry-core/ecma/operations/ecma-function-object.h b/jerry-core/ecma/operations/ecma-function-object.h index 76e76065cd..7e230ac1be 100644 --- a/jerry-core/ecma/operations/ecma-function-object.h +++ b/jerry-core/ecma/operations/ecma-function-object.h @@ -34,6 +34,7 @@ ecma_op_create_function_object (ecma_string_t* formal_parameter_list_p[], ecma_length_t formal_parameters_number, ecma_object_t *scope_p, bool is_strict, + bool do_instantiate_arguments_object, opcode_counter_t first_opcode_idx); extern ecma_object_t* ecma_op_create_external_function_object (ecma_external_pointer_t code_p); @@ -60,6 +61,7 @@ ecma_op_function_declaration (ecma_object_t *lex_env_p, ecma_string_t* formal_parameter_list_p[], ecma_length_t formal_parameter_list_length, bool is_strict, + bool do_instantiate_arguments_object, bool is_configurable_bindings); /** diff --git a/jerry-core/ecma/operations/ecma-objects-arguments.cpp b/jerry-core/ecma/operations/ecma-objects-arguments.cpp index 505ad1628f..77200632f2 100644 --- a/jerry-core/ecma/operations/ecma-objects-arguments.cpp +++ b/jerry-core/ecma/operations/ecma-objects-arguments.cpp @@ -41,14 +41,13 @@ * @return pointer to newly created Arguments object */ ecma_object_t* -ecma_create_arguments_object (ecma_object_t *func_obj_p, /**< callee function */ - ecma_object_t *lex_env_p, /**< lexical environment the Arguments - object is created for */ - ecma_collection_iterator_t *formal_params_iter_p, /**< formal parameters - collection iterator */ - const ecma_value_t *arguments_list_p, /**< list of arguments */ - ecma_length_t arguments_list_length, /**< length of arguments' list */ - bool is_strict) /**< flag indicating whether strict mode is enabled */ +ecma_op_create_arguments_object (ecma_object_t *func_obj_p, /**< callee function */ + ecma_object_t *lex_env_p, /**< lexical environment the Arguments + object is created for */ + ecma_collection_header_t *formal_params_p, /**< formal parameters collection */ + const ecma_value_t *arguments_list_p, /**< list of arguments */ + ecma_length_t arguments_list_length, /**< length of arguments' list */ + bool is_strict) /**< flag indicating whether strict mode is enabled */ { // 1. ecma_number_t *len_p = ecma_alloc_number (); @@ -110,7 +109,7 @@ ecma_create_arguments_object (ecma_object_t *func_obj_p, /**< callee function */ prop_desc.is_configurable = true; } - ecma_string_t *indx_string_p = ecma_new_ecma_string_from_number (ecma_uint32_to_number (indx)); + ecma_string_t *indx_string_p = ecma_new_ecma_string_from_uint32 (indx); completion = ecma_op_object_define_own_property (obj_p, indx_string_p, @@ -121,84 +120,96 @@ ecma_create_arguments_object (ecma_object_t *func_obj_p, /**< callee function */ ecma_deref_ecma_string (indx_string_p); } - const ecma_length_t formal_params_number = formal_params_iter_p->header_p->unit_number; - if (!is_strict - && arguments_list_length > 0 - && formal_params_number > 0) + if (formal_params_p != NULL) { - // 8. - ecma_object_t *map_p = ecma_op_create_object_object_noarg (); + const ecma_length_t formal_params_number = formal_params_p->unit_number; - // 11.c - MEM_DEFINE_LOCAL_ARRAY (formal_params, formal_params_number, ecma_string_t *); + ecma_collection_iterator_t formal_params_iterator; + ecma_collection_iterator_init (&formal_params_iterator, formal_params_p); - JERRY_ASSERT (formal_params_iter_p->current_value_p == NULL); - uint32_t param_index; - for (param_index = 0; - ecma_collection_iterator_next (formal_params_iter_p); - param_index++) + if (!is_strict + && arguments_list_length > 0 + && formal_params_number > 0) { - JERRY_ASSERT (formal_params_iter_p->current_value_p != NULL); - JERRY_ASSERT (param_index < formal_params_number); + // 8. + ecma_object_t *map_p = ecma_op_create_object_object_noarg (); - JERRY_ASSERT (ecma_is_value_string (*formal_params_iter_p->current_value_p)); - formal_params[param_index] = ecma_get_string_from_value (*formal_params_iter_p->current_value_p); - } - JERRY_ASSERT (param_index == formal_params_number); + // 11.c + MEM_DEFINE_LOCAL_ARRAY (formal_params, formal_params_number, ecma_string_t *); - for (int32_t indx = formal_params_number - 1; - indx >= 0; - indx--) - { - // i. - ecma_string_t *name_p = formal_params[indx]; - bool is_first_occurence = true; - - // ii. - for (int32_t indx2 = indx + 1; - indx2 < formal_params_number; - indx2++) + JERRY_ASSERT (formal_params_iterator.current_value_p == NULL); + uint32_t param_index; + for (param_index = 0; + ecma_collection_iterator_next (&formal_params_iterator); + param_index++) { - if (ecma_compare_ecma_strings (name_p, formal_params[indx2])) - { - is_first_occurence = false; - } + JERRY_ASSERT (formal_params_iterator.current_value_p != NULL); + JERRY_ASSERT (param_index < formal_params_number); + + JERRY_ASSERT (ecma_is_value_string (*formal_params_iterator.current_value_p)); + formal_params[param_index] = ecma_get_string_from_value (*formal_params_iterator.current_value_p); } + JERRY_ASSERT (param_index == formal_params_number); - if (is_first_occurence) + for (int32_t indx = formal_params_number - 1; + indx >= 0; + indx--) { - ecma_string_t *indx_string_p = ecma_new_ecma_string_from_number (ecma_uint32_to_number ((uint32_t) indx)); + // i. + ecma_string_t *name_p = formal_params[indx]; + bool is_first_occurence = true; - prop_desc = ecma_make_empty_property_descriptor (); + // ii. + for (int32_t indx2 = indx + 1; + indx2 < formal_params_number; + indx2++) { - prop_desc.is_value_defined = true; - prop_desc.value = ecma_make_string_value (name_p); + if (ecma_compare_ecma_strings (name_p, formal_params[indx2])) + { + is_first_occurence = false; + + break; + } } - completion = ecma_op_object_define_own_property (map_p, - indx_string_p, - &prop_desc, - false); - JERRY_ASSERT (ecma_is_completion_value_normal_true (completion)); + if (is_first_occurence) + { + ecma_string_t *indx_string_p = ecma_new_ecma_string_from_uint32 ((uint32_t) indx); + + prop_desc = ecma_make_empty_property_descriptor (); + { + prop_desc.is_value_defined = true; + prop_desc.value = ecma_make_string_value (name_p); + + prop_desc.is_configurable_defined = true; + prop_desc.is_configurable = true; + } - ecma_deref_ecma_string (indx_string_p); + completion = ecma_op_object_define_own_property (map_p, + indx_string_p, + &prop_desc, + false); + JERRY_ASSERT (ecma_is_completion_value_normal_true (completion)); + + ecma_deref_ecma_string (indx_string_p); + } } - } - MEM_FINALIZE_LOCAL_ARRAY (formal_params); + MEM_FINALIZE_LOCAL_ARRAY (formal_params); - // 12. - ecma_set_object_type (obj_p, ECMA_OBJECT_TYPE_ARGUMENTS); + // 12. + ecma_set_object_type (obj_p, ECMA_OBJECT_TYPE_ARGUMENTS); - ecma_property_t *parameters_map_prop_p = ecma_create_internal_property (obj_p, - ECMA_INTERNAL_PROPERTY_PARAMETERS_MAP); - ECMA_SET_POINTER (parameters_map_prop_p->u.internal_property.value, map_p); + ecma_property_t *parameters_map_prop_p = ecma_create_internal_property (obj_p, + ECMA_INTERNAL_PROPERTY_PARAMETERS_MAP); + ECMA_SET_POINTER (parameters_map_prop_p->u.internal_property.value, map_p); - ecma_property_t *scope_prop_p = ecma_create_internal_property (map_p, - ECMA_INTERNAL_PROPERTY_SCOPE); - ECMA_SET_POINTER (scope_prop_p->u.internal_property.value, lex_env_p); + ecma_property_t *scope_prop_p = ecma_create_internal_property (map_p, + ECMA_INTERNAL_PROPERTY_SCOPE); + ECMA_SET_POINTER (scope_prop_p->u.internal_property.value, lex_env_p); - ecma_deref_object (map_p); + ecma_deref_object (map_p); + } } // 13. @@ -264,11 +275,14 @@ ecma_create_arguments_object (ecma_object_t *func_obj_p, /**< callee function */ } return obj_p; -} /* ecma_create_arguments_object */ +} /* ecma_op_create_arguments_object */ /** * Get value of function's argument mapped to index of Arguments object. * + * Note: + * The procedure emulates execution of function described by MakeArgGetter + * * @return completion value * Returned value must be freed with ecma_free_completion_value */ @@ -437,32 +451,36 @@ ecma_op_arguments_object_define_own_property (ecma_object_t *obj_p, /**< the obj // i. if (property_desc_p->is_value_defined) { - completion = ecma_op_object_put (map_p, - property_name_p, - property_desc_p->value, - is_throw); - } + /* emulating execution of function described by MakeArgSetter */ + ecma_property_t *scope_prop_p = ecma_get_internal_property (map_p, ECMA_INTERNAL_PROPERTY_SCOPE); + ecma_object_t *lex_env_p = ECMA_GET_NON_NULL_POINTER (ecma_object_t, + scope_prop_p->u.internal_property.value); - if (unlikely (ecma_is_completion_value_throw (completion))) - { - ret_value = completion; + ecma_property_t *mapped_prop_p = ecma_op_object_get_own_property (map_p, property_name_p); + ecma_value_t arg_name_prop_value = ecma_get_named_data_property_value (mapped_prop_p); + + ecma_string_t *arg_name_p = ecma_get_string_from_value (arg_name_prop_value); + + completion = ecma_op_set_mutable_binding (lex_env_p, + arg_name_p, + property_desc_p->value, + true); + JERRY_ASSERT (ecma_is_completion_value_empty (completion)); } - else - { - // ii. - if (property_desc_p->is_writable_defined - && !property_desc_p->is_writable) - { - completion = ecma_op_object_delete (map_p, - property_name_p, - false); - JERRY_ASSERT (ecma_is_completion_value_normal_true (completion)); - } + // ii. + if (property_desc_p->is_writable_defined + && !property_desc_p->is_writable) + { + completion = ecma_op_object_delete (map_p, + property_name_p, + false); - // 6. - ret_value = ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_TRUE); + JERRY_ASSERT (ecma_is_completion_value_normal_true (completion)); } + + // 6. + ret_value = ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_TRUE); } } else diff --git a/jerry-core/ecma/operations/ecma-objects-arguments.h b/jerry-core/ecma/operations/ecma-objects-arguments.h index ebe558aa4f..5c3c2314ab 100644 --- a/jerry-core/ecma/operations/ecma-objects-arguments.h +++ b/jerry-core/ecma/operations/ecma-objects-arguments.h @@ -20,12 +20,12 @@ #include "ecma-helpers.h" extern ecma_object_t* -ecma_create_arguments_object (ecma_object_t *func_obj_p, - ecma_object_t *lex_env_p, - ecma_collection_iterator_t *formal_params_iter_p, - const ecma_value_t *arguments_list_p, - ecma_length_t arguments_list_length, - bool is_strict); +ecma_op_create_arguments_object (ecma_object_t *func_obj_p, + ecma_object_t *lex_env_p, + ecma_collection_header_t *formal_params_p, + const ecma_value_t *arguments_list_p, + ecma_length_t arguments_list_length, + bool is_strict); extern ecma_completion_value_t ecma_op_arguments_object_get (ecma_object_t *obj_p, ecma_string_t *property_name_p); diff --git a/jerry-core/jerry.cpp b/jerry-core/jerry.cpp index a7e679736d..326e62cfdb 100644 --- a/jerry-core/jerry.cpp +++ b/jerry-core/jerry.cpp @@ -1225,7 +1225,7 @@ jerry_parse (const char* source_p, /**< script source */ bool is_show_mem_stats_per_opcode = ((jerry_flags & JERRY_FLAG_MEM_STATS_PER_OPCODE) != 0); - init_int (opcodes, is_show_mem_stats_per_opcode); + vm_init (opcodes, is_show_mem_stats_per_opcode); return true; } /* jerry_parse */ @@ -1240,7 +1240,7 @@ jerry_run (void) { jerry_assert_api_available (); - return run_int (); + return vm_run_global (); } /* jerry_run */ /** * Simple jerry runner diff --git a/jerry-core/parser/js/opcodes-dumper.cpp b/jerry-core/parser/js/opcodes-dumper.cpp index e2ea1b0cb5..7faef75d66 100644 --- a/jerry-core/parser/js/opcodes-dumper.cpp +++ b/jerry-core/parser/js/opcodes-dumper.cpp @@ -2383,11 +2383,27 @@ dump_variable_declaration (literal_index_t lit_id) serializer_dump_op_meta (create_op_meta_100 (opcode, lit_id)); } -void -dump_strict_mode_header (void) +opcode_counter_t +dump_scope_code_flags_for_rewrite (void) { - const opcode_t opcode = getop_meta (OPCODE_META_TYPE_STRICT_CODE, INVALID_VALUE, INVALID_VALUE); + opcode_counter_t oc = serializer_get_current_opcode_counter (); + + const opcode_t opcode = getop_meta (OPCODE_META_TYPE_SCOPE_CODE_FLAGS, INVALID_VALUE, INVALID_VALUE); serializer_dump_op_meta (create_op_meta_000 (opcode)); + + return oc; +} + +void +rewrite_scope_code_flags (opcode_counter_t scope_code_flags_oc, + opcode_scope_code_flags_t scope_flags) +{ + JERRY_ASSERT ((idx_t) scope_flags == scope_flags); + + op_meta opm = serializer_get_op_meta (scope_code_flags_oc); + JERRY_ASSERT (opm.op.op_idx == OPCODE (meta)); + opm.op.data.meta.data_1 = (idx_t) scope_flags; + serializer_rewrite_op_meta (scope_code_flags_oc, opm); } void diff --git a/jerry-core/parser/js/opcodes-dumper.h b/jerry-core/parser/js/opcodes-dumper.h index ef6165ebf3..0da07c27e5 100644 --- a/jerry-core/parser/js/opcodes-dumper.h +++ b/jerry-core/parser/js/opcodes-dumper.h @@ -217,7 +217,9 @@ void dump_throw (operand); bool dumper_variable_declaration_exists (literal_index_t); void dump_variable_declaration (literal_index_t); -void dump_strict_mode_header (void); +opcode_counter_t dump_scope_code_flags_for_rewrite (void); +void rewrite_scope_code_flags (opcode_counter_t scope_code_flags_oc, + opcode_scope_code_flags_t scope_flags); void dump_reg_var_decl_for_rewrite (void); void rewrite_reg_var_decl (void); diff --git a/jerry-core/parser/js/parser.cpp b/jerry-core/parser/js/parser.cpp index ab14fb37a2..788fb9573c 100644 --- a/jerry-core/parser/js/parser.cpp +++ b/jerry-core/parser/js/parser.cpp @@ -2648,10 +2648,16 @@ preparse_scope (bool is_global) const locus start_loc = tok.loc; const token_type end_tt = is_global ? TOK_EOF : TOK_CLOSE_BRACE; + opcode_counter_t scope_code_flags_oc = dump_scope_code_flags_for_rewrite (); + + bool is_ref_arguments_identifier = false; + bool is_ref_eval_identifier = false; + bool is_use_strict = false; + if (token_is (TOK_STRING) && literal_equal_s (lexer_get_literal_by_id (token_data ()), "use strict")) { scopes_tree_set_strict_mode (STACK_TOP (scopes), true); - dump_strict_mode_header (); + is_use_strict = true; } lexer_set_strict_mode (scopes_tree_strict_mode (STACK_TOP (scopes))); @@ -2683,11 +2689,45 @@ preparse_scope (bool is_global) } else { + if (token_is (TOK_NAME)) + { + if (literal_equal_type_s (lexer_get_literal_by_id (token_data ()), + "arguments")) + { + is_ref_arguments_identifier = true; + } + + if (literal_equal_type_s (lexer_get_literal_by_id (token_data ()), + "eval")) + { + is_ref_eval_identifier = true; + } + } + process_keyword_names (); } skip_newlines (); } + opcode_scope_code_flags_t scope_flags = OPCODE_SCOPE_CODE_FLAGS__EMPTY; + + if (is_use_strict) + { + scope_flags = (opcode_scope_code_flags_t) (scope_flags | OPCODE_SCOPE_CODE_FLAGS_STRICT); + } + + if (!is_ref_arguments_identifier) + { + scope_flags = (opcode_scope_code_flags_t) (scope_flags | OPCODE_SCOPE_CODE_FLAGS_NOT_REF_ARGUMENTS_IDENTIFIER); + } + + if (!is_ref_eval_identifier) + { + scope_flags = (opcode_scope_code_flags_t) (scope_flags | OPCODE_SCOPE_CODE_FLAGS_NOT_REF_EVAL_IDENTIFIER); + } + + rewrite_scope_code_flags (scope_code_flags_oc, scope_flags); + if (start_loc != tok.loc) { lexer_seek (start_loc); diff --git a/jerry-core/parser/js/scopes-tree.cpp b/jerry-core/parser/js/scopes-tree.cpp index 11637894c3..5d33a1ec3a 100644 --- a/jerry-core/parser/js/scopes-tree.cpp +++ b/jerry-core/parser/js/scopes-tree.cpp @@ -354,7 +354,7 @@ generate_opcode (scopes_tree tree, opcode_counter_t opc_index, lit_id_hash_table case OPCODE_META_TYPE_CATCH: case OPCODE_META_TYPE_FINALLY: case OPCODE_META_TYPE_END_TRY_CATCH_FINALLY: - case OPCODE_META_TYPE_STRICT_CODE: + case OPCODE_META_TYPE_SCOPE_CODE_FLAGS: { change_uid (om, lit_ids, 0x000); break; @@ -493,7 +493,7 @@ count_new_literals_in_opcode (scopes_tree tree, opcode_counter_t opc_index) case OPCODE_META_TYPE_CATCH: case OPCODE_META_TYPE_FINALLY: case OPCODE_META_TYPE_END_TRY_CATCH_FINALLY: - case OPCODE_META_TYPE_STRICT_CODE: + case OPCODE_META_TYPE_SCOPE_CODE_FLAGS: { insert_uids_to_lit_id_map (om, 0x000); break; diff --git a/jerry-core/vm/opcodes-ecma-try-catch-finally.cpp b/jerry-core/vm/opcodes-ecma-try-catch-finally.cpp index 0980292bb0..92ac626056 100644 --- a/jerry-core/vm/opcodes-ecma-try-catch-finally.cpp +++ b/jerry-core/vm/opcodes-ecma-try-catch-finally.cpp @@ -37,12 +37,12 @@ opfunc_try_block (opcode_t opdata, /**< operation data */ int_data->pos++; - ecma_completion_value_t try_completion = run_int_loop (int_data); + ecma_completion_value_t try_completion = vm_loop (int_data); JERRY_ASSERT ((!ecma_is_completion_value_empty (try_completion) && int_data->pos <= try_end_oc) || (ecma_is_completion_value_empty (try_completion) && int_data->pos == try_end_oc)); int_data->pos = try_end_oc; - opcode_t next_opcode = read_opcode (int_data->pos); + opcode_t next_opcode = vm_get_opcode (int_data->pos); JERRY_ASSERT (next_opcode.op_idx == __op__idx_meta); if (ecma_is_completion_value_exit (try_completion)) @@ -58,7 +58,7 @@ opfunc_try_block (opcode_t opdata, /**< operation data */ if (ecma_is_completion_value_throw (try_completion)) { - next_opcode = read_opcode (int_data->pos); + next_opcode = vm_get_opcode (int_data->pos); JERRY_ASSERT (next_opcode.op_idx == __op__idx_meta); JERRY_ASSERT (next_opcode.data.meta.type == OPCODE_META_TYPE_CATCH_EXCEPTION_IDENTIFIER); @@ -87,7 +87,7 @@ opfunc_try_block (opcode_t opdata, /**< operation data */ int_data->lex_env_p = catch_env_p; ecma_free_completion_value (try_completion); - try_completion = run_int_loop (int_data); + try_completion = vm_loop (int_data); int_data->lex_env_p = old_env_p; @@ -100,7 +100,7 @@ opfunc_try_block (opcode_t opdata, /**< operation data */ int_data->pos = catch_end_oc; } - next_opcode = read_opcode (int_data->pos); + next_opcode = vm_get_opcode (int_data->pos); JERRY_ASSERT (next_opcode.op_idx == __op__idx_meta); if (ecma_is_completion_value_exit (try_completion)) @@ -114,7 +114,7 @@ opfunc_try_block (opcode_t opdata, /**< operation data */ read_meta_opcode_counter (OPCODE_META_TYPE_FINALLY, int_data) + int_data->pos); int_data->pos++; - ecma_completion_value_t finally_completion = run_int_loop (int_data); + ecma_completion_value_t finally_completion = vm_loop (int_data); JERRY_ASSERT ((!ecma_is_completion_value_empty (finally_completion) && int_data->pos <= finally_end_oc) || (ecma_is_completion_value_empty (finally_completion) && int_data->pos == finally_end_oc)); int_data->pos = finally_end_oc; @@ -126,7 +126,7 @@ opfunc_try_block (opcode_t opdata, /**< operation data */ } } - next_opcode = read_opcode (int_data->pos++); + next_opcode = vm_get_opcode (int_data->pos++); JERRY_ASSERT (next_opcode.op_idx == __op__idx_meta); JERRY_ASSERT (next_opcode.data.meta.type == OPCODE_META_TYPE_END_TRY_CATCH_FINALLY); diff --git a/jerry-core/vm/opcodes-varg.cpp b/jerry-core/vm/opcodes-varg.cpp index 92cbd01e53..a070224777 100644 --- a/jerry-core/vm/opcodes-varg.cpp +++ b/jerry-core/vm/opcodes-varg.cpp @@ -40,11 +40,11 @@ fill_varg_list (int_data_t *int_data, /**< interpreter context */ arg_index < args_number; arg_index++) { - ecma_completion_value_t evaluate_arg_completion = run_int_loop (int_data); + ecma_completion_value_t evaluate_arg_completion = vm_loop (int_data); if (ecma_is_completion_value_normal (evaluate_arg_completion)) { - opcode_t next_opcode = read_opcode (int_data->pos); + opcode_t next_opcode = vm_get_opcode (int_data->pos); JERRY_ASSERT (next_opcode.op_idx == __op__idx_meta); JERRY_ASSERT (next_opcode.data.meta.type == OPCODE_META_TYPE_VARG); @@ -92,7 +92,7 @@ fill_params_list (int_data_t *int_data, /**< interpreter context */ param_index < params_number; param_index++) { - opcode_t next_opcode = read_opcode (int_data->pos); + opcode_t next_opcode = vm_get_opcode (int_data->pos); JERRY_ASSERT (next_opcode.op_idx == __op__idx_meta); JERRY_ASSERT (next_opcode.data.meta.type == OPCODE_META_TYPE_VARG); diff --git a/jerry-core/vm/opcodes.cpp b/jerry-core/vm/opcodes.cpp index 7c8e0b9fda..3b8446fd56 100644 --- a/jerry-core/vm/opcodes.cpp +++ b/jerry-core/vm/opcodes.cpp @@ -443,19 +443,26 @@ function_declaration (int_data_t *int_data, /**< interpreter context */ ecma_length_t args_number) /**< number of arguments */ { bool is_strict = int_data->is_strict; + bool do_instantiate_arguments_object = true; const bool is_configurable_bindings = int_data->is_eval_code; const opcode_counter_t function_code_end_oc = (opcode_counter_t) ( read_meta_opcode_counter (OPCODE_META_TYPE_FUNCTION_END, int_data) + int_data->pos); int_data->pos++; - opcode_t next_opcode = read_opcode (int_data->pos); - if (next_opcode.op_idx == __op__idx_meta - && next_opcode.data.meta.type == OPCODE_META_TYPE_STRICT_CODE) + opcode_scope_code_flags_t scope_flags = vm_get_scope_flags (int_data->pos++); + + if (scope_flags & OPCODE_SCOPE_CODE_FLAGS_STRICT) { is_strict = true; - - int_data->pos++; + } + if ((scope_flags & OPCODE_SCOPE_CODE_FLAGS_NOT_REF_ARGUMENTS_IDENTIFIER) + && (scope_flags & OPCODE_SCOPE_CODE_FLAGS_NOT_REF_EVAL_IDENTIFIER)) + { + /* the code doesn't use 'arguments' identifier + * and doesn't perform direct call to eval, + * so Arguments object can't be referenced */ + do_instantiate_arguments_object = false; } ecma_string_t *function_name_string_p = ecma_new_ecma_string_from_lit_index (function_name_lit_id); @@ -466,6 +473,7 @@ function_declaration (int_data_t *int_data, /**< interpreter context */ args_names, args_number, is_strict, + do_instantiate_arguments_object, is_configurable_bindings); ecma_deref_ecma_string (function_name_string_p); @@ -543,18 +551,25 @@ opfunc_func_expr_n (opcode_t opdata, /**< operation data */ fill_params_list (int_data, params_number, params_names); bool is_strict = int_data->is_strict; + bool do_instantiate_arguments_object = true; function_code_end_oc = (opcode_counter_t) (read_meta_opcode_counter (OPCODE_META_TYPE_FUNCTION_END, int_data) + int_data->pos); int_data->pos++; - opcode_t next_opcode = read_opcode (int_data->pos); - if (next_opcode.op_idx == __op__idx_meta - && next_opcode.data.meta.type == OPCODE_META_TYPE_STRICT_CODE) + opcode_scope_code_flags_t scope_flags = vm_get_scope_flags (int_data->pos++); + + if (scope_flags & OPCODE_SCOPE_CODE_FLAGS_STRICT) { is_strict = true; - - int_data->pos++; + } + if ((scope_flags & OPCODE_SCOPE_CODE_FLAGS_NOT_REF_ARGUMENTS_IDENTIFIER) + && (scope_flags & OPCODE_SCOPE_CODE_FLAGS_NOT_REF_EVAL_IDENTIFIER)) + { + /* the code doesn't use 'arguments' identifier + * and doesn't perform direct call to eval, + * so Arguments object can't be referenced */ + do_instantiate_arguments_object = false; } ecma_object_t *scope_p; @@ -580,6 +595,7 @@ opfunc_func_expr_n (opcode_t opdata, /**< operation data */ params_number, scope_p, is_strict, + do_instantiate_arguments_object, int_data->pos); ret_value = set_variable_value (int_data, lit_oc, @@ -638,7 +654,7 @@ opfunc_call_n (opcode_t opdata, /**< operation data */ idx_t this_arg_var_idx = INVALID_VALUE; idx_t args_number; - opcode_t next_opcode = read_opcode (int_data->pos); + opcode_t next_opcode = vm_get_opcode (int_data->pos); if (next_opcode.op_idx == __op__idx_meta && next_opcode.data.meta.type == OPCODE_META_TYPE_THIS_ARG) { @@ -896,13 +912,13 @@ opfunc_obj_decl (opcode_t opdata, /**< operation data */ prop_index < args_number; prop_index++) { - ecma_completion_value_t evaluate_prop_completion = run_int_loop (int_data); + ecma_completion_value_t evaluate_prop_completion = vm_loop (int_data); if (ecma_is_completion_value_normal (evaluate_prop_completion)) { JERRY_ASSERT (ecma_is_completion_value_empty (evaluate_prop_completion)); - opcode_t next_opcode = read_opcode (int_data->pos); + opcode_t next_opcode = vm_get_opcode (int_data->pos); JERRY_ASSERT (next_opcode.op_idx == __op__idx_meta); const opcode_meta_type type = (opcode_meta_type) next_opcode.data.meta.type; @@ -1305,13 +1321,13 @@ opfunc_with (opcode_t opdata, /**< operation data */ true); int_data->lex_env_p = new_env_p; - ecma_completion_value_t evaluation_completion = run_int_loop (int_data); + ecma_completion_value_t evaluation_completion = vm_loop (int_data); if (ecma_is_completion_value_normal (evaluation_completion)) { JERRY_ASSERT (ecma_is_completion_value_empty (evaluation_completion)); - opcode_t meta_opcode = read_opcode (int_data->pos); + opcode_t meta_opcode = vm_get_opcode (int_data->pos); JERRY_ASSERT (meta_opcode.op_idx == __op__idx_meta); JERRY_ASSERT (meta_opcode.data.meta.type == OPCODE_META_TYPE_END_WITH); @@ -1651,12 +1667,7 @@ opfunc_meta (opcode_t opdata, /**< operation data */ return ecma_make_meta_completion_value (); } - case OPCODE_META_TYPE_STRICT_CODE: - { - FIXME (/* Handle in run_int_from_pos */); - return ecma_make_meta_completion_value (); - } - + case OPCODE_META_TYPE_SCOPE_CODE_FLAGS: case OPCODE_META_TYPE_UNDEFINED: case OPCODE_META_TYPE_THIS_ARG: case OPCODE_META_TYPE_FUNCTION_END: @@ -1695,7 +1706,7 @@ opcode_counter_t read_meta_opcode_counter (opcode_meta_type expected_type, /**< expected type of meta opcode */ int_data_t *int_data) /**< interpreter context */ { - opcode_t meta_opcode = read_opcode (int_data->pos); + opcode_t meta_opcode = vm_get_opcode (int_data->pos); JERRY_ASSERT (meta_opcode.data.meta.type == expected_type); const idx_t data_1 = meta_opcode.data.meta.data_1; diff --git a/jerry-core/vm/opcodes.h b/jerry-core/vm/opcodes.h index d29ed832aa..7996ebd218 100644 --- a/jerry-core/vm/opcodes.h +++ b/jerry-core/vm/opcodes.h @@ -70,9 +70,23 @@ typedef enum OPCODE_META_TYPE_CATCH_EXCEPTION_IDENTIFIER, /**< literal index containing name of variable with exception object */ OPCODE_META_TYPE_FINALLY, /**< mark of beginning of finally block containing pointer to end of finally block */ OPCODE_META_TYPE_END_TRY_CATCH_FINALLY, /**< mark of end of try-catch, try-finally, try-catch-finally blocks */ - OPCODE_META_TYPE_STRICT_CODE /**< mark of beginning of strict code */ + OPCODE_META_TYPE_SCOPE_CODE_FLAGS /**< set of flags indicating various properties of the scope's code + * (See also: opcode_scope_code_flags_t) */ } opcode_meta_type; +/** + * Flags indicating various properties of a scope's code + */ +typedef enum : idx_t +{ + OPCODE_SCOPE_CODE_FLAGS__EMPTY = (0u), /**< initializer for empty flag set */ + OPCODE_SCOPE_CODE_FLAGS_STRICT = (1u << 0), /**< code is strict mode code */ + OPCODE_SCOPE_CODE_FLAGS_NOT_REF_ARGUMENTS_IDENTIFIER = (1u << 1), /**< code doesn't reference + * 'arguments' identifier */ + OPCODE_SCOPE_CODE_FLAGS_NOT_REF_EVAL_IDENTIFIER = (1u << 2) /**< code doesn't reference + * 'eval' identifier */ +} opcode_scope_code_flags_t; + /** * Interpreter context */ diff --git a/jerry-core/vm/pretty-printer.cpp b/jerry-core/vm/pretty-printer.cpp index 229afbb096..2441759934 100644 --- a/jerry-core/vm/pretty-printer.cpp +++ b/jerry-core/vm/pretty-printer.cpp @@ -625,9 +625,35 @@ pp_op_meta (opcode_counter_t oc, op_meta opm, bool rewrite) printf ("end try"); break; } - case OPCODE_META_TYPE_STRICT_CODE: + case OPCODE_META_TYPE_SCOPE_CODE_FLAGS: { - printf ("use strict;"); + if (opm.op.data.meta.data_1 != INVALID_VALUE) + { + idx_t scope_flags = opm.op.data.meta.data_1; + + if (scope_flags & OPCODE_SCOPE_CODE_FLAGS_STRICT) + { + printf ("[use strict] "); + scope_flags &= (idx_t) ~(OPCODE_SCOPE_CODE_FLAGS_STRICT); + } + if (scope_flags & OPCODE_SCOPE_CODE_FLAGS_NOT_REF_ARGUMENTS_IDENTIFIER) + { + printf ("[no 'arguments'] "); + scope_flags &= (idx_t) ~(OPCODE_SCOPE_CODE_FLAGS_NOT_REF_ARGUMENTS_IDENTIFIER); + } + if (scope_flags & OPCODE_SCOPE_CODE_FLAGS_NOT_REF_EVAL_IDENTIFIER) + { + printf ("[no 'eval'] "); + scope_flags &= (idx_t) ~(OPCODE_SCOPE_CODE_FLAGS_NOT_REF_EVAL_IDENTIFIER); + } + + JERRY_ASSERT (scope_flags == 0); + } + else + { + printf ("[to be rewritten]"); + } + break; } default: diff --git a/jerry-core/vm/vm.cpp b/jerry-core/vm/vm.cpp index a7e195f7ee..834b178b79 100644 --- a/jerry-core/vm/vm.cpp +++ b/jerry-core/vm/vm.cpp @@ -235,7 +235,7 @@ interp_mem_stats_opcode_enter (opcode_counter_t opcode_position, out_pools_stats_p, true, false); - opcode_t opcode = read_opcode (opcode_position); + opcode_t opcode = vm_get_opcode (opcode_position); printf ("%s-- Opcode: %s (position %u) --\n", indent_prefix, __op_names[opcode.op_idx], (uint32_t) opcode_position); @@ -280,7 +280,7 @@ interp_mem_stats_opcode_exit (int_data_t *int_data_p, int_data_p->context_peak_allocated_pool_chunks = JERRY_MAX (int_data_p->context_peak_allocated_pool_chunks, pools_stats_after.allocated_chunks); - opcode_t opcode = read_opcode (opcode_position); + opcode_t opcode = vm_get_opcode (opcode_position); printf ("%s Allocated heap bytes: %5u -> %5u (%+5d, local %5u, peak %5u)\n", indent_prefix, @@ -336,8 +336,8 @@ interp_mem_stats_opcode_exit (int_data_t *int_data_p, * Initialize interpreter. */ void -init_int (const opcode_t *program_p, /**< pointer to byte-code program */ - bool dump_mem_stats) /** dump per-opcode memory usage change statistics */ +vm_init (const opcode_t *program_p, /**< pointer to byte-code program */ + bool dump_mem_stats) /** dump per-opcode memory usage change statistics */ { #ifdef MEM_STATS interp_mem_stats_enabled = dump_mem_stats; @@ -348,10 +348,13 @@ init_int (const opcode_t *program_p, /**< pointer to byte-code program */ JERRY_ASSERT (__program == NULL); __program = program_p; -} /* init_int */ +} /* vm_init */ +/** + * Run global code + */ jerry_completion_code_t -run_int (void) +vm_run_global (void) { JERRY_ASSERT (__program != NULL); JERRY_ASSERT (vm_top_context_p == NULL); @@ -363,22 +366,21 @@ run_int (void) bool is_strict = false; opcode_counter_t start_pos = 0; - opcode_t first_opcode = read_opcode (start_pos); - if (first_opcode.op_idx == __op__idx_meta - && first_opcode.data.meta.type == OPCODE_META_TYPE_STRICT_CODE) + opcode_scope_code_flags_t scope_flags = vm_get_scope_flags (start_pos++); + + if (scope_flags & OPCODE_SCOPE_CODE_FLAGS_STRICT) { is_strict = true; - start_pos++; } ecma_object_t *glob_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_GLOBAL); ecma_object_t *lex_env_p = ecma_get_global_environment (); - ecma_completion_value_t completion = run_int_from_pos (start_pos, - ecma_make_object_value (glob_obj_p), - lex_env_p, - is_strict, - false); + ecma_completion_value_t completion = vm_run_from_pos (start_pos, + ecma_make_object_value (glob_obj_p), + lex_env_p, + is_strict, + false); jerry_completion_code_t ret_code; @@ -408,10 +410,20 @@ run_int (void) JERRY_ASSERT (vm_top_context_p == NULL); return ret_code; -} +} /* vm_run_global */ +/** + * Run interpreter loop using specified context + * + * Note: + * The interpreter loop stops upon receiving completion value that is normal completion value. + * + * @return If the received completion value is not meta completion value (ECMA_COMPLETION_TYPE_META), then + * the completion value is returned as is; + * Otherwise - the completion value is discarded and normal empty completion value is returned. + */ ecma_completion_value_t -run_int_loop (int_data_t *int_data) +vm_loop (int_data_t *int_data) /**< interpreter context */ { ecma_completion_value_t completion; @@ -470,14 +482,17 @@ run_int_loop (int_data_t *int_data) return completion; } -} +} /* vm_loop */ +/** + * Run the code, starting from specified opcode + */ ecma_completion_value_t -run_int_from_pos (opcode_counter_t start_pos, - ecma_value_t this_binding_value, - ecma_object_t *lex_env_p, - bool is_strict, - bool is_eval_code) +vm_run_from_pos (opcode_counter_t start_pos, /**< identifier of starting opcode */ + ecma_value_t this_binding_value, /**< value of 'ThisBinding' */ + ecma_object_t *lex_env_p, /**< lexical environment to use */ + bool is_strict, /**< is the code is strict mode code (ECMA-262 v5, 10.1.1) */ + bool is_eval_code) /**< is the code is eval code (ECMA-262 v5, 10.1) */ { ecma_completion_value_t completion; @@ -510,7 +525,7 @@ run_int_from_pos (opcode_counter_t start_pos, interp_mem_stats_context_enter (&int_data, start_pos); #endif /* MEM_STATS */ - completion = run_int_loop (&int_data); + completion = vm_loop (&int_data); JERRY_ASSERT (ecma_is_completion_value_normal (completion) || ecma_is_completion_value_throw (completion) @@ -530,16 +545,30 @@ run_int_from_pos (opcode_counter_t start_pos, MEM_FINALIZE_LOCAL_ARRAY (regs); return completion; -} +} /* vm_run_from_pos */ /** * Get specified opcode from the program. */ opcode_t -read_opcode (opcode_counter_t counter) /**< opcode counter */ +vm_get_opcode (opcode_counter_t counter) /**< opcode counter */ { return __program[ counter ]; -} /* read_opcode */ +} /* vm_get_opcode */ + +/** + * Get scope code flags from opcode specified by opcode counter + * + * @return mask of scope code flags + */ +opcode_scope_code_flags_t +vm_get_scope_flags (opcode_counter_t counter) /**< opcode counter */ +{ + opcode_t flags_opcode = vm_get_opcode (counter); + JERRY_ASSERT (flags_opcode.op_idx == __op__idx_meta + && flags_opcode.data.meta.type == OPCODE_META_TYPE_SCOPE_CODE_FLAGS); + return (opcode_scope_code_flags_t) flags_opcode.data.meta.data_1; +} /* vm_get_scope_flags */ /** * Get this binding of current execution context diff --git a/jerry-core/vm/vm.h b/jerry-core/vm/vm.h index e9707a37b1..d584c7f2af 100644 --- a/jerry-core/vm/vm.h +++ b/jerry-core/vm/vm.h @@ -20,16 +20,17 @@ #include "jrt.h" #include "opcodes.h" -void init_int (const opcode_t* program_p, bool dump_mem_stats); -jerry_completion_code_t run_int (void); -ecma_completion_value_t run_int_loop (int_data_t *int_data); -ecma_completion_value_t run_int_from_pos (opcode_counter_t start_pos, - ecma_value_t this_binding_value, - ecma_object_t *lex_env_p, - bool is_strict, - bool is_eval_code); +extern void vm_init (const opcode_t* program_p, bool dump_mem_stats); +extern jerry_completion_code_t vm_run_global (void); +extern ecma_completion_value_t vm_loop (int_data_t *int_data); +extern ecma_completion_value_t vm_run_from_pos (opcode_counter_t start_pos, + ecma_value_t this_binding_value, + ecma_object_t *lex_env_p, + bool is_strict, + bool is_eval_code); -opcode_t read_opcode (opcode_counter_t counter); +extern opcode_t vm_get_opcode (opcode_counter_t counter); +extern opcode_scope_code_flags_t vm_get_scope_flags (opcode_counter_t counter); extern ecma_value_t vm_get_this_binding (void); extern ecma_object_t* vm_get_lex_env (void); diff --git a/tests/jerry-test-suite/precommit_test_list b/tests/jerry-test-suite/precommit_test_list index 8d91540719..9a9f3f091e 100644 --- a/tests/jerry-test-suite/precommit_test_list +++ b/tests/jerry-test-suite/precommit_test_list @@ -89,6 +89,20 @@ ./tests/jerry-test-suite/11/11.02/11.02.03/11.02.03-008.js ./tests/jerry-test-suite/11/11.02/11.02.03/11.02.03-017.js ./tests/jerry-test-suite/11/11.02/11.02.03/11.02.03-021.js +./tests/jerry-test-suite/11/11.02/11.02.04/11.02.04-001.js +./tests/jerry-test-suite/11/11.02/11.02.04/11.02.04-002.js +./tests/jerry-test-suite/11/11.02/11.02.04/11.02.04-003.js +./tests/jerry-test-suite/11/11.02/11.02.04/11.02.04-004.js +./tests/jerry-test-suite/11/11.02/11.02.04/11.02.04-005.js +./tests/jerry-test-suite/11/11.02/11.02.04/11.02.04-006.js +./tests/jerry-test-suite/11/11.02/11.02.04/11.02.04-007.js +./tests/jerry-test-suite/11/11.02/11.02.04/11.02.04-008.js +./tests/jerry-test-suite/11/11.02/11.02.04/11.02.04-009.js +./tests/jerry-test-suite/11/11.02/11.02.04/11.02.04-010.js +./tests/jerry-test-suite/11/11.02/11.02.04/11.02.04-011.js +./tests/jerry-test-suite/11/11.02/11.02.04/11.02.04-012.js +./tests/jerry-test-suite/11/11.02/11.02.04/11.02.04-013.js +./tests/jerry-test-suite/11/11.02/11.02.04/11.02.04-014.js ./tests/jerry-test-suite/11/11.02/11.02.04/11.02.04-016.js ./tests/jerry-test-suite/11/11.02/11.02.04/11.02.04-018.js ./tests/jerry-test-suite/11/11.02/11.02.04/11.02.04-019.js @@ -932,8 +946,11 @@ ./tests/jerry-test-suite/13/13-005.js ./tests/jerry-test-suite/13/13-006.js ./tests/jerry-test-suite/13/13-007.js +./tests/jerry-test-suite/13/13-008.js ./tests/jerry-test-suite/13/13-009.js ./tests/jerry-test-suite/13/13-010.js +./tests/jerry-test-suite/13/13-012.js +./tests/jerry-test-suite/13/13-013.js ./tests/jerry-test-suite/13/13.01/13.01-001.js ./tests/jerry-test-suite/13/13.02/13.02-001.js ./tests/jerry-test-suite/13/13.02/13.02-003.js diff --git a/tests/jerry-test-suite/unsupported_list b/tests/jerry-test-suite/unsupported_list index 824fa5d8ba..d530667182 100644 --- a/tests/jerry-test-suite/unsupported_list +++ b/tests/jerry-test-suite/unsupported_list @@ -5,25 +5,6 @@ > Regular expressions ./tests/jerry-test-suite/07/07.08/07.08.05/07.08.05-001.js -> Arguments object -./tests/jerry-test-suite/11/11.02/11.02.04/11.02.04-001.js -./tests/jerry-test-suite/11/11.02/11.02.04/11.02.04-002.js -./tests/jerry-test-suite/11/11.02/11.02.04/11.02.04-003.js -./tests/jerry-test-suite/11/11.02/11.02.04/11.02.04-004.js -./tests/jerry-test-suite/11/11.02/11.02.04/11.02.04-005.js -./tests/jerry-test-suite/11/11.02/11.02.04/11.02.04-006.js -./tests/jerry-test-suite/11/11.02/11.02.04/11.02.04-007.js -./tests/jerry-test-suite/11/11.02/11.02.04/11.02.04-008.js -./tests/jerry-test-suite/11/11.02/11.02.04/11.02.04-009.js -./tests/jerry-test-suite/11/11.02/11.02.04/11.02.04-010.js -./tests/jerry-test-suite/11/11.02/11.02.04/11.02.04-011.js -./tests/jerry-test-suite/11/11.02/11.02.04/11.02.04-012.js -./tests/jerry-test-suite/11/11.02/11.02.04/11.02.04-013.js -./tests/jerry-test-suite/11/11.02/11.02.04/11.02.04-014.js -./tests/jerry-test-suite/13/13-008.js -./tests/jerry-test-suite/13/13-012.js -./tests/jerry-test-suite/13/13-013.js - > eval ./tests/jerry-test-suite/11/11.04/11.04.01/11.04.01-013.js diff --git a/tests/jerry/arguments.js b/tests/jerry/arguments.js new file mode 100644 index 0000000000..e2a5724c25 --- /dev/null +++ b/tests/jerry/arguments.js @@ -0,0 +1,108 @@ +// 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. + +function f (a, b, c) +{ + return arguments; +} + +args = f(); +assert (args[0] === undefined); + +args = f (1, 2, 3, 4, 5); +assert (args[0] === 1); +assert (args[1] === 2); +assert (args[2] === 3); +assert (args[3] === 4); +assert (args[4] === 5); +assert (args[5] === undefined); + +assert (args.callee === f); +assert (typeof args.caller === 'undefined'); + +function g (a, b, c) +{ + assert (arguments[0] === 1); + assert (arguments[1] === undefined); + assert (arguments[2] === undefined); + + a = 'a'; + b = 'b'; + c = 'c'; + + assert (arguments[0] === 'a'); + assert (arguments[1] === 'b'); + assert (arguments[2] === 'c'); + + arguments [0] = 1; + arguments [1] = 2; + arguments [2] = 3; + + assert (a === 1); + assert (b === 2); + assert (c === 3); + + delete arguments [0]; + arguments[0] = 'new value'; + assert (a === 1); + + a = 'a'; + b = 'b'; + c = 'c'; + + assert (arguments[0] === 'new value'); + assert (arguments[1] === 'b'); + assert (arguments[2] === 'c'); +} + +g (1); + +fn_expr = function (a, b, c) +{ + 'use strict'; + + assert (arguments[0] === 1); + assert (arguments[1] === undefined); + assert (arguments[2] === undefined); + + a = 'a'; + b = 'b'; + c = 'c'; + + assert (arguments[0] === 1); + assert (arguments[1] === undefined); + assert (arguments[2] === undefined); + + arguments [0] = 1; + arguments [1] = 'p'; + arguments [2] = 'q'; + + assert (a === 'a'); + assert (b === 'b'); + assert (c === 'c'); + + delete arguments [0]; + arguments[0] = 'new value'; + assert (a === 'a'); + + a = 'a'; + b = 'b'; + c = 'c'; + + assert (arguments[0] === 'new value'); + assert (arguments[1] === 'p'); + assert (arguments[2] === 'q'); +} + +fn_expr (1); diff --git a/tests/unit/test_preparser.cpp b/tests/unit/test_preparser.cpp index 6e5eb0eb2d..d551bb8aec 100644 --- a/tests/unit/test_preparser.cpp +++ b/tests/unit/test_preparser.cpp @@ -40,6 +40,10 @@ main (int __attr_unused___ argc, opcode_t opcodes[] = { + getop_meta (OPCODE_META_TYPE_SCOPE_CODE_FLAGS, // [ ] + OPCODE_SCOPE_CODE_FLAGS_NOT_REF_ARGUMENTS_IDENTIFIER + | OPCODE_SCOPE_CODE_FLAGS_NOT_REF_EVAL_IDENTIFIER, + INVALID_VALUE), getop_reg_var_decl (128, 129), // var tmp128 .. tmp129; getop_var_decl (0), // var a; getop_assignment (129, 1, 1), // tmp129 = 1: SMALLINT;