From d4537eb0d1824ee7175541106b6f23ec97ddb038 Mon Sep 17 00:00:00 2001 From: Ruben Ayrapetyan Date: Tue, 26 May 2015 20:34:07 +0300 Subject: [PATCH] Instantiation of Arguments object. JerryScript-DCO-1.0-Signed-off-by: Ruben Ayrapetyan r.ayrapetyan@samsung.com --- .../ecma/operations/ecma-function-object.cpp | 123 +++++++---- .../operations/ecma-objects-arguments.cpp | 206 ++++++++++-------- .../ecma/operations/ecma-objects-arguments.h | 12 +- tests/jerry/arguments.js | 108 +++++++++ 4 files changed, 305 insertions(+), 144 deletions(-) create mode 100644 tests/jerry/arguments.js diff --git a/jerry-core/ecma/operations/ecma-function-object.cpp b/jerry-core/ecma/operations/ecma-function-object.cpp index 54647f185..1a8918a37 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" @@ -379,60 +380,58 @@ 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) + for (size_t n = 0; + n < formal_parameters_count; + n++) { - 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); - - 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)) + ecma_value_t v; + if (n >= arguments_list_len) { - return completion; + v = ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED); + } + else + { + v = arguments_list_p[n]; } - JERRY_ASSERT (ecma_is_completion_value_empty (completion)); + bool is_moved = ecma_collection_iterator_next (&formal_params_iterator); + JERRY_ASSERT (is_moved); - completion = ecma_op_set_mutable_binding (env_p, - formal_parameter_name_string_p, - v, - is_strict); + 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); - 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)); + } } } @@ -451,7 +450,43 @@ ecma_function_call_setup_args_variables (ecma_object_t *func_obj_p, /**< Functio * so instantiation of Arguments object here, in general, is supposed to not affect resource consumption * significantly. */ - JERRY_UNIMPLEMENTED ("Instantiate Arguments object and setup 'arguments' implicit variable"); + + 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 (is_strict) + { + 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)); + } + + ecma_deref_object (args_obj_p); + } + + ecma_deref_ecma_string (arguments_string_p); } return ecma_make_empty_completion_value (); diff --git a/jerry-core/ecma/operations/ecma-objects-arguments.cpp b/jerry-core/ecma/operations/ecma-objects-arguments.cpp index 505ad1628..77200632f 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])) + 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); + + 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++) { - is_first_occurence = false; + if (ecma_compare_ecma_strings (name_p, formal_params[indx2])) + { + is_first_occurence = false; + + break; + } + } + + 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; + } + + 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); } } - if (is_first_occurence) - { - ecma_string_t *indx_string_p = ecma_new_ecma_string_from_number (ecma_uint32_to_number ((uint32_t) indx)); + MEM_FINALIZE_LOCAL_ARRAY (formal_params); - prop_desc = ecma_make_empty_property_descriptor (); - { - prop_desc.is_value_defined = true; - prop_desc.value = ecma_make_string_value (name_p); - } + // 12. + ecma_set_object_type (obj_p, ECMA_OBJECT_TYPE_ARGUMENTS); - 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_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_deref_ecma_string (indx_string_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); } - - MEM_FINALIZE_LOCAL_ARRAY (formal_params); - - // 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 *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); } // 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); + + 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)); } - if (unlikely (ecma_is_completion_value_throw (completion))) + // ii. + if (property_desc_p->is_writable_defined + && !property_desc_p->is_writable) { - ret_value = 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); + completion = ecma_op_object_delete (map_p, + property_name_p, + false); - JERRY_ASSERT (ecma_is_completion_value_normal_true (completion)); - } - - // 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 ebe558aa4..5c3c2314a 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/tests/jerry/arguments.js b/tests/jerry/arguments.js new file mode 100644 index 000000000..e2a5724c2 --- /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);