Instantiation of Arguments object.

JerryScript-DCO-1.0-Signed-off-by: Ruben Ayrapetyan r.ayrapetyan@samsung.com
This commit is contained in:
Ruben Ayrapetyan
2015-05-26 20:34:07 +03:00
parent ed4ff8e5bb
commit d4537eb0d1
4 changed files with 305 additions and 144 deletions
@@ -23,6 +23,7 @@
#include "ecma-lex-env.h" #include "ecma-lex-env.h"
#include "ecma-objects.h" #include "ecma-objects.h"
#include "ecma-objects-general.h" #include "ecma-objects-general.h"
#include "ecma-objects-arguments.h"
#include "ecma-try-catch-macro.h" #include "ecma-try-catch-macro.h"
#include "jrt.h" #include "jrt.h"
@@ -379,11 +380,8 @@ 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_p = ECMA_GET_POINTER (ecma_collection_header_t,
formal_parameters_prop_p->u.internal_property.value); 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_t formal_params_iterator;
@@ -435,6 +433,7 @@ ecma_function_call_setup_args_variables (ecma_object_t *func_obj_p, /**< Functio
JERRY_ASSERT (ecma_is_completion_value_empty (completion)); JERRY_ASSERT (ecma_is_completion_value_empty (completion));
} }
} }
}
if (do_instantiate_arguments_object) if (do_instantiate_arguments_object)
{ {
@@ -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 * so instantiation of Arguments object here, in general, is supposed to not affect resource consumption
* significantly. * 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 (); return ecma_make_empty_completion_value ();
@@ -41,11 +41,10 @@
* @return pointer to newly created Arguments object * @return pointer to newly created Arguments object
*/ */
ecma_object_t* ecma_object_t*
ecma_create_arguments_object (ecma_object_t *func_obj_p, /**< callee function */ ecma_op_create_arguments_object (ecma_object_t *func_obj_p, /**< callee function */
ecma_object_t *lex_env_p, /**< lexical environment the Arguments ecma_object_t *lex_env_p, /**< lexical environment the Arguments
object is created for */ object is created for */
ecma_collection_iterator_t *formal_params_iter_p, /**< formal parameters ecma_collection_header_t *formal_params_p, /**< formal parameters collection */
collection iterator */
const ecma_value_t *arguments_list_p, /**< list of arguments */ const ecma_value_t *arguments_list_p, /**< list of arguments */
ecma_length_t arguments_list_length, /**< length of arguments' list */ ecma_length_t arguments_list_length, /**< length of arguments' list */
bool is_strict) /**< flag indicating whether strict mode is enabled */ bool is_strict) /**< flag indicating whether strict mode is enabled */
@@ -110,7 +109,7 @@ ecma_create_arguments_object (ecma_object_t *func_obj_p, /**< callee function */
prop_desc.is_configurable = true; 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, completion = ecma_op_object_define_own_property (obj_p,
indx_string_p, indx_string_p,
@@ -121,7 +120,13 @@ ecma_create_arguments_object (ecma_object_t *func_obj_p, /**< callee function */
ecma_deref_ecma_string (indx_string_p); ecma_deref_ecma_string (indx_string_p);
} }
const ecma_length_t formal_params_number = formal_params_iter_p->header_p->unit_number; if (formal_params_p != NULL)
{
const ecma_length_t formal_params_number = formal_params_p->unit_number;
ecma_collection_iterator_t formal_params_iterator;
ecma_collection_iterator_init (&formal_params_iterator, formal_params_p);
if (!is_strict if (!is_strict
&& arguments_list_length > 0 && arguments_list_length > 0
&& formal_params_number > 0) && formal_params_number > 0)
@@ -132,17 +137,17 @@ ecma_create_arguments_object (ecma_object_t *func_obj_p, /**< callee function */
// 11.c // 11.c
MEM_DEFINE_LOCAL_ARRAY (formal_params, formal_params_number, ecma_string_t *); MEM_DEFINE_LOCAL_ARRAY (formal_params, formal_params_number, ecma_string_t *);
JERRY_ASSERT (formal_params_iter_p->current_value_p == NULL); JERRY_ASSERT (formal_params_iterator.current_value_p == NULL);
uint32_t param_index; uint32_t param_index;
for (param_index = 0; for (param_index = 0;
ecma_collection_iterator_next (formal_params_iter_p); ecma_collection_iterator_next (&formal_params_iterator);
param_index++) param_index++)
{ {
JERRY_ASSERT (formal_params_iter_p->current_value_p != NULL); JERRY_ASSERT (formal_params_iterator.current_value_p != NULL);
JERRY_ASSERT (param_index < formal_params_number); JERRY_ASSERT (param_index < formal_params_number);
JERRY_ASSERT (ecma_is_value_string (*formal_params_iter_p->current_value_p)); JERRY_ASSERT (ecma_is_value_string (*formal_params_iterator.current_value_p));
formal_params[param_index] = ecma_get_string_from_value (*formal_params_iter_p->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); JERRY_ASSERT (param_index == formal_params_number);
@@ -162,17 +167,22 @@ ecma_create_arguments_object (ecma_object_t *func_obj_p, /**< callee function */
if (ecma_compare_ecma_strings (name_p, formal_params[indx2])) if (ecma_compare_ecma_strings (name_p, formal_params[indx2]))
{ {
is_first_occurence = false; is_first_occurence = false;
break;
} }
} }
if (is_first_occurence) if (is_first_occurence)
{ {
ecma_string_t *indx_string_p = ecma_new_ecma_string_from_number (ecma_uint32_to_number ((uint32_t) indx)); ecma_string_t *indx_string_p = ecma_new_ecma_string_from_uint32 ((uint32_t) indx);
prop_desc = ecma_make_empty_property_descriptor (); prop_desc = ecma_make_empty_property_descriptor ();
{ {
prop_desc.is_value_defined = true; prop_desc.is_value_defined = true;
prop_desc.value = ecma_make_string_value (name_p); 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, completion = ecma_op_object_define_own_property (map_p,
@@ -200,6 +210,7 @@ ecma_create_arguments_object (ecma_object_t *func_obj_p, /**< callee function */
ecma_deref_object (map_p); ecma_deref_object (map_p);
} }
}
// 13. // 13.
if (!is_strict) if (!is_strict)
@@ -264,11 +275,14 @@ ecma_create_arguments_object (ecma_object_t *func_obj_p, /**< callee function */
} }
return obj_p; return obj_p;
} /* ecma_create_arguments_object */ } /* ecma_op_create_arguments_object */
/** /**
* Get value of function's argument mapped to index of 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 * @return completion value
* Returned value must be freed with ecma_free_completion_value * Returned value must be freed with ecma_free_completion_value
*/ */
@@ -437,18 +451,23 @@ ecma_op_arguments_object_define_own_property (ecma_object_t *obj_p, /**< the obj
// i. // i.
if (property_desc_p->is_value_defined) if (property_desc_p->is_value_defined)
{ {
completion = ecma_op_object_put (map_p, /* emulating execution of function described by MakeArgSetter */
property_name_p, 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, property_desc_p->value,
is_throw); true);
JERRY_ASSERT (ecma_is_completion_value_empty (completion));
} }
if (unlikely (ecma_is_completion_value_throw (completion)))
{
ret_value = completion;
}
else
{
// ii. // ii.
if (property_desc_p->is_writable_defined if (property_desc_p->is_writable_defined
&& !property_desc_p->is_writable) && !property_desc_p->is_writable)
@@ -464,7 +483,6 @@ ecma_op_arguments_object_define_own_property (ecma_object_t *obj_p, /**< the obj
ret_value = ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_TRUE); ret_value = ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_TRUE);
} }
} }
}
else else
{ {
ret_value = ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_TRUE); ret_value = ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_TRUE);
@@ -20,9 +20,9 @@
#include "ecma-helpers.h" #include "ecma-helpers.h"
extern ecma_object_t* extern ecma_object_t*
ecma_create_arguments_object (ecma_object_t *func_obj_p, ecma_op_create_arguments_object (ecma_object_t *func_obj_p,
ecma_object_t *lex_env_p, ecma_object_t *lex_env_p,
ecma_collection_iterator_t *formal_params_iter_p, ecma_collection_header_t *formal_params_p,
const ecma_value_t *arguments_list_p, const ecma_value_t *arguments_list_p,
ecma_length_t arguments_list_length, ecma_length_t arguments_list_length,
bool is_strict); bool is_strict);
+108
View File
@@ -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);