Skip to content

Commit b37d7fd

Browse files
author
Ruben Ayrapetyan
committed
Introducing a function object's flag indicating whether Arguments object should be instantiated upon call of the function.
The Arguments object is supposed to be unnecessary if function's code: - doesn't reference 'arguments' identifier; - doesn't reference 'eval' identifier (so, it doesn't perform direct call to eval). JerryScript-DCO-1.0-Signed-off-by: Ruben Ayrapetyan r.ayrapetyan@samsung.com
1 parent 5f9d7a2 commit b37d7fd

File tree

3 files changed

+81
-14
lines changed

3 files changed

+81
-14
lines changed

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

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,48 +37,60 @@
3737
*/
3838

3939
/**
40-
* Pack 'is_strict' flag and opcode index to value
40+
* Pack 'is_strict', 'do_instantiate_arguments_object' flags and opcode index to value
4141
* that can be stored in an [[Code]] internal property.
4242
*
4343
* @return packed value
4444
*/
4545
static uint32_t
4646
ecma_pack_code_internal_property_value (bool is_strict, /**< is code strict? */
47+
bool do_instantiate_args_obj, /**< should an Arguments object be
48+
* instantiated for the code */
4749
opcode_counter_t opcode_idx) /**< index of first opcode */
4850
{
4951
uint32_t value = opcode_idx;
5052
const uint32_t is_strict_bit_offset = (uint32_t) (sizeof (value) * JERRY_BITSINBYTE - 1);
53+
const uint32_t do_instantiate_arguments_object_bit_offset = (uint32_t) (sizeof (value) * JERRY_BITSINBYTE - 2);
5154

5255
JERRY_ASSERT (((value) & (1u << is_strict_bit_offset)) == 0);
56+
JERRY_ASSERT (((value) & (1u << do_instantiate_arguments_object_bit_offset)) == 0);
5357

5458
if (is_strict)
5559
{
5660
value |= (1u << is_strict_bit_offset);
5761
}
5862

63+
if (do_instantiate_args_obj)
64+
{
65+
value |= (1u << do_instantiate_arguments_object_bit_offset);
66+
}
67+
5968
return value;
6069
} /* ecma_pack_code_internal_property_value */
6170

6271
/**
63-
* Unpack 'is_strict' flag and opcode index from value
72+
* Unpack 'is_strict', 'do_instantiate_arguments_object' flags and opcode index from value
6473
* that can be stored in an [[Code]] internal property.
6574
*
6675
* @return opcode index
6776
*/
6877
static opcode_counter_t
6978
ecma_unpack_code_internal_property_value (uint32_t value, /**< packed value */
70-
bool* out_is_strict_p) /**< out: is code strict? */
79+
bool* out_is_strict_p, /**< out: is code strict? */
80+
bool* out_do_instantiate_args_obj_p) /**< should an Arguments object be
81+
* instantiated for the code */
7182
{
7283
JERRY_ASSERT (out_is_strict_p != NULL);
84+
JERRY_ASSERT (out_do_instantiate_args_obj_p != NULL);
7385

7486
const uint32_t is_strict_bit_offset = (uint32_t) (sizeof (value) * JERRY_BITSINBYTE - 1);
87+
const uint32_t do_instantiate_arguments_object_bit_offset = (uint32_t) (sizeof (value) * JERRY_BITSINBYTE - 2);
7588

76-
bool is_strict = ((value & (1u << is_strict_bit_offset)) != 0);
77-
*out_is_strict_p = is_strict;
78-
79-
opcode_counter_t opcode_idx = (opcode_counter_t) (value & ~(1u << is_strict_bit_offset));
89+
*out_is_strict_p = ((value & (1u << is_strict_bit_offset)) != 0);
90+
*out_do_instantiate_args_obj_p = ((value & (1u << do_instantiate_arguments_object_bit_offset)) != 0);
91+
value &= ~((1u << is_strict_bit_offset) | (1u << do_instantiate_arguments_object_bit_offset));
8092

81-
return opcode_idx;
93+
return (opcode_counter_t) value;
8294
} /* ecma_unpack_code_internal_property_value */
8395

8496
/**
@@ -144,6 +156,8 @@ ecma_op_create_function_object (ecma_string_t* formal_parameter_list_p[], /**< f
144156
ecma_length_t formal_parameters_number, /**< formal parameters list's length */
145157
ecma_object_t *scope_p, /**< function's scope */
146158
bool is_strict, /**< 'strict' flag */
159+
bool do_instantiate_arguments_object, /**< should an Arguments object be instantiated
160+
* for the function object upon call */
147161
opcode_counter_t first_opcode_idx) /**< index of first opcode of function's body */
148162
{
149163
// 1., 4., 13.
@@ -185,6 +199,7 @@ ecma_op_create_function_object (ecma_string_t* formal_parameter_list_p[], /**< f
185199
// 12.
186200
ecma_property_t *code_prop_p = ecma_create_internal_property (f, ECMA_INTERNAL_PROPERTY_CODE);
187201
code_prop_p->u.internal_property.value = ecma_pack_code_internal_property_value (is_strict,
202+
do_instantiate_arguments_object,
188203
first_opcode_idx);
189204

190205
// 14.
@@ -339,10 +354,11 @@ ecma_op_create_external_function_object (ecma_external_pointer_t code_p) /**< po
339354
} /* ecma_op_create_external_function_object */
340355

341356
/**
342-
* Setup variables for arguments listed in formal parameter list.
357+
* Setup variables for arguments listed in formal parameter list,
358+
* and, if necessary, Arguments object with 'arguments' binding.
343359
*
344360
* See also:
345-
* Declaration binding instantiation (ECMA-262 v5, 10.5), block 4
361+
* Declaration binding instantiation (ECMA-262 v5, 10.5), block 4 and 7
346362
*
347363
* @return completion value
348364
* Returned value must be freed with ecma_free_completion_value
@@ -352,7 +368,10 @@ ecma_function_call_setup_args_variables (ecma_object_t *func_obj_p, /**< Functio
352368
ecma_object_t *env_p, /**< lexical environment */
353369
const ecma_value_t *arguments_list_p, /**< arguments list */
354370
ecma_length_t arguments_list_len, /**< length of argument list */
355-
bool is_strict) /**< flag indicating strict mode */
371+
bool is_strict, /**< flag indicating strict mode */
372+
bool do_instantiate_arguments_object) /**< flag indicating whether
373+
* Arguments object should be
374+
* instantiated */
356375
{
357376
ecma_property_t *formal_parameters_prop_p = ecma_get_internal_property (func_obj_p,
358377
ECMA_INTERNAL_PROPERTY_FORMAL_PARAMETERS);
@@ -417,6 +436,24 @@ ecma_function_call_setup_args_variables (ecma_object_t *func_obj_p, /**< Functio
417436
}
418437
}
419438

439+
if (do_instantiate_arguments_object)
440+
{
441+
/*
442+
* According to ECMA-262 v5, 10.5, the Arguments object should be instantiated
443+
* after instantiating declared functions, and only if there is no binding named 'arguments'
444+
* by that time.
445+
*
446+
* However, we can setup Arguments object and 'arguments' binding here, because:
447+
* - instantiation of Arguments object itself doesn't have any side effects;
448+
* - if 'arguments' is name of a declared function in current scope,
449+
* value of the binding would be overwritten, execution would proceed in correct state.
450+
* - declaration of function, named 'arguments', is considered to be unrecommended (and so, rare) case,
451+
* so instantiation of Arguments object here, in general, is supposed to not affect resource consumption
452+
* significantly.
453+
*/
454+
JERRY_UNIMPLEMENTED ("Instantiate Arguments object and setup 'arguments' implicit variable");
455+
}
456+
420457
return ecma_make_empty_completion_value ();
421458
} /* ecma_function_call_setup_args_variables */
422459

@@ -543,8 +580,11 @@ ecma_op_function_call (ecma_object_t *func_obj_p, /**< Function object */
543580
uint32_t code_prop_value = code_prop_p->u.internal_property.value;
544581

545582
bool is_strict;
583+
bool do_instantiate_args_obj;
546584
// 8.
547-
opcode_counter_t code_first_opcode_idx = ecma_unpack_code_internal_property_value (code_prop_value, &is_strict);
585+
opcode_counter_t code_first_opcode_idx = ecma_unpack_code_internal_property_value (code_prop_value,
586+
&is_strict,
587+
&do_instantiate_args_obj);
548588

549589
ecma_value_t this_binding;
550590
// 1.
@@ -576,7 +616,8 @@ ecma_op_function_call (ecma_object_t *func_obj_p, /**< Function object */
576616
local_env_p,
577617
arguments_list_p,
578618
arguments_list_len,
579-
is_strict),
619+
is_strict,
620+
do_instantiate_args_obj),
580621
ret_value);
581622

582623
ecma_completion_value_t completion = vm_run_from_pos (code_first_opcode_idx,
@@ -769,14 +810,18 @@ ecma_op_function_declaration (ecma_object_t *lex_env_p, /**< lexical environment
769810
ecma_string_t* formal_parameter_list_p[], /**< formal parameters list */
770811
ecma_length_t formal_parameter_list_length, /**< length of formal parameters list */
771812
bool is_strict, /**< flag indicating if function is declared in strict mode code */
813+
bool do_instantiate_arguments_object, /**< flag, indicating whether an Arguments object
814+
* should be instantiated for the function object
815+
* upon call */
772816
bool is_configurable_bindings) /**< flag indicating whether function
773-
is declared in eval code */
817+
* is declared in eval code */
774818
{
775819
// b.
776820
ecma_object_t *func_obj_p = ecma_op_create_function_object (formal_parameter_list_p,
777821
formal_parameter_list_length,
778822
lex_env_p,
779823
is_strict,
824+
do_instantiate_arguments_object,
780825
function_code_opcode_idx);
781826

782827
// c.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ ecma_op_create_function_object (ecma_string_t* formal_parameter_list_p[],
3434
ecma_length_t formal_parameters_number,
3535
ecma_object_t *scope_p,
3636
bool is_strict,
37+
bool do_instantiate_arguments_object,
3738
opcode_counter_t first_opcode_idx);
3839
extern ecma_object_t*
3940
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,
6061
ecma_string_t* formal_parameter_list_p[],
6162
ecma_length_t formal_parameter_list_length,
6263
bool is_strict,
64+
bool do_instantiate_arguments_object,
6365
bool is_configurable_bindings);
6466

6567
/**

jerry-core/vm/opcodes.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ function_declaration (int_data_t *int_data, /**< interpreter context */
443443
ecma_length_t args_number) /**< number of arguments */
444444
{
445445
bool is_strict = int_data->is_strict;
446+
bool do_instantiate_arguments_object = true;
446447
const bool is_configurable_bindings = int_data->is_eval_code;
447448

448449
const opcode_counter_t function_code_end_oc = (opcode_counter_t) (
@@ -455,6 +456,14 @@ function_declaration (int_data_t *int_data, /**< interpreter context */
455456
{
456457
is_strict = true;
457458
}
459+
if ((scope_flags & OPCODE_SCOPE_CODE_FLAGS_NOT_REF_ARGUMENTS_IDENTIFIER)
460+
&& (scope_flags & OPCODE_SCOPE_CODE_FLAGS_NOT_REF_EVAL_IDENTIFIER))
461+
{
462+
/* the code doesn't use 'arguments' identifier
463+
* and doesn't perform direct call to eval,
464+
* so Arguments object can't be referenced */
465+
do_instantiate_arguments_object = false;
466+
}
458467

459468
ecma_string_t *function_name_string_p = ecma_new_ecma_string_from_lit_index (function_name_lit_id);
460469

@@ -464,6 +473,7 @@ function_declaration (int_data_t *int_data, /**< interpreter context */
464473
args_names,
465474
args_number,
466475
is_strict,
476+
do_instantiate_arguments_object,
467477
is_configurable_bindings);
468478
ecma_deref_ecma_string (function_name_string_p);
469479

@@ -541,6 +551,7 @@ opfunc_func_expr_n (opcode_t opdata, /**< operation data */
541551
fill_params_list (int_data, params_number, params_names);
542552

543553
bool is_strict = int_data->is_strict;
554+
bool do_instantiate_arguments_object = true;
544555

545556
function_code_end_oc = (opcode_counter_t) (read_meta_opcode_counter (OPCODE_META_TYPE_FUNCTION_END,
546557
int_data) + int_data->pos);
@@ -552,6 +563,14 @@ opfunc_func_expr_n (opcode_t opdata, /**< operation data */
552563
{
553564
is_strict = true;
554565
}
566+
if ((scope_flags & OPCODE_SCOPE_CODE_FLAGS_NOT_REF_ARGUMENTS_IDENTIFIER)
567+
&& (scope_flags & OPCODE_SCOPE_CODE_FLAGS_NOT_REF_EVAL_IDENTIFIER))
568+
{
569+
/* the code doesn't use 'arguments' identifier
570+
* and doesn't perform direct call to eval,
571+
* so Arguments object can't be referenced */
572+
do_instantiate_arguments_object = false;
573+
}
555574

556575
ecma_object_t *scope_p;
557576
ecma_string_t *function_name_string_p = NULL;
@@ -576,6 +595,7 @@ opfunc_func_expr_n (opcode_t opdata, /**< operation data */
576595
params_number,
577596
scope_p,
578597
is_strict,
598+
do_instantiate_arguments_object,
579599
int_data->pos);
580600

581601
ret_value = set_variable_value (int_data, lit_oc,

0 commit comments

Comments
 (0)