Skip to content

Commit a6bd26f

Browse files
Instantiation of Arguments object.
JerryScript-DCO-1.0-Signed-off-by: Ruben Ayrapetyan [email protected]
1 parent 3ed8aa1 commit a6bd26f

File tree

4 files changed

+193
-45
lines changed

4 files changed

+193
-45
lines changed

jerry-core/ecma/operations/ecma-function-object.cpp

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "ecma-lex-env.h"
2424
#include "ecma-objects.h"
2525
#include "ecma-objects-general.h"
26+
#include "ecma-objects-arguments.h"
2627
#include "ecma-try-catch-macro.h"
2728
#include "jrt.h"
2829

@@ -450,7 +451,45 @@ ecma_function_call_setup_args_variables (ecma_object_t *func_obj_p, /**< Functio
450451
* so instantiation of Arguments object here, in general, is supposed to not affect resource consumption
451452
* significantly.
452453
*/
453-
JERRY_UNIMPLEMENTED ("Instantiate Arguments object and setup 'arguments' implicit variable");
454+
455+
ecma_string_t *arguments_string_p = ecma_get_magic_string (ECMA_MAGIC_STRING_ARGUMENTS);
456+
457+
bool binding_already_declared = ecma_op_has_binding (env_p, arguments_string_p);
458+
459+
if (!binding_already_declared)
460+
{
461+
ecma_collection_iterator_init (&formal_params_iterator, formal_parameters_p);
462+
463+
ecma_object_t *args_obj_p = ecma_op_create_arguments_object (func_obj_p,
464+
env_p,
465+
formal_parameters_p,
466+
arguments_list_p,
467+
arguments_list_len,
468+
is_strict);
469+
470+
if (is_strict)
471+
{
472+
ecma_op_create_immutable_binding (env_p, arguments_string_p);
473+
ecma_op_initialize_immutable_binding (env_p, arguments_string_p, ecma_make_object_value (args_obj_p));
474+
}
475+
else
476+
{
477+
ecma_completion_value_t completion = ecma_op_create_mutable_binding (env_p,
478+
arguments_string_p,
479+
false);
480+
JERRY_ASSERT (ecma_is_completion_value_empty (completion));
481+
482+
completion = ecma_op_set_mutable_binding (env_p,
483+
arguments_string_p,
484+
ecma_make_object_value (args_obj_p),
485+
false);
486+
JERRY_ASSERT (ecma_is_completion_value_empty (completion));
487+
}
488+
489+
ecma_deref_object (args_obj_p);
490+
}
491+
492+
ecma_deref_ecma_string (arguments_string_p);
454493
}
455494

456495
return ecma_make_empty_completion_value ();

jerry-core/ecma/operations/ecma-objects-arguments.cpp

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,13 @@
4141
* @return pointer to newly created Arguments object
4242
*/
4343
ecma_object_t*
44-
ecma_create_arguments_object (ecma_object_t *func_obj_p, /**< callee function */
45-
ecma_object_t *lex_env_p, /**< lexical environment the Arguments
46-
object is created for */
47-
ecma_collection_iterator_t *formal_params_iter_p, /**< formal parameters
48-
collection iterator */
49-
const ecma_value_t *arguments_list_p, /**< list of arguments */
50-
ecma_length_t arguments_list_length, /**< length of arguments' list */
51-
bool is_strict) /**< flag indicating whether strict mode is enabled */
44+
ecma_op_create_arguments_object (ecma_object_t *func_obj_p, /**< callee function */
45+
ecma_object_t *lex_env_p, /**< lexical environment the Arguments
46+
object is created for */
47+
ecma_collection_header_t *formal_params_p, /**< formal parameters collection */
48+
const ecma_value_t *arguments_list_p, /**< list of arguments */
49+
ecma_length_t arguments_list_length, /**< length of arguments' list */
50+
bool is_strict) /**< flag indicating whether strict mode is enabled */
5251
{
5352
// 1.
5453
ecma_number_t *len_p = ecma_alloc_number ();
@@ -110,7 +109,7 @@ ecma_create_arguments_object (ecma_object_t *func_obj_p, /**< callee function */
110109
prop_desc.is_configurable = true;
111110
}
112111

113-
ecma_string_t *indx_string_p = ecma_new_ecma_string_from_number (ecma_uint32_to_number (indx));
112+
ecma_string_t *indx_string_p = ecma_new_ecma_string_from_uint32 (indx);
114113

115114
completion = ecma_op_object_define_own_property (obj_p,
116115
indx_string_p,
@@ -121,7 +120,11 @@ ecma_create_arguments_object (ecma_object_t *func_obj_p, /**< callee function */
121120
ecma_deref_ecma_string (indx_string_p);
122121
}
123122

124-
const ecma_length_t formal_params_number = formal_params_iter_p->header_p->unit_number;
123+
const ecma_length_t formal_params_number = formal_params_p->unit_number;
124+
125+
ecma_collection_iterator_t formal_params_iterator;
126+
ecma_collection_iterator_init (&formal_params_iterator, formal_params_p);
127+
125128
if (!is_strict
126129
&& arguments_list_length > 0
127130
&& formal_params_number > 0)
@@ -132,17 +135,17 @@ ecma_create_arguments_object (ecma_object_t *func_obj_p, /**< callee function */
132135
// 11.c
133136
MEM_DEFINE_LOCAL_ARRAY (formal_params, formal_params_number, ecma_string_t *);
134137

135-
JERRY_ASSERT (formal_params_iter_p->current_value_p == NULL);
138+
JERRY_ASSERT (formal_params_iterator.current_value_p == NULL);
136139
uint32_t param_index;
137140
for (param_index = 0;
138-
ecma_collection_iterator_next (formal_params_iter_p);
141+
ecma_collection_iterator_next (&formal_params_iterator);
139142
param_index++)
140143
{
141-
JERRY_ASSERT (formal_params_iter_p->current_value_p != NULL);
144+
JERRY_ASSERT (formal_params_iterator.current_value_p != NULL);
142145
JERRY_ASSERT (param_index < formal_params_number);
143146

144-
JERRY_ASSERT (ecma_is_value_string (*formal_params_iter_p->current_value_p));
145-
formal_params[param_index] = ecma_get_string_from_value (*formal_params_iter_p->current_value_p);
147+
JERRY_ASSERT (ecma_is_value_string (*formal_params_iterator.current_value_p));
148+
formal_params[param_index] = ecma_get_string_from_value (*formal_params_iterator.current_value_p);
146149
}
147150
JERRY_ASSERT (param_index == formal_params_number);
148151

@@ -162,17 +165,22 @@ ecma_create_arguments_object (ecma_object_t *func_obj_p, /**< callee function */
162165
if (ecma_compare_ecma_strings (name_p, formal_params[indx2]))
163166
{
164167
is_first_occurence = false;
168+
169+
break;
165170
}
166171
}
167172

168173
if (is_first_occurence)
169174
{
170-
ecma_string_t *indx_string_p = ecma_new_ecma_string_from_number (ecma_uint32_to_number ((uint32_t) indx));
175+
ecma_string_t *indx_string_p = ecma_new_ecma_string_from_uint32 ((uint32_t) indx);
171176

172177
prop_desc = ecma_make_empty_property_descriptor ();
173178
{
174179
prop_desc.is_value_defined = true;
175180
prop_desc.value = ecma_make_string_value (name_p);
181+
182+
prop_desc.is_configurable_defined = true;
183+
prop_desc.is_configurable = true;
176184
}
177185

178186
completion = ecma_op_object_define_own_property (map_p,
@@ -264,11 +272,14 @@ ecma_create_arguments_object (ecma_object_t *func_obj_p, /**< callee function */
264272
}
265273

266274
return obj_p;
267-
} /* ecma_create_arguments_object */
275+
} /* ecma_op_create_arguments_object */
268276

269277
/**
270278
* Get value of function's argument mapped to index of Arguments object.
271279
*
280+
* Note:
281+
* The procedure emulates execution of function described by MakeArgGetter
282+
*
272283
* @return completion value
273284
* Returned value must be freed with ecma_free_completion_value
274285
*/
@@ -437,32 +448,36 @@ ecma_op_arguments_object_define_own_property (ecma_object_t *obj_p, /**< the obj
437448
// i.
438449
if (property_desc_p->is_value_defined)
439450
{
440-
completion = ecma_op_object_put (map_p,
441-
property_name_p,
442-
property_desc_p->value,
443-
is_throw);
444-
}
451+
/* emulating execution of function described by MakeArgSetter */
452+
ecma_property_t *scope_prop_p = ecma_get_internal_property (map_p, ECMA_INTERNAL_PROPERTY_SCOPE);
453+
ecma_object_t *lex_env_p = ECMA_GET_NON_NULL_POINTER (ecma_object_t,
454+
scope_prop_p->u.internal_property.value);
445455

446-
if (unlikely (ecma_is_completion_value_throw (completion)))
447-
{
448-
ret_value = completion;
456+
ecma_property_t *mapped_prop_p = ecma_op_object_get_own_property (map_p, property_name_p);
457+
ecma_value_t arg_name_prop_value = ecma_get_named_data_property_value (mapped_prop_p);
458+
459+
ecma_string_t *arg_name_p = ecma_get_string_from_value (arg_name_prop_value);
460+
461+
completion = ecma_op_set_mutable_binding (lex_env_p,
462+
arg_name_p,
463+
property_desc_p->value,
464+
true);
465+
JERRY_ASSERT (ecma_is_completion_value_empty (completion));
449466
}
450-
else
451-
{
452-
// ii.
453-
if (property_desc_p->is_writable_defined
454-
&& !property_desc_p->is_writable)
455-
{
456-
completion = ecma_op_object_delete (map_p,
457-
property_name_p,
458-
false);
459467

460-
JERRY_ASSERT (ecma_is_completion_value_normal_true (completion));
461-
}
468+
// ii.
469+
if (property_desc_p->is_writable_defined
470+
&& !property_desc_p->is_writable)
471+
{
472+
completion = ecma_op_object_delete (map_p,
473+
property_name_p,
474+
false);
462475

463-
// 6.
464-
ret_value = ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_TRUE);
476+
JERRY_ASSERT (ecma_is_completion_value_normal_true (completion));
465477
}
478+
479+
// 6.
480+
ret_value = ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_TRUE);
466481
}
467482
}
468483
else

jerry-core/ecma/operations/ecma-objects-arguments.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
#include "ecma-helpers.h"
2121

2222
extern ecma_object_t*
23-
ecma_create_arguments_object (ecma_object_t *func_obj_p,
24-
ecma_object_t *lex_env_p,
25-
ecma_collection_iterator_t *formal_params_iter_p,
26-
const ecma_value_t *arguments_list_p,
27-
ecma_length_t arguments_list_length,
28-
bool is_strict);
23+
ecma_op_create_arguments_object (ecma_object_t *func_obj_p,
24+
ecma_object_t *lex_env_p,
25+
ecma_collection_header_t *formal_params_p,
26+
const ecma_value_t *arguments_list_p,
27+
ecma_length_t arguments_list_length,
28+
bool is_strict);
2929

3030
extern ecma_completion_value_t ecma_op_arguments_object_get (ecma_object_t *obj_p,
3131
ecma_string_t *property_name_p);

tests/jerry/arguments.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
function f (a, b, c)
2+
{
3+
return arguments;
4+
}
5+
6+
args = f();
7+
assert (args[0] === undefined);
8+
9+
args = f (1, 2, 3, 4, 5);
10+
assert (args[0] === 1);
11+
assert (args[1] === 2);
12+
assert (args[2] === 3);
13+
assert (args[3] === 4);
14+
assert (args[4] === 5);
15+
assert (args[5] === undefined);
16+
17+
assert (args.callee === f);
18+
assert (typeof args.caller === 'undefined');
19+
20+
function g (a, b, c)
21+
{
22+
assert (arguments[0] === 1);
23+
assert (arguments[1] === undefined);
24+
assert (arguments[2] === undefined);
25+
26+
a = 'a';
27+
b = 'b';
28+
c = 'c';
29+
30+
assert (arguments[0] === 'a');
31+
assert (arguments[1] === 'b');
32+
assert (arguments[2] === 'c');
33+
34+
arguments [0] = 1;
35+
arguments [1] = 2;
36+
arguments [2] = 3;
37+
38+
assert (a === 1);
39+
assert (b === 2);
40+
assert (c === 3);
41+
42+
delete arguments [0];
43+
arguments[0] = 'new value';
44+
assert (a === 1);
45+
46+
a = 'a';
47+
b = 'b';
48+
c = 'c';
49+
50+
assert (arguments[0] === 'new value');
51+
assert (arguments[1] === 'b');
52+
assert (arguments[2] === 'c');
53+
}
54+
55+
g (1);
56+
57+
fn_expr = function (a, b, c)
58+
{
59+
'use strict';
60+
61+
assert (arguments[0] === 1);
62+
assert (arguments[1] === undefined);
63+
assert (arguments[2] === undefined);
64+
65+
a = 'a';
66+
b = 'b';
67+
c = 'c';
68+
69+
assert (arguments[0] === 1);
70+
assert (arguments[1] === undefined);
71+
assert (arguments[2] === undefined);
72+
73+
arguments [0] = 1;
74+
arguments [1] = 'p';
75+
arguments [2] = 'q';
76+
77+
assert (a === 'a');
78+
assert (b === 'b');
79+
assert (c === 'c');
80+
81+
delete arguments [0];
82+
arguments[0] = 'new value';
83+
assert (a === 'a');
84+
85+
a = 'a';
86+
b = 'b';
87+
c = 'c';
88+
89+
assert (arguments[0] === 'new value');
90+
assert (arguments[1] === 'p');
91+
assert (arguments[2] === 'q');
92+
}
93+
94+
fn_expr (1);

0 commit comments

Comments
 (0)