Skip to content

Introduce jerry_get_resource_name API function #3236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions docs/02.API-REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -7556,6 +7556,105 @@ main (void)
- [jerry_create_external_function](#jerry_create_external_function)


## jerry_get_resurce_name

**Summary**

Get the resource name (usually a file name) of the currently executed script or the given function object.

This function is typically called from native callbacks.

*Notes*:
- Returned value must be freed with [jerry_release_value](#jerry_release_value) when it
is no longer needed.
- This feature depends on build option (`JERRY_LINE_INFO`) and can be checked
in runtime with the `JERRY_FEATURE_LINE_INFO` feature enum value,
see: [jerry_is_feature_enabled](#jerry_is_feature_enabled).

**Prototype**

```c
jerry_value_t
jerry_get_resurce_name (void);
```

- return string value constructed from
- the currently executed function object's resource name, if the given value is undefined
- resource name of the function object, if the given value is a function object
- "<anonymous>", otherwise

**Example**

[doctest]: # (name="02.API-REFERENCE-jsresourcename.c")

```c
#include <stdio.h>
#include <string.h>
#include "jerryscript.h"

static jerry_value_t
resource_name_handler (const jerry_value_t function_obj,
const jerry_value_t this_val,
const jerry_value_t args_p[],
const jerry_length_t args_count)
{
jerry_value_t undefined_value = jerry_create_undefined ();
jerry_value_t resource_name = jerry_get_resource_name (args_count > 0 ? args_p[0] : undefined_value);
jerry_release_value (undefined_value);

return resource_name;
} /* resource_name_handler */

int
main (void)
{
jerry_init (JERRY_INIT_EMPTY);

jerry_value_t global = jerry_get_global_object ();

/* Register the "resourceName" method. */
{
jerry_value_t func = jerry_create_external_function (resource_name_handler);
jerry_value_t name = jerry_create_string ((const jerry_char_t *) "resourceName");
jerry_value_t result = jerry_set_property (global, name, func);
jerry_release_value (result);
jerry_release_value (name);
jerry_release_value (func);
}

jerry_release_value (global);

const jerry_char_t source[] = "function myFunction() { return resourceName() }; myFunction()";
const jerry_char_t resource[] = "demo.js";

jerry_value_t program = jerry_parse (resource,
sizeof (resource) - 1,
source,
sizeof (source) - 1,
JERRY_PARSE_NO_OPTS);

if (!jerry_value_is_error (program))
{
/* `run_result` contains "demo.js" */
jerry_value_t run_result = jerry_run (program);

/* usage of `run_result` */

jerry_release_value (run_result);
}

jerry_release_value (program);
jerry_cleanup ();

return 0;
}
```

**See also**

- [jerry_create_external_function](#jerry_create_external_function)


# ArrayBuffer and TypedArray functions

These APIs all depend on the ES2015-subset profile.
Expand Down
43 changes: 43 additions & 0 deletions jerry-core/api/jerry.c
Original file line number Diff line number Diff line change
Expand Up @@ -3354,6 +3354,49 @@ jerry_get_backtrace (uint32_t max_depth) /**< depth limit of the backtrace */
return vm_get_backtrace (max_depth);
} /* jerry_get_backtrace */

/**
* Get the resource name (usually a file name) of the currently executed script or the given function object
*
* Note: returned value must be freed with jerry_release_value, when it is no longer needed
*
* @return JS string constructed from
* - the currently executed function object's resource name, if the given value is undefined
* - resource name of the function object, if the given value is a function object
* - "<anonymous>", otherwise
*/
jerry_value_t
jerry_get_resource_name (const jerry_value_t value) /**< jerry api value */
{
#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ES2015_MODULE_SYSTEM)
if (ecma_is_value_undefined (value))
{
if (JERRY_CONTEXT (vm_top_context_p) != NULL)
{
return ecma_copy_value (JERRY_CONTEXT (vm_top_context_p)->resource_name);
}
}
#endif /* ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ES2015_MODULE_SYSTEM) */
#if ENABLED (JERRY_LINE_INFO)
else if (ecma_is_value_object (value))
{
ecma_object_t *obj_p = ecma_get_object_from_value (value);

if (ecma_get_object_type (obj_p) == ECMA_OBJECT_TYPE_FUNCTION
&& !ecma_get_object_is_builtin (obj_p))
{
ecma_extended_object_t *ext_func_p = (ecma_extended_object_t *) obj_p;

const ecma_compiled_code_t *bytecode_data_p = ecma_op_function_get_compiled_code (ext_func_p);

return ecma_copy_value (ecma_op_resource_name (bytecode_data_p));
}
}
#endif /* ENABLED (JERRY_LINE_INFO) */

JERRY_UNUSED (value);
return ecma_make_magic_string_value (LIT_MAGIC_STRING_RESOURCE_ANON);
} /* jerry_get_resource_name */

/**
* Check if the given value is an ArrayBuffer object.
*
Expand Down
39 changes: 39 additions & 0 deletions jerry-core/ecma/operations/ecma-function-object.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,45 @@
* @{
*/

#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ES2015_MODULE_SYSTEM)
/**
* Get the resource name from the compiled code header
*
* @return resource name as ecma-string
*/
ecma_value_t
ecma_op_resource_name (const ecma_compiled_code_t *bytecode_header_p)
{
JERRY_ASSERT (bytecode_header_p != NULL);

ecma_length_t formal_params_number = 0;

if (CBC_NON_STRICT_ARGUMENTS_NEEDED (bytecode_header_p))
{
if (bytecode_header_p->status_flags & CBC_CODE_FLAGS_UINT16_ARGUMENTS)
{
cbc_uint16_arguments_t *args_p = (cbc_uint16_arguments_t *) bytecode_header_p;

formal_params_number = args_p->argument_end;
}
else
{
cbc_uint8_arguments_t *args_p = (cbc_uint8_arguments_t *) bytecode_header_p;

formal_params_number = args_p->argument_end;
}
}

uint8_t *byte_p = (uint8_t *) bytecode_header_p;
byte_p += ((size_t) bytecode_header_p->size) << JMEM_ALIGNMENT_LOG;

ecma_value_t *resource_name_p = (ecma_value_t *) byte_p;
resource_name_p -= formal_params_number;

return resource_name_p[-1];
} /* ecma_op_resource_name */
#endif /* ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ES2015_MODULE_SYSTEM) */

/**
* Checks whether the type is a normal or arrow function.
*
Expand Down
4 changes: 4 additions & 0 deletions jerry-core/ecma/operations/ecma-function-object.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@

bool ecma_is_normal_or_arrow_function (ecma_object_type_t type);

#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ES2015_MODULE_SYSTEM)
ecma_value_t ecma_op_resource_name (const ecma_compiled_code_t *bytecode_header_p);
#endif /* ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ES2015_MODULE_SYSTEM) */

bool ecma_op_is_callable (ecma_value_t value);
bool ecma_op_object_is_callable (ecma_object_t *obj_p);
bool ecma_is_constructor (ecma_value_t value);
Expand Down
1 change: 1 addition & 0 deletions jerry-core/include/jerryscript-core.h
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,7 @@ jerry_context_t *jerry_create_context (uint32_t heap_size, jerry_context_alloc_t
*/
void jerry_set_vm_exec_stop_callback (jerry_vm_exec_stop_callback_t stop_cb, void *user_p, uint32_t frequency);
jerry_value_t jerry_get_backtrace (uint32_t max_depth);
jerry_value_t jerry_get_resource_name (const jerry_value_t value);

/**
* Array buffer components.
Expand Down
13 changes: 0 additions & 13 deletions jerry-core/lit/lit-magic-strings.inc.h
Original file line number Diff line number Diff line change
Expand Up @@ -599,9 +599,7 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SET_UTC_DATE_UL, "setUTCDate")
#if ENABLED (JERRY_BUILTIN_STRING) && ENABLED (JERRY_ES2015_BUILTIN)
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_STARTS_WITH, "startsWith")
#endif
#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) || ENABLED (JERRY_ES2015_MODULE_SYSTEM)
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_RESOURCE_ANON, "<anonymous>")
#endif
#if ENABLED (JERRY_ES2015_BUILTIN_DATAVIEW) \
|| ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY)
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_ARRAY_BUFFER_UL, "ArrayBuffer")
Expand Down Expand Up @@ -832,18 +830,7 @@ LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (10, LIT_MAGIC_STRING_COPY_WITHIN)
#else
LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (10, LIT_MAGIC_STRING_ENUMERABLE)
#endif
#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) || ENABLED (JERRY_ES2015_MODULE_SYSTEM)
LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (11, LIT_MAGIC_STRING_RESOURCE_ANON)
#elif ENABLED (JERRY_ES2015_BUILTIN_DATAVIEW) \
|| ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY)
LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (11, LIT_MAGIC_STRING_ARRAY_BUFFER_UL)
#elif ENABLED (JERRY_BUILTIN_ERRORS)
LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (11, LIT_MAGIC_STRING_SYNTAX_ERROR_UL)
#elif ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY)
LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (11, LIT_MAGIC_STRING_UINT16_ARRAY_UL)
#else
LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (11, LIT_MAGIC_STRING_CONSTRUCTOR)
#endif
#if ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY)
LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (12, LIT_MAGIC_STRING_FLOAT32_ARRAY_UL)
#elif ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY) && ENABLED (JERRY_NUMBER_TYPE_FLOAT64)
Expand Down
4 changes: 3 additions & 1 deletion jerry-core/parser/js/js-parser-statm.c
Original file line number Diff line number Diff line change
Expand Up @@ -2312,12 +2312,14 @@ parser_parse_statements (parser_context_t *context_p) /**< context */
}
#endif /* ENABLED (JERRY_DEBUGGER) */

#if ENABLED (JERRY_LINE_INFO)
#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ES2015_MODULE_SYSTEM)
if (JERRY_CONTEXT (resource_name) != ECMA_VALUE_UNDEFINED)
{
parser_emit_cbc_ext (context_p, CBC_EXT_RESOURCE_NAME);
parser_flush_cbc (context_p);
}
#endif /* ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ES2015_MODULE_SYSTEM) */
#if ENABLED (JERRY_LINE_INFO)
context_p->last_line_info_line = 0;
#endif /* ENABLED (JERRY_LINE_INFO) */

Expand Down
8 changes: 4 additions & 4 deletions jerry-core/parser/js/js-parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -1160,12 +1160,12 @@ parser_post_processing (parser_context_t *context_p) /**< context */
total_size += context_p->argument_count * sizeof (ecma_value_t);
}

#if ENABLED (JERRY_LINE_INFO)
#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ES2015_MODULE_SYSTEM)
if (JERRY_CONTEXT (resource_name) != ECMA_VALUE_UNDEFINED)
{
total_size += sizeof (ecma_value_t);
}
#endif /* ENABLED (JERRY_LINE_INFO) */
#endif /* ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ES2015_MODULE_SYSTEM) */

#if ENABLED (JERRY_SNAPSHOT_SAVE)
total_size_used = total_size;
Expand Down Expand Up @@ -1558,7 +1558,7 @@ parser_post_processing (parser_context_t *context_p) /**< context */
}
}

#if ENABLED (JERRY_LINE_INFO)
#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ES2015_MODULE_SYSTEM)
if (JERRY_CONTEXT (resource_name) != ECMA_VALUE_UNDEFINED)
{
ecma_value_t *resource_name_p = (ecma_value_t *) (((uint8_t *) compiled_code_p) + total_size);
Expand All @@ -1571,7 +1571,7 @@ parser_post_processing (parser_context_t *context_p) /**< context */

resource_name_p[-1] = JERRY_CONTEXT (resource_name);
}
#endif /* ENABLED (JERRY_LINE_INFO) */
#endif /* ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ES2015_MODULE_SYSTEM) */

#if ENABLED (JERRY_DEBUGGER)
if (JERRY_CONTEXT (debugger_flags) & JERRY_DEBUGGER_CONNECTED)
Expand Down
4 changes: 3 additions & 1 deletion jerry-core/vm/vm-defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ typedef struct vm_frame_ctx_t
#endif /* defined (JERRY_DEBUGGER) || ENABLED (JERRY_LINE_INFO) */
ecma_value_t this_binding; /**< this binding */
ecma_value_t block_result; /**< block result */
#if ENABLED (JERRY_LINE_INFO)
#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ES2015_MODULE_SYSTEM)
ecma_value_t resource_name; /**< current resource name (usually a file name) */
#endif /* ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ES2015_MODULE_SYSTEM) */
#if ENABLED (JERRY_LINE_INFO)
uint32_t current_line; /**< currently executed line */
#endif /* ENABLED (JERRY_LINE_INFO) */
uint16_t context_depth; /**< current context depth */
Expand Down
34 changes: 7 additions & 27 deletions jerry-core/vm/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -3384,36 +3384,14 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
continue;
}
#endif /* ENABLED (JERRY_DEBUGGER) */
#if ENABLED (JERRY_LINE_INFO)
#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ES2015_MODULE_SYSTEM)
case VM_OC_RESOURCE_NAME:
{
ecma_length_t formal_params_number = 0;

if (CBC_NON_STRICT_ARGUMENTS_NEEDED (bytecode_header_p))
{
if (bytecode_header_p->status_flags & CBC_CODE_FLAGS_UINT16_ARGUMENTS)
{
cbc_uint16_arguments_t *args_p = (cbc_uint16_arguments_t *) bytecode_header_p;

formal_params_number = args_p->argument_end;
}
else
{
cbc_uint8_arguments_t *args_p = (cbc_uint8_arguments_t *) bytecode_header_p;

formal_params_number = args_p->argument_end;
}
}

uint8_t *byte_p = (uint8_t *) bytecode_header_p;
byte_p += ((size_t) bytecode_header_p->size) << JMEM_ALIGNMENT_LOG;

ecma_value_t *resource_name_p = (ecma_value_t *) byte_p;
resource_name_p -= formal_params_number;

frame_ctx_p->resource_name = resource_name_p[-1];
frame_ctx_p->resource_name = ecma_op_resource_name (bytecode_header_p);
continue;
}
#endif /* ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ES2015_MODULE_SYSTEM) */
#if ENABLED (JERRY_LINE_INFO)
case VM_OC_LINE:
{
uint32_t value = 0;
Expand Down Expand Up @@ -3840,8 +3818,10 @@ vm_run (const ecma_compiled_code_t *bytecode_header_p, /**< byte-code data heade
#endif /* defined (JERRY_DEBUGGER) || ENABLED (JERRY_LINE_INFO) */
frame_ctx.this_binding = this_binding_value;
frame_ctx.block_result = ECMA_VALUE_UNDEFINED;
#if ENABLED (JERRY_LINE_INFO)
#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ES2015_MODULE_SYSTEM)
frame_ctx.resource_name = ECMA_VALUE_UNDEFINED;
#endif /* ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ES2015_MODULE_SYSTEM) */
#if ENABLED (JERRY_LINE_INFO)
frame_ctx.current_line = 0;
#endif /* ENABLED (JERRY_LINE_INFO) */
frame_ctx.context_depth = 0;
Expand Down
Loading