Support shared user data for scripts (#4710)

The same data is returned for the script and all of its functions,
including those which are created by an eval call.

JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
Zoltan Herczeg
2021-07-20 10:33:23 +02:00
committed by GitHub
parent 9ff25dbc12
commit 713d90b5a0
20 changed files with 780 additions and 126 deletions
+1
View File
@@ -77,6 +77,7 @@ set(SOURCE_UNIT_TEST_MAIN_MODULES
test-regexp.c
test-regression-3588.c
test-resource-name.c
test-script-user-value.c
test-snapshot.c
test-special-proxy.c
test-string-to-number.c
+192
View File
@@ -0,0 +1,192 @@
/* 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 user_values[4];
#define USER_VALUES_SIZE (sizeof (user_values) / sizeof (jerry_value_t))
static void
test_parse (const char *source_p, /**< source code */
jerry_parse_options_t *options_p, /**< options passed to jerry_parse */
bool run_code) /**< run the code after parsing */
{
for (size_t i = 0; i < USER_VALUES_SIZE; i++)
{
options_p->user_value = user_values[i];
jerry_value_t result = jerry_parse ((const jerry_char_t *) source_p,
strlen (source_p),
options_p);
TEST_ASSERT (!jerry_value_is_error (result));
if (run_code)
{
jerry_value_t parse_result = result;
result = jerry_run (result);
jerry_release_value (parse_result);
TEST_ASSERT (!jerry_value_is_error (result));
}
jerry_value_t user_value = jerry_get_user_value (result);
jerry_value_t compare_value = jerry_binary_operation (JERRY_BIN_OP_STRICT_EQUAL,
user_value,
user_values[i]);
TEST_ASSERT (jerry_value_is_true (compare_value));
jerry_release_value (compare_value);
jerry_release_value (user_value);
jerry_release_value (result);
}
} /* test_parse */
static void
test_parse_function (const char *source_p, /**< source code */
jerry_parse_options_t *options_p, /**< options passed to jerry_parse */
bool run_code) /**< run the code after parsing */
{
for (size_t i = 0; i < USER_VALUES_SIZE; i++)
{
options_p->user_value = user_values[i];
jerry_value_t result = jerry_parse_function ((const jerry_char_t *) "",
0,
(const jerry_char_t *) source_p,
strlen (source_p),
options_p);
TEST_ASSERT (!jerry_value_is_error (result));
if (run_code)
{
jerry_value_t parse_result = result;
jerry_value_t this_value = jerry_create_undefined ();
result = jerry_call_function (result, this_value, NULL, 0);
jerry_release_value (parse_result);
jerry_release_value (this_value);
TEST_ASSERT (!jerry_value_is_error (result));
}
jerry_value_t user_value = jerry_get_user_value (result);
jerry_value_t compare_value = jerry_binary_operation (JERRY_BIN_OP_STRICT_EQUAL,
user_value,
user_values[i]);
TEST_ASSERT (jerry_value_is_true (compare_value));
jerry_release_value (compare_value);
jerry_release_value (user_value);
jerry_release_value (result);
}
} /* test_parse_function */
int
main (void)
{
TEST_INIT ();
jerry_init (JERRY_INIT_EMPTY);
user_values[0] = jerry_create_object ();
user_values[1] = jerry_create_null ();
user_values[2] = jerry_create_number (5.5);
user_values[3] = jerry_create_string ((const jerry_char_t *) "AnyString...");
jerry_parse_options_t parse_options;
const char *source_p = TEST_STRING_LITERAL ("");
parse_options.options = JERRY_PARSE_HAS_USER_VALUE;
test_parse (source_p, &parse_options, false);
test_parse_function (source_p, &parse_options, false);
if (jerry_is_feature_enabled (JERRY_FEATURE_MODULE))
{
parse_options.options = JERRY_PARSE_MODULE | JERRY_PARSE_HAS_USER_VALUE;
test_parse (source_p, &parse_options, false);
}
source_p = TEST_STRING_LITERAL ("function f() { }\n"
"f");
parse_options.options = JERRY_PARSE_HAS_USER_VALUE;
test_parse (source_p, &parse_options, true);
source_p = TEST_STRING_LITERAL ("function f() { return function() {} }\n"
"f()");
parse_options.options = JERRY_PARSE_HAS_USER_VALUE;
test_parse (source_p, &parse_options, true);
source_p = TEST_STRING_LITERAL ("return function() {}");
parse_options.options = JERRY_PARSE_HAS_USER_VALUE;
test_parse_function (source_p, &parse_options, true);
source_p = TEST_STRING_LITERAL ("eval('function f() {}')\n"
"f");
parse_options.options = JERRY_PARSE_HAS_USER_VALUE;
test_parse (source_p, &parse_options, true);
source_p = TEST_STRING_LITERAL ("eval('function f() { return eval(\\'(function () {})\\') }')\n"
"f()");
parse_options.options = JERRY_PARSE_HAS_USER_VALUE;
test_parse (source_p, &parse_options, true);
source_p = TEST_STRING_LITERAL ("eval('function f() {}')\n"
"return f");
parse_options.options = JERRY_PARSE_HAS_USER_VALUE;
test_parse_function (source_p, &parse_options, true);
source_p = TEST_STRING_LITERAL ("eval('function f() { return eval(\\'(function () {})\\') }')\n"
"return f()");
parse_options.options = JERRY_PARSE_HAS_USER_VALUE;
test_parse_function (source_p, &parse_options, true);
source_p = TEST_STRING_LITERAL ("function f() {}\n"
"f.bind(1)");
parse_options.options = JERRY_PARSE_HAS_USER_VALUE;
test_parse (source_p, &parse_options, true);
source_p = TEST_STRING_LITERAL ("function f() {}\n"
"f.bind(1).bind(2, 3)");
parse_options.options = JERRY_PARSE_HAS_USER_VALUE;
test_parse (source_p, &parse_options, true);
source_p = TEST_STRING_LITERAL ("function f() {}\n"
"return f.bind(1)");
parse_options.options = JERRY_PARSE_HAS_USER_VALUE;
test_parse_function (source_p, &parse_options, true);
source_p = TEST_STRING_LITERAL ("function f() {}\n"
"return f.bind(1).bind(2, 3)");
parse_options.options = JERRY_PARSE_HAS_USER_VALUE;
test_parse_function (source_p, &parse_options, true);
for (size_t i = 0; i < USER_VALUES_SIZE; i++)
{
jerry_value_t result = jerry_get_user_value (user_values[i]);
TEST_ASSERT (jerry_value_is_undefined (result));
jerry_release_value (result);
}
for (size_t i = 0; i < USER_VALUES_SIZE; i++)
{
jerry_release_value (user_values[i]);
}
jerry_cleanup ();
return 0;
} /* main */
+79 -6
View File
@@ -89,7 +89,8 @@ static void test_function_snapshot (void)
jerry_value_t function_obj = jerry_exec_snapshot (function_snapshot_buffer,
function_snapshot_size,
0,
JERRY_SNAPSHOT_EXEC_LOAD_AS_FUNCTION);
JERRY_SNAPSHOT_EXEC_LOAD_AS_FUNCTION,
NULL);
TEST_ASSERT (!jerry_value_is_error (function_obj));
TEST_ASSERT (jerry_value_is_function (function_obj));
@@ -117,7 +118,7 @@ static void test_function_snapshot (void)
static void arguments_test_exec_snapshot (uint32_t *snapshot_p, size_t snapshot_size, uint32_t exec_snapshot_flags)
{
jerry_init (JERRY_INIT_EMPTY);
jerry_value_t res = jerry_exec_snapshot (snapshot_p, snapshot_size, 0, exec_snapshot_flags);
jerry_value_t res = jerry_exec_snapshot (snapshot_p, snapshot_size, 0, exec_snapshot_flags, NULL);
TEST_ASSERT (!jerry_value_is_error (res));
TEST_ASSERT (jerry_value_is_number (res));
double raw_value = jerry_get_number_value (res);
@@ -176,7 +177,7 @@ static void test_exec_snapshot (uint32_t *snapshot_p, size_t snapshot_size, uint
sizeof (magic_string_lengths) / sizeof (jerry_length_t),
magic_string_lengths);
jerry_value_t res = jerry_exec_snapshot (snapshot_p, snapshot_size, 0, exec_snapshot_flags);
jerry_value_t res = jerry_exec_snapshot (snapshot_p, snapshot_size, 0, exec_snapshot_flags, NULL);
TEST_ASSERT (!jerry_value_is_error (res));
TEST_ASSERT (jerry_value_is_string (res));
@@ -190,6 +191,76 @@ static void test_exec_snapshot (uint32_t *snapshot_p, size_t snapshot_size, uint
jerry_cleanup ();
} /* test_exec_snapshot */
static void test_snapshot_with_user (void)
{
if (jerry_is_feature_enabled (JERRY_FEATURE_SNAPSHOT_SAVE)
&& jerry_is_feature_enabled (JERRY_FEATURE_SNAPSHOT_EXEC))
{
static uint32_t snapshot_buffer[SNAPSHOT_BUFFER_SIZE];
const jerry_char_t code_to_snapshot[] = TEST_STRING_LITERAL (
"function f() {}\n"
"f"
);
jerry_init (JERRY_INIT_EMPTY);
jerry_value_t result = jerry_generate_snapshot (code_to_snapshot,
sizeof (code_to_snapshot) - 1,
NULL,
0,
snapshot_buffer,
SNAPSHOT_BUFFER_SIZE);
TEST_ASSERT (!jerry_value_is_error (result)
&& jerry_value_is_number (result));
size_t snapshot_size = (size_t) jerry_get_number_value (result);
jerry_release_value (result);
for (int i = 0; i < 3; i++)
{
jerry_exec_snapshot_option_values_t snapshot_exec_options;
if (i == 0)
{
snapshot_exec_options.user_value = jerry_create_object ();
}
else if (i == 1)
{
snapshot_exec_options.user_value = jerry_create_number (-3.5);
}
else
{
snapshot_exec_options.user_value = jerry_create_string ((const jerry_char_t *) "AnyString...");
}
result = jerry_exec_snapshot (snapshot_buffer,
snapshot_size,
0,
JERRY_SNAPSHOT_EXEC_HAS_USER_VALUE,
&snapshot_exec_options);
TEST_ASSERT (!jerry_value_is_error (result)
&& jerry_value_is_function (result));
jerry_value_t user_value = jerry_get_user_value (result);
jerry_release_value (result);
result = jerry_binary_operation (JERRY_BIN_OP_STRICT_EQUAL,
user_value,
snapshot_exec_options.user_value);
TEST_ASSERT (jerry_value_is_true (result));
jerry_release_value (result);
jerry_release_value (user_value);
jerry_release_value (snapshot_exec_options.user_value);
}
jerry_cleanup ();
}
} /* test_snapshot_with_user */
int
main (void)
{
@@ -227,7 +298,7 @@ main (void)
jerry_release_value (generate_result);
/* Static snapshots are not supported by default. */
jerry_value_t exec_result = jerry_exec_snapshot (snapshot_buffer, snapshot_size, 0, 0);
jerry_value_t exec_result = jerry_exec_snapshot (snapshot_buffer, snapshot_size, 0, 0, NULL);
TEST_ASSERT (jerry_value_is_error (exec_result));
jerry_release_value (exec_result);
@@ -308,12 +379,12 @@ main (void)
jerry_init (JERRY_INIT_EMPTY);
jerry_value_t res = jerry_exec_snapshot (merged_snapshot_buffer, merged_size, 0, 0);
jerry_value_t res = jerry_exec_snapshot (merged_snapshot_buffer, merged_size, 0, 0, NULL);
TEST_ASSERT (!jerry_value_is_error (res));
TEST_ASSERT (jerry_get_number_value (res) == 123);
jerry_release_value (res);
res = jerry_exec_snapshot (merged_snapshot_buffer, merged_size, 1, 0);
res = jerry_exec_snapshot (merged_snapshot_buffer, merged_size, 1, 0, NULL);
TEST_ASSERT (!jerry_value_is_error (res));
TEST_ASSERT (jerry_get_number_value (res) == 456);
jerry_release_value (res);
@@ -393,5 +464,7 @@ main (void)
test_function_arguments_snapshot ();
test_snapshot_with_user ();
return 0;
} /* main */