Introduce jerry_get_resource_name API function (#3236)
This new API function adds possibility to query the resource name of the currently executed script (including modules) or a function object. This patch closes #2170. JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik frobert@inf.u-szeged.hu
This commit is contained in:
committed by
Dániel Bátyai
parent
55423ab82a
commit
525c35f148
@@ -4,25 +4,27 @@ Stopped at tests/debugger/do_variables.js:15
|
||||
level | type
|
||||
0 | global
|
||||
(jerry-debugger) variables
|
||||
name | type | value
|
||||
f | Function |
|
||||
addX | Function |
|
||||
z | undefined | undefined
|
||||
c | undefined | undefined
|
||||
print | Function |
|
||||
gc | Function |
|
||||
assert | Function |
|
||||
name | type | value
|
||||
f | Function |
|
||||
addX | Function |
|
||||
z | undefined | undefined
|
||||
c | undefined | undefined
|
||||
resourceName | Function |
|
||||
print | Function |
|
||||
gc | Function |
|
||||
assert | Function |
|
||||
(jerry-debugger) variables 1
|
||||
name | type | value
|
||||
(jerry-debugger) variables 0
|
||||
name | type | value
|
||||
f | Function |
|
||||
addX | Function |
|
||||
z | undefined | undefined
|
||||
c | undefined | undefined
|
||||
print | Function |
|
||||
gc | Function |
|
||||
assert | Function |
|
||||
name | type | value
|
||||
f | Function |
|
||||
addX | Function |
|
||||
z | undefined | undefined
|
||||
c | undefined | undefined
|
||||
resourceName | Function |
|
||||
print | Function |
|
||||
gc | Function |
|
||||
assert | Function |
|
||||
(jerry-debugger) b tests/debugger/do_variables.js:20
|
||||
Breakpoint 1 at tests/debugger/do_variables.js:20 (in function() at line:19, col:10)
|
||||
(jerry-debugger) c
|
||||
@@ -40,15 +42,16 @@ n | Number | 9
|
||||
name | type | value
|
||||
x | Number | 3
|
||||
(jerry-debugger) variables 2
|
||||
name | type | value
|
||||
addThree | Function |
|
||||
f | Function |
|
||||
addX | Function |
|
||||
z | Number | 5
|
||||
c | Number | 4
|
||||
print | Function |
|
||||
gc | Function |
|
||||
assert | Function |
|
||||
name | type | value
|
||||
addThree | Function |
|
||||
f | Function |
|
||||
addX | Function |
|
||||
z | Number | 5
|
||||
c | Number | 4
|
||||
resourceName | Function |
|
||||
print | Function |
|
||||
gc | Function |
|
||||
assert | Function |
|
||||
(jerry-debugger) b tests/debugger/do_variables.js:30
|
||||
Breakpoint 2 at tests/debugger/do_variables.js:30 (in f() at line:28, col:1)
|
||||
(jerry-debugger) c
|
||||
@@ -58,16 +61,17 @@ level | type
|
||||
0 | local
|
||||
1 | global
|
||||
(jerry-debugger) variables 1
|
||||
name | type | value
|
||||
d | Number | 12
|
||||
addThree | Function |
|
||||
f | Function |
|
||||
addX | Function |
|
||||
z | Number | 5
|
||||
c | Number | 4
|
||||
print | Function |
|
||||
gc | Function |
|
||||
assert | Function |
|
||||
name | type | value
|
||||
d | Number | 12
|
||||
addThree | Function |
|
||||
f | Function |
|
||||
addX | Function |
|
||||
z | Number | 5
|
||||
c | Number | 4
|
||||
resourceName | Function |
|
||||
print | Function |
|
||||
gc | Function |
|
||||
assert | Function |
|
||||
(jerry-debugger) variables 0
|
||||
name | type | value
|
||||
b | undefined | undefined
|
||||
|
||||
@@ -51,4 +51,4 @@ try {
|
||||
/* Check properties of a */
|
||||
assert(Object.keys(a) == "one,two");
|
||||
/* Check properties of global object */
|
||||
assert(Object.keys(this) == "assert,gc,print,a,fail,fail_two");
|
||||
assert(Object.keys(this) == "assert,gc,print,resourceName,a,fail,fail_two");
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
/* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
#include "jerryscript.h"
|
||||
#include "test-common.h"
|
||||
|
||||
static jerry_value_t
|
||||
resource_name_handler (const jerry_value_t function_obj, /**< function object */
|
||||
const jerry_value_t this_val, /**< this value */
|
||||
const jerry_value_t args_p[], /**< argument list */
|
||||
const jerry_length_t args_count) /**< argument count */
|
||||
{
|
||||
(void) function_obj;
|
||||
(void) this_val;
|
||||
|
||||
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)
|
||||
{
|
||||
TEST_INIT ();
|
||||
|
||||
if (!jerry_is_feature_enabled (JERRY_FEATURE_LINE_INFO))
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Line info support is disabled!\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
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 char *source_1 = ("function f1 () {\n"
|
||||
" if (resourceName() !== 'demo1.js') return false; \n"
|
||||
" if (resourceName(f1) !== 'demo1.js') return false; \n"
|
||||
" if (resourceName(5) !== '<anonymous>') return false; \n"
|
||||
" return f1; \n"
|
||||
"} \n"
|
||||
"f1();");
|
||||
const char *resource_1 = "demo1.js";
|
||||
|
||||
jerry_value_t program = jerry_parse ((const jerry_char_t *) resource_1,
|
||||
strlen (resource_1),
|
||||
(const jerry_char_t *) source_1,
|
||||
strlen (source_1),
|
||||
JERRY_PARSE_NO_OPTS);
|
||||
TEST_ASSERT (!jerry_value_is_error (program));
|
||||
|
||||
jerry_value_t run_result = jerry_run (program);
|
||||
TEST_ASSERT (!jerry_value_is_error (run_result));
|
||||
TEST_ASSERT (jerry_value_is_object (run_result));
|
||||
|
||||
jerry_value_t resource_value = jerry_get_resource_name (run_result);
|
||||
jerry_value_t resource1_name_value = jerry_create_string ((const jerry_char_t *) resource_1);
|
||||
TEST_ASSERT (jerry_binary_operation (JERRY_BIN_OP_STRICT_EQUAL, resource_value, resource1_name_value));
|
||||
jerry_release_value (resource1_name_value);
|
||||
jerry_release_value (resource_value);
|
||||
|
||||
jerry_release_value (run_result);
|
||||
jerry_release_value (program);
|
||||
|
||||
const char *source_2 = ("function f2 () { \n"
|
||||
" if (resourceName() !== 'demo2.js') return false; \n"
|
||||
" if (resourceName(f2) !== 'demo2.js') return false; \n"
|
||||
" if (resourceName(f1) !== 'demo1.js') return false; \n"
|
||||
" if (resourceName(Object.prototype) !== '<anonymous>') return false; \n"
|
||||
" if (resourceName(Function) !== '<anonymous>') return false; \n"
|
||||
" return f2; \n"
|
||||
"} \n"
|
||||
"f2(); \n");
|
||||
const char *resource_2 = "demo2.js";
|
||||
|
||||
|
||||
program = jerry_parse ((const jerry_char_t *) resource_2,
|
||||
strlen (resource_2),
|
||||
(const jerry_char_t *) source_2,
|
||||
strlen (source_2),
|
||||
JERRY_PARSE_NO_OPTS);
|
||||
TEST_ASSERT (!jerry_value_is_error (program));
|
||||
|
||||
run_result = jerry_run (program);
|
||||
TEST_ASSERT (!jerry_value_is_error (run_result));
|
||||
TEST_ASSERT (jerry_value_is_object (run_result));
|
||||
|
||||
resource_value = jerry_get_resource_name (run_result);
|
||||
jerry_value_t resource2_name_value = jerry_create_string ((const jerry_char_t *) resource_2);
|
||||
TEST_ASSERT (jerry_binary_operation (JERRY_BIN_OP_STRICT_EQUAL, resource_value, resource2_name_value));
|
||||
jerry_release_value (resource2_name_value);
|
||||
jerry_release_value (resource_value);
|
||||
|
||||
jerry_release_value (run_result);
|
||||
jerry_release_value (program);
|
||||
|
||||
jerry_cleanup ();
|
||||
|
||||
return 0;
|
||||
} /* main */
|
||||
Reference in New Issue
Block a user