Skip to content

Commit 42b4b91

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 072aedb commit 42b4b91

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
@@ -7551,6 +7551,105 @@ main (void)
75517551
- [jerry_create_external_function](#jerry_create_external_function)
75527552

75537553

7554+
## jerry_get_resurce_name
7555+
7556+
**Summary**
7557+
7558+
Get the resource name (usually a file name) of the currently executed script or the given function object.
7559+
7560+
This function is typically called from native callbacks.
7561+
7562+
*Notes*:
7563+
- Returned value must be freed with [jerry_release_value](#jerry_release_value) when it
7564+
is no longer needed.
7565+
- This feature depends on build option (`JERRY_LINE_INFO`) and can be checked
7566+
in runtime with the `JERRY_FEATURE_LINE_INFO` feature enum value,
7567+
see: [jerry_is_feature_enabled](#jerry_is_feature_enabled).
7568+
7569+
**Prototype**
7570+
7571+
```c
7572+
jerry_value_t
7573+
jerry_get_resurce_name (void);
7574+
```
7575+
7576+
- return string value constructed from
7577+
- the currently executed function object's resource name, if the given value is undefined
7578+
- resource name of the function object, if the given value is a function object
7579+
- "<anonymous>", otherwise
7580+
7581+
**Example**
7582+
7583+
[doctest]: # (name="02.API-REFERENCE-jsresourcename.c")
7584+
7585+
```c
7586+
#include <stdio.h>
7587+
#include <string.h>
7588+
#include "jerryscript.h"
7589+
7590+
static jerry_value_t
7591+
resource_name_handler (const jerry_value_t function_obj,
7592+
const jerry_value_t this_val,
7593+
const jerry_value_t args_p[],
7594+
const jerry_length_t args_count)
7595+
{
7596+
jerry_value_t undefined_value = jerry_create_undefined ();
7597+
jerry_value_t resource_name = jerry_get_resource_name (args_count > 0 ? args_p[0] : undefined_value);
7598+
jerry_release_value (undefined_value);
7599+
7600+
return resource_name;
7601+
} /* resource_name_handler */
7602+
7603+
int
7604+
main (void)
7605+
{
7606+
jerry_init (JERRY_INIT_EMPTY);
7607+
7608+
jerry_value_t global = jerry_get_global_object ();
7609+
7610+
/* Register the "resourceName" method. */
7611+
{
7612+
jerry_value_t func = jerry_create_external_function (resource_name_handler);
7613+
jerry_value_t name = jerry_create_string ((const jerry_char_t *) "resourceName");
7614+
jerry_value_t result = jerry_set_property (global, name, func);
7615+
jerry_release_value (result);
7616+
jerry_release_value (name);
7617+
jerry_release_value (func);
7618+
}
7619+
7620+
jerry_release_value (global);
7621+
7622+
const jerry_char_t source[] = "function myFunction() { return resourceName() }; myFunction()";
7623+
const jerry_char_t resource[] = "demo.js";
7624+
7625+
jerry_value_t program = jerry_parse (resource,
7626+
sizeof (resource) - 1,
7627+
source,
7628+
sizeof (source) - 1,
7629+
JERRY_PARSE_NO_OPTS);
7630+
7631+
if (!jerry_value_is_error (program))
7632+
{
7633+
/* `run_result` contains "demo.js" */
7634+
jerry_value_t run_result = jerry_run (program);
7635+
7636+
/* usage of `run_result` */
7637+
7638+
jerry_release_value (run_result);
7639+
}
7640+
7641+
jerry_release_value (program);
7642+
jerry_cleanup ();
7643+
7644+
return 0;
7645+
}
7646+
```
7647+
7648+
**See also**
7649+
7650+
- [jerry_create_external_function](#jerry_create_external_function)
7651+
7652+
75547653
# ArrayBuffer and TypedArray functions
75557654

75567655
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
@@ -3339,6 +3339,47 @@ jerry_get_backtrace (uint32_t max_depth) /**< depth limit of the backtrace */
33393339
return vm_get_backtrace (max_depth);
33403340
} /* jerry_get_backtrace */
33413341

3342+
/**
3343+
* Get the resource name (usually a file name) of the currently executed script or the given function object
3344+
*
3345+
* Note: returned value must be freed with jerry_release_value, when it is no longer needed
3346+
*
3347+
* @return JS string constructed from
3348+
* - the currently executed function object's resource name, if the given value is undefined
3349+
* - resource name of the function object, if the given value is a function object
3350+
* - "<anonymous>", otherwise
3351+
*/
3352+
jerry_value_t
3353+
jerry_get_resource_name (const jerry_value_t value) /**< jerry api value */
3354+
{
3355+
#if ENABLED (JERRY_LINE_INFO)
3356+
if (ecma_is_value_undefined (value))
3357+
{
3358+
if (!ecma_is_value_undefined (JERRY_CONTEXT (resource_name)))
3359+
{
3360+
return ecma_copy_value (JERRY_CONTEXT (resource_name));
3361+
}
3362+
}
3363+
else if (ecma_is_value_object (value))
3364+
{
3365+
ecma_object_t *obj_p = ecma_get_object_from_value (value);
3366+
3367+
if (ecma_get_object_type (obj_p) == ECMA_OBJECT_TYPE_FUNCTION
3368+
&& !ecma_get_object_is_builtin (obj_p))
3369+
{
3370+
ecma_extended_object_t *ext_func_p = (ecma_extended_object_t *) obj_p;
3371+
3372+
const ecma_compiled_code_t *bytecode_data_p = ecma_op_function_get_compiled_code (ext_func_p);
3373+
3374+
return ecma_copy_value (ecma_op_resource_name (bytecode_data_p));
3375+
}
3376+
}
3377+
#endif /* ENABLED (JERRY_LINE_INFO) */
3378+
3379+
JERRY_UNUSED (value);
3380+
return ecma_make_magic_string_value (LIT_MAGIC_STRING_RESOURCE_ANON);
3381+
} /* jerry_get_resource_name */
3382+
33423383
/**
33433384
* Check if the given value is an ArrayBuffer object.
33443385
*

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
@@ -600,6 +600,7 @@ jerry_context_t *jerry_create_context (uint32_t heap_size, jerry_context_alloc_t
600600
*/
601601
void jerry_set_vm_exec_stop_callback (jerry_vm_exec_stop_callback_t stop_cb, void *user_p, uint32_t frequency);
602602
jerry_value_t jerry_get_backtrace (uint32_t max_depth);
603+
jerry_value_t jerry_get_resource_name (const jerry_value_t value);
603604

604605
/**
605606
* 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)