Skip to content

Commit 9750e16

Browse files
committed
Introduce jerry_get_resource_name API function
This new API function adds possibility to query the resource name of a function object. This patch closes #2170. JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik frobert@inf.u-szeged.hu
1 parent 3d797b8 commit 9750e16

File tree

13 files changed

+397
-74
lines changed

13 files changed

+397
-74
lines changed

docs/02.API-REFERENCE.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7397,6 +7397,105 @@ main (void)
73977397
- [jerry_create_external_function](#jerry_create_external_function)
73987398

73997399

7400+
## jerry_get_resurce_name
7401+
7402+
**Summary**
7403+
7404+
Get the resource name (usually a file name) of the currently executed script or the given function object.
7405+
7406+
This function is typically called from native callbacks.
7407+
7408+
*Notes*:
7409+
- Returned value must be freed with [jerry_release_value](#jerry_release_value) when it
7410+
is no longer needed.
7411+
- This feature depends on build option (`JERRY_LINE_INFO`) and can be checked
7412+
in runtime with the `JERRY_FEATURE_LINE_INFO` feature enum value,
7413+
see: [jerry_is_feature_enabled](#jerry_is_feature_enabled).
7414+
7415+
**Prototype**
7416+
7417+
```c
7418+
jerry_value_t
7419+
jerry_get_resurce_name (void);
7420+
```
7421+
7422+
- return string value constructed from
7423+
- the currently executed function object's resource name, if the given value is undefined
7424+
- resource name of the function object, if the given value is a function object
7425+
- "<anonymous>", otherwise
7426+
7427+
**Example**
7428+
7429+
[doctest]: # (name="02.API-REFERENCE-jsresourcename.c")
7430+
7431+
```c
7432+
#include <stdio.h>
7433+
#include <string.h>
7434+
#include "jerryscript.h"
7435+
7436+
static jerry_value_t
7437+
resource_name_handler (const jerry_value_t function_obj,
7438+
const jerry_value_t this_val,
7439+
const jerry_value_t args_p[],
7440+
const jerry_length_t args_count)
7441+
{
7442+
jerry_value_t undefined_value = jerry_create_undefined ();
7443+
jerry_value_t resource_name = jerry_get_resource_name (args_count > 0 ? args_p[0] : undefined_value);
7444+
jerry_release_value (undefined_value);
7445+
7446+
return resource_name;
7447+
} /* resource_name_handler */
7448+
7449+
int
7450+
main (void)
7451+
{
7452+
jerry_init (JERRY_INIT_EMPTY);
7453+
7454+
jerry_value_t global = jerry_get_global_object ();
7455+
7456+
/* Register the "resourceName" method. */
7457+
{
7458+
jerry_value_t func = jerry_create_external_function (resource_name_handler);
7459+
jerry_value_t name = jerry_create_string ((const jerry_char_t *) "resourceName");
7460+
jerry_value_t result = jerry_set_property (global, name, func);
7461+
jerry_release_value (result);
7462+
jerry_release_value (name);
7463+
jerry_release_value (func);
7464+
}
7465+
7466+
jerry_release_value (global);
7467+
7468+
const jerry_char_t source[] = "function myFunction() { return resourceName() }; myFunction()";
7469+
const jerry_char_t resource[] = "demo.js";
7470+
7471+
jerry_value_t program = jerry_parse (resource,
7472+
sizeof (resource) - 1,
7473+
source,
7474+
sizeof (source) - 1,
7475+
JERRY_PARSE_NO_OPTS);
7476+
7477+
if (!jerry_value_is_error (program))
7478+
{
7479+
/* `run_result` contains "demo.js" */
7480+
jerry_value_t run_result = jerry_run (program);
7481+
7482+
/* usage of `run_result` */
7483+
7484+
jerry_release_value (run_result);
7485+
}
7486+
7487+
jerry_release_value (program);
7488+
jerry_cleanup ();
7489+
7490+
return 0;
7491+
}
7492+
```
7493+
7494+
**See also**
7495+
7496+
- [jerry_create_external_function](#jerry_create_external_function)
7497+
7498+
74007499
# ArrayBuffer and TypedArray functions
74017500

74027501
These APIs all depend on the ES2015-subset profile.

jerry-core/api/jerry.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3277,6 +3277,47 @@ jerry_get_backtrace (uint32_t max_depth) /**< depth limit of the backtrace */
32773277
return vm_get_backtrace (max_depth);
32783278
} /* jerry_get_backtrace */
32793279

3280+
/**
3281+
* Get the resource name (usually a file name) of the currently executed script or the given function object
3282+
*
3283+
* Note: returned value must be freed with jerry_release_value, when it is no longer needed
3284+
*
3285+
* @return JS string constructed from
3286+
* - the currently executed function object's resource name, if the given value is undefined
3287+
* - resource name of the function object, if the given value is a function object
3288+
* - "<anonymous>", otherwise
3289+
*/
3290+
jerry_value_t
3291+
jerry_get_resource_name (const jerry_value_t value) /**< jerry api value */
3292+
{
3293+
#if ENABLED (JERRY_LINE_INFO)
3294+
if (ecma_is_value_undefined (value))
3295+
{
3296+
if (!ecma_is_value_undefined (JERRY_CONTEXT (resource_name)))
3297+
{
3298+
return ecma_copy_value (JERRY_CONTEXT (resource_name));
3299+
}
3300+
}
3301+
else if (ecma_is_value_object (value))
3302+
{
3303+
ecma_object_t *obj_p = ecma_get_object_from_value (value);
3304+
3305+
if (ecma_get_object_type (obj_p) == ECMA_OBJECT_TYPE_FUNCTION
3306+
&& !ecma_get_object_is_builtin (obj_p))
3307+
{
3308+
ecma_extended_object_t *ext_func_p = (ecma_extended_object_t *) obj_p;
3309+
3310+
const ecma_compiled_code_t *bytecode_data_p = ecma_op_function_get_compiled_code (ext_func_p);
3311+
3312+
return ecma_copy_value (ecma_op_resource_name (bytecode_data_p));
3313+
}
3314+
}
3315+
#endif /* ENABLED (JERRY_LINE_INFO) */
3316+
3317+
JERRY_UNUSED (value);
3318+
return ecma_make_magic_string_value (LIT_MAGIC_STRING_RESOURCE_ANON);
3319+
} /* jerry_get_resource_name */
3320+
32803321
/**
32813322
* Check if the given value is an ArrayBuffer object.
32823323
*

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,45 @@
3434
* @{
3535
*/
3636

37+
#if ENABLED (JERRY_LINE_INFO)
38+
/**
39+
* Get the resource name from the compiled code header
40+
*
41+
* @return resource name as ecma-string
42+
*/
43+
ecma_value_t
44+
ecma_op_resource_name (const ecma_compiled_code_t *bytecode_header_p)
45+
{
46+
JERRY_ASSERT (bytecode_header_p != NULL);
47+
48+
ecma_length_t formal_params_number = 0;
49+
50+
if (CBC_NON_STRICT_ARGUMENTS_NEEDED (bytecode_header_p))
51+
{
52+
if (bytecode_header_p->status_flags & CBC_CODE_FLAGS_UINT16_ARGUMENTS)
53+
{
54+
cbc_uint16_arguments_t *args_p = (cbc_uint16_arguments_t *) bytecode_header_p;
55+
56+
formal_params_number = args_p->argument_end;
57+
}
58+
else
59+
{
60+
cbc_uint8_arguments_t *args_p = (cbc_uint8_arguments_t *) bytecode_header_p;
61+
62+
formal_params_number = args_p->argument_end;
63+
}
64+
}
65+
66+
uint8_t *byte_p = (uint8_t *) bytecode_header_p;
67+
byte_p += ((size_t) bytecode_header_p->size) << JMEM_ALIGNMENT_LOG;
68+
69+
ecma_value_t *resource_name_p = (ecma_value_t *) byte_p;
70+
resource_name_p -= formal_params_number;
71+
72+
return resource_name_p[-1];
73+
} /* ecma_op_resource_name */
74+
#endif /* ENABLED (JERRY_LINE_INFO) */
75+
3776
/**
3877
* Checks whether the type is a normal or arrow function.
3978
*

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828

2929
bool ecma_is_normal_or_arrow_function (ecma_object_type_t type);
3030

31+
#if ENABLED (JERRY_LINE_INFO)
32+
ecma_value_t ecma_op_resource_name (const ecma_compiled_code_t *bytecode_header_p);
33+
#endif /* ENABLED (JERRY_LINE_INFO) */
34+
3135
bool ecma_op_is_callable (ecma_value_t value);
3236
bool ecma_is_constructor (ecma_value_t value);
3337

jerry-core/include/jerryscript-core.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,7 @@ jerry_context_t *jerry_create_context (uint32_t heap_size, jerry_context_alloc_t
586586
*/
587587
void jerry_set_vm_exec_stop_callback (jerry_vm_exec_stop_callback_t stop_cb, void *user_p, uint32_t frequency);
588588
jerry_value_t jerry_get_backtrace (uint32_t max_depth);
589+
jerry_value_t jerry_get_resource_name (const jerry_value_t value);
589590

590591
/**
591592
* Array buffer components.

jerry-core/lit/lit-magic-strings.inc.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -596,9 +596,7 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SET_UTC_DATE_UL, "setUTCDate")
596596
#if ENABLED (JERRY_BUILTIN_STRING) && ENABLED (JERRY_ES2015_BUILTIN)
597597
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_STARTS_WITH, "startsWith")
598598
#endif
599-
#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) || ENABLED (JERRY_ES2015_MODULE_SYSTEM)
600599
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_RESOURCE_ANON, "<anonymous>")
601-
#endif
602600
#if ENABLED (JERRY_ES2015_BUILTIN_DATAVIEW) \
603601
|| ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY)
604602
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_ARRAY_BUFFER_UL, "ArrayBuffer")
@@ -825,18 +823,7 @@ LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (10, LIT_MAGIC_STRING_COPY_WITHIN)
825823
#else
826824
LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (10, LIT_MAGIC_STRING_ENUMERABLE)
827825
#endif
828-
#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) || ENABLED (JERRY_ES2015_MODULE_SYSTEM)
829826
LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (11, LIT_MAGIC_STRING_RESOURCE_ANON)
830-
#elif ENABLED (JERRY_ES2015_BUILTIN_DATAVIEW) \
831-
|| ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY)
832-
LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (11, LIT_MAGIC_STRING_ARRAY_BUFFER_UL)
833-
#elif ENABLED (JERRY_BUILTIN_ERRORS)
834-
LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (11, LIT_MAGIC_STRING_SYNTAX_ERROR_UL)
835-
#elif ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY)
836-
LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (11, LIT_MAGIC_STRING_UINT16_ARRAY_UL)
837-
#else
838-
LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (11, LIT_MAGIC_STRING_CONSTRUCTOR)
839-
#endif
840827
#if ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY)
841828
LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (12, LIT_MAGIC_STRING_FLOAT32_ARRAY_UL)
842829
#elif ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY) && ENABLED (JERRY_NUMBER_TYPE_FLOAT64)

jerry-core/vm/vm.c

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3351,31 +3351,7 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
33513351
#if ENABLED (JERRY_LINE_INFO)
33523352
case VM_OC_RESOURCE_NAME:
33533353
{
3354-
ecma_length_t formal_params_number = 0;
3355-
3356-
if (CBC_NON_STRICT_ARGUMENTS_NEEDED (bytecode_header_p))
3357-
{
3358-
if (bytecode_header_p->status_flags & CBC_CODE_FLAGS_UINT16_ARGUMENTS)
3359-
{
3360-
cbc_uint16_arguments_t *args_p = (cbc_uint16_arguments_t *) bytecode_header_p;
3361-
3362-
formal_params_number = args_p->argument_end;
3363-
}
3364-
else
3365-
{
3366-
cbc_uint8_arguments_t *args_p = (cbc_uint8_arguments_t *) bytecode_header_p;
3367-
3368-
formal_params_number = args_p->argument_end;
3369-
}
3370-
}
3371-
3372-
uint8_t *byte_p = (uint8_t *) bytecode_header_p;
3373-
byte_p += ((size_t) bytecode_header_p->size) << JMEM_ALIGNMENT_LOG;
3374-
3375-
ecma_value_t *resource_name_p = (ecma_value_t *) byte_p;
3376-
resource_name_p -= formal_params_number;
3377-
3378-
frame_ctx_p->resource_name = resource_name_p[-1];
3354+
frame_ctx_p->resource_name = ecma_op_resource_name (bytecode_header_p);
33793355
continue;
33803356
}
33813357
case VM_OC_LINE:
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* Copyright JS Foundation and other contributors, http://js.foundation
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
#include "jerryscript-ext/handler.h"
17+
18+
/**
19+
* Get the resource name (usually a file name) of the currently executed script or the given function object
20+
*
21+
* Note: returned value must be freed with jerry_release_value, when it is no longer needed
22+
*
23+
* @return JS string constructed from
24+
* - the currently executed function object's resource name, if the given value is undefined
25+
* - resource name of the function object, if the given value is a function object
26+
* - "<anonymous>", otherwise
27+
*/
28+
jerry_value_t
29+
jerryx_handler_resource_name (const jerry_value_t func_obj_val, /**< function object */
30+
const jerry_value_t this_p, /**< this arg */
31+
const jerry_value_t args_p[], /**< function arguments */
32+
const jerry_length_t args_cnt) /**< number of function arguments */
33+
{
34+
(void) func_obj_val; /* unused */
35+
(void) this_p; /* unused */
36+
37+
jerry_value_t undefined_value = jerry_create_undefined ();
38+
jerry_value_t resource_name = jerry_get_resource_name (args_cnt > 0 ? args_p[0] : undefined_value);
39+
jerry_release_value (undefined_value);
40+
41+
return resource_name;
42+
} /* jerryx_handler_resource_name */

jerry-ext/include/jerryscript-ext/handler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ jerry_value_t jerryx_handler_gc (const jerry_value_t func_obj_val, const jerry_v
4444
const jerry_value_t args_p[], const jerry_length_t args_cnt);
4545
jerry_value_t jerryx_handler_print (const jerry_value_t func_obj_val, const jerry_value_t this_p,
4646
const jerry_value_t args_p[], const jerry_length_t args_cnt);
47+
jerry_value_t jerryx_handler_resource_name (const jerry_value_t func_obj_val, const jerry_value_t this_p,
48+
const jerry_value_t args_p[], const jerry_length_t args_cnt);
4749

4850
/**
4951
* Struct used by the `jerryx_set_functions` method to

jerry-main/main-unix.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ init_engine (jerry_init_flag_t flags, /**< initialized flags for the engine */
459459
register_js_function ("assert", jerryx_handler_assert);
460460
register_js_function ("gc", jerryx_handler_gc);
461461
register_js_function ("print", jerryx_handler_print);
462+
register_js_function ("resourceName", jerryx_handler_resource_name);
462463
} /* init_engine */
463464

464465
int

0 commit comments

Comments
 (0)