Skip to content

Commit f1dcdd4

Browse files
committed
Implement function name support for script functions and classes
JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik [email protected]
1 parent 589af6d commit f1dcdd4

28 files changed

+1014
-308
lines changed

jerry-core/api/jerry-snapshot.c

Lines changed: 251 additions & 34 deletions
Large diffs are not rendered by default.

jerry-core/api/jerry.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ jerry_parse (const jerry_char_t *resource_name_p, /**< resource name (usually a
430430
#if ENABLED (JERRY_PARSER)
431431
jerry_assert_api_available ();
432432

433-
#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) || ENABLED (JERRY_ES2015_MODULE_SYSTEM)
433+
#if ENABLED (JERRY_RESOURCE_NAME)
434434
if (resource_name_length == 0)
435435
{
436436
JERRY_CONTEXT (resource_name) = ecma_make_magic_string_value (LIT_MAGIC_STRING_RESOURCE_ANON);
@@ -440,7 +440,7 @@ jerry_parse (const jerry_char_t *resource_name_p, /**< resource name (usually a
440440
JERRY_CONTEXT (resource_name) = ecma_find_or_create_literal_string (resource_name_p,
441441
(lit_utf8_size_t) resource_name_length);
442442
}
443-
#endif /* ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) || ENABLED (JERRY_ES2015_MODULE_SYSTEM) */
443+
#endif /* ENABLED (JERRY_RESOURCE_NAME) */
444444

445445
ecma_compiled_code_t *bytecode_data_p;
446446
ecma_value_t parse_status;
@@ -508,7 +508,7 @@ jerry_parse_function (const jerry_char_t *resource_name_p, /**< resource name (u
508508
ecma_compiled_code_t *bytecode_data_p;
509509
ecma_value_t parse_status;
510510

511-
#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) || ENABLED (JERRY_ES2015_MODULE_SYSTEM)
511+
#if ENABLED (JERRY_RESOURCE_NAME)
512512
if (resource_name_length == 0)
513513
{
514514
JERRY_CONTEXT (resource_name) = ecma_make_magic_string_value (LIT_MAGIC_STRING_RESOURCE_ANON);
@@ -518,7 +518,7 @@ jerry_parse_function (const jerry_char_t *resource_name_p, /**< resource name (u
518518
JERRY_CONTEXT (resource_name) = ecma_find_or_create_literal_string (resource_name_p,
519519
(lit_utf8_size_t) resource_name_length);
520520
}
521-
#endif /* ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) || ENABLED (JERRY_ES2015_MODULE_SYSTEM) */
521+
#endif /* ENABLED (JERRY_RESOURCE_NAME) */
522522

523523
if (arg_list_p == NULL)
524524
{
@@ -3549,15 +3549,15 @@ jerry_get_backtrace (uint32_t max_depth) /**< depth limit of the backtrace */
35493549
jerry_value_t
35503550
jerry_get_resource_name (const jerry_value_t value) /**< jerry api value */
35513551
{
3552-
#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ES2015_MODULE_SYSTEM)
3552+
#if ENABLED (JERRY_RESOURCE_NAME)
35533553
if (ecma_is_value_undefined (value))
35543554
{
35553555
if (JERRY_CONTEXT (vm_top_context_p) != NULL)
35563556
{
35573557
return ecma_copy_value (JERRY_CONTEXT (vm_top_context_p)->resource_name);
35583558
}
35593559
}
3560-
#endif /* ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ES2015_MODULE_SYSTEM) */
3560+
#endif /* ENABLED (JERRY_RESOURCE_NAME) */
35613561
#if ENABLED (JERRY_LINE_INFO)
35623562
else if (ecma_is_value_object (value))
35633563
{

jerry-core/config.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,4 +684,13 @@
684684
# define JERRY_ES2015_BUILTIN_CONTAINER 0
685685
#endif
686686

687+
/**
688+
* Resource name relatey types into a single guard
689+
*/
690+
#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) || ENABLED (JERRY_ES2015_MODULE_SYSTEM)
691+
# define JERRY_RESOURCE_NAME 1
692+
#else
693+
# define JERRY_RESOURCE_NAME 0
694+
#endif
695+
687696
#endif /* !JERRYSCRIPT_CONFIG_H */

jerry-core/ecma/base/ecma-globals.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ enum
206206
ECMA_VALUE_UNINITIALIZED = ECMA_MAKE_VALUE (10), /**< a special value for uninitialized let/const declarations */
207207
ECMA_VALUE_SPREAD_ELEMENT = ECMA_MAKE_VALUE (11), /**< a special value for spread elements in array initialization
208208
* or function call argument list */
209+
ECMA_VALUE_ANONYMOUS = ECMA_MAKE_VALUE (12), /**< represents anonymous function declaration */
209210
};
210211

211212
#if !ENABLED (JERRY_NUMBER_TYPE_FLOAT64)

jerry-core/ecma/base/ecma-helpers.c

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,19 +1439,11 @@ ecma_bytecode_deref (ecma_compiled_code_t *bytecode_p) /**< byte code pointer */
14391439
#endif /* ENABLED (JERRY_DEBUGGER) */
14401440

14411441
#if ENABLED (JERRY_ES2015)
1442+
ecma_value_t *base_p = ecma_compiled_code_resolve_arguments_start (bytecode_p);
1443+
14421444
if (bytecode_p->status_flags & CBC_CODE_FLAG_HAS_TAGGED_LITERALS)
14431445
{
1444-
ecma_length_t formal_params_number = ecma_compiled_code_get_formal_params (bytecode_p);
1445-
1446-
uint8_t *byte_p = (uint8_t *) bytecode_p;
1447-
byte_p += ((size_t) bytecode_p->size) << JMEM_ALIGNMENT_LOG;
1448-
1449-
ecma_value_t *tagged_base_p = (ecma_value_t *) byte_p;
1450-
tagged_base_p -= formal_params_number;
1451-
1452-
ecma_collection_t *coll_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_collection_t, tagged_base_p[-1]);
1453-
1454-
ecma_collection_destroy (coll_p);
1446+
ecma_collection_destroy (ECMA_GET_INTERNAL_VALUE_POINTER (ecma_collection_t, base_p[-2]));
14551447
}
14561448
#endif /* ENABLED (JERRY_ES2015) */
14571449

@@ -1484,17 +1476,18 @@ ecma_compiled_code_get_tagged_template_collection (const ecma_compiled_code_t *b
14841476
JERRY_ASSERT (bytecode_header_p != NULL);
14851477
JERRY_ASSERT (bytecode_header_p->status_flags & CBC_CODE_FLAG_HAS_TAGGED_LITERALS);
14861478

1487-
uint8_t *byte_p = (uint8_t *) bytecode_header_p;
1488-
byte_p += ((size_t) bytecode_header_p->size) << JMEM_ALIGNMENT_LOG;
1479+
ecma_value_t *tagged_base_p = ecma_compiled_code_resolve_arguments_start (bytecode_header_p);
14891480

1490-
ecma_value_t *tagged_base_p = (ecma_value_t *) byte_p;
1491-
tagged_base_p -= ecma_compiled_code_get_formal_params (bytecode_header_p);
1481+
if (!(bytecode_header_p->status_flags & CBC_CODE_FLAGS_CLASS_CONSTRUCTOR))
1482+
{
1483+
tagged_base_p--;
1484+
}
14921485

14931486
return ECMA_GET_INTERNAL_VALUE_POINTER (ecma_collection_t, tagged_base_p[-1]);
14941487
} /* ecma_compiled_code_get_tagged_template_collection */
14951488
#endif /* ENABLED (JERRY_ES2015) */
14961489

1497-
#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ES2015_MODULE_SYSTEM) || ENABLED (JERRY_ES2015)
1490+
#if ENABLED (JERRY_RESOURCE_NAME) || ENABLED (JERRY_ES2015)
14981491
/**
14991492
* Get the number of formal parameters of the compiled code
15001493
*
@@ -1515,7 +1508,35 @@ ecma_compiled_code_get_formal_params (const ecma_compiled_code_t *bytecode_heade
15151508

15161509
return ((cbc_uint8_arguments_t *) bytecode_header_p)->argument_end;
15171510
} /* ecma_compiled_code_get_formal_params */
1518-
#endif /* ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ES2015_MODULE_SYSTEM) || ENABLED (JERRY_ES2015) */
1511+
1512+
/**
1513+
* Resolve the position of the arguments list start of the compiled code
1514+
*
1515+
* @return start position of the arguments list start of the compiled code
1516+
*/
1517+
ecma_value_t *
1518+
ecma_compiled_code_resolve_arguments_start (const ecma_compiled_code_t *bytecode_header_p)
1519+
{
1520+
JERRY_ASSERT (bytecode_header_p != NULL);
1521+
1522+
uint8_t *byte_p = (uint8_t *) bytecode_header_p;
1523+
byte_p += ((size_t) bytecode_header_p->size) << JMEM_ALIGNMENT_LOG;
1524+
1525+
return ((ecma_value_t *) byte_p) - ecma_compiled_code_get_formal_params (bytecode_header_p);
1526+
} /* ecma_compiled_code_resolve_arguments_start */
1527+
1528+
/**
1529+
* Resolve the position of the function name of the compiled code
1530+
*
1531+
* @return position of the function name of the compiled code
1532+
*/
1533+
inline ecma_value_t * JERRY_ATTR_ALWAYS_INLINE
1534+
ecma_compiled_code_resolve_function_name (const ecma_compiled_code_t *bytecode_header_p)
1535+
{
1536+
JERRY_ASSERT (bytecode_header_p != NULL);
1537+
return ecma_compiled_code_resolve_arguments_start (bytecode_header_p) - 1;
1538+
} /* ecma_compiled_code_resolve_function_name */
1539+
#endif /* ENABLED (JERRY_RESOURCE_NAME) || ENABLED (JERRY_ES2015) */
15191540

15201541
#if (JERRY_STACK_LIMIT != 0)
15211542
/**

jerry-core/ecma/base/ecma-helpers.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,9 +488,11 @@ void ecma_bytecode_deref (ecma_compiled_code_t *bytecode_p);
488488
#if ENABLED (JERRY_ES2015)
489489
ecma_collection_t *ecma_compiled_code_get_tagged_template_collection (const ecma_compiled_code_t *bytecode_header_p);
490490
#endif /* ENABLED (JERRY_ES2015) */
491-
#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ES2015_MODULE_SYSTEM) || ENABLED (JERRY_ES2015)
491+
#if ENABLED (JERRY_RESOURCE_NAME) || ENABLED (JERRY_ES2015)
492492
ecma_length_t ecma_compiled_code_get_formal_params (const ecma_compiled_code_t *bytecode_p);
493-
#endif /* ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ES2015_MODULE_SYSTEM) || ENABLED (JERRY_ES2015) */
493+
ecma_value_t *ecma_compiled_code_resolve_arguments_start (const ecma_compiled_code_t *bytecode_header_p);
494+
ecma_value_t *ecma_compiled_code_resolve_function_name (const ecma_compiled_code_t *bytecode_header_p);
495+
#endif /* ENABLED (JERRY_RESOURCE_NAME) || ENABLED (JERRY_ES2015) */
494496
#if (JERRY_STACK_LIMIT != 0)
495497
uintptr_t ecma_get_current_stack_usage (void);
496498
#endif /* (JERRY_STACK_LIMIT != 0) */

jerry-core/ecma/base/ecma-literal-storage.c

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ ecma_save_literals_add_compiled_code (const ecma_compiled_code_t *compiled_code_
329329
ecma_collection_t *lit_pool_p) /**< list of known values */
330330
{
331331
ecma_value_t *literal_p;
332-
uint32_t argument_end = 0;
332+
uint32_t argument_end;
333333
uint32_t register_end;
334334
uint32_t const_literal_end;
335335
uint32_t literal_end;
@@ -345,11 +345,7 @@ ecma_save_literals_add_compiled_code (const ecma_compiled_code_t *compiled_code_
345345
register_end = args_p->register_end;
346346
const_literal_end = args_p->const_literal_end - register_end;
347347
literal_end = args_p->literal_end - register_end;
348-
349-
if (compiled_code_p->status_flags & CBC_CODE_FLAGS_MAPPED_ARGUMENTS_NEEDED)
350-
{
351-
argument_end = args_p->argument_end;
352-
}
348+
argument_end = args_p->argument_end;
353349
}
354350
else
355351
{
@@ -360,16 +356,15 @@ ecma_save_literals_add_compiled_code (const ecma_compiled_code_t *compiled_code_
360356
register_end = args_p->register_end;
361357
const_literal_end = args_p->const_literal_end - register_end;
362358
literal_end = args_p->literal_end - register_end;
363-
364-
if (compiled_code_p->status_flags & CBC_CODE_FLAGS_MAPPED_ARGUMENTS_NEEDED)
365-
{
366-
argument_end = args_p->argument_end;
367-
}
359+
argument_end = args_p->argument_end;
368360
}
369361

370-
for (uint32_t i = 0; i < argument_end; i++)
362+
if (compiled_code_p->status_flags & CBC_CODE_FLAGS_MAPPED_ARGUMENTS_NEEDED)
371363
{
372-
ecma_save_literals_append_value (literal_p[i], lit_pool_p);
364+
for (uint32_t i = 0; i < argument_end; i++)
365+
{
366+
ecma_save_literals_append_value (literal_p[i], lit_pool_p);
367+
}
373368
}
374369

375370
for (uint32_t i = 0; i < const_literal_end; i++)
@@ -389,17 +384,36 @@ ecma_save_literals_add_compiled_code (const ecma_compiled_code_t *compiled_code_
389384
}
390385
}
391386

392-
if (argument_end != 0)
387+
uint8_t *byte_p = (uint8_t *) compiled_code_p;
388+
byte_p += ((size_t) compiled_code_p->size) << JMEM_ALIGNMENT_LOG;
389+
literal_p = ((ecma_value_t *) byte_p);
390+
391+
if (compiled_code_p->status_flags & CBC_CODE_FLAGS_MAPPED_ARGUMENTS_NEEDED)
393392
{
394-
uint8_t *byte_p = (uint8_t *) compiled_code_p;
395-
byte_p += ((size_t) compiled_code_p->size) << JMEM_ALIGNMENT_LOG;
396-
literal_p = ((ecma_value_t *) byte_p) - argument_end;
393+
literal_p -= argument_end;
397394

398395
for (uint32_t i = 0; i < argument_end; i++)
399396
{
400397
ecma_save_literals_append_value (literal_p[i], lit_pool_p);
401398
}
402399
}
400+
401+
#if ENABLED (JERRY_ES2015)
402+
/* function name */
403+
if (!(compiled_code_p->status_flags & CBC_CODE_FLAGS_CLASS_CONSTRUCTOR))
404+
{
405+
ecma_save_literals_append_value (literal_p[-1], lit_pool_p);
406+
literal_p--;
407+
}
408+
#endif /* ENABLED (JERRY_ES2015) */
409+
410+
#if ENABLED (JERRY_RESOURCE_NAME)
411+
int32_t resource_name_index = (compiled_code_p->status_flags & CBC_CODE_FLAG_HAS_TAGGED_LITERALS) ? -2 : -1;
412+
413+
/* resource name */
414+
ecma_save_literals_append_value (literal_p[resource_name_index], lit_pool_p);
415+
#endif /* ENABLED (JERRY_RESOURCE_NAME) */
416+
403417
} /* ecma_save_literals_add_compiled_code */
404418

405419
/**

jerry-core/ecma/builtin-objects/ecma-builtin-function-prototype.c

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,11 @@ ecma_builtin_function_prototype_object_bind (ecma_object_t *this_arg_obj_p , /**
297297
}
298298

299299
#if ENABLED (JERRY_ES2015)
300+
if (prototype_obj_p != NULL)
301+
{
302+
ecma_deref_object (prototype_obj_p);
303+
}
304+
300305
ecma_integer_value_t len = 0;
301306
ecma_string_t *len_string = ecma_get_magic_string (LIT_MAGIC_STRING_LENGTH);
302307
ecma_property_descriptor_t prop_desc;
@@ -307,6 +312,7 @@ ecma_builtin_function_prototype_object_bind (ecma_object_t *this_arg_obj_p , /**
307312
#if ENABLED (JERRY_ES2015_BUILTIN_PROXY)
308313
if (ECMA_IS_VALUE_ERROR (status))
309314
{
315+
ecma_deref_object (function_p);
310316
return status;
311317
}
312318
#endif /* ENABLED (JERRY_ES2015_BUILTIN_PROXY) */
@@ -319,6 +325,7 @@ ecma_builtin_function_prototype_object_bind (ecma_object_t *this_arg_obj_p , /**
319325

320326
if (ECMA_IS_VALUE_ERROR (len_value))
321327
{
328+
ecma_deref_object (function_p);
322329
return len_value;
323330
}
324331

@@ -328,14 +335,35 @@ ecma_builtin_function_prototype_object_bind (ecma_object_t *this_arg_obj_p , /**
328335
ecma_op_to_integer (len_value, &len_num);
329336
len = (ecma_integer_value_t) len_num;
330337
}
338+
ecma_free_value (len_value);
331339
}
332340

333341
bound_func_p->target_length = len;
334342

335-
if (prototype_obj_p != NULL)
343+
/* 12. */
344+
ecma_value_t name_value = ecma_op_object_get_by_magic_id (this_arg_obj_p, LIT_MAGIC_STRING_NAME);
345+
if (ECMA_IS_VALUE_ERROR (name_value))
336346
{
337-
ecma_deref_object (prototype_obj_p);
347+
ecma_deref_object (function_p);
348+
return name_value;
338349
}
350+
351+
ecma_stringbuilder_t builder = ecma_stringbuilder_create_raw ((const lit_utf8_byte_t *) "bound ", 6);
352+
353+
if (ecma_is_value_string (name_value))
354+
{
355+
ecma_stringbuilder_append (&builder, ecma_get_string_from_value (name_value));
356+
}
357+
358+
ecma_free_value (name_value);
359+
360+
ecma_property_value_t *name_prop_value_p;
361+
name_prop_value_p = ecma_create_named_data_property (function_p,
362+
ecma_get_magic_string (LIT_MAGIC_STRING_NAME),
363+
ECMA_PROPERTY_FLAG_CONFIGURABLE,
364+
NULL);
365+
366+
name_prop_value_p->value = ecma_make_string_value (ecma_stringbuilder_finalize (&builder));
339367
#endif /* ENABLED (JERRY_ES2015) */
340368

341369
/*

jerry-core/ecma/builtin-objects/ecma-builtin-function.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
#include "js-parser.h"
2525
#include "lit-magic-strings.h"
2626

27-
#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) || ENABLED (JERRY_ES2015_MODULE_SYSTEM)
27+
#if ENABLED (JERRY_RESOURCE_NAME)
2828
#include "jcontext.h"
29-
#endif /* ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) || ENABLED (JERRY_ES2015_MODULE_SYSTEM) */
29+
#endif /* ENABLED (JERRY_RESOURCE_NAME) */
3030

3131
#define ECMA_BUILTINS_INTERNAL
3232
#include "ecma-builtins-internal.h"

jerry-core/ecma/operations/ecma-eval.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ ecma_op_eval_chars_buffer (const lit_utf8_byte_t *code_p, /**< code characters b
9393

9494
parse_opts |= ECMA_PARSE_EVAL;
9595

96-
#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES)
96+
#if ENABLED (JERRY_RESOURCE_NAME)
9797
JERRY_CONTEXT (resource_name) = ecma_make_magic_string_value (LIT_MAGIC_STRING_RESOURCE_EVAL);
98-
#endif /* ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) */
98+
#endif /* ENABLED (JERRY_RESOURCE_NAME) */
9999

100100
#if ENABLED (JERRY_ES2015)
101101
ECMA_CLEAR_LOCAL_PARSE_OPTS ();

0 commit comments

Comments
 (0)