Skip to content

Commit 3ed8aa1

Browse files
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 [email protected]
1 parent 0b2e183 commit 3ed8aa1

File tree

3 files changed

+80
-14
lines changed

3 files changed

+80
-14
lines changed

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

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,48 +37,59 @@
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
}
62+
if (do_instantiate_args_obj)
63+
{
64+
value |= (1u << do_instantiate_arguments_object_bit_offset);
65+
}
5866

5967
return value;
6068
} /* ecma_pack_code_internal_property_value */
6169

6270
/**
63-
* Unpack 'is_strict' flag and opcode index from value
71+
* Unpack 'is_strict', 'do_instantiate_arguments_object' flags and opcode index from value
6472
* that can be stored in an [[Code]] internal property.
6573
*
6674
* @return opcode index
6775
*/
6876
static opcode_counter_t
6977
ecma_unpack_code_internal_property_value (uint32_t value, /**< packed value */
70-
bool* out_is_strict_p) /**< out: is code strict? */
78+
bool* out_is_strict_p, /**< out: is code strict? */
79+
bool* out_do_instantiate_args_obj_p) /**< should an Arguments object be
80+
* instantiated for the code */
7181
{
7282
JERRY_ASSERT (out_is_strict_p != NULL);
83+
JERRY_ASSERT (out_do_instantiate_args_obj_p != NULL);
7384

7485
const uint32_t is_strict_bit_offset = (uint32_t) (sizeof (value) * JERRY_BITSINBYTE - 1);
86+
const uint32_t do_instantiate_arguments_object_bit_offset = (uint32_t) (sizeof (value) * JERRY_BITSINBYTE - 2);
7587

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));
88+
*out_is_strict_p = ((value & (1u << is_strict_bit_offset)) != 0);
89+
*out_do_instantiate_args_obj_p = ((value & (1u << do_instantiate_arguments_object_bit_offset)) != 0);
90+
value &= ~((1u << is_strict_bit_offset) | (1u << do_instantiate_arguments_object_bit_offset));
8091

81-
return opcode_idx;
92+
return (opcode_counter_t) value;
8293
} /* ecma_unpack_code_internal_property_value */
8394

8495
/**
@@ -144,6 +155,8 @@ ecma_op_create_function_object (ecma_string_t* formal_parameter_list_p[], /**< f
144155
ecma_length_t formal_parameters_number, /**< formal parameters list's length */
145156
ecma_object_t *scope_p, /**< function's scope */
146157
bool is_strict, /**< 'strict' flag */
158+
bool do_instantiate_arguments_object, /**< should an Arguments object be instantiated
159+
* for the function object upon call */
147160
opcode_counter_t first_opcode_idx) /**< index of first opcode of function's body */
148161
{
149162
// 1., 4., 13.
@@ -185,6 +198,7 @@ ecma_op_create_function_object (ecma_string_t* formal_parameter_list_p[], /**< f
185198
// 12.
186199
ecma_property_t *code_prop_p = ecma_create_internal_property (f, ECMA_INTERNAL_PROPERTY_CODE);
187200
code_prop_p->u.internal_property.value = ecma_pack_code_internal_property_value (is_strict,
201+
do_instantiate_arguments_object,
188202
first_opcode_idx);
189203

190204
// 14.
@@ -339,10 +353,11 @@ ecma_op_create_external_function_object (ecma_external_pointer_t code_p) /**< po
339353
} /* ecma_op_create_external_function_object */
340354

341355
/**
342-
* Setup variables for arguments listed in formal parameter list.
356+
* Setup variables for arguments listed in formal parameter list,
357+
* and, if necessary, Arguments object with 'arguments' binding.
343358
*
344359
* See also:
345-
* Declaration binding instantiation (ECMA-262 v5, 10.5), block 4
360+
* Declaration binding instantiation (ECMA-262 v5, 10.5), block 4 and 7
346361
*
347362
* @return completion value
348363
* Returned value must be freed with ecma_free_completion_value
@@ -352,7 +367,10 @@ ecma_function_call_setup_args_variables (ecma_object_t *func_obj_p, /**< Functio
352367
ecma_object_t *env_p, /**< lexical environment */
353368
const ecma_value_t *arguments_list_p, /**< arguments list */
354369
ecma_length_t arguments_list_len, /**< length of argument list */
355-
bool is_strict) /**< flag indicating strict mode */
370+
bool is_strict, /**< flag indicating strict mode */
371+
bool do_instantiate_arguments_object) /**< flag indicating whether
372+
* Arguments object should be
373+
* instantiated */
356374
{
357375
ecma_property_t *formal_parameters_prop_p = ecma_get_internal_property (func_obj_p,
358376
ECMA_INTERNAL_PROPERTY_FORMAL_PARAMETERS);
@@ -417,6 +435,24 @@ ecma_function_call_setup_args_variables (ecma_object_t *func_obj_p, /**< Functio
417435
}
418436
}
419437

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

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

545581
bool is_strict;
582+
bool do_instantiate_args_obj;
546583
// 8.
547-
opcode_counter_t code_first_opcode_idx = ecma_unpack_code_internal_property_value (code_prop_value, &is_strict);
584+
opcode_counter_t code_first_opcode_idx = ecma_unpack_code_internal_property_value (code_prop_value,
585+
&is_strict,
586+
&do_instantiate_args_obj);
548587

549588
ecma_value_t this_binding;
550589
// 1.
@@ -576,7 +615,8 @@ ecma_op_function_call (ecma_object_t *func_obj_p, /**< Function object */
576615
local_env_p,
577616
arguments_list_p,
578617
arguments_list_len,
579-
is_strict),
618+
is_strict,
619+
do_instantiate_args_obj),
580620
ret_value);
581621

582622
ecma_completion_value_t completion = vm_run_from_pos (code_first_opcode_idx,
@@ -768,14 +808,18 @@ ecma_op_function_declaration (ecma_object_t *lex_env_p, /**< lexical environment
768808
ecma_string_t* formal_parameter_list_p[], /**< formal parameters list */
769809
ecma_length_t formal_parameter_list_length, /**< length of formal parameters list */
770810
bool is_strict, /**< flag indicating if function is declared in strict mode code */
811+
bool do_instantiate_arguments_object, /**< flag, indicating whether an Arguments object
812+
* should be instantiated for the function object
813+
* upon call */
771814
bool is_configurable_bindings) /**< flag indicating whether function
772-
is declared in eval code */
815+
* is declared in eval code */
773816
{
774817
// b.
775818
ecma_object_t *func_obj_p = ecma_op_create_function_object (formal_parameter_list_p,
776819
formal_parameter_list_length,
777820
lex_env_p,
778821
is_strict,
822+
do_instantiate_arguments_object,
779823
function_code_opcode_idx);
780824

781825
// 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)