From db3a21280f7734290f75eef3ba53f6a4ca7140bb Mon Sep 17 00:00:00 2001 From: Yonggang Luo Date: Mon, 9 Dec 2024 21:54:19 +0800 Subject: [PATCH] Reduce call to strlen when possible Add buffer_size parameter for function jerry_port_log_buffer, so that jerry_port_log_buffer won't need calculate strlen when printing Introduce jerry_sz_ascii jerry_sz_cesu8 jerry_sz_utf8 and use it Replace all the usage of jerry_string_sz with jerry_sz_ascii,jerry_sz_cesu8 - `jerry_string_external_sz` replaced by `jerry_string_external` - partial `jerry_string` usage replaced by `jerry_sz_ascii`, `jerry_sz_cesu8`, `jerry_sz_utf8` JerryScript-DCO-1.0-Signed-off-by: Yonggang Luo luoyonggang@gmail.com --- docs/01.CONFIGURATION.md | 2 +- docs/02.API-REFERENCE.md | 230 ++++++------- docs/03.API-EXAMPLE.md | 51 +-- docs/05.PORT-API.md | 5 +- docs/07.DEBUGGER.md | 5 +- docs/09.EXT-REFERENCE-ARG.md | 42 +-- docs/10.EXT-REFERENCE-HANDLER.md | 19 +- docs/11.EXT-REFERENCE-AUTORELEASE.md | 2 +- docs/16.MIGRATION-GUIDE.md | 2 + jerry-core/api/jerry-module.c | 6 +- jerry-core/api/jerry-snapshot.c | 8 +- jerry-core/api/jerryscript.c | 305 ++++++++++-------- jerry-core/debugger/debugger.c | 16 +- jerry-core/ecma/base/ecma-errors.c | 35 +- jerry-core/ecma/base/ecma-errors.h | 3 - jerry-core/ecma/base/ecma-globals.h | 2 + ...iltin-async-from-sync-iterator-prototype.c | 19 +- .../ecma-builtin-async-generator-prototype.c | 13 +- jerry-core/ecma/operations/ecma-exceptions.c | 52 ++- jerry-core/ecma/operations/ecma-exceptions.h | 2 + jerry-core/include/jerryscript-compiler.h | 7 + jerry-core/include/jerryscript-core.h | 49 ++- jerry-core/include/jerryscript-port.h | 5 +- jerry-core/include/jerryscript-types.h | 18 +- jerry-core/jrt/jrt.h | 5 - jerry-core/lit/lit-strings.c | 45 ++- jerry-core/lit/lit-strings.h | 2 +- jerry-ext/arg/arg-transform-functions.c | 23 +- jerry-ext/arg/arg.c | 14 +- jerry-ext/common/jext-common.h | 7 +- jerry-ext/debugger/debugger-common.c | 2 +- jerry-ext/debugger/debugger-serial.c | 2 +- jerry-ext/debugger/debugger-tcp.c | 2 +- jerry-ext/include/jerryscript-ext/arg.h | 4 +- jerry-ext/include/jerryscript-ext/module.h | 28 +- .../include/jerryscript-ext/properties.h | 62 ++-- jerry-ext/module/module.c | 24 +- jerry-ext/util/handlers.c | 2 +- jerry-ext/util/print.c | 2 +- jerry-ext/util/properties.c | 14 +- jerry-ext/util/repl.c | 2 +- jerry-ext/util/sources.c | 16 +- jerry-ext/util/test262.c | 22 +- jerry-main/benchmark/main-benchmark.c | 2 +- jerry-main/main-desktop.c | 15 +- jerry-main/main-snapshot.c | 7 +- jerry-port/common/jerry-port-io.c | 9 +- .../baremetal-sdk/espressif/main/jerry-main.c | 2 +- .../baremetal-sdk/espressif/main/jerry-port.c | 9 +- .../baremetal-sdk/particle/source/main.cpp | 6 +- targets/os/mbedos/jerry-main.cpp | 9 +- targets/os/mbedos/jerry-port.cpp | 13 +- targets/os/nuttx/jerry-main.c | 13 +- targets/os/nuttx/jerry-port.c | 10 +- targets/os/riot/source/jerry-main.c | 2 +- targets/os/zephyr/src/jerry-main.c | 2 +- tests/unit-core/test-abort.c | 6 +- .../test-api-binary-operations-arithmetics.c | 48 +-- .../test-api-binary-operations-comparisons.c | 64 ++-- .../test-api-binary-operations-instanceof.c | 10 +- tests/unit-core/test-api-errortype.c | 4 +- tests/unit-core/test-api-functiontype.c | 4 +- tests/unit-core/test-api-iteratortype.c | 4 +- .../test-api-object-property-names.c | 29 +- tests/unit-core/test-api-objecttype.c | 4 +- tests/unit-core/test-api-promise.c | 8 +- tests/unit-core/test-api-property.c | 6 +- .../test-api-set-and-clear-error-flag.c | 14 +- tests/unit-core/test-api-strings.c | 4 +- tests/unit-core/test-api-value-type.c | 10 +- tests/unit-core/test-api.c | 70 ++-- tests/unit-core/test-arraybuffer.c | 9 +- tests/unit-core/test-backtrace.c | 33 +- tests/unit-core/test-bigint.c | 2 +- tests/unit-core/test-common.h | 2 - tests/unit-core/test-container-operation.c | 7 +- tests/unit-core/test-container.c | 10 +- tests/unit-core/test-dataview.c | 4 +- tests/unit-core/test-error-callback.c | 6 +- tests/unit-core/test-external-string.c | 37 +-- .../unit-core/test-from-property-descriptor.c | 10 +- tests/unit-core/test-get-own-property.c | 6 +- tests/unit-core/test-has-property.c | 2 +- tests/unit-core/test-internal-properties.c | 12 +- tests/unit-core/test-is-eval-code.c | 4 +- tests/unit-core/test-json.c | 16 +- tests/unit-core/test-literal-storage.c | 2 +- tests/unit-core/test-module-dynamic.c | 10 +- tests/unit-core/test-module-import-meta.c | 4 +- tests/unit-core/test-module.c | 27 +- tests/unit-core/test-native-instanceof.c | 2 +- tests/unit-core/test-native-pointer.c | 4 +- tests/unit-core/test-newtarget.c | 5 +- tests/unit-core/test-number-converter.c | 2 +- tests/unit-core/test-objects-foreach.c | 6 +- tests/unit-core/test-promise.c | 17 +- tests/unit-core/test-proxy.c | 26 +- tests/unit-core/test-realm.c | 30 +- tests/unit-core/test-regexp-dotall-unicode.c | 6 +- tests/unit-core/test-regexp.c | 6 +- tests/unit-core/test-regression-3588.c | 2 +- tests/unit-core/test-script-user-value.c | 4 +- tests/unit-core/test-snapshot.c | 4 +- tests/unit-core/test-source-name.c | 16 +- tests/unit-core/test-special-proxy.c | 2 +- tests/unit-core/test-string-to-number.c | 17 +- tests/unit-core/test-symbol.c | 30 +- tests/unit-core/test-to-property-descriptor.c | 13 +- tests/unit-core/test-typedarray.c | 52 +-- tests/unit-core/test-vm-exec-stop.c | 2 +- tests/unit-core/test-vm-throw.c | 4 +- tests/unit-ext/module/jerry-module-test.c | 4 +- tests/unit-ext/module/my-custom-module.c | 9 +- tests/unit-ext/test-common.h | 2 - tests/unit-ext/test-ext-arg.c | 64 ++-- tests/unit-ext/test-ext-method-register.c | 29 +- tests/unit-ext/test-ext-module-canonical.c | 8 +- tests/unit-ext/test-ext-module-empty.c | 6 +- 118 files changed, 1089 insertions(+), 1046 deletions(-) diff --git a/docs/01.CONFIGURATION.md b/docs/01.CONFIGURATION.md index 1a263e73fa..506d280bfb 100644 --- a/docs/01.CONFIGURATION.md +++ b/docs/01.CONFIGURATION.md @@ -155,7 +155,7 @@ Enabling this feature provides detailed error messages where available, like lin ### Logging -This option can be used to enable log messages during runtime. When enabled the engine will use the `jerry_port_log` port API function to print relevant log messages. +This option can be used to enable log messages during runtime. When enabled the engine will use the `jerry_port_log_buffer` port API function to print relevant log messages. This feature is disabled by default. | Options | | diff --git a/docs/02.API-REFERENCE.md b/docs/02.API-REFERENCE.md index 3610957414..8328b34124 100644 --- a/docs/02.API-REFERENCE.md +++ b/docs/02.API-REFERENCE.md @@ -751,7 +751,6 @@ typedef void (*jerry_value_free_callback_t) (void *native_p); **See also** -- [jerry_string_external_sz](#jerry_string_external_sz) - [jerry_string_external](#jerry_string_external) - [jerry_arraybuffer_external](#jerry_arraybuffer_external) @@ -809,7 +808,6 @@ typedef void (*jerry_external_string_free_cb_t) (jerry_char_t *string_p, **See also** - [jerry_string_external_on_free](#jerry_string_external_on_free) -- [jerry_string_external_sz](#jerry_string_external_sz) - [jerry_string_external](#jerry_string_external) ## jerry_error_object_created_cb_t @@ -1887,7 +1885,7 @@ main (void) jerry_parse_options_t parse_options; parse_options.options = JERRY_PARSE_STRICT_MODE | JERRY_PARSE_HAS_SOURCE_NAME | JERRY_PARSE_HAS_START; - parse_options.source_name = jerry_string_sz ("hello.js"); + parse_options.source_name = jerry_sz_ascii ("hello.js"); /* This example script is extracted from the middle of a file. */ parse_options.start_line = 10; parse_options.start_column = 1; @@ -1920,7 +1918,7 @@ main (void) /* Specify the argument list to parse a function. */ jerry_parse_options_t parse_options; parse_options.options = JERRY_PARSE_HAS_ARGUMENT_LIST; - parse_options.argument_list = jerry_string_sz ("a, b"); + parse_options.argument_list = jerry_sz_ascii ("a, b"); const jerry_char_t function_code[] = "return a + b;"; jerry_value_t parsed_function = jerry_parse (function_code, sizeof (function_code) - 1, &parse_options); @@ -1990,11 +1988,11 @@ main (void) { jerry_init (JERRY_INIT_EMPTY); - jerry_value_t script_value = jerry_string_sz ("print ('Hello, World!');"); + jerry_value_t script_value = jerry_sz_ascii ("print ('Hello, World!');"); jerry_parse_options_t parse_options; parse_options.options = JERRY_PARSE_STRICT_MODE | JERRY_PARSE_HAS_SOURCE_NAME | JERRY_PARSE_HAS_START; - parse_options.source_name = jerry_string_sz ("hello.js"); + parse_options.source_name = jerry_sz_ascii ("hello.js"); /* This example script is extracted from the middle of a file. */ parse_options.start_line = 10; parse_options.start_column = 1; @@ -3020,7 +3018,7 @@ main (void) { jerry_init (JERRY_INIT_EMPTY); - jerry_value_t string_value = jerry_string_sz ("Symbol description string"); + jerry_value_t string_value = jerry_sz_ascii ("Symbol description string"); jerry_value_t symbol_value = jerry_symbol_with_description (string_value); jerry_value_free (string_value); @@ -3081,7 +3079,7 @@ main (void) { jerry_init (JERRY_INIT_EMPTY); - jerry_value_t string_value = jerry_string_sz ("12345678"); + jerry_value_t string_value = jerry_sz_ascii ("12345678"); jerry_value_t bigint_value = jerry_value_to_bigint (string_value); jerry_value_free (string_value); @@ -3565,7 +3563,7 @@ main (void) jerry_value_t constructor = jerry_function_external (my_constructor); /* External functions does not have a prototype by default, so we need to create one */ - jerry_value_t prototype_str = jerry_string_sz ("prototype"); + jerry_value_t prototype_str = jerry_sz_ascii ("prototype"); jerry_value_free (jerry_object_set (constructor, prototype_str, base_obj)); jerry_value_free (prototype_str); @@ -3750,7 +3748,7 @@ jerry_error_type (const jerry_value_t value); ```c { - jerry_value_t error_obj = jerry_error_sz (JERRY_ERROR_RANGE, "error msg"); + jerry_value_t error_obj = jerry_error_sz (JERRY_ERROR_RANGE, jerry_sz_ascii ("error msg")); jerry_error_t error_type = jerry_error_type (error_obj); // error_type is now JERRY_ERROR_RANGE. @@ -3868,7 +3866,7 @@ void main(void) jerry_error_on_created (error_object_created_callback, NULL); - jerry_value_free (jerry_error_sz (JERRY_ERROR_COMMON, "Message")); + jerry_value_free (jerry_error_sz (JERRY_ERROR_COMMON, jerry_sz_ascii ("Message"))); jerry_cleanup (); } /* main */ @@ -4062,7 +4060,7 @@ throw_exception (const jerry_call_info_t *call_info_p, /**< call info */ (void) argv; (void) argc; - jerry_value_t result_value = jerry_throw_sz (JERRY_ERROR_COMMON, "Error!"); + jerry_value_t result_value = jerry_throw_sz (JERRY_ERROR_COMMON, jerry_sz_ascii ("Error!")); /* Ignore calling the vm_throw_callback function. */ jerry_exception_allow_capture (result_value, false); @@ -4079,7 +4077,7 @@ main (void) jerry_value_t global_object_value = jerry_current_realm (); jerry_value_t function_value = jerry_function_external (throw_exception); - jerry_value_t function_name_value = jerry_string_sz ("throw_exception"); + jerry_value_t function_name_value = jerry_sz_ascii ("throw_exception"); jerry_value_free (jerry_object_set (global_object_value, function_name_value, function_value)); jerry_value_free (function_name_value); @@ -4172,7 +4170,7 @@ jerry_string_size (const jerry_value_t value, JERRY_ENCODING_CESU8); ```c { const char char_array[] = "a string"; - jerry_value_t string = jerry_string_sz (char_array); + jerry_value_t string = jerry_sz_ascii (char_array); jerry_size_t string_size = jerry_string_size (string, JERRY_ENCODING_CESU8); @@ -4184,7 +4182,7 @@ jerry_string_size (const jerry_value_t value, JERRY_ENCODING_CESU8); **See also** -- [jerry_string_sz](#jerry_string_sz) +- [jerry_sz_ascii](#jerry_sz_ascii) - [jerry_string_length](#jerry_string_length) - [jerry_validate_string](#jerry_validate_string) @@ -4215,7 +4213,7 @@ jerry_string_length (const jerry_value_t value); ```c { const char char_array[] = "a string"; - jerry_value_t string = jerry_string_sz (char_array); + jerry_value_t string = jerry_sz_ascii (char_array); jerry_length_t string_length = jerry_string_length (string); @@ -4227,7 +4225,7 @@ jerry_string_length (const jerry_value_t value); **See also** -- [jerry_string_sz](#jerry_string_sz) +- [jerry_sz_ascii](#jerry_sz_ascii) - [jerry_string_size](#jerry_string_size) - [jerry_validate_string](#jerry_validate_string) @@ -4279,7 +4277,7 @@ main (void) jerry_value_t value; // create or copy value - value = jerry_string_sz ("Demo string"); + value = jerry_sz_ascii ("Demo string"); // Read the string into a byte buffer. jerry_size_t string_size = jerry_string_size (value, JERRY_ENCODING_CESU8); @@ -4301,7 +4299,7 @@ main (void) **See also** -- [jerry_string_sz](#jerry_string_sz) +- [jerry_sz_ascii](#jerry_sz_ascii) - [jerry_string_size](#jerry_string_size) - [jerry_strig_to_buffer](#jerry_strig_to_buffer) @@ -4353,8 +4351,8 @@ main (void) jerry_string_external_on_free (external_string_free_callback); - const char *string_p = "This is a long external string, should not be duplicated!"; - jerry_value_t external_string = jerry_string_external_sz (string_p, NULL); +#define string_p "This is a long external string, should not be duplicated!" + jerry_value_t external_string = jerry_string_external (JERRY_ZSTR_ARG (string_p), NULL); /* The external_string_free_callback is called. */ jerry_value_free (external_string); @@ -4367,7 +4365,6 @@ main (void) - [jerry_external_string_free_cb_t](#jerry_external_string_free_cb_t) - [jerry_string_user_ptr](#jerry_string_user_ptr) -- [jerry_string_external_sz](#jerry_string_external_sz) - [jerry_string_external](#jerry_string_external) @@ -4381,7 +4378,7 @@ Returns the user pointer assigned to an external string. - In some cases (e.g. when the string is also a magic string registered by [jerry_register_magic_strings](#jerry_register_magic_strings)), the string is a normal string without a user pointer even if it is created - by [jerry_string_external_sz](#jerry_string_external_sz). + by [jerry_string_external](#jerry_string_external). **Prototype** @@ -4414,9 +4411,9 @@ main (void) { jerry_init (JERRY_INIT_EMPTY); - const char *string_p = "This is a long external string, should not be duplicated!"; +#define string_p "This is a long external string, should not be duplicated!" - jerry_value_t external_string = jerry_string_external_sz (string_p, (void *) &user_value); + jerry_value_t external_string = jerry_string_external (JERRY_ZSTR_ARG (string_p), (void *) &user_value); bool is_external; void *user_p = jerry_string_user_ptr (external_string, &is_external); @@ -4437,7 +4434,6 @@ main (void) **See also** - [jerry_string_set_external_string_free_callback](#jerry_string_set_external_string_free_callback) -- [jerry_string_external_sz](#jerry_string_external_sz) - [jerry_string_external](#jerry_string_external) @@ -4867,7 +4863,7 @@ module_resolve_callback (const jerry_value_t specifier, jerry_parse_options_t parse_options; parse_options.options = JERRY_PARSE_MODULE | JERRY_PARSE_HAS_SOURCE_NAME; - parse_options.source_name = jerry_string_sz ("b.mjs"); + parse_options.source_name = jerry_sz_ascii ("b.mjs"); jerry_value_t result = jerry_parse (script, sizeof (script) - 1, &parse_options); jerry_value_free (parse_options.source_name); @@ -4883,7 +4879,7 @@ main (void) jerry_parse_options_t parse_options; parse_options.options = JERRY_PARSE_MODULE | JERRY_PARSE_HAS_SOURCE_NAME; - parse_options.source_name = jerry_string_sz ("a.mjs"); + parse_options.source_name = jerry_sz_ascii ("a.mjs"); jerry_value_t ret_value = jerry_parse (script, sizeof (script) - 1, &parse_options); jerry_value_free (parse_options.source_name); @@ -4941,7 +4937,7 @@ main (void) jerry_parse_options_t parse_options; parse_options.options = JERRY_PARSE_MODULE | JERRY_PARSE_HAS_SOURCE_NAME; - parse_options.source_name = jerry_string_sz ("a.mjs"); + parse_options.source_name = jerry_sz_ascii ("a.mjs"); jerry_value_t module_value = jerry_parse (script, sizeof (script) - 1, &parse_options); jerry_value_free (parse_options.source_name); @@ -5002,7 +4998,7 @@ main (void) jerry_parse_options_t parse_options; parse_options.options = JERRY_PARSE_MODULE | JERRY_PARSE_HAS_SOURCE_NAME; - parse_options.source_name = jerry_string_sz ("a.mjs"); + parse_options.source_name = jerry_sz_ascii ("a.mjs"); jerry_value_t module_value = jerry_parse (script, sizeof (script) - 1, &parse_options); jerry_value_free (parse_options.source_name); @@ -5081,7 +5077,7 @@ main (void) jerry_parse_options_t parse_options; parse_options.options = JERRY_PARSE_MODULE | JERRY_PARSE_HAS_SOURCE_NAME; - parse_options.source_name = jerry_string_sz ("a.mjs"); + parse_options.source_name = jerry_sz_ascii ("a.mjs"); jerry_value_t module_value = jerry_parse (script, sizeof (script) - 1, &parse_options); jerry_value_free (parse_options.source_name); @@ -5140,8 +5136,8 @@ module_import_meta_callback (const jerry_value_t module, /**< module */ (void) user_p; /* Create a property for the meta object */ - jerry_value_t property_name_value = jerry_string_sz ("prop"); - jerry_value_t property_value = jerry_string_sz ("prop"); + jerry_value_t property_name_value = jerry_sz_ascii ("prop"); + jerry_value_t property_value = jerry_sz_ascii ("prop"); jerry_value_t result_value = jerry_object_set (meta_object, property_name_value, property_value); jerry_value_free (result_value); jerry_value_free (property_value); @@ -5220,7 +5216,7 @@ main (void) jerry_parse_options_t parse_options; parse_options.options = JERRY_PARSE_MODULE | JERRY_PARSE_HAS_SOURCE_NAME; - parse_options.source_name = jerry_string_sz ("a.mjs"); + parse_options.source_name = jerry_sz_ascii ("a.mjs"); jerry_value_t module_value = jerry_parse (script, sizeof (script) - 1, &parse_options); jerry_value_free (parse_options.source_name); @@ -5292,7 +5288,7 @@ main (void) jerry_parse_options_t parse_options; parse_options.options = JERRY_PARSE_MODULE | JERRY_PARSE_HAS_SOURCE_NAME; - parse_options.source_name = jerry_string_sz ("a.mjs"); + parse_options.source_name = jerry_sz_ascii ("a.mjs"); jerry_value_t module_value = jerry_parse (script, sizeof (script) - 1, &parse_options); jerry_value_free (parse_options.source_name); @@ -5359,7 +5355,7 @@ main (void) jerry_parse_options_t parse_options; parse_options.options = JERRY_PARSE_MODULE | JERRY_PARSE_HAS_SOURCE_NAME; - parse_options.source_name = jerry_string_sz ("a.mjs"); + parse_options.source_name = jerry_sz_ascii ("a.mjs"); jerry_value_t module_value = jerry_parse (script, sizeof (script) - 1, &parse_options); jerry_value_free (parse_options.source_name); @@ -5461,11 +5457,11 @@ main (void) parse_options.options = JERRY_PARSE_HAS_SOURCE_NAME | JERRY_PARSE_HAS_USER_VALUE; /* Resource is usually used for debugging purposes, e.g. for generating backtrace. */ - parse_options.source_name = jerry_string_sz ("dir/my_script.js"); + parse_options.source_name = jerry_sz_ascii ("dir/my_script.js"); /* User value should provide information for resolving dynamic imports. * In this case it contains the full path excluding the filename. */ - parse_options.user_value = jerry_string_sz ("/home/user/dir"); + parse_options.user_value = jerry_sz_ascii ("/home/user/dir"); jerry_value_t script_value = jerry_parse (script, sizeof (script) - 1, &parse_options); jerry_value_free (parse_options.source_name); @@ -5478,8 +5474,8 @@ main (void) const jerry_char_t module_script[] = "export var a = 5"; parse_options.options = JERRY_PARSE_MODULE | JERRY_PARSE_HAS_SOURCE_NAME | JERRY_PARSE_HAS_USER_VALUE; - parse_options.source_name = jerry_string_sz ("modules/my_module.mjs"); - parse_options.user_value = jerry_string_sz ("/home/user/dir/modules"); + parse_options.source_name = jerry_sz_ascii ("modules/my_module.mjs"); + parse_options.user_value = jerry_sz_ascii ("/home/user/dir/modules"); jerry_value_t module_value = jerry_parse (module_script, sizeof (module_script) - 1, &parse_options); jerry_value_free (parse_options.source_name); @@ -5556,8 +5552,8 @@ main (void) jerry_value_t exports[2] = { - jerry_string_sz ("first_export"), - jerry_string_sz ("second_export") + jerry_sz_ascii ("first_export"), + jerry_sz_ascii ("second_export") }; jerry_value_t native_module = jerry_native_module (NULL, exports, 2); @@ -5617,7 +5613,7 @@ main (void) { jerry_init (JERRY_INIT_EMPTY); - jerry_value_t export = jerry_string_sz ("an_export"); + jerry_value_t export = jerry_sz_ascii ("an_export"); jerry_value_t native_module = jerry_native_module (NULL, &export, 1); @@ -5678,7 +5674,7 @@ main (void) { jerry_init (JERRY_INIT_EMPTY); - jerry_value_t export = jerry_string_sz ("an_export"); + jerry_value_t export = jerry_sz_ascii ("an_export"); jerry_value_t native_module = jerry_native_module (NULL, &export, 1); @@ -5941,7 +5937,7 @@ jerry_property_descriptor_to_object (const jerry_property_descriptor_t *src_prop static void example (void) { - jerry_value_t prop_name = jerry_string_sz ("value"); + jerry_value_t prop_name = jerry_sz_ascii ("value"); jerry_property_descriptor_t prop_desc = jerry_property_descriptor (); @@ -5994,7 +5990,7 @@ static void example (void) { jerry_value_t object = jerry_object (); - jerry_value_t prop_name = jerry_string_sz ("value"); + jerry_value_t prop_name = jerry_sz_ascii ("value"); jerry_value_t value = jerry_boolean (true); jerry_property_descriptor_t prop_desc; @@ -6218,7 +6214,7 @@ main (void) { jerry_init (JERRY_INIT_EMPTY); - jerry_value_t string_value = jerry_string_sz ("foo"); + jerry_value_t string_value = jerry_sz_ascii ("foo"); jerry_value_t symbol_value = jerry_symbol_with_description (string_value); jerry_value_free (string_value); @@ -6276,7 +6272,7 @@ main (void) { jerry_init (JERRY_INIT_EMPTY); - jerry_value_t string_value = jerry_string_sz ("foo"); + jerry_value_t string_value = jerry_sz_ascii ("foo"); jerry_value_t symbol_value = jerry_symbol_with_description (string_value); jerry_value_free (string_value); @@ -6926,7 +6922,7 @@ jerry_error (jerry_error_t error_type, jerry_value_t message); ```c { - jerry_value_t message = jerry_string_sz ("error"); + jerry_value_t message = jerry_sz_ascii ("error"); jerry_value_t error_obj = jerry_error (JERRY_ERROR_COMMON, message); ... // usage of error_obj @@ -6948,7 +6944,9 @@ jerry_error (jerry_error_t error_type, jerry_value_t message); **Summary** -Create new JavaScript Error object, using the a zero-terminated string as the error message. +Create an Error object with the provided string value as the error `message`. +If the `message` value is not a string, the created error will not have a message property. +The `message` will be freed in this function. *Note*: Returned value must be freed with [jerry_value_free](#jerry_value_free) when it is no longer needed. @@ -6957,18 +6955,18 @@ is no longer needed. ```c jerry_value_t -jerry_error_sz (jerry_error_t error_type, const char *message_p); +jerry_error_sz (jerry_error_t error_type, const jerry_value_t message); ``` - `error_type` - type of the error -- `message_p` - value of 'message' property of the constructed error object +- `message` - message of the error that will be free/take - return value - constructed error object **Example** ```c { - jerry_value_t error_obj = jerry_error_sz (JERRY_ERROR_TYPE, "error"); + jerry_value_t error_obj = jerry_error_sz (JERRY_ERROR_TYPE, jerry_sz_ascii ("error")); ... // usage of error_obj @@ -7090,7 +7088,7 @@ main (void) jerry_value_t glob_obj = jerry_current_realm (); // after this, script can invoke the native handler through "handler_field (1, 2, 3);" - jerry_value_t prop_name = jerry_string_sz ("handler_field"); + jerry_value_t prop_name = jerry_sz_ascii ("handler_field"); // set property and release the return value without any check jerry_value_free (jerry_object_set (glob_obj, prop_name, func_val)); jerry_value_free (prop_name); @@ -7468,11 +7466,11 @@ main (void) - [jerry_value_free](#jerry_value_free) -## jerry_string_sz +## jerry_sz_ascii **Summary** -Create string from a zero-terminated ASCII string. +Create string from a zero-terminated ASCII string literal. *Note*: Returned value must be freed with [jerry_value_free](#jerry_value_free) when it is no longer needed. @@ -7481,7 +7479,7 @@ is no longer needed. ```c jerry_value_t -jerry_string_sz (const char *str_p); +jerry_sz_ascii (const char *str_p); ``` - `str_p` - non-null pointer to string @@ -7492,7 +7490,7 @@ jerry_string_sz (const char *str_p); ```c { const char char_array[] = "a string"; - jerry_value_t string_value = jerry_string_sz (char_array); + jerry_value_t string_value = jerry_sz_ascii (char_array); ... // usage of string_value @@ -7547,55 +7545,7 @@ jerry_string (const jerry_char_t *buffer_p, **See also** - [jerry_validate_string](#jerry_validate_string) -- [jerry_string_sz](#jerry_string_sz) - - -## jerry_string_external_sz - -**Summary** - -Create an external string from a zero-terminated ASCII string. The string buffer passed to the function -should not be modified until the free callback is called. This function can be used to avoid -the duplication of large strings. - -*Note*: - - The free callback can be set by [jerry_string_external_on_free](#jerry_string_external_on_free) - - Returned value must be freed with [jerry_value_free](#jerry_value_free) - when it is no longer needed. - -**Prototype** - -```c -jerry_value_t -jerry_string_external_sz (const char *str_p, void *user_p); -``` - -- `str_p` - non-null pointer to string -- `user_p` - user pointer passed to the callback when the string is freed -- return value - value of the created string - -*New in version 2.4*. - -*Changed in version [[NEXT_RELEASE]]*: `free_cb` is replaced by `user_p`. - -**Example** - -```c -{ - const char* string_p = "a large and immutable string: this is a story about ...."; - jerry_value_t string_value = jerry_string_external_sz (string_p, NULL); - - ... // usage of string_value - - jerry_value_free (string_value); -} -``` - -**See also** - -- [jerry_string_external](#jerry_string_external) -- [jerry_string_external_on_free](#jerry_string_external_on_free) - +- [jerry_sz_ascii](#jerry_sz_ascii) ## jerry_string_external @@ -7647,7 +7597,7 @@ jerry_string_external (const jerry_char_t *str_p, **See also** - [jerry_validate_string](#jerry_validate_string) -- [jerry_string_external_sz](#jerry_string_external_sz) +- [jerry_string_external](#jerry_string_external) - [jerry_string_external_on_free](#jerry_string_external_on_free) @@ -7688,7 +7638,7 @@ main (void) { jerry_init (JERRY_INIT_EMPTY); - jerry_value_t string_value = jerry_string_sz ("Symbol description string"); + jerry_value_t string_value = jerry_sz_ascii ("Symbol description string"); jerry_value_t symbol_value = jerry_symbol_with_description (string_value); // The description value is no longer needed @@ -7794,10 +7744,10 @@ jerry_regexp_sz (const jerry_char_t *pattern_p, uint16_t flags); ```c { - jerry_char_t pattern_p = "[cgt]gggtaaa|tttaccc[acg]"; +#define pattern "[cgt]gggtaaa|tttaccc[acg]" uint16_t pattern_flags = JERRY_REGEXP_FLAG_IGNORE_CASE; - jerry_value_t regexp = jerry_regexp_sz (pattern_p, pattern_flags); + jerry_value_t regexp = jerry_regexp_sz (jerry_sz_ascii (pattern), pattern_flags); ... @@ -8220,7 +8170,7 @@ main (void) jerry_init (JERRY_INIT_EMPTY); jerry_value_t global_object = jerry_current_realm (); - jerry_value_t prop_name = jerry_string_sz ("handler_field"); + jerry_value_t prop_name = jerry_sz_ascii ("handler_field"); jerry_value_t has_prop_js = jerry_object_has (global_object, prop_name); bool has_prop = jerry_value_is_true (has_prop_js); @@ -8281,7 +8231,7 @@ main (void) jerry_init (JERRY_INIT_EMPTY); jerry_value_t global_object = jerry_current_realm (); - jerry_value_t prop_name = jerry_string_sz ("handler_field"); + jerry_value_t prop_name = jerry_sz_ascii ("handler_field"); jerry_value_t has_prop_js = jerry_object_has_own (global_object, prop_name); bool has_prop = jerry_value_is_true (has_prop_js); @@ -8343,7 +8293,7 @@ main (void) jerry_init (JERRY_INIT_EMPTY); jerry_value_t global_object = jerry_current_realm (); - jerry_value_t prop_name = jerry_string_sz ("hidden_property"); + jerry_value_t prop_name = jerry_sz_ascii ("hidden_property"); bool has_internal_js_prop = jerry_object_has_internal (global_object, prop_name); @@ -8386,7 +8336,7 @@ jerry_object_delete (const jerry_value_t obj_val, ```c { jerry_value_t global_object = jerry_current_realm (); - jerry_value_t prop_name = jerry_string_sz ("my_prop"); + jerry_value_t prop_name = jerry_sz_ascii ("my_prop"); jerry_value_t delete_result = jerry_object_delete (global_object, prop_name); /* use "delete_result" */ @@ -8482,7 +8432,7 @@ jerry_object_delete_internal (const jerry_value_t obj_val, ```c { jerry_value_t global_object = jerry_current_realm (); - jerry_value_t prop_name = jerry_string_sz ("hidden_property"); + jerry_value_t prop_name = jerry_sz_ascii ("hidden_property"); bool delete_result = jerry_object_delete_internal (global_object, prop_name); /* use "delete_result" */ @@ -8535,7 +8485,7 @@ main (void) jerry_init (JERRY_INIT_EMPTY); jerry_value_t global_object = jerry_current_realm (); - jerry_value_t prop_name = jerry_string_sz ("Object"); + jerry_value_t prop_name = jerry_sz_ascii ("Object"); jerry_value_t prop_value = jerry_object_get (global_object, prop_name); @@ -8656,7 +8606,7 @@ main (void) jerry_init (JERRY_INIT_EMPTY); jerry_value_t global_object = jerry_current_realm (); - jerry_value_t prop_name = jerry_string_sz ("Object"); + jerry_value_t prop_name = jerry_sz_ascii ("Object"); bool found; jerry_value_t prop_value = jerry_object_find_own (global_object, prop_name, global_object, &found); @@ -8723,7 +8673,7 @@ main (void) jerry_init (JERRY_INIT_EMPTY); jerry_value_t global_object = jerry_current_realm (); - jerry_value_t prop_name = jerry_string_sz ("hidden_property"); + jerry_value_t prop_name = jerry_sz_ascii ("hidden_property"); jerry_value_t prop_value = jerry_object_get_internal (global_object, prop_name); @@ -8778,7 +8728,7 @@ jerry_object_set (const jerry_value_t obj_val, ... // create or copy value to set jerry_value_t glob_obj = jerry_current_realm (); - jerry_value_t prop_name = jerry_string_sz ("my_prop"); + jerry_value_t prop_name = jerry_sz_ascii ("my_prop"); jerry_value_t set_result = jerry_object_set (glob_obj, prop_name, value_to_set); @@ -8902,7 +8852,7 @@ main (void) jerry_init (JERRY_INIT_EMPTY); jerry_value_t global_object = jerry_current_realm (); - jerry_value_t prop_name = jerry_string_sz ("hidden_property"); + jerry_value_t prop_name = jerry_sz_ascii ("hidden_property"); jerry_value_t value_to_set = jerry_number (5); bool set_result = jerry_object_set_internal (global_object, prop_name, value_to_set); @@ -9026,7 +8976,7 @@ main (void) prop_desc.value = value_to_set; // add the property as "my_prop" for the global object - jerry_value_t prop_name = jerry_string_sz ("my_prop"); + jerry_value_t prop_name = jerry_sz_ascii ("my_prop"); jerry_value_t return_value = jerry_object_define_own_prop (global_obj_val, prop_name, &prop_desc); if (jerry_value_is_exception (return_value)) { @@ -9110,7 +9060,7 @@ main (void) prop_desc.setter = jerry_function_external (method_setter); // add the property as "my_prop" for the global object - jerry_value_t prop_name = jerry_string_sz ("my_prop"); + jerry_value_t prop_name = jerry_sz_ascii ("my_prop"); jerry_value_t return_value = jerry_object_define_own_prop (global_obj_val, prop_name, &prop_desc); if (jerry_value_is_exception (return_value)) { @@ -9178,7 +9128,7 @@ jerry_object_get_own_prop (const jerry_value_t obj_val, jerry_property_descriptor_t prop_desc = jerry_property_descriptor (); - jerry_value_t prop_name = jerry_string_sz ("my_prop"); + jerry_value_t prop_name = jerry_sz_ascii ("my_prop"); jerry_object_get_own_prop (global_obj_val, prop_name, &prop_desc); jerry_value_free (prop_name); @@ -10049,7 +9999,7 @@ main (void) /* Construct an example object with a single property. */ jerry_value_t object = jerry_object (); { - jerry_value_t test_property = jerry_string_sz ("DemoProp"); + jerry_value_t test_property = jerry_sz_ascii ("DemoProp"); jerry_value_t test_value = jerry_number (3); /* By default all properties added to an object are enumerable. */ jerry_value_t set_result = jerry_object_set (object, test_property, test_value); @@ -10157,7 +10107,7 @@ main (void) jerry_value_t test_object = jerry_object (); { - jerry_value_t test_property = jerry_string_sz ("DemoProp"); + jerry_value_t test_property = jerry_sz_ascii ("DemoProp"); jerry_value_t test_value = jerry_number (3); jerry_value_t set_result = jerry_object_set (test_object, test_property, test_value); /* The `set_result` should be checked if it is an exception or not. */ @@ -10169,7 +10119,7 @@ main (void) { /* Register the test object into the global object. */ jerry_value_t global_object = jerry_current_realm (); - jerry_value_t demo_property = jerry_string_sz ("DemoObject"); + jerry_value_t demo_property = jerry_sz_ascii ("DemoObject"); jerry_value_t set_result = jerry_object_set (global_object, demo_property, test_object); /* The `set_result` should be checked if it is an exception or not. */ jerry_value_free (set_result); @@ -10183,7 +10133,7 @@ main (void) /* Look up the test object base on a property name. */ find_my_object_info_t search_info = { - .property_name = jerry_string_sz ("DemoProp") + .property_name = jerry_sz_ascii ("DemoProp") }; if (jerry_foreach_live_object (find_my_object, &search_info)) @@ -10300,7 +10250,7 @@ add_object_with_nativeptr (int foo_value) /* Register the test object into the global object. */ jerry_value_t global_object = jerry_current_realm (); - jerry_value_t demo_property = jerry_string_sz ("DemoObject"); + jerry_value_t demo_property = jerry_sz_ascii ("DemoObject"); jerry_value_t set_result = jerry_object_set (global_object, demo_property, test_object); /* The `set_result` should be checked if it is an exception or not. */ jerry_value_free (set_result); @@ -10679,7 +10629,7 @@ main (void) jerry_parse_options_t parse_options; parse_options.options = JERRY_PARSE_HAS_ARGUMENT_LIST; - parse_options.argument_list = jerry_string_sz ("a, b"); + parse_options.argument_list = jerry_sz_ascii ("a, b"); jerry_value_t parse_result = jerry_parse (function_to_snapshot, sizeof (function_to_snapshot) - 1, @@ -10911,7 +10861,7 @@ main (void) /* Register the "capture_backtrace" method. */ { jerry_value_t func = jerry_function_external (backtrace_handler); - jerry_value_t name = jerry_string_sz ("backtrace"); + jerry_value_t name = jerry_sz_ascii ("backtrace"); jerry_value_t result = jerry_object_set (global, name, func); jerry_value_free (result); jerry_value_free (name); @@ -10927,7 +10877,7 @@ main (void) jerry_parse_options_t parse_options; parse_options.options = JERRY_PARSE_HAS_SOURCE_NAME; - parse_options.source_name = jerry_string_sz ("demo_memory.js"); + parse_options.source_name = jerry_sz_ascii ("demo_memory.js"); jerry_value_t program = jerry_parse ((const jerry_char_t *) source, strlen (source), @@ -11016,7 +10966,7 @@ main (void) /* Register the "dump_backtrace" method. */ { jerry_value_t func = jerry_function_external (backtrace_handler); - jerry_value_t name = jerry_string_sz ("backtrace"); + jerry_value_t name = jerry_sz_ascii ("backtrace"); jerry_value_t result = jerry_object_set (global, name, func); jerry_value_free (result); jerry_value_free (name); @@ -11032,7 +10982,7 @@ main (void) jerry_parse_options_t parse_options; parse_options.options = JERRY_PARSE_HAS_SOURCE_NAME; - parse_options.source_name = jerry_string_sz ("demo_backtrace.js"); + parse_options.source_name = jerry_sz_ascii ("demo_backtrace.js"); jerry_value_t program = jerry_parse ((const jerry_char_t *) source, strlen (source), @@ -11402,7 +11352,7 @@ vm_exec_stop_callback (void *user_p) } // The value will be automatically wrapped into an exception. - return jerry_string_sz ("Abort script"); + return jerry_sz_ascii ("Abort script"); } int @@ -11487,7 +11437,7 @@ main (void) /* Register the sourceName" method. */ { jerry_value_t func = jerry_function_external (source_name_handler); - jerry_value_t name = jerry_string_sz ("sourceName"); + jerry_value_t name = jerry_sz_ascii ("sourceName"); jerry_value_t result = jerry_object_set (global, name, func); jerry_value_free (result); jerry_value_free (name); @@ -11500,7 +11450,7 @@ main (void) jerry_parse_options_t parse_options; parse_options.options = JERRY_PARSE_HAS_SOURCE_NAME; - parse_options.source_name = jerry_string_sz ("demo.js"); + parse_options.source_name = jerry_sz_ascii ("demo.js"); jerry_value_t program = jerry_parse (source, sizeof (source) - 1, @@ -12738,8 +12688,8 @@ main (void) jerry_value_t obj = jerry_object (); { - jerry_value_t key = jerry_string_sz ("name"); - jerry_value_t value = jerry_string_sz ("John"); + jerry_value_t key = jerry_sz_ascii ("name"); + jerry_value_t value = jerry_sz_ascii ("John"); jerry_value_free (jerry_object_set (obj, key, value)); jerry_value_free (key); jerry_value_free (value); @@ -12876,7 +12826,7 @@ main (void) jerry_init (JERRY_INIT_EMPTY); jerry_value_t map = jerry_container (JERRY_CONTAINER_TYPE_MAP, NULL, 0); - jerry_value_t key_str = jerry_string_sz ("number"); + jerry_value_t key_str = jerry_sz_ascii ("number"); jerry_value_t number = jerry_number (10); jerry_value_t args[2] = {key_str, number}; diff --git a/docs/03.API-EXAMPLE.md b/docs/03.API-EXAMPLE.md index e8616cc44e..6c844e218f 100644 --- a/docs/03.API-EXAMPLE.md +++ b/docs/03.API-EXAMPLE.md @@ -168,7 +168,7 @@ This method will be implemented in C and will be called from the JavaScript code For this a few extra API methods are required: - `jerry_current_realm` -- `jerry_string_sz` +- `jerry_sz_ascii` - `jerry_object_set` - `jerry_function_external` @@ -207,7 +207,7 @@ main (void) /* Get the "global" object */ jerry_value_t global_object = jerry_current_realm (); /* Create a "print" JS string */ - jerry_value_t property_name_print = jerry_string_sz ("print"); + jerry_value_t property_name_print = jerry_sz_ascii ("print"); /* Create a function from a native C method (this function will be called from JS) */ jerry_value_t property_value_func = jerry_function_external (print_handler); /* Add the "print" property with the function value to the "global" object */ @@ -326,7 +326,7 @@ main (void) /* Get the "global" object */ jerry_value_t global_object = jerry_current_realm (); /* Create a "print" JS string */ - jerry_value_t property_name_print = jerry_string_sz ("print"); + jerry_value_t property_name_print = jerry_sz_ascii ("print"); /* Create a function from a native C method (this function will be called from JS) */ jerry_value_t property_value_func = jerry_function_external (print_handler); /* Add the "print" property with the function value to the "global" object */ @@ -393,9 +393,17 @@ In this example the following extension methods are used: In further examples this "print" handler will be used. +The `api-example-6.c` file should contain the following code: + +[doctest]: # () + ```c +#include + #include "jerryscript.h" + #include "jerryscript-ext/handlers.h" +#include "jerryscript-ext/properties.h" int main (void) @@ -407,7 +415,7 @@ main (void) jerry_init (JERRY_INIT_EMPTY); /* Register 'print' function from the extensions to the global object */ - jerryx_register_global ("print", jerryx_handler_print); + jerryx_register_global (jerry_sz_ascii ("print"), jerryx_handler_print); /* Setup Global scope code */ jerry_value_t parsed_code = jerry_parse (script, script_size, NULL); @@ -470,14 +478,14 @@ main (void) jerry_init (JERRY_INIT_EMPTY); /* Register 'print' function from the extensions */ - jerryx_register_global ("print", jerryx_handler_print); + jerryx_register_global (jerry_sz_ascii ("print"), jerryx_handler_print); /* Getting pointer to the Global object */ jerry_value_t global_object = jerry_current_realm (); /* Constructing strings */ - jerry_value_t prop_name = jerry_string_sz ("my_var"); - jerry_value_t prop_value = jerry_string_sz ("Hello from C!"); + jerry_value_t prop_name = jerry_sz_ascii ("my_var"); + jerry_value_t prop_value = jerry_sz_ascii ("Hello from C!"); /* Setting the string value as a property of the Global object */ jerry_value_t set_result = jerry_object_set (global_object, prop_name, prop_value); @@ -729,7 +737,7 @@ main (void) jerry_init (JERRY_INIT_EMPTY); /* Register 'print' function from the extensions */ - jerryx_register_global ("print", jerryx_handler_print); + jerryx_register_global (jerry_sz_ascii ("print"), jerryx_handler_print); while (!is_done) { @@ -808,10 +816,8 @@ In this example (`api-example-9.c`) an object with a native function is added to #include "jerryscript-ext/handlers.h" #include "jerryscript-ext/properties.h" -struct my_struct -{ - const char *msg; -} my_struct; + +jerry_string_t my_struct; /** * Get a string from a native object @@ -821,7 +827,7 @@ get_msg_handler (const jerry_call_info_t *call_info_p, /**< call information */ const jerry_value_t *args_p, /**< function arguments */ const jerry_length_t args_cnt) /**< number of function arguments */ { - return jerry_string_sz (my_struct.msg); + return jerry_string_ascii (my_struct.ptr, my_struct.size); } /* get_msg_handler */ int @@ -831,10 +837,13 @@ main (void) jerry_init (JERRY_INIT_EMPTY); /* Register 'print' function from the extensions */ - jerryx_register_global ("print", jerryx_handler_print); + jerryx_register_global (jerry_sz_ascii ("print"), jerryx_handler_print); /* Do something with the native object */ - my_struct.msg = "Hello, World!"; + { + static const jerry_string_t hello = { JERRY_ZSTR_ARG ("Hello, World!") }; + my_struct = hello; + } /* Create an empty JS object */ jerry_value_t object = jerry_object (); @@ -843,7 +852,7 @@ main (void) jerry_value_t func_obj = jerry_function_external (get_msg_handler); /* Set the native function as a property of the empty JS object */ - jerry_value_t prop_name = jerry_string_sz ("myFunc"); + jerry_value_t prop_name = jerry_sz_ascii ("myFunc"); jerry_value_free (jerry_object_set (object, prop_name, func_obj)); jerry_value_free (prop_name); jerry_value_free (func_obj); @@ -852,7 +861,7 @@ main (void) jerry_value_t global_object = jerry_current_realm (); /* Add the JS object to the global context */ - prop_name = jerry_string_sz ("MyObject"); + prop_name = jerry_sz_ascii ("MyObject"); jerry_value_free (jerry_object_set (global_object, prop_name, object)); jerry_value_free (prop_name); jerry_value_free (object); @@ -928,7 +937,7 @@ add_handler (const jerry_call_info_t *call_info_p, /**< call information */ /* Note: that the argument count check is ignored for the example's case */ /* Get 'this.x' */ - jerry_value_t prop_name = jerry_string_sz ("x"); + jerry_value_t prop_name = jerry_sz_ascii ("x"); jerry_value_t x_val = jerry_object_get (call_info_p->this_value, prop_name); if (!jerry_value_is_exception (x_val)) @@ -958,7 +967,7 @@ main (void) jerry_init (JERRY_INIT_EMPTY); /* Register 'print' function from the extensions */ - jerryx_register_global ("print", jerryx_handler_print); + jerryx_register_global (jerry_sz_ascii ("print"), jerryx_handler_print); /* Create a JS object */ const jerry_char_t my_js_object[] = " \ @@ -983,7 +992,7 @@ main (void) jerry_value_t add_func_obj = jerry_function_external (add_handler); /* Set the native function as a property of previously created MyObject */ - jerry_value_t prop_name = jerry_string_sz ("add2x"); + jerry_value_t prop_name = jerry_sz_ascii ("add2x"); jerry_value_free (jerry_object_set (my_js_obj_val, prop_name, add_func_obj)); jerry_value_free (add_func_obj); jerry_value_free (prop_name); @@ -1058,7 +1067,7 @@ main (void) jerry_init (JERRY_INIT_EMPTY); /* Register the print function */ - jerryx_register_global ("print", jerryx_handler_print); + jerryx_register_global (jerry_sz_ascii ("print"), jerryx_handler_print); /* Evaluate the script */ jerry_value_t eval_ret = jerry_eval (script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS); diff --git a/docs/05.PORT-API.md b/docs/05.PORT-API.md index 298a1c4402..bdacf42f65 100644 --- a/docs/05.PORT-API.md +++ b/docs/05.PORT-API.md @@ -112,8 +112,11 @@ void jerry_port_context_free (void); * * The implementation can decide whether error and debug messages are logged to * the console, or saved to a database or to a file. + * + * @param buffer_p: input buffer + * @param buffer_size: data size */ -void jerry_port_log (const char *message_p); +void jerry_port_log_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size); ``` ```c diff --git a/docs/07.DEBUGGER.md b/docs/07.DEBUGGER.md index 09c9c582d6..e2af036a3b 100644 --- a/docs/07.DEBUGGER.md +++ b/docs/07.DEBUGGER.md @@ -318,9 +318,8 @@ wait_for_source_callback (const jerry_char_t *source_name_p, /**< source name */ jerry_parse_options_t parse_options; parse_options.options = JERRY_PARSE_HAS_SOURCE_NAME; - parse_options.source_name = jerry_string ((const jerry_char_t *) source_name_p, - (jerry_size_t) source_name_size, - JERRY_ENCODING_UTF8); + parse_options.source_name = jerry_string_utf8 ((const jerry_char_t *) source_name_p, + (jerry_size_t) source_name_size); jerry_value_t ret_val = jerry_parse (source_p, source_size, diff --git a/docs/09.EXT-REFERENCE-ARG.md b/docs/09.EXT-REFERENCE-ARG.md index 191bb627f3..95efef412a 100644 --- a/docs/09.EXT-REFERENCE-ARG.md +++ b/docs/09.EXT-REFERENCE-ARG.md @@ -612,6 +612,7 @@ jerryx_arg_object_properties (const jerryx_arg_object_props_t *obj_prop_p, ```c #include "jerryscript.h" + #include "jerryscript-ext/arg.h" /** @@ -628,39 +629,31 @@ my_external_handler (const jerry_value_t function_obj, { bool required_bool; double required_num; - double optional_num = 1234.567; // default value + double optional_num = 1234.567; // default value /* "prop_name_p" defines the name list of the expected properties' names. */ - const char *prop_name_p[] = { "enable", "data", "extra_data" }; + const jerry_value_t prop_name_p[] = { jerry_sz_ascii ("enable"), + jerry_sz_ascii ("data"), + jerry_sz_ascii ("extra_data") }; /* "prop_mapping" defines the steps to transform properties to C variables. */ - const jerryx_arg_t prop_mapping[] = - { - jerryx_arg_boolean (&required_bool, JERRYX_ARG_COERCE, JERRYX_ARG_REQUIRED), - jerryx_arg_number (&required_num, JERRYX_ARG_COERCE, JERRYX_ARG_REQUIRED), - jerryx_arg_number (&optional_num, JERRYX_ARG_COERCE, JERRYX_ARG_OPTIONAL) - }; + const jerryx_arg_t prop_mapping[] = { jerryx_arg_boolean (&required_bool, JERRYX_ARG_COERCE, JERRYX_ARG_REQUIRED), + jerryx_arg_number (&required_num, JERRYX_ARG_COERCE, JERRYX_ARG_REQUIRED), + jerryx_arg_number (&optional_num, JERRYX_ARG_COERCE, JERRYX_ARG_OPTIONAL) }; /* Prepare the jerryx_arg_object_props_t instance. */ - const jerryx_arg_object_props_t prop_info = - { - .name_p = (const jerry_char_t **) prop_name_p, - .name_cnt = 3, - .c_arg_p = prop_mapping, - .c_arg_cnt = 3 - }; + const jerryx_arg_object_props_t prop_info = { .name_p = prop_name_p, + .name_cnt = 3, + .c_arg_p = prop_mapping, + .c_arg_cnt = 3 }; /* It is the mapping used in the jerryx_arg_transform_args. */ - const jerryx_arg_t mapping[] = - { - jerryx_arg_object_properties (&prop_info, JERRYX_ARG_REQUIRED) - }; + const jerryx_arg_t mapping[] = { jerryx_arg_object_properties (&prop_info, JERRYX_ARG_REQUIRED) }; + + jerry_value_list_free (prop_name_p, sizeof (prop_name_p) / sizeof (prop_name_p[0])); /* Validate and transform. */ - const jerry_value_t rv = jerryx_arg_transform_args (args_p, - args_count, - mapping, - 1); + const jerry_value_t rv = jerryx_arg_transform_args (args_p, args_count, mapping, 1); if (jerry_value_is_exception (rv)) { @@ -673,9 +666,8 @@ my_external_handler (const jerry_value_t function_obj, * required_bool, required_num and optional_num can now be used. */ - return jerry_undefined (); /* Or return something more meaningful. */ + return jerry_undefined (); /* Or return something more meaningful. */ } - ``` **See also** diff --git a/docs/10.EXT-REFERENCE-HANDLER.md b/docs/10.EXT-REFERENCE-HANDLER.md index 5767ee27d0..1df81efe55 100644 --- a/docs/10.EXT-REFERENCE-HANDLER.md +++ b/docs/10.EXT-REFERENCE-HANDLER.md @@ -120,8 +120,8 @@ main (int argc, char **argv) jerryx_property_entry methods[] = { - { "demo", jerry_function_external (handler) }, - { NULL, 0 }, + JERRYX_PROPERTY_FUNCTION("demo", handler), + JERRYX_PROPERTY_LIST_END(), }; jerry_value_t global = jerry_current_realm (); @@ -362,7 +362,7 @@ longer needed. ```c jerry_value_t -jerryx_register_global (const char *name_p, +jerryx_register_global (jerry_value_t function_name_val, jerry_external_handler_t handler_p); ``` @@ -381,14 +381,15 @@ jerryx_register_global (const char *name_p, #include "jerryscript-ext/properties.h" static const struct { - const char *name_p; + const jerry_char_t *name_p; + jerry_size_t name_size; jerry_external_handler_t handler_p; } common_functions[] = { - { "assert", jerryx_handler_assert }, - { "gc", jerryx_handler_gc }, - { "print", jerryx_handler_print }, - { NULL, NULL } + { JERRY_ZSTR_ARG ("assert"), jerryx_handler_assert }, + { JERRY_ZSTR_ARG ("gc"), jerryx_handler_gc }, + { JERRY_ZSTR_ARG ("print"), jerryx_handler_print }, + { NULL, 0, NULL } }; static void @@ -398,7 +399,7 @@ register_common_functions (void) for (int i = 0; common_functions[i].name_p != NULL && !jerry_value_is_exception (ret); i++) { - ret = jerryx_register_global (common_functions[i].name_p, + ret = jerryx_register_global (jerry_string_ascii (common_functions[i].name_p, common_functions[i].name_size), common_functions[i].handler_p); } diff --git a/docs/11.EXT-REFERENCE-AUTORELEASE.md b/docs/11.EXT-REFERENCE-AUTORELEASE.md index 7d46247e48..17607d1d9f 100644 --- a/docs/11.EXT-REFERENCE-AUTORELEASE.md +++ b/docs/11.EXT-REFERENCE-AUTORELEASE.md @@ -21,7 +21,7 @@ using the `__cleanup__` variable attribute. For other compilers, no support has static void foo (bool enable) { - JERRYX_AR_VALUE_T bar = jerry_string_sz ("..."); + JERRYX_AR_VALUE_T bar = jerry_sz_ascii ("..."); if (enable) { diff --git a/docs/16.MIGRATION-GUIDE.md b/docs/16.MIGRATION-GUIDE.md index 553330cb1b..c175c0bf48 100644 --- a/docs/16.MIGRATION-GUIDE.md +++ b/docs/16.MIGRATION-GUIDE.md @@ -38,6 +38,8 @@ For more information on the current API methods please check the [API reference] - `jerry_get_value_without_error_flag` replaced by `jerry_get_value_from_error` - `jerry_parse_and_save_snapshot` replaced by `jerry_generate_snapshot` - `jerry_parse_and_save_function_snapshot` replaced by `jerry_generate_function_snapshot` +- `jerry_string_external_sz` replaced by `jerry_string_external` +- `jerry_string_sz` replaced by `jerry_sz_ascii` or `jerry_string_cesu8` ***Removed unused configuration macros*** diff --git a/jerry-core/api/jerry-module.c b/jerry-core/api/jerry-module.c index c0443866a8..9f0488045c 100644 --- a/jerry-core/api/jerry-module.c +++ b/jerry-core/api/jerry-module.c @@ -16,7 +16,7 @@ #include "jerryscript-core.h" #include "jerryscript-port.h" -#include "ecma-errors.h" +#include "ecma-globals.h" #include "lit-magic-strings.h" #include "lit-strings.h" @@ -162,7 +162,7 @@ jerry_module_resolve (const jerry_value_t specifier, /**< module specifier strin if (path_p == NULL) { - return jerry_throw_sz (JERRY_ERROR_SYNTAX, "Failed to resolve module"); + return jerry_throw_sz (JERRY_ERROR_SYNTAX, jerry_sz_ascii ("Failed to resolve module")); } jerry_value_t realm = jerry_current_realm (); @@ -192,7 +192,7 @@ jerry_module_resolve (const jerry_value_t specifier, /**< module specifier strin jerry_value_free (realm); jerry_port_path_free (path_p); - return jerry_throw_sz (JERRY_ERROR_SYNTAX, "Module file not found"); + return jerry_throw_sz (JERRY_ERROR_SYNTAX, jerry_sz_ascii ("Module file not found")); } jerry_parse_options_t parse_options; diff --git a/jerry-core/api/jerry-snapshot.c b/jerry-core/api/jerry-snapshot.c index 33760b299f..2f4e194971 100644 --- a/jerry-core/api/jerry-snapshot.c +++ b/jerry-core/api/jerry-snapshot.c @@ -140,7 +140,7 @@ snapshot_add_compiled_code (const ecma_compiled_code_t *compiled_code_p, /**< co size_t snapshot_buffer_size, /**< snapshot buffer size */ snapshot_globals_t *globals_p) /**< snapshot globals */ { - const char *error_buffer_too_small_p = "Snapshot buffer too small"; +#define error_buffer_too_small_p "Snapshot buffer too small" if (!ecma_is_value_empty (globals_p->snapshot_error)) { @@ -180,7 +180,7 @@ snapshot_add_compiled_code (const ecma_compiled_code_t *compiled_code_p, /**< co /* Regular expression. */ if (globals_p->snapshot_buffer_write_offset + sizeof (ecma_compiled_code_t) > snapshot_buffer_size) { - globals_p->snapshot_error = jerry_throw_sz (JERRY_ERROR_RANGE, error_buffer_too_small_p); + globals_p->snapshot_error = jerry_throw_sz (JERRY_ERROR_RANGE, jerry_sz_ascii (error_buffer_too_small_p)); return 0; } @@ -201,7 +201,7 @@ snapshot_add_compiled_code (const ecma_compiled_code_t *compiled_code_p, /**< co buffer_p, buffer_size)) { - globals_p->snapshot_error = jerry_throw_sz (JERRY_ERROR_RANGE, error_buffer_too_small_p); + globals_p->snapshot_error = jerry_throw_sz (JERRY_ERROR_RANGE, jerry_sz_ascii (error_buffer_too_small_p)); /* cannot return inside ECMA_FINALIZE_UTF8_STRING */ } @@ -235,7 +235,7 @@ snapshot_add_compiled_code (const ecma_compiled_code_t *compiled_code_p, /**< co compiled_code_p, ((size_t) compiled_code_p->size) << JMEM_ALIGNMENT_LOG)) { - globals_p->snapshot_error = jerry_throw_sz (JERRY_ERROR_RANGE, error_buffer_too_small_p); + globals_p->snapshot_error = jerry_throw_sz (JERRY_ERROR_RANGE, jerry_sz_ascii (error_buffer_too_small_p)); return 0; } diff --git a/jerry-core/api/jerryscript.c b/jerry-core/api/jerryscript.c index 60f8023fd0..354e5b3aa2 100644 --- a/jerry-core/api/jerryscript.c +++ b/jerry-core/api/jerryscript.c @@ -2349,6 +2349,19 @@ jerry_value_free (jerry_value_t value) /**< value */ ecma_free_value (value); } /* jerry_value_free */ +/** + * Release ownership of the argument value list + */ +void +jerry_value_list_free (const jerry_value_t *value_list, /**< value list */ + jerry_size_t value_list_count) /**< value list count */ +{ + for (jerry_size_t i = 0; i < value_list_count; i += 1) + { + jerry_value_free (value_list[i]); + } +} /* jerry_value_list_free */ + /** * Create an array object value * @@ -2391,39 +2404,22 @@ jerry_error (jerry_error_t error_type, /**< type of error */ { jerry_assert_api_enabled (); - ecma_string_t *message_p = NULL; - if (ecma_is_value_string (message)) - { - message_p = ecma_get_string_from_value (message); - } - - ecma_object_t *error_object_p = ecma_new_standard_error ((jerry_error_t) error_type, message_p); - - return ecma_make_object_value (error_object_p); + return ecma_error_value (error_type, message); } /* jerry_error */ /** - * Create an Error object with a zero-terminated string as a message. If the message string is NULL, the created error - * will not have a message property. - * + * Create an Error object with the provided string value as the error `message`. + * If the `message` value is not a string, the created error will not have a `message` property. + * The `message` will be freed in this function. * @return Error object */ jerry_value_t jerry_error_sz (jerry_error_t error_type, /**< type of error */ - const char *message_p) /**< value of 'message' property - * of constructed error object */ + const jerry_value_t message) /**< message of the error that will be free/take */ { - jerry_value_t message = ECMA_VALUE_UNDEFINED; - - if (message_p != NULL) - { - message = jerry_string_sz (message_p); - } - - ecma_value_t error = jerry_error (error_type, message); - ecma_free_value (message); + jerry_assert_api_enabled (); - return error; + return ecma_error_value_taked (error_type, message); } /* jerry_error_sz */ /** @@ -2440,17 +2436,18 @@ jerry_throw (jerry_error_t error_type, /**< type of error */ } /* jerry_throw */ /** - * Create an exception by constructing an Error object with the specified type and the provided zero-terminated ASCII - * string as the error message. If the message string is NULL, the created error will not have a message property. + * Create an exception by constructing an Error object with the specified type and the + * provided string value as the error message. If the message value is not a string, + * the created error will not have a message property. + * The message will be freed in this function. * * @return exception value */ jerry_value_t jerry_throw_sz (jerry_error_t error_type, /**< type of error */ - const char *message_p) /**< value of 'message' property - * of constructed error object */ + const jerry_value_t message) /**< message of the error that will be free/take */ { - return jerry_throw_value (jerry_error_sz (error_type, message_p), true); + return jerry_throw_value (jerry_error_sz (error_type, message), true); } /* jerry_throw_sz */ /** @@ -2657,23 +2654,66 @@ jerry_proxy_custom (const jerry_value_t target, /**< target argument */ } /* jerry_proxy_custom */ /** - * Create string value from the input zero-terminated ASCII string. + * Create a string value from the input buffer using the ASCII encoding. + * The content of the buffer is assumed to be valid in the ASCII encoding, + * it's the callers responsibility to validate the input. + * + * See also: lit_is_valid_ascii_string + * + * @return created string + */ +jerry_value_t +jerry_string_ascii (const jerry_char_t *buffer_p, /**< pointer to buffer */ + jerry_size_t buffer_size) /**< buffer size */ +{ + jerry_assert_api_enabled (); + JERRY_ASSERT (lit_is_valid_ascii_string (buffer_p, buffer_size)); + return ecma_make_string_value (ecma_new_ecma_string_from_ascii (buffer_p, buffer_size)); +} /* jerry_string_ascii */ + +/** + * Create a string value from the input buffer using the CESU-8 encoding. + * The content of the buffer is assumed to be valid in the CESU-8 encoding, + * it's the callers responsibility to validate the input. + * + * See also: lit_is_valid_cesu8_string + * + * @return created string + */ +jerry_value_t +jerry_string_cesu8 (const jerry_char_t *buffer_p, /**< pointer to buffer */ + jerry_size_t buffer_size) /**< buffer size */ +{ + jerry_assert_api_enabled (); + JERRY_ASSERT (lit_is_valid_cesu8_string (buffer_p, buffer_size)); + return ecma_make_string_value (ecma_new_ecma_string_from_utf8 (buffer_p, buffer_size)); + +} /* jerry_string_cesu8 */ + +/** + * Create a string value from the input buffer using the UTF-8 encoding. + * The content of the buffer is assumed to be valid in the UTF-8 encoding, + * it's the callers responsibility to validate the input. + * + * See also: lit_is_valid_utf8_string * * @return created string */ jerry_value_t -jerry_string_sz (const char *str_p) /**< pointer to string */ +jerry_string_utf8 (const jerry_char_t *buffer_p, /**< pointer to buffer */ + jerry_size_t buffer_size) /**< buffer size */ { - const jerry_char_t *data_p = (const jerry_char_t *) str_p; - return jerry_string (data_p, lit_zt_utf8_string_size (data_p), JERRY_ENCODING_CESU8); -} /* jerry_string_sz */ + jerry_assert_api_enabled (); + JERRY_ASSERT (lit_is_valid_utf8_string (buffer_p, buffer_size, true)); + return ecma_make_string_value (ecma_new_ecma_string_from_utf8_converted_to_cesu8 (buffer_p, buffer_size)); +} /* jerry_string_utf8 */ /** * Create a string value from the input buffer using the specified encoding. * The content of the buffer is assumed to be valid in the specified encoding, it's the callers responsibility to * validate the input. * - * See also: jerry_validate_string + * See also: jerry_string_ascii jerry_string_cesu8 jerry_string_utf8 * * @return created string */ @@ -2682,50 +2722,34 @@ jerry_string (const jerry_char_t *buffer_p, /**< pointer to buffer */ jerry_size_t buffer_size, /**< buffer size */ jerry_encoding_t encoding) /**< buffer encoding */ { - jerry_assert_api_enabled (); - ecma_string_t *ecma_str_p = NULL; - JERRY_ASSERT (jerry_validate_string (buffer_p, buffer_size, encoding)); - switch (encoding) { + case JERRY_ENCODING_ASCII: + { + return jerry_string_ascii (buffer_p, buffer_size); + } case JERRY_ENCODING_CESU8: { - ecma_str_p = ecma_new_ecma_string_from_utf8 (buffer_p, buffer_size); - break; + return jerry_string_cesu8 (buffer_p, buffer_size); } case JERRY_ENCODING_UTF8: { - ecma_str_p = ecma_new_ecma_string_from_utf8_converted_to_cesu8 (buffer_p, buffer_size); - break; + return jerry_string_utf8 (buffer_p, buffer_size); } default: { + jerry_assert_api_enabled (); return jerry_throw_sz (JERRY_ERROR_TYPE, ecma_get_error_msg (ECMA_ERR_INVALID_ENCODING)); } } - - return ecma_make_string_value (ecma_str_p); } /* jerry_string */ -/** - * Create external string from input zero-terminated ASCII string. - * - * @return created external string - */ -jerry_value_t -jerry_string_external_sz (const char *str_p, /**< pointer to string */ - void *user_p) /**< user pointer passed to the callback when the string is freed */ -{ - const jerry_char_t *data_p = (const jerry_char_t *) str_p; - return jerry_string_external (data_p, lit_zt_utf8_string_size (data_p), user_p); -} /* jerry_string_external_sz */ - /** * Create external string from a valid CESU-8 encoded string. * The content of the buffer is assumed be encoded correctly, it's the callers responsibility to * validate the input. * - * See also: jerry_validate_string + * See also: lit_is_valid_cesu8_string * * @return created external string */ @@ -2736,7 +2760,7 @@ jerry_string_external (const jerry_char_t *buffer_p, /**< pointer to string */ { jerry_assert_api_enabled (); - JERRY_ASSERT (jerry_validate_string (buffer_p, buffer_size, JERRY_ENCODING_CESU8)); + JERRY_ASSERT (lit_is_valid_cesu8_string (buffer_p, buffer_size)); ecma_string_t *ecma_str_p = ecma_new_ecma_external_string_from_cesu8 (buffer_p, buffer_size, user_p); return ecma_make_string_value (ecma_str_p); } /* jerry_string_external_sz_sz */ @@ -2790,18 +2814,20 @@ jerry_bigint (const uint64_t *digits_p, /**< BigInt digits (lowest digit first) /** * Creates a RegExp object with the given ASCII pattern and flags. * + * Note: + * `pattern_sz` will be freed in this function + * * @return value of the constructed RegExp object. */ jerry_value_t -jerry_regexp_sz (const char *pattern_p, /**< RegExp pattern as zero-terminated ASCII string */ +jerry_regexp_sz (const jerry_value_t pattern_sz, /**< RegExp pattern string that will be free/take */ uint16_t flags) /**< RegExp flags */ { jerry_assert_api_enabled (); - jerry_value_t pattern = jerry_string_sz (pattern_p); - jerry_value_t result = jerry_regexp (pattern, flags); + jerry_value_t result = jerry_regexp (pattern_sz, flags); - jerry_value_free (pattern); + jerry_value_free (pattern_sz); return jerry_return (result); } /* jerry_regexp_sz */ @@ -3135,18 +3161,20 @@ jerry_object_has (const jerry_value_t object, /**< object value */ /** * Checks whether the object or it's prototype objects have the given property. * + * Note: + * `key_sz` will be freed in this function + * * @return raised error - if the operation fail * true/false API value - depend on whether the property exists */ jerry_value_t jerry_object_has_sz (const jerry_value_t object, /**< object value */ - const char *key_p) /**< property key */ + const jerry_value_t key_sz) /**< property key that will be free/take */ { jerry_assert_api_enabled (); - jerry_value_t key_str = jerry_string_sz (key_p); - jerry_value_t result = jerry_object_has (object, key_str); - ecma_free_value (key_str); + jerry_value_t result = jerry_object_has (object, key_sz); + ecma_free_value (key_sz); return result; } /* jerry_object_has */ @@ -3236,18 +3264,20 @@ jerry_object_delete (jerry_value_t object, /**< object value */ /** * Delete a property from an object. * + * Note: + * `key_sz` will be freed in this function + * * @return boolean value - wether the property was deleted successfully * exception - otherwise */ jerry_value_t jerry_object_delete_sz (jerry_value_t object, /**< object value */ - const char *key_p) /**< property key */ + const jerry_value_t key_sz) /**< property name that will be free/take*/ { jerry_assert_api_enabled (); - jerry_value_t key_str = jerry_string_sz (key_p); - jerry_value_t result = jerry_object_delete (object, key_str); - ecma_free_value (key_str); + jerry_value_t result = jerry_object_delete (object, key_sz); + ecma_free_value (key_sz); return result; } /* jerry_object_delete */ @@ -3351,6 +3381,7 @@ jerry_object_get (const jerry_value_t object, /**< object value */ * Get value of a property to the specified object with the given name. * * Note: + * `key_sz` will be freed in this function * returned value must be freed with jerry_value_free, when it is no longer needed. * * @return value of the property - if success @@ -3358,13 +3389,12 @@ jerry_object_get (const jerry_value_t object, /**< object value */ */ jerry_value_t jerry_object_get_sz (const jerry_value_t object, /**< object value */ - const char *key_p) /**< property key */ + const jerry_value_t key_sz) /**< property key that will be free/take */ { jerry_assert_api_enabled (); - jerry_value_t key_str = jerry_string_sz (key_p); - jerry_value_t result = jerry_object_get (object, key_str); - ecma_free_value (key_str); + jerry_value_t result = jerry_object_get (object, key_sz); + ecma_free_value (key_sz); return result; } /* jerry_object_get */ @@ -3529,6 +3559,7 @@ jerry_object_set (jerry_value_t object, /**< object value */ * Set a property to the specified object with the given name. * * Note: + * `key_sz` will be freed in this function * returned value must be freed with jerry_value_free, when it is no longer needed. * * @return true value - if the operation was successful @@ -3536,14 +3567,13 @@ jerry_object_set (jerry_value_t object, /**< object value */ */ jerry_value_t jerry_object_set_sz (jerry_value_t object, /**< object value */ - const char *key_p, /**< property key */ + const jerry_value_t key_sz, /**< property key that will be free/take */ const jerry_value_t value) /**< value to set */ { jerry_assert_api_enabled (); - jerry_value_t key_str = jerry_string_sz (key_p); - jerry_value_t result = jerry_object_set (object, key_str, value); - ecma_free_value (key_str); + jerry_value_t result = jerry_object_set (object, key_sz, value); + ecma_free_value (key_sz); return result; } /* jerry_object_set */ @@ -5045,6 +5075,10 @@ jerry_validate_string (const jerry_char_t *buffer_p, /**< string buffer */ { switch (encoding) { + case JERRY_ENCODING_ASCII: + { + return lit_is_valid_ascii_string (buffer_p, buffer_size); + } case JERRY_ENCODING_CESU8: { return lit_is_valid_cesu8_string (buffer_p, buffer_size); @@ -5082,49 +5116,33 @@ jerry_log_set_level (jerry_log_level_t level) * Log a zero-terminated string message. * * @param str_p: message + * @param str_size: size of message */ static void -jerry_log_string (const char *str_p) +jerry_log_string (const jerry_char_t *str_p, jerry_size_t str_size) { - jerry_port_log (str_p); + jerry_port_log_buffer (str_p, str_size); #if JERRY_DEBUGGER if (jerry_debugger_is_connected ()) { - jerry_debugger_send_string (JERRY_DEBUGGER_OUTPUT_RESULT, - JERRY_DEBUGGER_OUTPUT_LOG, - (const uint8_t *) str_p, - strlen (str_p)); + jerry_debugger_send_string (JERRY_DEBUGGER_OUTPUT_RESULT, JERRY_DEBUGGER_OUTPUT_LOG, str_p, str_size); } #endif /* JERRY_DEBUGGER */ } /* jerry_log_string */ /** - * Log a fixed-size string message. + * Log a zero-terminated number string message. * - * @param str_p: message - * @param size: size - * @param buffer_p: buffer to use + * @param cursor_p: the number string cursor + * @param buffer_p: buffer used to construct the number string */ static void -jerry_log_string_fixed (const char *str_p, size_t size, char *buffer_p) +jerry_log_cursor (const jerry_char_t *cursor_p, jerry_char_t *buffer_p) { - const size_t batch_size = JERRY_LOG_BUFFER_SIZE - 1; - - while (size > batch_size) - { - memcpy (buffer_p, str_p, batch_size); - buffer_p[batch_size] = '\0'; - jerry_log_string (buffer_p); - - str_p += batch_size; - size -= batch_size; - } - - memcpy (buffer_p, str_p, size); - buffer_p[size] = '\0'; - jerry_log_string (buffer_p); -} /* jerry_log_string_fixed */ + jerry_char_t *tail_p = buffer_p + JERRY_LOG_BUFFER_SIZE - 1; + jerry_log_string (cursor_p, (jerry_size_t) (tail_p - cursor_p)); +} /* jerry_log_cursor */ /** * Format an unsigned number. @@ -5137,11 +5155,13 @@ jerry_log_string_fixed (const char *str_p, size_t size, char *buffer_p) * * @return formatted number as string */ -static char * -jerry_format_unsigned (unsigned int num, uint8_t width, char padding, uint8_t radix, char *buffer_p) +static jerry_char_t * +jerry_format_unsigned (unsigned int num, uint8_t width, jerry_char_t padding, uint8_t radix, jerry_char_t *buffer_p) { - static const char digits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; - char *cursor_p = buffer_p + JERRY_LOG_BUFFER_SIZE; + static const jerry_char_t digits[] = { + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' + }; + jerry_char_t *cursor_p = buffer_p + JERRY_LOG_BUFFER_SIZE; *(--cursor_p) = '\0'; uint8_t count = 0; @@ -5171,8 +5191,8 @@ jerry_format_unsigned (unsigned int num, uint8_t width, char padding, uint8_t ra * * @return formatted number as string */ -static char * -jerry_format_int (int num, uint8_t width, char padding, char *buffer_p) +static jerry_char_t * +jerry_format_int (int num, uint8_t width, jerry_char_t padding, jerry_char_t *buffer_p) { if (num >= 0) { @@ -5183,15 +5203,15 @@ jerry_format_int (int num, uint8_t width, char padding, char *buffer_p) if (padding == '0' && width > 0) { - char *cursor_p = jerry_format_unsigned ((unsigned) num, --width, padding, 10, buffer_p); + jerry_char_t *cursor_p = jerry_format_unsigned ((unsigned) num, --width, padding, 10, buffer_p); *(--cursor_p) = '-'; return cursor_p; } - char *cursor_p = jerry_format_unsigned ((unsigned) num, 0, ' ', 10, buffer_p); + jerry_char_t *cursor_p = jerry_format_unsigned ((unsigned) num, 0, ' ', 10, buffer_p); *(--cursor_p) = '-'; - char *indent_p = buffer_p + JERRY_LOG_BUFFER_SIZE - width - 1; + jerry_char_t *indent_p = buffer_p + JERRY_LOG_BUFFER_SIZE - width - 1; while (cursor_p > indent_p) { @@ -5225,9 +5245,9 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...) } va_list vl; - char buffer_p[JERRY_LOG_BUFFER_SIZE]; - uint32_t buffer_index = 0; - const char *cursor_p = format_p; + jerry_char_t buffer_p[JERRY_LOG_BUFFER_SIZE]; + jerry_size_t buffer_index = 0; + const jerry_char_t *cursor_p = (const jerry_char_t *) format_p; va_start (vl, format_p); while (*cursor_p != '\0') @@ -5235,7 +5255,7 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...) if (*cursor_p == '%' || buffer_index > JERRY_LOG_BUFFER_SIZE - 2) { buffer_p[buffer_index] = '\0'; - jerry_log_string (buffer_p); + jerry_log_string (buffer_p, buffer_index); buffer_index = 0; } @@ -5247,8 +5267,8 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...) ++cursor_p; uint8_t width = 0; - size_t precision = 0; - char padding = ' '; + jerry_size_t precision = 0; + jerry_char_t padding = ' '; if (*cursor_p == '0') { @@ -5268,7 +5288,7 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...) } else if (*cursor_p == '.' && *(cursor_p + 1) == '*') { - precision = (size_t) va_arg (vl, int); + precision = (jerry_size_t) va_arg (vl, int); cursor_p += 2; } @@ -5283,36 +5303,59 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...) { case 's': { - char *str_p = va_arg (vl, char *); + jerry_char_t *str_p = va_arg (vl, jerry_char_t *); if (precision == 0) { - jerry_log_string (str_p); + jerry_log_string (str_p, (jerry_size_t) strlen ((const char *) str_p)); break; } - jerry_log_string_fixed (str_p, precision, buffer_p); + jerry_log_string (str_p, precision); + break; + } + case 'S': + { + jerry_value_t str = va_arg (vl, jerry_value_t); + ecma_string_t *string2_p = ecma_op_to_string (str); + lit_utf8_size_t cesu8_string2_size; + lit_utf8_size_t cesu8_string2_length; + lit_utf8_byte_t uint32_to_string_buffer[ECMA_MAX_CHARS_IN_STRINGIFIED_UINT32]; + uint8_t flags = ECMA_STRING_FLAG_EMPTY; + + const lit_utf8_byte_t *cesu8_string2_p = ecma_string_get_chars (string2_p, + &cesu8_string2_size, + &cesu8_string2_length, + uint32_to_string_buffer, + &flags); + jerry_log_string (cesu8_string2_p, cesu8_string2_size); + if (flags & ECMA_STRING_FLAG_MUST_BE_FREED) + { + jmem_heap_free_block ((void *) cesu8_string2_p, cesu8_string2_size); + } + + ecma_deref_ecma_string (string2_p); break; } case 'c': { /* Arguments of types narrower than int are promoted to int for variadic functions */ - buffer_p[buffer_index++] = (char) va_arg (vl, int); + buffer_p[buffer_index++] = (jerry_char_t) va_arg (vl, int); break; } case 'd': { - jerry_log_string (jerry_format_int (va_arg (vl, int), width, padding, buffer_p)); + jerry_log_cursor (jerry_format_int (va_arg (vl, int), width, padding, buffer_p), buffer_p); break; } case 'u': { - jerry_log_string (jerry_format_unsigned (va_arg (vl, unsigned int), width, padding, 10, buffer_p)); + jerry_log_cursor (jerry_format_unsigned (va_arg (vl, unsigned int), width, padding, 10, buffer_p), buffer_p); break; } case 'x': { - jerry_log_string (jerry_format_unsigned (va_arg (vl, unsigned int), width, padding, 16, buffer_p)); + jerry_log_cursor (jerry_format_unsigned (va_arg (vl, unsigned int), width, padding, 16, buffer_p), buffer_p); break; } default: @@ -5326,7 +5369,7 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...) if (buffer_index > 0) { buffer_p[buffer_index] = '\0'; - jerry_log_string (buffer_p); + jerry_log_string (buffer_p, buffer_index); } va_end (vl); diff --git a/jerry-core/debugger/debugger.c b/jerry-core/debugger/debugger.c index 2d77f88328..5715f8cf19 100644 --- a/jerry-core/debugger/debugger.c +++ b/jerry-core/debugger/debugger.c @@ -596,7 +596,7 @@ jerry_debugger_send_eval (const lit_utf8_byte_t *eval_string_p, /**< evaluated s ecma_free_value (result); const lit_utf8_byte_t *string_p = lit_get_magic_string_utf8 (id); - jerry_debugger_send_string (JERRY_DEBUGGER_EVAL_RESULT, type, string_p, strlen ((const char *) string_p)); + jerry_debugger_send_string (JERRY_DEBUGGER_EVAL_RESULT, type, string_p, lit_get_magic_string_size (id)); return false; } } @@ -1482,15 +1482,17 @@ jerry_debugger_exception_object_to_string (ecma_value_t exception_obj_value) /** ecma_property_value_t *prop_value_p = ECMA_PROPERTY_VALUE_PTR (property_p); - if (!ecma_is_value_string (prop_value_p->value)) + if (ecma_is_value_string (prop_value_p->value)) { - return ecma_stringbuilder_finalize (&builder); + ecma_string_t *string_p = ecma_get_string_from_value (prop_value_p->value); + if (!ecma_string_is_empty (string_p)) + { + ecma_stringbuilder_append_byte (&builder, LIT_CHAR_COLON); + ecma_stringbuilder_append_byte (&builder, LIT_CHAR_SP); + ecma_stringbuilder_append (&builder, string_p); + } } - ecma_stringbuilder_append_byte (&builder, LIT_CHAR_COLON); - ecma_stringbuilder_append_byte (&builder, LIT_CHAR_SP); - ecma_stringbuilder_append (&builder, ecma_get_string_from_value (prop_value_p->value)); - return ecma_stringbuilder_finalize (&builder); } /* jerry_debugger_exception_object_to_string */ diff --git a/jerry-core/ecma/base/ecma-errors.c b/jerry-core/ecma/base/ecma-errors.c index 7592fe829b..5212bc5257 100644 --- a/jerry-core/ecma/base/ecma-errors.c +++ b/jerry-core/ecma/base/ecma-errors.c @@ -15,6 +15,9 @@ #include "ecma-errors.h" +#include "ecma-globals.h" +#include "ecma-helpers.h" + #if JERRY_ERROR_MESSAGES /** * Struct to store ecma error message with its size. @@ -37,35 +40,21 @@ static ecma_error_message_t ecma_error_messages[] JERRY_ATTR_CONST_DATA = { #endif /* JERRY_ERROR_MESSAGES */ /** - * Get specified ecma error as zero-terminated string + * Get error string of specified ecma error * - * @return pointer to zero-terminated ecma error + * @return created string for ecma error */ -const char * +ecma_value_t ecma_get_error_msg (ecma_error_msg_t id) /**< ecma error id */ { JERRY_ASSERT (id != ECMA_IS_VALID_CONSTRUCTOR); - + ecma_string_t *ecma_str_p; #if JERRY_ERROR_MESSAGES - return ecma_error_messages[id].text; + ecma_error_message_t *msg = ecma_error_messages + id; + JERRY_ASSERT (lit_is_valid_ascii_string ((const lit_utf8_byte_t *) msg->text, msg->size)); + ecma_str_p = ecma_new_ecma_string_from_ascii ((const lit_utf8_byte_t *) msg->text, msg->size); #else /* !JERRY_ERROR_MESSAGES */ - return NULL; + ecma_str_p = ecma_get_magic_string (LIT_MAGIC_STRING__EMPTY); #endif /* JERRY_ERROR_MESSAGES */ + return ecma_make_string_value (ecma_str_p); } /* ecma_get_error_msg */ - -/** - * Get size of specified ecma error - * - * @return size in bytes - */ -lit_utf8_size_t -ecma_get_error_size (ecma_error_msg_t id) /**< ecma error id */ -{ - JERRY_ASSERT (id != ECMA_IS_VALID_CONSTRUCTOR); - -#if JERRY_ERROR_MESSAGES - return ecma_error_messages[id].size; -#else /* !JERRY_ERROR_MESSAGES */ - return 0; -#endif /* JERRY_ERROR_MESSAGES */ -} /* ecma_get_error_size */ diff --git a/jerry-core/ecma/base/ecma-errors.h b/jerry-core/ecma/base/ecma-errors.h index 72947e6e69..2880992cbe 100644 --- a/jerry-core/ecma/base/ecma-errors.h +++ b/jerry-core/ecma/base/ecma-errors.h @@ -33,7 +33,4 @@ typedef enum ECMA_IS_VALID_CONSTRUCTOR /* used as return value when checking constructor */ } ecma_error_msg_t; -const char* ecma_get_error_msg (ecma_error_msg_t id); -lit_utf8_size_t ecma_get_error_size (ecma_error_msg_t id); - #endif /* !ECMA_ERRORS_H */ diff --git a/jerry-core/ecma/base/ecma-globals.h b/jerry-core/ecma/base/ecma-globals.h index 75c70e196b..4d89719dd9 100644 --- a/jerry-core/ecma/base/ecma-globals.h +++ b/jerry-core/ecma/base/ecma-globals.h @@ -2264,6 +2264,8 @@ typedef enum */ #define ECMA_PRIVATE_PROPERTY_KIND(prop) ((prop) & ((ECMA_PRIVATE_PROPERTY_STATIC_FLAG - 1))) +ecma_value_t ecma_get_error_msg (ecma_error_msg_t id); + /** * @} * @} diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-async-from-sync-iterator-prototype.c b/jerry-core/ecma/builtin-objects/ecma-builtin-async-from-sync-iterator-prototype.c index 43308c86a6..161aaaca35 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-async-from-sync-iterator-prototype.c +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-async-from-sync-iterator-prototype.c @@ -252,27 +252,14 @@ ecma_builtin_async_from_sync_iterator_prototype_do (ecma_async_from_sync_iterato { ecma_free_value (call_result); -#if JERRY_ERROR_MESSAGES - const lit_utf8_byte_t *msg_p = (lit_utf8_byte_t *) ecma_get_error_msg (ECMA_ERR_ARGUMENT_IS_NOT_AN_OBJECT); - lit_utf8_size_t msg_size = ecma_get_error_size (ECMA_ERR_ARGUMENT_IS_NOT_AN_OBJECT); - ecma_string_t *error_msg_p = ecma_new_ecma_string_from_ascii (msg_p, msg_size); -#else /* !JERRY_ERROR_MESSAGES */ - ecma_string_t *error_msg_p = ecma_get_magic_string (LIT_MAGIC_STRING__EMPTY); -#endif /* JERRY_ERROR_MESSAGES */ - - ecma_object_t *type_error_obj_p = ecma_new_standard_error (JERRY_ERROR_TYPE, error_msg_p); - -#if JERRY_ERROR_MESSAGES - ecma_deref_ecma_string (error_msg_p); -#endif /* JERRY_ERROR_MESSAGES */ - - ecma_value_t type_error = ecma_make_object_value (type_error_obj_p); + ecma_value_t type_error = + ecma_error_value_taked (JERRY_ERROR_TYPE, ecma_get_error_msg (ECMA_ERR_ARGUMENT_IS_NOT_AN_OBJECT)); /* 10.a. */ ecma_value_t reject = ecma_op_function_call (ecma_get_object_from_value (capability_p->reject), ECMA_VALUE_UNDEFINED, &type_error, 1); JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (reject)); - ecma_deref_object (type_error_obj_p); + ecma_free_value (type_error); ecma_free_value (reject); /* 10.b. */ diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-async-generator-prototype.c b/jerry-core/ecma/builtin-objects/ecma-builtin-async-generator-prototype.c index 474bd87740..0ec06071e5 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-async-generator-prototype.c +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-async-generator-prototype.c @@ -109,16 +109,11 @@ ecma_builtin_async_generator_prototype_dispatch_routine (uint8_t builtin_routine if (executable_object_p == NULL) { - const char *msg_p = ecma_get_error_msg (ECMA_ERR_ARGUMENT_THIS_NOT_ASYNC_GENERATOR); - lit_utf8_size_t msg_size = ecma_get_error_size (ECMA_ERR_ARGUMENT_THIS_NOT_ASYNC_GENERATOR); - ecma_string_t *error_msg_p = ecma_new_ecma_string_from_ascii ((const lit_utf8_byte_t *) msg_p, msg_size); - - ecma_object_t *type_error_obj_p = ecma_new_standard_error (JERRY_ERROR_TYPE, error_msg_p); - ecma_deref_ecma_string (error_msg_p); - + ecma_value_t error_value = + ecma_error_value_taked (JERRY_ERROR_TYPE, ecma_get_error_msg (ECMA_ERR_ARGUMENT_THIS_NOT_ASYNC_GENERATOR)); ecma_value_t promise = ecma_op_create_promise_object (ECMA_VALUE_EMPTY, ECMA_VALUE_UNDEFINED, NULL); - ecma_reject_promise (promise, ecma_make_object_value (type_error_obj_p)); - ecma_deref_object (type_error_obj_p); + ecma_reject_promise (promise, error_value); + ecma_free_value (error_value); return promise; } diff --git a/jerry-core/ecma/operations/ecma-exceptions.c b/jerry-core/ecma/operations/ecma-exceptions.c index 675aaf92f9..5ea1417d21 100644 --- a/jerry-core/ecma/operations/ecma-exceptions.c +++ b/jerry-core/ecma/operations/ecma-exceptions.c @@ -168,6 +168,42 @@ ecma_new_standard_error (jerry_error_t error_type, /**< native error type */ return error_object_p; } /* ecma_new_standard_error */ +/** + * Create an Error object with the provided string value as the error message. + * If the message value is not a string, the created error will not have a message property. + * + * @return Error object + */ +ecma_value_t +ecma_error_value (jerry_error_t error_type, /**< type of error */ + ecma_value_t message) /**< message of the error */ +{ + ecma_string_t *message_p = NULL; + if (ecma_is_value_string (message)) + { + message_p = ecma_get_string_from_value (message); + } + + ecma_object_t *error_object_p = ecma_new_standard_error ((jerry_error_t) error_type, message_p); + + return ecma_make_object_value (error_object_p); +} /* ecma_error_value */ + +/** + * Create an Error object with the provided string value as the error message. + * If the message value is not a string, the created error will not have a message property. + * The message will be freed in this function. + * @return Error object + */ +ecma_value_t +ecma_error_value_taked (jerry_error_t error_type, /**< type of error */ + ecma_value_t message) /**< message of the error that will be free/take */ +{ + ecma_value_t error = ecma_error_value (error_type, message); + ecma_free_value (message); + return error; +} /* ecma_error_value_taked */ + /** * aggregate-error object constructor. * @@ -298,21 +334,7 @@ ecma_value_t ecma_raise_standard_error (jerry_error_t error_type, /**< error type */ ecma_error_msg_t msg) /**< error message */ { - ecma_object_t *error_obj_p; - const lit_utf8_byte_t *str_p = (lit_utf8_byte_t *) ecma_get_error_msg (msg); - - if (msg != ECMA_ERR_EMPTY) - { - ecma_string_t *error_msg_p = ecma_new_ecma_external_string_from_cesu8 (str_p, ecma_get_error_size (msg), NULL); - error_obj_p = ecma_new_standard_error (error_type, error_msg_p); - ecma_deref_ecma_string (error_msg_p); - } - else - { - error_obj_p = ecma_new_standard_error (error_type, NULL); - } - - jcontext_raise_exception (ecma_make_object_value (error_obj_p)); + jcontext_raise_exception (ecma_error_value_taked (error_type, ecma_get_error_msg (msg))); return ECMA_VALUE_ERROR; } /* ecma_raise_standard_error */ diff --git a/jerry-core/ecma/operations/ecma-exceptions.h b/jerry-core/ecma/operations/ecma-exceptions.h index c367a6bf5e..6935f1d86e 100644 --- a/jerry-core/ecma/operations/ecma-exceptions.h +++ b/jerry-core/ecma/operations/ecma-exceptions.h @@ -29,6 +29,8 @@ jerry_error_t ecma_get_error_type (ecma_object_t *error_object_p); ecma_object_t *ecma_new_standard_error (jerry_error_t error_type, ecma_string_t *message_string_p); +ecma_value_t ecma_error_value (jerry_error_t error_type, ecma_value_t message); +ecma_value_t ecma_error_value_taked (jerry_error_t error_type, ecma_value_t message); #if JERRY_ERROR_MESSAGES ecma_value_t ecma_raise_standard_error_with_format (jerry_error_t error_type, const char *msg_p, ...); #endif /* JERRY_ERROR_MESSAGES */ diff --git a/jerry-core/include/jerryscript-compiler.h b/jerry-core/include/jerryscript-compiler.h index 4732f010f7..41117d237e 100644 --- a/jerry-core/include/jerryscript-compiler.h +++ b/jerry-core/include/jerryscript-compiler.h @@ -185,6 +185,13 @@ void *__cdecl _alloca (size_t _Size); #define JERRY_VLA(type, name, size) type name[size] #endif /* !JERRY_VLA */ +/** + * Helper to make sure unused parameters, variables, or expressions trigger no compiler warning. + */ +#ifndef JERRY_UNUSED +#define JERRY_UNUSED(x) ((void) (x)) +#endif /* !JERRY_UNUSED */ + /** * @} */ diff --git a/jerry-core/include/jerryscript-core.h b/jerry-core/include/jerryscript-core.h index b034b10919..a03304d235 100644 --- a/jerry-core/include/jerryscript-core.h +++ b/jerry-core/include/jerryscript-core.h @@ -174,6 +174,7 @@ bool jerry_frame_is_strict (jerry_frame_t *frame_p); /* Reference management */ jerry_value_t JERRY_ATTR_WARN_UNUSED_RESULT jerry_value_copy (const jerry_value_t value); void jerry_value_free (jerry_value_t value); +void jerry_value_list_free (const jerry_value_t *value_list, jerry_size_t value_list_count); /** * @defgroup jerry-api-value-checks Type inspection @@ -255,8 +256,8 @@ jerry_value_t jerry_binary_op (jerry_binary_op_t operation, const jerry_value_t * @defgroup jerry-api-exception-ctor Constructors * @{ */ -jerry_value_t jerry_throw (jerry_error_t type, const jerry_value_t message); -jerry_value_t jerry_throw_sz (jerry_error_t type, const char *message_p); +jerry_value_t jerry_throw (jerry_error_t error_type, const jerry_value_t message); +jerry_value_t jerry_throw_sz (jerry_error_t error_type, const jerry_value_t message); jerry_value_t jerry_throw_value (jerry_value_t value, bool take_ownership); jerry_value_t jerry_throw_abort (jerry_value_t value, bool take_ownership); /** @@ -428,9 +429,10 @@ void jerry_bigint_to_digits (const jerry_value_t value, uint64_t *digits_p, uint * @{ */ jerry_value_t jerry_string (const jerry_char_t *buffer_p, jerry_size_t buffer_size, jerry_encoding_t encoding); -jerry_value_t jerry_string_sz (const char *str_p); +jerry_value_t jerry_string_ascii (const jerry_char_t *buffer_p, jerry_size_t buffer_size); +jerry_value_t jerry_string_cesu8 (const jerry_char_t *buffer_p, jerry_size_t buffer_size); +jerry_value_t jerry_string_utf8 (const jerry_char_t *buffer_p, jerry_size_t buffer_size); jerry_value_t jerry_string_external (const jerry_char_t *buffer_p, jerry_size_t buffer_size, void *user_p); -jerry_value_t jerry_string_external_sz (const char *str_p, void *user_p); /** * jerry-api-string-cotr @} */ @@ -550,7 +552,7 @@ bool jerry_object_foreach (const jerry_value_t object, jerry_object_property_for * @{ */ jerry_value_t jerry_object_set (jerry_value_t object, const jerry_value_t key, const jerry_value_t value); -jerry_value_t jerry_object_set_sz (jerry_value_t object, const char *key_p, const jerry_value_t value); +jerry_value_t jerry_object_set_sz (jerry_value_t object, const jerry_value_t key_sz, const jerry_value_t value); jerry_value_t jerry_object_set_index (jerry_value_t object, uint32_t index, const jerry_value_t value); jerry_value_t jerry_object_define_own_prop (jerry_value_t object, const jerry_value_t key, @@ -568,7 +570,7 @@ void jerry_object_set_native_ptr (jerry_value_t object, * @{ */ jerry_value_t jerry_object_has (const jerry_value_t object, const jerry_value_t key); -jerry_value_t jerry_object_has_sz (const jerry_value_t object, const char *key_p); +jerry_value_t jerry_object_has_sz (const jerry_value_t object, const jerry_value_t key_sz); jerry_value_t jerry_object_has_own (const jerry_value_t object, const jerry_value_t key); bool jerry_object_has_internal (const jerry_value_t object, const jerry_value_t key); bool jerry_object_has_native_ptr (const jerry_value_t object, const jerry_object_native_info_t *native_info_p); @@ -581,7 +583,7 @@ bool jerry_object_has_native_ptr (const jerry_value_t object, const jerry_object * @{ */ jerry_value_t jerry_object_get (const jerry_value_t object, const jerry_value_t key); -jerry_value_t jerry_object_get_sz (const jerry_value_t object, const char *key_p); +jerry_value_t jerry_object_get_sz (const jerry_value_t object, const jerry_value_t key_sz); jerry_value_t jerry_object_get_index (const jerry_value_t object, uint32_t index); jerry_value_t jerry_object_get_own_prop (const jerry_value_t object, const jerry_value_t key, @@ -602,7 +604,7 @@ jerry_value_t jerry_object_find_own (const jerry_value_t object, * @{ */ jerry_value_t jerry_object_delete (jerry_value_t object, const jerry_value_t key); -jerry_value_t jerry_object_delete_sz (const jerry_value_t object, const char *key_p); +jerry_value_t jerry_object_delete_sz (const jerry_value_t object, const jerry_value_t key_sz); jerry_value_t jerry_object_delete_index (jerry_value_t object, uint32_t index); bool jerry_object_delete_internal (jerry_value_t object, const jerry_value_t key); bool jerry_object_delete_native_ptr (jerry_value_t object, const jerry_object_native_info_t *native_info_p); @@ -1015,7 +1017,7 @@ jerry_value_t jerry_container_op (jerry_container_op_t operation, * @{ */ jerry_value_t jerry_regexp (const jerry_value_t pattern, uint16_t flags); -jerry_value_t jerry_regexp_sz (const char *pattern_p, uint16_t flags); +jerry_value_t jerry_regexp_sz (const jerry_value_t pattern_sz, uint16_t flags); /** * jerry-api-regexp-ctor @} */ @@ -1033,8 +1035,8 @@ jerry_value_t jerry_regexp_sz (const char *pattern_p, uint16_t flags); * @defgroup jerry-api-error-ctor Constructors * @{ */ -jerry_value_t jerry_error (jerry_error_t type, const jerry_value_t message); -jerry_value_t jerry_error_sz (jerry_error_t type, const char *message_p); +jerry_value_t jerry_error (jerry_error_t error_type, const jerry_value_t message); +jerry_value_t jerry_error_sz (jerry_error_t error_type, const jerry_value_t message); /** * jerry-api-error-ctor @} */ @@ -1204,6 +1206,31 @@ jerry_value_t jerry_realm_set_this (jerry_value_t realm, jerry_value_t this_valu JERRY_C_API_END +/** + * Create string value from the zero-terminated ASCII encoded literal string. + * The content of the buffer is assumed be encoded correctly, it's the callers responsibility to + * validate the input. + * + * @return created string + */ +#define jerry_sz_ascii(str) jerry_string_ascii (JERRY_ZSTR_ARG (str)) +/** + * Create string value from the zero-terminated CESU-8 encoded literal string. + * The content of the buffer is assumed be encoded correctly, it's the callers responsibility to + * validate the input. + * + * @return created string + */ +#define jerry_sz_cesu8(str) jerry_string_cesu8 (JERRY_ZSTR_ARG (str)) +/** + * Create string value from the zero-terminated UTF-8 encoded literal string. + * The content of the buffer is assumed be encoded correctly, it's the callers responsibility to + * validate the input. + * + * @return created string + */ +#define jerry_sz_utf8(str) jerry_string_utf8 (JERRY_ZSTR_ARG (str)) + #endif /* !JERRYSCRIPT_CORE_H */ /* vim: set fdm=marker fmr=@{,@}: */ diff --git a/jerry-core/include/jerryscript-port.h b/jerry-core/include/jerryscript-port.h index 0d8b34e1d4..cb8a19ce28 100644 --- a/jerry-core/include/jerryscript-port.h +++ b/jerry-core/include/jerryscript-port.h @@ -139,8 +139,11 @@ void jerry_port_context_free (void); * * The implementation can decide whether error and debug messages are logged to * the console, or saved to a database or to a file. + * + * @param buffer_p: input buffer that is a zero-terminated UTF-8 string + * @param buffer_size: data size */ -void jerry_port_log (const char *message_p); +void jerry_port_log_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size); /** * Print a buffer to standard output diff --git a/jerry-core/include/jerryscript-types.h b/jerry-core/include/jerryscript-types.h index f0766b91ca..27c4e94916 100644 --- a/jerry-core/include/jerryscript-types.h +++ b/jerry-core/include/jerryscript-types.h @@ -153,6 +153,15 @@ typedef uint32_t jerry_length_t; */ typedef uint32_t jerry_value_t; +/** + * Description of a JerryScript string for arguments passing + */ +typedef struct +{ + const jerry_char_t *ptr; /**< pointer to the string */ + jerry_size_t size; /**< size of thestring */ +} jerry_string_t; + /** * Option bits for jerry_parse_options_t. */ @@ -246,6 +255,7 @@ typedef enum */ typedef enum { + JERRY_ENCODING_ASCII, /**< ascii encoding */ JERRY_ENCODING_CESU8, /**< cesu-8 encoding */ JERRY_ENCODING_UTF8, /**< utf-8 encoding */ } jerry_encoding_t; @@ -856,10 +866,16 @@ typedef void (*jerry_arraybuffer_free_cb_t) (jerry_arraybuffer_type_t buffer_typ void *arraybuffer_user_p, void *user_p); +/** + * Helper to to ensure str is a string literal + */ +#define JERRY_ZSTR_ENSURE_STRING_LITERAL(str) str "" + /** * Helper to expand string literal to [string pointer, string size] argument pair. */ -#define JERRY_ZSTR_ARG(str) ((const jerry_char_t *) (str)), ((jerry_size_t) (sizeof (str) - 1)) +#define JERRY_ZSTR_ARG(str) \ + ((const jerry_char_t *) (JERRY_ZSTR_ENSURE_STRING_LITERAL (str))), ((jerry_size_t) (sizeof (str) - 1)) /** * @} diff --git a/jerry-core/jrt/jrt.h b/jerry-core/jrt/jrt.h index 2c1f308d96..9c18653f81 100644 --- a/jerry-core/jrt/jrt.h +++ b/jerry-core/jrt/jrt.h @@ -30,11 +30,6 @@ */ #define JERRY_BITSINBYTE 8 -/* - * Make sure unused parameters, variables, or expressions trigger no compiler warning. - */ -#define JERRY_UNUSED(x) ((void) (x)) - #define JERRY_UNUSED_1(_1) JERRY_UNUSED (_1) #define JERRY_UNUSED_2(_1, _2) JERRY_UNUSED (_1), JERRY_UNUSED_1 (_2) #define JERRY_UNUSED_3(_1, _2, _3) JERRY_UNUSED (_1), JERRY_UNUSED_2 (_2, _3) diff --git a/jerry-core/lit/lit-strings.c b/jerry-core/lit/lit-strings.c index edc75f8a2a..b25a98fe39 100644 --- a/jerry-core/lit/lit-strings.c +++ b/jerry-core/lit/lit-strings.c @@ -26,6 +26,35 @@ #define LIT_UTF8_2_BYTE_MAX 0x8f /**< utf8 two byte max */ #define LIT_UTF8_VALID_TWO_BYTE_START 0xc2 /**< utf8 two byte start */ +/** + * Validate ascii string + * + * + * @return true if ascii string is well-formed + * false otherwise + */ +bool +lit_is_valid_ascii_string (const lit_utf8_byte_t *ascii_buf_p, /**< ascii string */ + lit_utf8_size_t buf_size) /**< string size */ +{ + const unsigned char *end = buf_size + ascii_buf_p; + + const unsigned char *idx = (const unsigned char *) ascii_buf_p; + + while (idx < end) + { + const uint8_t first_byte = *idx++; + + if (first_byte < LIT_UTF8_EXTRA_BYTE_MARKER) + { + continue; + } + return false; + } + + return true; +} /* lit_is_valid_ascii_string */ + /** * Validate utf-8 string * @@ -251,22 +280,6 @@ lit_utf16_encode_code_point (lit_code_point_t cp, /**< the code point we encode return 2; } /* lit_utf16_encode_code_point */ -/** - * Calculate size of a zero-terminated utf-8 string - * - * NOTE: - * - string cannot be NULL - * - string should not contain zero characters in the middle - * - * @return size of a string - */ -lit_utf8_size_t -lit_zt_utf8_string_size (const lit_utf8_byte_t *utf8_str_p) /**< zero-terminated utf-8 string */ -{ - JERRY_ASSERT (utf8_str_p != NULL); - return (lit_utf8_size_t) strlen ((const char *) utf8_str_p); -} /* lit_zt_utf8_string_size */ - /** * Calculate length of a cesu-8 encoded string * diff --git a/jerry-core/lit/lit-strings.h b/jerry-core/lit/lit-strings.h index d00480e641..f868d130b5 100644 --- a/jerry-core/lit/lit-strings.h +++ b/jerry-core/lit/lit-strings.h @@ -84,6 +84,7 @@ #define LIT_UTF8_FIRST_BYTE_MAX (0xF8) /* validation */ +bool lit_is_valid_ascii_string (const lit_utf8_byte_t *ascii_buf_p, lit_utf8_size_t buf_size); bool lit_is_valid_utf8_string (const lit_utf8_byte_t *utf8_buf_p, lit_utf8_size_t buf_size, bool strict); bool lit_is_valid_cesu8_string (const lit_utf8_byte_t *cesu8_buf_p, lit_utf8_size_t buf_size); @@ -92,7 +93,6 @@ bool lit_is_code_point_utf16_low_surrogate (lit_code_point_t code_point); bool lit_is_code_point_utf16_high_surrogate (lit_code_point_t code_point); /* size */ -lit_utf8_size_t lit_zt_utf8_string_size (const lit_utf8_byte_t *utf8_str_p); lit_utf8_size_t lit_get_utf8_size_of_cesu8_string (const lit_utf8_byte_t *cesu8_buf_p, lit_utf8_size_t cesu8_buf_size); /* length */ diff --git a/jerry-ext/arg/arg-transform-functions.c b/jerry-ext/arg/arg-transform-functions.c index 264c2d29dc..ff6e8e0e62 100644 --- a/jerry-ext/arg/arg-transform-functions.c +++ b/jerry-ext/arg/arg-transform-functions.c @@ -56,7 +56,7 @@ jerryx_arg_transform_number_strict_common (jerryx_arg_js_iterator_t *js_arg_iter if (!jerry_value_is_number (js_arg)) { - return jerry_throw_sz (JERRY_ERROR_TYPE, "It is not a number."); + return jerry_throw_sz (JERRY_ERROR_TYPE, jerry_sz_ascii ("It is not a number.")); } *number_p = jerry_value_as_number (js_arg); @@ -83,7 +83,7 @@ jerryx_arg_transform_number_common (jerryx_arg_js_iterator_t *js_arg_iter_p, /** { jerry_value_free (to_number); - return jerry_throw_sz (JERRY_ERROR_TYPE, "It can not be converted to a number."); + return jerry_throw_sz (JERRY_ERROR_TYPE, jerry_sz_ascii ("It can not be converted to a number.")); } *number_p = jerry_value_as_number (to_number); @@ -133,14 +133,14 @@ jerryx_arg_helper_process_double (double *d, /**< [in, out] the number to be pro { if (*d != *d) /* isnan (*d) triggers conversion warning on clang<9 */ { - return jerry_throw_sz (JERRY_ERROR_TYPE, "The number is NaN."); + return jerry_throw_sz (JERRY_ERROR_TYPE, jerry_sz_ascii ("The number is NaN.")); } if (option.clamp == JERRYX_ARG_NO_CLAMP) { if (*d > max || *d < min) { - return jerry_throw_sz (JERRY_ERROR_TYPE, "The number is out of range."); + return jerry_throw_sz (JERRY_ERROR_TYPE, jerry_sz_ascii ("The number is out of range.")); } } else @@ -220,7 +220,7 @@ jerryx_arg_transform_boolean_strict (jerryx_arg_js_iterator_t *js_arg_iter_p, /* if (!jerry_value_is_boolean (js_arg)) { - return jerry_throw_sz (JERRY_ERROR_TYPE, "It is not a boolean."); + return jerry_throw_sz (JERRY_ERROR_TYPE, jerry_sz_ascii ("It is not a boolean.")); } bool *dest = c_arg_p->dest; @@ -268,7 +268,7 @@ jerryx_arg_string_to_buffer_common_routine (jerry_value_t js_arg, /**< JS arg */ if (size > target_buf_size - 1) { - return jerry_throw_sz (JERRY_ERROR_TYPE, "Buffer size is not large enough."); + return jerry_throw_sz (JERRY_ERROR_TYPE, jerry_sz_ascii ("Buffer size is not large enough.")); } jerry_string_to_buffer (js_arg, encoding, target_p, target_buf_size); @@ -292,7 +292,7 @@ jerryx_arg_transform_string_strict_common (jerryx_arg_js_iterator_t *js_arg_iter if (!jerry_value_is_string (js_arg)) { - return jerry_throw_sz (JERRY_ERROR_TYPE, "It is not a string."); + return jerry_throw_sz (JERRY_ERROR_TYPE, jerry_sz_ascii ("It is not a string.")); } return jerryx_arg_string_to_buffer_common_routine (js_arg, c_arg_p, encoding); @@ -317,7 +317,7 @@ jerryx_arg_transform_string_common (jerryx_arg_js_iterator_t *js_arg_iter_p, /** { jerry_value_free (to_string); - return jerry_throw_sz (JERRY_ERROR_TYPE, "It can not be converted to a string."); + return jerry_throw_sz (JERRY_ERROR_TYPE, jerry_sz_ascii ("It can not be converted to a string.")); } jerry_value_t ret = jerryx_arg_string_to_buffer_common_routine (to_string, c_arg_p, encoding); @@ -404,7 +404,7 @@ jerryx_arg_transform_function (jerryx_arg_js_iterator_t *js_arg_iter_p, /**< ava if (!jerry_value_is_function (js_arg)) { - return jerry_throw_sz (JERRY_ERROR_TYPE, "It is not a function."); + return jerry_throw_sz (JERRY_ERROR_TYPE, jerry_sz_ascii ("It is not a function.")); } jerry_value_t *func_p = c_arg_p->dest; @@ -428,7 +428,7 @@ jerryx_arg_transform_native_pointer (jerryx_arg_js_iterator_t *js_arg_iter_p, /* if (!jerry_value_is_object (js_arg)) { - return jerry_throw_sz (JERRY_ERROR_TYPE, "It is not an object."); + return jerry_throw_sz (JERRY_ERROR_TYPE, jerry_sz_ascii ("It is not an object.")); } const jerry_object_native_info_t *expected_info_p; @@ -438,7 +438,8 @@ jerryx_arg_transform_native_pointer (jerryx_arg_js_iterator_t *js_arg_iter_p, /* if (*ptr_p == NULL) { - return jerry_throw_sz (JERRY_ERROR_TYPE, "The object has no native pointer or type does not match."); + return jerry_throw_sz (JERRY_ERROR_TYPE, + jerry_sz_ascii ("The object has no native pointer or type does not match.")); } return jerry_undefined (); diff --git a/jerry-ext/arg/arg.c b/jerry-ext/arg/arg.c index 87638b1d3a..61d09f2819 100644 --- a/jerry-ext/arg/arg.c +++ b/jerry-ext/arg/arg.c @@ -77,7 +77,7 @@ jerryx_arg_transform_this_and_args (const jerry_value_t this_val, /**< the this_ { jerry_value_free (ret); - return jerry_throw_sz (JERRY_ERROR_TYPE, "'this' validation failed."); + return jerry_throw_sz (JERRY_ERROR_TYPE, jerry_sz_ascii ("'this' validation failed.")); } return jerryx_arg_transform_args (js_arg_p, js_arg_cnt, c_arg_p + 1, c_arg_cnt - 1); @@ -92,23 +92,21 @@ jerryx_arg_transform_this_and_args (const jerry_value_t this_val, /**< the this_ */ jerry_value_t jerryx_arg_transform_object_properties (const jerry_value_t obj_val, /**< the JS object */ - const jerry_char_t **name_p, /**< property name list of the JS object */ + const jerry_value_t *name_p, /**< property name list of the JS object */ const jerry_length_t name_cnt, /**< count of the name list */ const jerryx_arg_t *c_arg_p, /**< points to the array of transformation steps */ jerry_length_t c_arg_cnt) /**< the count of the `c_arg_p` array */ { if (!jerry_value_is_object (obj_val)) { - return jerry_throw_sz (JERRY_ERROR_TYPE, "Not an object."); + return jerry_throw_sz (JERRY_ERROR_TYPE, jerry_sz_ascii ("Not an object.")); } JERRY_VLA (jerry_value_t, prop, name_cnt); - for (jerry_length_t i = 0; i < name_cnt; i++, name_p++) + for (jerry_length_t i = 0; i < name_cnt; i++) { - const jerry_value_t name_str = jerry_string_sz ((char *) (*name_p)); - prop[i] = jerry_object_get (obj_val, name_str); - jerry_value_free (name_str); + prop[i] = jerry_object_get (obj_val, name_p[i]); if (jerry_value_is_exception (prop[i])) { @@ -144,7 +142,7 @@ jerryx_arg_transform_array (const jerry_value_t array_val, /**< points to the JS { if (!jerry_value_is_array (array_val)) { - return jerry_throw_sz (JERRY_ERROR_TYPE, "Not an array."); + return jerry_throw_sz (JERRY_ERROR_TYPE, jerry_sz_ascii ("Not an array.")); } JERRY_VLA (jerry_value_t, arr, c_arg_cnt); diff --git a/jerry-ext/common/jext-common.h b/jerry-ext/common/jext-common.h index 946de890ba..afba6f35f9 100644 --- a/jerry-ext/common/jext-common.h +++ b/jerry-ext/common/jext-common.h @@ -22,11 +22,6 @@ #include "jerryscript-port.h" #include "jerryscript.h" -/* - * Make sure unused parameters, variables, or expressions trigger no compiler warning. - */ -#define JERRYX_UNUSED(x) ((void) (x)) - /* * Asserts * @@ -70,7 +65,7 @@ void JERRY_ATTR_NORETURN jerry_unreachable (const char *file, const char *functi { \ if (false) \ { \ - JERRYX_UNUSED (x); \ + JERRY_UNUSED (x); \ } \ } while (0) diff --git a/jerry-ext/debugger/debugger-common.c b/jerry-ext/debugger/debugger-common.c index 9b03f2239b..3f2915df95 100644 --- a/jerry-ext/debugger/debugger-common.c +++ b/jerry-ext/debugger/debugger-common.c @@ -35,7 +35,7 @@ jerryx_debugger_after_connect (bool success) /**< tells whether the connection jerry_debugger_transport_close (); } #else /* !(defined (JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1)) */ - JERRYX_UNUSED (success); + JERRY_UNUSED (success); #endif /* defined (JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1) */ } /* jerryx_debugger_after_connect */ diff --git a/jerry-ext/debugger/debugger-serial.c b/jerry-ext/debugger/debugger-serial.c index 3a4215f087..8a0c5ff204 100644 --- a/jerry-ext/debugger/debugger-serial.c +++ b/jerry-ext/debugger/debugger-serial.c @@ -402,7 +402,7 @@ jerryx_debugger_serial_create (const char *config) /**< specify the configuratio bool jerryx_debugger_serial_create (const char *config) { - JERRYX_UNUSED (config); + JERRY_UNUSED (config); return false; } /* jerryx_debugger_serial_create */ diff --git a/jerry-ext/debugger/debugger-tcp.c b/jerry-ext/debugger/debugger-tcp.c index 8483470559..76cbc9a12d 100644 --- a/jerry-ext/debugger/debugger-tcp.c +++ b/jerry-ext/debugger/debugger-tcp.c @@ -399,7 +399,7 @@ jerryx_debugger_tcp_create (uint16_t port) /**< listening port */ bool jerryx_debugger_tcp_create (uint16_t port) { - JERRYX_UNUSED (port); + JERRY_UNUSED (port); return false; } /* jerryx_debugger_tcp_create */ diff --git a/jerry-ext/include/jerryscript-ext/arg.h b/jerry-ext/include/jerryscript-ext/arg.h index aa70878f6f..e397815c51 100644 --- a/jerry-ext/include/jerryscript-ext/arg.h +++ b/jerry-ext/include/jerryscript-ext/arg.h @@ -45,7 +45,7 @@ typedef jerry_value_t (*jerryx_arg_transform_func_t) (jerryx_arg_js_iterator_t * */ typedef struct { - const jerry_char_t **name_p; /**< property name list of the JS object */ + const jerry_value_t *name_p; /**< property name list of the JS object */ jerry_length_t name_cnt; /**< count of the name list */ const jerryx_arg_t *c_arg_p; /**< points to the array of transformation steps */ jerry_length_t c_arg_cnt; /**< the count of the `c_arg_p` array */ @@ -82,7 +82,7 @@ jerry_value_t jerryx_arg_transform_args (const jerry_value_t *js_arg_p, jerry_length_t c_arg_cnt); jerry_value_t jerryx_arg_transform_object_properties (const jerry_value_t obj_val, - const jerry_char_t **name_p, + const jerry_value_t *name_p, const jerry_length_t name_cnt, const jerryx_arg_t *c_arg_p, jerry_length_t c_arg_cnt); diff --git a/jerry-ext/include/jerryscript-ext/module.h b/jerry-ext/include/jerryscript-ext/module.h index 32166abcf7..1ed72b4952 100644 --- a/jerry-ext/include/jerryscript-ext/module.h +++ b/jerry-ext/include/jerryscript-ext/module.h @@ -31,7 +31,7 @@ typedef jerry_value_t (*jerryx_native_module_on_resolve_t) (void); */ typedef struct jerryx_native_module_t { - const jerry_char_t *name_p; /**< name of the module */ + jerry_string_t name; /**< name of the module */ const jerryx_native_module_on_resolve_t on_resolve_p; /**< function that returns a new instance of the module */ struct jerryx_native_module_t *next_p; /**< pointer to next module in the list */ } jerryx_native_module_t; @@ -99,19 +99,19 @@ typedef struct jerryx_native_module_t */ #define JERRYX_NATIVE_MODULE(module_name, on_resolve_cb) JERRYX_NATIVE_MODULE_IMPLEM (module_name, on_resolve_cb) -#define JERRYX_NATIVE_MODULE_IMPLEM(module_name, on_resolve_cb) \ - static jerryx_native_module_t _##module_name##_definition = { .name_p = (jerry_char_t *) #module_name, \ - .on_resolve_p = (on_resolve_cb), \ - .next_p = NULL }; \ - \ - JERRYX_MODULE_CONSTRUCTOR (module_name##_register) \ - { \ - jerryx_native_module_register (&_##module_name##_definition); \ - } \ - \ - JERRYX_MODULE_DESTRUCTOR (module_name##_unregister) \ - { \ - jerryx_native_module_unregister (&_##module_name##_definition); \ +#define JERRYX_NATIVE_MODULE_IMPLEM(module_name, on_resolve_cb) \ + static jerryx_native_module_t _##module_name##_definition = { .name = { JERRY_ZSTR_ARG (#module_name) }, \ + .on_resolve_p = (on_resolve_cb), \ + .next_p = NULL }; \ + \ + JERRYX_MODULE_CONSTRUCTOR (module_name##_register) \ + { \ + jerryx_native_module_register (&_##module_name##_definition); \ + } \ + \ + JERRYX_MODULE_DESTRUCTOR (module_name##_unregister) \ + { \ + jerryx_native_module_unregister (&_##module_name##_definition); \ } /** diff --git a/jerry-ext/include/jerryscript-ext/properties.h b/jerry-ext/include/jerryscript-ext/properties.h index 3ad644680d..cbd818daed 100644 --- a/jerry-ext/include/jerryscript-ext/properties.h +++ b/jerry-ext/include/jerryscript-ext/properties.h @@ -16,7 +16,7 @@ #ifndef JERRYX_PROPERTIES_H #define JERRYX_PROPERTIES_H -#include "jerryscript-types.h" +#include "jerryscript-core.h" JERRY_C_API_BEGIN @@ -24,7 +24,7 @@ JERRY_C_API_BEGIN * Handler registration helper */ -bool jerryx_register_global (const char *name_p, jerry_external_handler_t handler_p); +bool jerryx_register_global (jerry_value_t function_name_val, jerry_external_handler_t handler_p); /** * Struct used by the `jerryx_set_functions` method to @@ -32,44 +32,44 @@ bool jerryx_register_global (const char *name_p, jerry_external_handler_t handle */ typedef struct { - const char *name; /**< name of the property to add */ + jerry_value_t name; /**< name of the property to add */ jerry_value_t value; /**< value of the property */ } jerryx_property_entry; -#define JERRYX_PROPERTY_NUMBER(NAME, NUMBER) \ - (jerryx_property_entry) \ - { \ - NAME, jerry_number (NUMBER) \ +#define JERRYX_PROPERTY_NUMBER(NAME, NUMBER) \ + (jerryx_property_entry) \ + { \ + jerry_sz_ascii (NAME), jerry_number (NUMBER) \ } -#define JERRYX_PROPERTY_STRING(NAME, STR, SIZE) \ - (jerryx_property_entry) \ - { \ - NAME, jerry_string ((const jerry_char_t *) STR, SIZE, JERRY_ENCODING_UTF8) \ +#define JERRYX_PROPERTY_STRING(NAME, STR, SIZE) \ + (jerryx_property_entry) \ + { \ + jerry_sz_ascii (NAME), jerry_string_utf8 ((const jerry_char_t *) STR, SIZE) \ } -#define JERRYX_PROPERTY_STRING_SZ(NAME, STR) \ - (jerryx_property_entry) \ - { \ - NAME, jerry_string_sz (STR) \ +#define JERRYX_PROPERTY_STRING_SZ(NAME, STR) \ + (jerryx_property_entry) \ + { \ + jerry_sz_ascii (NAME), jerry_sz_ascii (STR) \ } -#define JERRYX_PROPERTY_BOOLEAN(NAME, VALUE) \ - (jerryx_property_entry) \ - { \ - NAME, jerry_boolean (VALUE) \ +#define JERRYX_PROPERTY_BOOLEAN(NAME, VALUE) \ + (jerryx_property_entry) \ + { \ + jerry_sz_ascii (NAME), jerry_boolean (VALUE) \ } -#define JERRYX_PROPERTY_FUNCTION(NAME, FUNC) \ - (jerryx_property_entry) \ - { \ - NAME, jerry_function_external (FUNC) \ +#define JERRYX_PROPERTY_FUNCTION(NAME, FUNC) \ + (jerryx_property_entry) \ + { \ + jerry_sz_ascii (NAME), jerry_function_external (FUNC) \ } -#define JERRYX_PROPERTY_UNDEFINED(NAME) \ - (jerryx_property_entry) \ - { \ - NAME, jerry_undefined () \ +#define JERRYX_PROPERTY_UNDEFINED(NAME) \ + (jerryx_property_entry) \ + { \ + jerry_sz_ascii (NAME), jerry_undefined () \ } -#define JERRYX_PROPERTY_LIST_END() \ - (jerryx_property_entry) \ - { \ - NULL, 0 \ +#define JERRYX_PROPERTY_LIST_END() \ + (jerryx_property_entry) \ + { \ + jerry_undefined (), jerry_undefined () \ } /** diff --git a/jerry-ext/module/module.c b/jerry-ext/module/module.c index 01f7d151e6..7404642b84 100644 --- a/jerry-ext/module/module.c +++ b/jerry-ext/module/module.c @@ -19,9 +19,9 @@ #include "jerryscript.h" -static const char *module_name_property_name = "moduleName"; -static const char *module_not_found = "Module not found"; -static const char *module_name_not_string = "Module name is not a string"; +#define module_name_property_name "moduleName" +#define module_not_found "Module not found" +#define module_name_not_string "Module name is not a string" /** * Create an error related to modules @@ -33,11 +33,11 @@ static const char *module_name_not_string = "Module name is not a string"; */ static jerry_value_t jerryx_module_create_error (jerry_error_t error_type, /**< the type of error to create */ - const char *message, /**< the error message */ + jerry_value_t message, /**< the error message */ const jerry_value_t module_name) /**< the module name */ { jerry_value_t error_object = jerry_error_sz (error_type, message); - jerry_value_t property_name = jerry_string_sz (module_name_property_name); + jerry_value_t property_name = jerry_sz_ascii (module_name_property_name); jerry_value_free (jerry_object_set (error_object, property_name, module_name)); @@ -164,7 +164,7 @@ jerryx_module_add_to_cache (jerry_value_t cache, /**< cache to which to add the return ret; } /* jerryx_module_add_to_cache */ -static const char *on_resolve_absent = "Module on_resolve () must not be NULL"; +#define on_resolve_absent "Module on_resolve () must not be NULL" /** * Declare and define the default module resolver - one which examines what modules are defined in the above linker @@ -184,13 +184,13 @@ jerryx_resolve_native_module (const jerry_value_t canonical_name, /**< canonical /* Look for the module by its name in the list of module definitions. */ for (module_p = first_module_p; module_p != NULL; module_p = module_p->next_p) { - if (module_p->name_p != NULL && strlen ((char *) module_p->name_p) == name_size - && !strncmp ((char *) module_p->name_p, (char *) name_string, name_size)) + if (module_p->name.size == name_size && !strncmp ((char *) module_p->name.ptr, (char *) name_string, name_size)) { /* If we find the module by its name we load it and cache it if it has an on_resolve () and complain otherwise. */ (*result) = - ((module_p->on_resolve_p) ? module_p->on_resolve_p () - : jerryx_module_create_error (JERRY_ERROR_TYPE, on_resolve_absent, canonical_name)); + ((module_p->on_resolve_p) + ? module_p->on_resolve_p () + : jerryx_module_create_error (JERRY_ERROR_TYPE, jerry_sz_ascii (on_resolve_absent), canonical_name)); return true; } } @@ -218,7 +218,7 @@ jerryx_module_resolve_local (const jerry_value_t name, /**< name of the module t { if (result != NULL) { - *result = jerryx_module_create_error (JERRY_ERROR_COMMON, module_name_not_string, name); + *result = jerryx_module_create_error (JERRY_ERROR_COMMON, jerry_sz_ascii (module_name_not_string), name); } goto done; } @@ -268,7 +268,7 @@ jerryx_module_resolve_local (const jerry_value_t name, /**< name of the module t } /* If none of the resolvers manage to find the module, complain with "Module not found" */ - *result = jerryx_module_create_error (JERRY_ERROR_COMMON, module_not_found, name); + *result = jerryx_module_create_error (JERRY_ERROR_COMMON, jerry_sz_ascii (module_not_found), name); done: /* Release the canonical names as returned by the various resolvers. */ diff --git a/jerry-ext/util/handlers.c b/jerry-ext/util/handlers.c index 10d2a1ed0b..24d24b4f71 100644 --- a/jerry-ext/util/handlers.c +++ b/jerry-ext/util/handlers.c @@ -193,7 +193,7 @@ jerryx_handler_source_received (const jerry_char_t *source_name_p, /**< resource jerry_parse_options_t parse_options; parse_options.options = JERRY_PARSE_HAS_SOURCE_NAME; - parse_options.source_name = jerry_string (source_name_p, (jerry_size_t) source_name_size, JERRY_ENCODING_UTF8); + parse_options.source_name = jerry_string_utf8 (source_name_p, (jerry_size_t) source_name_size); jerry_value_t ret_val = jerry_parse (source_p, source_size, &parse_options); diff --git a/jerry-ext/util/print.c b/jerry-ext/util/print.c index 6e79a8eb75..a51d4cc48a 100644 --- a/jerry-ext/util/print.c +++ b/jerry-ext/util/print.c @@ -282,7 +282,7 @@ jerryx_print_unhandled_exception (jerry_value_t exception) /**< exception value if (jerry_value_is_object (value)) { - jerry_value_t backtrace_val = jerry_object_get_sz (value, "stack"); + jerry_value_t backtrace_val = jerry_object_get_sz (value, jerry_sz_ascii ("stack")); if (jerry_value_is_array (backtrace_val)) { diff --git a/jerry-ext/util/properties.c b/jerry-ext/util/properties.c index 58b4085db3..a327a7a53f 100644 --- a/jerry-ext/util/properties.c +++ b/jerry-ext/util/properties.c @@ -24,11 +24,10 @@ * false - otherwise. */ bool -jerryx_register_global (const char *name_p, /**< name of the function */ +jerryx_register_global (jerry_value_t function_name_val, /**< name of the function that will be free/take */ jerry_external_handler_t handler_p) /**< function callback */ { jerry_value_t global_obj_val = jerry_current_realm (); - jerry_value_t function_name_val = jerry_string_sz (name_p); jerry_value_t function_val = jerry_function_external (handler_p); jerry_value_t result_val = jerry_object_set (global_obj_val, function_name_val, function_val); @@ -70,14 +69,11 @@ jerryx_set_properties (const jerry_value_t target_object, /**< target object */ return JERRYX_SET_PROPERTIES_RESULT (jerry_undefined (), 0); } - for (; (entries[idx].name != NULL); idx++) + for (; !jerry_value_is_undefined (entries[idx].name); idx++) { const jerryx_property_entry *entry = &entries[idx]; - jerry_value_t prop_name = jerry_string_sz (entry->name); - jerry_value_t result = jerry_object_set (target_object, prop_name, entry->value); - - jerry_value_free (prop_name); + jerry_value_t result = jerry_object_set (target_object, entry->name, entry->value); // By API definition: // The jerry_object_set returns TRUE if there is no problem @@ -88,6 +84,7 @@ jerryx_set_properties (const jerry_value_t target_object, /**< target object */ return JERRYX_SET_PROPERTIES_RESULT (result, idx); } + jerry_value_free (entry->name); jerry_value_free (entry->value); jerry_value_free (result); } @@ -111,8 +108,9 @@ jerryx_release_property_entry (const jerryx_property_entry entries[], /**< list return; } - for (uint32_t idx = register_result.registered; entries[idx].name != NULL; idx++) + for (uint32_t idx = register_result.registered; !jerry_value_is_undefined (entries[idx].name); idx++) { + jerry_value_free (entries[idx].name); jerry_value_free (entries[idx].value); } } /* jerryx_release_property_entry */ diff --git a/jerry-ext/util/repl.c b/jerry-ext/util/repl.c index ad58ffc1f9..700b901ddf 100644 --- a/jerry-ext/util/repl.c +++ b/jerry-ext/util/repl.c @@ -50,7 +50,7 @@ jerryx_repl (const jerry_char_t *prompt_p, jerry_size_t prompt_size) if (!jerry_validate_string (line_p, length, JERRY_ENCODING_UTF8)) { jerry_port_line_free (line_p); - result = jerry_throw_sz (JERRY_ERROR_SYNTAX, "Input is not a valid UTF-8 string"); + result = jerry_throw_sz (JERRY_ERROR_SYNTAX, jerry_sz_ascii ("Input is not a valid UTF-8 string")); goto exception; } diff --git a/jerry-ext/util/sources.c b/jerry-ext/util/sources.c index dbd629ee7a..0620cd2f23 100644 --- a/jerry-ext/util/sources.c +++ b/jerry-ext/util/sources.c @@ -35,19 +35,18 @@ jerryx_source_parse_script (const char *path_p) if (source_p == NULL) { jerry_log (JERRY_LOG_LEVEL_ERROR, "Failed to open file: %s\n", path_p); - return jerry_throw_sz (JERRY_ERROR_SYNTAX, "Source file not found"); + return jerry_throw_sz (JERRY_ERROR_SYNTAX, jerry_sz_ascii ("Source file not found")); } if (!jerry_validate_string (source_p, source_size, JERRY_ENCODING_UTF8)) { jerry_port_source_free (source_p); - return jerry_throw_sz (JERRY_ERROR_SYNTAX, "Input is not a valid UTF-8 encoded string."); + return jerry_throw_sz (JERRY_ERROR_SYNTAX, jerry_sz_ascii ("Input is not a valid UTF-8 encoded string.")); } jerry_parse_options_t parse_options; parse_options.options = JERRY_PARSE_HAS_SOURCE_NAME; - parse_options.source_name = - jerry_string ((const jerry_char_t *) path_p, (jerry_size_t) strlen (path_p), JERRY_ENCODING_UTF8); + parse_options.source_name = jerry_string_utf8 ((const jerry_char_t *) path_p, (jerry_size_t) strlen (path_p)); jerry_value_t result = jerry_parse (source_p, source_size, &parse_options); @@ -75,8 +74,7 @@ jerryx_source_exec_script (const char *path_p) jerry_value_t jerryx_source_exec_module (const char *path_p) { - jerry_value_t specifier = - jerry_string ((const jerry_char_t *) path_p, (jerry_size_t) strlen (path_p), JERRY_ENCODING_UTF8); + jerry_value_t specifier = jerry_string_utf8 ((const jerry_char_t *) path_p, (jerry_size_t) strlen (path_p)); jerry_value_t referrer = jerry_undefined (); jerry_value_t module = jerry_module_resolve (specifier, referrer, NULL); @@ -118,7 +116,7 @@ jerryx_source_exec_snapshot (const char *path_p, size_t function_index) if (source_p == NULL) { jerry_log (JERRY_LOG_LEVEL_ERROR, "Failed to open file: %s\n", path_p); - return jerry_throw_sz (JERRY_ERROR_SYNTAX, "Snapshot file not found"); + return jerry_throw_sz (JERRY_ERROR_SYNTAX, jerry_sz_ascii ("Snapshot file not found")); } jerry_value_t result = @@ -148,7 +146,7 @@ jerryx_source_exec_stdin (void) source_p = realloc (source_p, new_size); if (source_p == NULL) { - return jerry_throw_sz (JERRY_ERROR_COMMON, "Out of memory."); + return jerry_throw_sz (JERRY_ERROR_COMMON, jerry_sz_ascii ("Out of memory.")); } memcpy (source_p + source_size, line_p, line_size); @@ -159,7 +157,7 @@ jerryx_source_exec_stdin (void) if (!jerry_validate_string (source_p, source_size, JERRY_ENCODING_UTF8)) { free (source_p); - return jerry_throw_sz (JERRY_ERROR_SYNTAX, "Input is not a valid UTF-8 encoded string."); + return jerry_throw_sz (JERRY_ERROR_SYNTAX, jerry_sz_ascii ("Input is not a valid UTF-8 encoded string.")); } jerry_value_t result = jerry_parse (source_p, source_size, NULL); diff --git a/jerry-ext/util/test262.c b/jerry-ext/util/test262.c index 278075d1d4..582f41c5d1 100644 --- a/jerry-ext/util/test262.c +++ b/jerry-ext/util/test262.c @@ -27,11 +27,11 @@ */ static void jerryx_test262_register_function (jerry_value_t test262_obj, /** $262 object */ - const char *name_p, /**< name of the function */ + jerry_value_t name_sz, /**< name of the function that will be free/take*/ jerry_external_handler_t handler_p) /**< function callback */ { jerry_value_t function_val = jerry_function_external (handler_p); - jerry_value_t result_val = jerry_object_set_sz (test262_obj, name_p, function_val); + jerry_value_t result_val = jerry_object_set_sz (test262_obj, name_sz, function_val); jerry_value_free (function_val); assert (!jerry_value_is_exception (result_val)); @@ -55,7 +55,7 @@ jerryx_test262_detach_array_buffer (const jerry_call_info_t *call_info_p, /**< c if (args_cnt < 1 || !jerry_value_is_arraybuffer (args_p[0])) { - return jerry_throw_sz (JERRY_ERROR_TYPE, "Expected an ArrayBuffer object"); + return jerry_throw_sz (JERRY_ERROR_TYPE, jerry_sz_ascii ("Expected an ArrayBuffer object")); } /* TODO: support the optional 'key' argument */ @@ -79,7 +79,7 @@ jerryx_test262_eval_script (const jerry_call_info_t *call_info_p, /**< call info if (args_cnt < 1 || !jerry_value_is_string (args_p[0])) { - return jerry_throw_sz (JERRY_ERROR_TYPE, "Expected a string"); + return jerry_throw_sz (JERRY_ERROR_TYPE, jerry_sz_ascii ("Expected a string")); } jerry_value_t ret_value = jerry_parse_value (args_p[0], NULL); @@ -133,12 +133,14 @@ jerryx_test262_create (jerry_value_t global_obj) /**< global object */ { jerry_value_t test262_object = jerry_object (); - jerryx_test262_register_function (test262_object, "detachArrayBuffer", jerryx_test262_detach_array_buffer); - jerryx_test262_register_function (test262_object, "evalScript", jerryx_test262_eval_script); - jerryx_test262_register_function (test262_object, "createRealm", jerryx_test262_create_realm); - jerryx_test262_register_function (test262_object, "gc", jerryx_handler_gc); + jerryx_test262_register_function (test262_object, + jerry_sz_ascii ("detachArrayBuffer"), + jerryx_test262_detach_array_buffer); + jerryx_test262_register_function (test262_object, jerry_sz_ascii ("evalScript"), jerryx_test262_eval_script); + jerryx_test262_register_function (test262_object, jerry_sz_ascii ("createRealm"), jerryx_test262_create_realm); + jerryx_test262_register_function (test262_object, jerry_sz_ascii ("gc"), jerryx_handler_gc); - jerry_value_t result = jerry_object_set_sz (test262_object, "global", global_obj); + jerry_value_t result = jerry_object_set_sz (test262_object, jerry_sz_ascii ("global"), global_obj); assert (!jerry_value_is_exception (result)); jerry_value_free (result); @@ -154,7 +156,7 @@ jerryx_test262_register (void) jerry_value_t global_obj = jerry_current_realm (); jerry_value_t test262_obj = jerryx_test262_create (global_obj); - jerry_value_t result = jerry_object_set_sz (global_obj, "$262", test262_obj); + jerry_value_t result = jerry_object_set_sz (global_obj, jerry_sz_ascii ("$262"), test262_obj); assert (!jerry_value_is_exception (result)); jerry_value_free (result); diff --git a/jerry-main/benchmark/main-benchmark.c b/jerry-main/benchmark/main-benchmark.c index 25cf741b62..0c94329bd3 100644 --- a/jerry-main/benchmark/main-benchmark.c +++ b/jerry-main/benchmark/main-benchmark.c @@ -87,7 +87,7 @@ run (void) if (source_p == NULL) { - ret_value = jerry_throw_sz (JERRY_ERROR_COMMON, ""); + ret_value = jerry_throw_sz (JERRY_ERROR_COMMON, jerry_sz_ascii ("")); break; } else diff --git a/jerry-main/main-desktop.c b/jerry-main/main-desktop.c index 920ea9ca6f..ade2a43732 100644 --- a/jerry-main/main-desktop.c +++ b/jerry-main/main-desktop.c @@ -100,11 +100,11 @@ main_init_engine (main_args_t *arguments_p) /**< main arguments */ jerryx_test262_register (); } - jerryx_register_global ("assert", jerryx_handler_assert); - jerryx_register_global ("gc", jerryx_handler_gc); - jerryx_register_global ("print", jerryx_handler_print); - jerryx_register_global ("sourceName", jerryx_handler_source_name); - jerryx_register_global ("createRealm", jerryx_handler_create_realm); + jerryx_register_global (jerry_sz_ascii ("assert"), jerryx_handler_assert); + jerryx_register_global (jerry_sz_ascii ("gc"), jerryx_handler_gc); + jerryx_register_global (jerry_sz_ascii ("print"), jerryx_handler_print); + jerryx_register_global (jerry_sz_ascii ("sourceName"), jerryx_handler_source_name); + jerryx_register_global (jerry_sz_ascii ("createRealm"), jerryx_handler_create_realm); } /* main_init_engine */ int @@ -245,7 +245,10 @@ main (int argc, char **argv) if (arguments.exit_cb_name_p != NULL) { jerry_value_t global = jerry_current_realm (); - jerry_value_t callback_fn = jerry_object_get_sz (global, arguments.exit_cb_name_p); + jerry_value_t callback_fn = + jerry_object_get_sz (global, + jerry_string_utf8 ((const jerry_char_t *) arguments.exit_cb_name_p, + (jerry_size_t) strlen (arguments.exit_cb_name_p))); jerry_value_free (global); if (jerry_value_is_function (callback_fn)) diff --git a/jerry-main/main-snapshot.c b/jerry-main/main-snapshot.c index f6c913fe48..915ae98d48 100644 --- a/jerry-main/main-snapshot.c +++ b/jerry-main/main-snapshot.c @@ -339,14 +339,13 @@ process_generate (cli_state_t *cli_state_p, /**< cli state */ /* To avoid cppcheck warning. */ parse_options.argument_list = 0; parse_options.source_name = - jerry_string ((const jerry_char_t *) file_name_p, (jerry_size_t) strlen (file_name_p), JERRY_ENCODING_UTF8); + jerry_string_utf8 ((const jerry_char_t *) file_name_p, (jerry_size_t) strlen (file_name_p)); if (function_args_p != NULL) { parse_options.options |= JERRY_PARSE_HAS_ARGUMENT_LIST; - parse_options.argument_list = jerry_string ((const jerry_char_t *) function_args_p, - (jerry_size_t) strlen (function_args_p), - JERRY_ENCODING_UTF8); + parse_options.argument_list = + jerry_string_utf8 ((const jerry_char_t *) function_args_p, (jerry_size_t) strlen (function_args_p)); } jerry_value_t snapshot_result = jerry_parse ((jerry_char_t *) source_p, source_length, &parse_options); diff --git a/jerry-port/common/jerry-port-io.c b/jerry-port/common/jerry-port-io.c index 26ae7c4445..eaa6633ca7 100644 --- a/jerry-port/common/jerry-port-io.c +++ b/jerry-port/common/jerry-port-io.c @@ -19,14 +19,11 @@ #include "jerryscript-port.h" -/** - * Default implementation of jerry_port_log. Prints log messages to stderr. - */ void JERRY_ATTR_WEAK -jerry_port_log (const char *message_p) /**< message */ +jerry_port_log_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size) { - fputs (message_p, stderr); -} /* jerry_port_log */ + fwrite (buffer_p, 1, buffer_size, stderr); +} /* jerry_port_log_buffer */ void JERRY_ATTR_WEAK jerry_port_print_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size) diff --git a/targets/baremetal-sdk/espressif/main/jerry-main.c b/targets/baremetal-sdk/espressif/main/jerry-main.c index 192bfef661..bd98905736 100644 --- a/targets/baremetal-sdk/espressif/main/jerry-main.c +++ b/targets/baremetal-sdk/espressif/main/jerry-main.c @@ -31,7 +31,7 @@ void app_main() jerry_log (JERRY_LOG_LEVEL_DEBUG, "This test run the following script code: %s", script); /* Register the print function in the global object */ - jerryx_register_global ("print", jerryx_handler_print); + jerryx_register_global (jerry_sz_ascii ("print"), jerryx_handler_print); /* Setup Global scope code */ ret_value = jerry_parse (script, sizeof (script) - 1, NULL); diff --git a/targets/baremetal-sdk/espressif/main/jerry-port.c b/targets/baremetal-sdk/espressif/main/jerry-port.c index 6655b21874..e79dc9cbb2 100644 --- a/targets/baremetal-sdk/espressif/main/jerry-port.c +++ b/targets/baremetal-sdk/espressif/main/jerry-port.c @@ -30,14 +30,11 @@ static const char ESP_JS_TAG[] = "JS"; -/** - * Provide log message implementation for the engine. - */ void -jerry_port_log (const char *message_p) /**< message */ +jerry_port_log_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size) { - ESP_LOGI (ESP_JS_TAG, "%s", message_p); -} /* jerry_port_log */ + ESP_LOGI (ESP_JS_TAG, "%s", buffer_p); +} /* jerry_port_log_buffer */ /** * Implementation of jerry_port_fatal. diff --git a/targets/baremetal-sdk/particle/source/main.cpp b/targets/baremetal-sdk/particle/source/main.cpp index b5cd6cebac..cb6f95d174 100644 --- a/targets/baremetal-sdk/particle/source/main.cpp +++ b/targets/baremetal-sdk/particle/source/main.cpp @@ -79,13 +79,13 @@ init_jerry () jerry_value_t prop_name; func_obj = jerry_function_external (set_led); - prop_name = jerry_string_sz ("setLed"); + prop_name = jerry_sz_ascii ("setLed"); jerry_value_free (jerry_object_set (object, prop_name, func_obj)); jerry_value_free (prop_name); jerry_value_free (func_obj); func_obj = jerry_function_external (js_delay); - prop_name = jerry_string_sz ("delay"); + prop_name = jerry_sz_ascii ("delay"); jerry_value_free (jerry_object_set (object, prop_name, func_obj)); jerry_value_free (prop_name); jerry_value_free (func_obj); @@ -94,7 +94,7 @@ init_jerry () jerry_value_t global_object = jerry_current_realm (); /* Add the JS object to the global context */ - prop_name = jerry_string_sz ("test"); + prop_name = jerry_sz_ascii ("test"); jerry_value_free (jerry_object_set (global_object, prop_name, object)); jerry_value_free (prop_name); jerry_value_free (object); diff --git a/targets/os/mbedos/jerry-main.cpp b/targets/os/mbedos/jerry-main.cpp index 4db968513c..d8e6f6789d 100644 --- a/targets/os/mbedos/jerry-main.cpp +++ b/targets/os/mbedos/jerry-main.cpp @@ -27,14 +27,15 @@ #define JERRY_STANDALONE_EXIT_CODE_FAIL (1) #if MBED_MAJOR_VERSION == 5 -static Serial serial(USBTX, USBRX, 115200); +static Serial serial (USBTX, USBRX, 115200); #elif MBED_MAJOR_VERSION == 6 -static BufferedSerial serial(USBTX, USBRX, 115200); +static BufferedSerial serial (USBTX, USBRX, 115200); #else #error Unsupported Mbed OS version. #endif -int main() +int +main () { /* Initialize engine */ jerry_init (JERRY_INIT_EMPTY); @@ -43,7 +44,7 @@ int main() jerry_log (JERRY_LOG_LEVEL_DEBUG, "This test run the following script code: [%s]\n\n", script); /* Register the print function in the global object. */ - jerryx_register_global ("print", jerryx_handler_print); + jerryx_register_global (jerry_sz_ascii ("print"), jerryx_handler_print); /* Setup Global scope code */ jerry_value_t ret_value = jerry_parse (script, sizeof (script) - 1, NULL); diff --git a/targets/os/mbedos/jerry-port.cpp b/targets/os/mbedos/jerry-port.cpp index 9ba1383c48..cf7bc6f550 100644 --- a/targets/os/mbedos/jerry-port.cpp +++ b/targets/os/mbedos/jerry-port.cpp @@ -29,23 +29,20 @@ jerry_port_fatal (jerry_fatal_code_t code) exit ((int) code); } /* jerry_port_fatal */ -/** - * Provide log message implementation for the engine. - */ void -jerry_port_log (const char *message_p) /**< message */ +jerry_port_log_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size) { - while (*message_p != '\0') + for (jerry_size_t i = 0; i < buffer_size; ++i) { - if (*message_p == '\n') + if (buffer_p[i] == '\n') { /* add CR for proper display in serial monitors */ fputc ('\r', stderr); } - fputc (*message_p++, stderr); + fputc (buffer_p[i], stderr); } -} /* jerry_port_log */ +} /* jerry_port_log_buffer */ /** * Dummy function to get the time zone adjustment. diff --git a/targets/os/nuttx/jerry-main.c b/targets/os/nuttx/jerry-main.c index d09c438355..2f4bf35a58 100644 --- a/targets/os/nuttx/jerry-main.c +++ b/targets/os/nuttx/jerry-main.c @@ -96,16 +96,17 @@ str_to_uint (const char *num_str_p, /**< string to convert */ * Register a JavaScript function in the global object. */ static void -register_js_function (const char *name_p, /**< name of the function */ +register_js_function (jerry_value_t function_name_val, /**< name of the function that will be free/take */ jerry_external_handler_t handler_p) /**< function callback */ { - jerry_value_t result_val = jerryx_register_global (name_p, handler_p); + jerry_value_t result_val = jerryx_register_global (jerry_value_copy (function_name_val), handler_p); if (jerry_value_is_exception (result_val)) { - jerry_log (JERRY_LOG_LEVEL_WARNING, "Warning: failed to register '%s' method.", name_p); + jerry_log (JERRY_LOG_LEVEL_WARNING, "Warning: failed to register '%S' method.", function_name_val); } + jerry_value_free (function_name_val); jerry_value_free (result_val); } /* register_js_function */ @@ -197,9 +198,9 @@ jerry_main (int argc, char *argv[]) jerryx_debugger_after_connect (jerryx_debugger_tcp_create (debug_port) && jerryx_debugger_ws_create ()); } - register_js_function ("assert", jerryx_handler_assert); - register_js_function ("gc", jerryx_handler_gc); - register_js_function ("print", jerryx_handler_print); + register_js_function (jerry_sz_ascii("assert"), jerryx_handler_assert); + register_js_function (jerry_sz_ascii("gc"), jerryx_handler_gc); + register_js_function (jerry_sz_ascii("print"), jerryx_handler_print); jerry_value_t ret_value = jerry_undefined (); int ret_code = JERRY_STANDALONE_EXIT_CODE_OK; diff --git a/targets/os/nuttx/jerry-port.c b/targets/os/nuttx/jerry-port.c index c4f65a44a6..f90c918826 100644 --- a/targets/os/nuttx/jerry-port.c +++ b/targets/os/nuttx/jerry-port.c @@ -19,14 +19,12 @@ #include "jerryscript-port.h" -/** - * Default implementation of jerry_port_log. Prints log messages to stderr. - */ void -jerry_port_log (const char *message_p) /**< message */ +jerry_port_log_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size) { - (void) message_p; -} /* jerry_port_log */ + JERRY_UNUSED (buffer_p); + JERRY_UNUSED (buffer_size); +} /* jerry_port_log_buffer */ /** * Read a line from standard input as a zero-terminated string. diff --git a/targets/os/riot/source/jerry-main.c b/targets/os/riot/source/jerry-main.c index 9ca1583c87..a748820f78 100644 --- a/targets/os/riot/source/jerry-main.c +++ b/targets/os/riot/source/jerry-main.c @@ -49,7 +49,7 @@ test_jerry (int argc, char **argv) jerry_init (JERRY_INIT_EMPTY); /* Register the print function in the global object. */ - jerryx_register_global ("print", jerryx_handler_print); + jerryx_register_global (jerry_sz_ascii ("print"), jerryx_handler_print); /* Setup Global scope code */ ret_value = jerry_parse (script, sizeof (script) - 1, NULL); diff --git a/targets/os/zephyr/src/jerry-main.c b/targets/os/zephyr/src/jerry-main.c index 6042fa69fe..79e5b7ef5c 100644 --- a/targets/os/zephyr/src/jerry-main.c +++ b/targets/os/zephyr/src/jerry-main.c @@ -47,7 +47,7 @@ main (void) zephyr_getline_init (); jerry_init (JERRY_INIT_EMPTY); - jerryx_register_global ("print", jerryx_handler_print); + jerryx_register_global (jerry_sz_ascii ("print"), jerryx_handler_print); jerryx_repl (JERRY_ZSTR_ARG ("js> ")); diff --git a/tests/unit-core/test-abort.c b/tests/unit-core/test-abort.c index e8d3329418..bc3447a141 100644 --- a/tests/unit-core/test-abort.c +++ b/tests/unit-core/test-abort.c @@ -25,7 +25,7 @@ callback_func (const jerry_call_info_t *call_info_p, const jerry_value_t args_p[ JERRY_UNUSED (args_p); JERRY_UNUSED (args_count); - jerry_value_t value = jerry_string_sz ("Abort run!"); + jerry_value_t value = jerry_sz_ascii ("Abort run!"); value = jerry_throw_abort (value, true); return value; } /* callback_func */ @@ -38,7 +38,7 @@ main (void) jerry_init (JERRY_INIT_EMPTY); jerry_value_t global = jerry_current_realm (); - jerry_value_t callback_name = jerry_string_sz ("callback"); + jerry_value_t callback_name = jerry_sz_ascii ("callback"); jerry_value_t func = jerry_function_external (callback_func); jerry_value_t res = jerry_object_set (global, callback_name, func); TEST_ASSERT (!jerry_value_is_exception (res)); @@ -98,7 +98,7 @@ main (void) jerry_value_free (parsed_code_val); /* Test flag overwrites. */ - jerry_value_t value = jerry_string_sz ("Error description"); + jerry_value_t value = jerry_sz_ascii ("Error description"); TEST_ASSERT (!jerry_value_is_abort (value)); TEST_ASSERT (!jerry_value_is_exception (value)); diff --git a/tests/unit-core/test-api-binary-operations-arithmetics.c b/tests/unit-core/test-api-binary-operations-arithmetics.c index 185dc12c01..f277512b6a 100644 --- a/tests/unit-core/test-api-binary-operations-arithmetics.c +++ b/tests/unit-core/test-api-binary-operations-arithmetics.c @@ -59,7 +59,7 @@ main (void) jerry_value_t obj1 = jerry_eval ((jerry_char_t *) "o={x:1};o", 9, JERRY_PARSE_NO_OPTS); jerry_value_t obj2 = jerry_eval ((jerry_char_t *) "o={x:1};o", 9, JERRY_PARSE_NO_OPTS); - jerry_value_t err1 = jerry_throw_sz (JERRY_ERROR_SYNTAX, "error"); + jerry_value_t err1 = jerry_throw_sz (JERRY_ERROR_SYNTAX, jerry_sz_ascii ("error")); test_nan_entry_t test_nans[] = { /* Testing addition (+) */ @@ -69,13 +69,13 @@ main (void) /* Testing subtraction (-), multiplication (*), division (/), remainder (%) */ T_ARI (jerry_number (3.1), jerry_undefined ()), - T_ARI (jerry_string_sz ("foo"), jerry_string_sz ("bar")), - T_ARI (jerry_string_sz ("foo"), jerry_undefined ()), - T_ARI (jerry_string_sz ("foo"), jerry_null ()), - T_ARI (jerry_string_sz ("foo"), jerry_number (5.0)), - T_ARI (jerry_undefined (), jerry_string_sz ("foo")), - T_ARI (jerry_null (), jerry_string_sz ("foo")), - T_ARI (jerry_number (5.0), jerry_string_sz ("foo")), + T_ARI (jerry_sz_ascii ("foo"), jerry_sz_ascii ("bar")), + T_ARI (jerry_sz_ascii ("foo"), jerry_undefined ()), + T_ARI (jerry_sz_ascii ("foo"), jerry_null ()), + T_ARI (jerry_sz_ascii ("foo"), jerry_number (5.0)), + T_ARI (jerry_undefined (), jerry_sz_ascii ("foo")), + T_ARI (jerry_null (), jerry_sz_ascii ("foo")), + T_ARI (jerry_number (5.0), jerry_sz_ascii ("foo")), T_ARI (jerry_undefined (), jerry_undefined ()), T_ARI (jerry_undefined (), jerry_null ()), T_ARI (jerry_null (), jerry_undefined ()), @@ -83,7 +83,7 @@ main (void) T_ARI (jerry_value_copy (obj1), jerry_value_copy (obj2)), T_ARI (jerry_value_copy (obj2), jerry_value_copy (obj1)), T_ARI (jerry_value_copy (obj2), jerry_undefined ()), - T_ARI (jerry_value_copy (obj1), jerry_string_sz ("foo")), + T_ARI (jerry_value_copy (obj1), jerry_sz_ascii ("foo")), T_ARI (jerry_value_copy (obj1), jerry_null ()), T_ARI (jerry_value_copy (obj1), jerry_boolean (true)), T_ARI (jerry_value_copy (obj1), jerry_boolean (false)), @@ -120,10 +120,10 @@ main (void) T (JERRY_BIN_OP_ADD, jerry_number (5.0), jerry_number (5.0), jerry_number (10.0)), T (JERRY_BIN_OP_ADD, jerry_number (3.1), jerry_number (10), jerry_number (13.1)), T (JERRY_BIN_OP_ADD, jerry_number (3.1), jerry_boolean (true), jerry_number (4.1)), - T (JERRY_BIN_OP_ADD, jerry_string_sz ("foo"), jerry_string_sz ("bar"), jerry_string_sz ("foobar")), - T (JERRY_BIN_OP_ADD, jerry_string_sz ("foo"), jerry_undefined (), jerry_string_sz ("fooundefined")), - T (JERRY_BIN_OP_ADD, jerry_string_sz ("foo"), jerry_null (), jerry_string_sz ("foonull")), - T (JERRY_BIN_OP_ADD, jerry_string_sz ("foo"), jerry_number (5.0), jerry_string_sz ("foo5")), + T (JERRY_BIN_OP_ADD, jerry_sz_ascii ("foo"), jerry_sz_ascii ("bar"), jerry_sz_ascii ("foobar")), + T (JERRY_BIN_OP_ADD, jerry_sz_ascii ("foo"), jerry_undefined (), jerry_sz_ascii ("fooundefined")), + T (JERRY_BIN_OP_ADD, jerry_sz_ascii ("foo"), jerry_null (), jerry_sz_ascii ("foonull")), + T (JERRY_BIN_OP_ADD, jerry_sz_ascii ("foo"), jerry_number (5.0), jerry_sz_ascii ("foo5")), T (JERRY_BIN_OP_ADD, jerry_null (), jerry_null (), jerry_number (0.0)), T (JERRY_BIN_OP_ADD, jerry_boolean (true), jerry_boolean (true), jerry_number (2.0)), @@ -133,21 +133,21 @@ main (void) T (JERRY_BIN_OP_ADD, jerry_value_copy (obj1), jerry_value_copy (obj1), - jerry_string_sz ("[object Object][object Object]")), + jerry_sz_ascii ("[object Object][object Object]")), T (JERRY_BIN_OP_ADD, jerry_value_copy (obj1), jerry_value_copy (obj2), - jerry_string_sz ("[object Object][object Object]")), + jerry_sz_ascii ("[object Object][object Object]")), T (JERRY_BIN_OP_ADD, jerry_value_copy (obj2), jerry_value_copy (obj1), - jerry_string_sz ("[object Object][object Object]")), - T (JERRY_BIN_OP_ADD, jerry_value_copy (obj1), jerry_null (), jerry_string_sz ("[object Object]null")), - T (JERRY_BIN_OP_ADD, jerry_value_copy (obj1), jerry_undefined (), jerry_string_sz ("[object Object]undefined")), - T (JERRY_BIN_OP_ADD, jerry_value_copy (obj1), jerry_boolean (true), jerry_string_sz ("[object Object]true")), - T (JERRY_BIN_OP_ADD, jerry_value_copy (obj1), jerry_boolean (false), jerry_string_sz ("[object Object]false")), - T (JERRY_BIN_OP_ADD, jerry_value_copy (obj1), jerry_number (5.0), jerry_string_sz ("[object Object]5")), - T (JERRY_BIN_OP_ADD, jerry_value_copy (obj1), jerry_string_sz ("foo"), jerry_string_sz ("[object Object]foo")), + jerry_sz_ascii ("[object Object][object Object]")), + T (JERRY_BIN_OP_ADD, jerry_value_copy (obj1), jerry_null (), jerry_sz_ascii ("[object Object]null")), + T (JERRY_BIN_OP_ADD, jerry_value_copy (obj1), jerry_undefined (), jerry_sz_ascii ("[object Object]undefined")), + T (JERRY_BIN_OP_ADD, jerry_value_copy (obj1), jerry_boolean (true), jerry_sz_ascii ("[object Object]true")), + T (JERRY_BIN_OP_ADD, jerry_value_copy (obj1), jerry_boolean (false), jerry_sz_ascii ("[object Object]false")), + T (JERRY_BIN_OP_ADD, jerry_value_copy (obj1), jerry_number (5.0), jerry_sz_ascii ("[object Object]5")), + T (JERRY_BIN_OP_ADD, jerry_value_copy (obj1), jerry_sz_ascii ("foo"), jerry_sz_ascii ("[object Object]foo")), /* Testing subtraction (-) */ T (JERRY_BIN_OP_SUB, jerry_number (5.0), jerry_number (5.0), jerry_number (0.0)), @@ -221,7 +221,7 @@ main (void) T_ERR (JERRY_BIN_OP_ADD, jerry_value_copy (obj3), jerry_boolean (true)), T_ERR (JERRY_BIN_OP_ADD, jerry_value_copy (obj3), jerry_boolean (false)), T_ERR (JERRY_BIN_OP_ADD, jerry_value_copy (obj3), jerry_value_copy (obj2)), - T_ERR (JERRY_BIN_OP_ADD, jerry_value_copy (obj3), jerry_string_sz ("foo")), + T_ERR (JERRY_BIN_OP_ADD, jerry_value_copy (obj3), jerry_sz_ascii ("foo")), /* Testing subtraction (-), multiplication (*), division (/), remainder (%) */ T_ARI (jerry_value_copy (obj3), jerry_undefined ()), @@ -229,7 +229,7 @@ main (void) T_ARI (jerry_value_copy (obj3), jerry_boolean (true)), T_ARI (jerry_value_copy (obj3), jerry_boolean (false)), T_ARI (jerry_value_copy (obj3), jerry_value_copy (obj2)), - T_ARI (jerry_value_copy (obj3), jerry_string_sz ("foo")), + T_ARI (jerry_value_copy (obj3), jerry_sz_ascii ("foo")), }; for (uint32_t idx = 0; idx < sizeof (error_tests) / sizeof (test_error_entry_t); idx++) diff --git a/tests/unit-core/test-api-binary-operations-comparisons.c b/tests/unit-core/test-api-binary-operations-comparisons.c index 7feb322e1e..b5ad36dc55 100644 --- a/tests/unit-core/test-api-binary-operations-comparisons.c +++ b/tests/unit-core/test-api-binary-operations-comparisons.c @@ -39,7 +39,7 @@ main (void) jerry_value_t obj1 = jerry_eval ((const jerry_char_t *) "o={x:1};o", 9, JERRY_PARSE_NO_OPTS); jerry_value_t obj2 = jerry_eval ((const jerry_char_t *) "o={x:1};o", 9, JERRY_PARSE_NO_OPTS); - jerry_value_t err1 = jerry_throw_sz (JERRY_ERROR_SYNTAX, "error"); + jerry_value_t err1 = jerry_throw_sz (JERRY_ERROR_SYNTAX, jerry_sz_ascii ("error")); test_entry_t tests[] = { /* Testing strict equal comparison */ @@ -47,10 +47,10 @@ main (void) T (JERRY_BIN_OP_STRICT_EQUAL, jerry_number (3.1), jerry_number (10), false), T (JERRY_BIN_OP_STRICT_EQUAL, jerry_number (3.1), jerry_undefined (), false), T (JERRY_BIN_OP_STRICT_EQUAL, jerry_number (3.1), jerry_boolean (true), false), - T (JERRY_BIN_OP_STRICT_EQUAL, jerry_string_sz ("example string"), jerry_string_sz ("example string"), true), - T (JERRY_BIN_OP_STRICT_EQUAL, jerry_string_sz ("example string"), jerry_undefined (), false), - T (JERRY_BIN_OP_STRICT_EQUAL, jerry_string_sz ("example string"), jerry_null (), false), - T (JERRY_BIN_OP_STRICT_EQUAL, jerry_string_sz ("example string"), jerry_number (5.0), false), + T (JERRY_BIN_OP_STRICT_EQUAL, jerry_sz_ascii ("example string"), jerry_sz_ascii ("example string"), true), + T (JERRY_BIN_OP_STRICT_EQUAL, jerry_sz_ascii ("example string"), jerry_undefined (), false), + T (JERRY_BIN_OP_STRICT_EQUAL, jerry_sz_ascii ("example string"), jerry_null (), false), + T (JERRY_BIN_OP_STRICT_EQUAL, jerry_sz_ascii ("example string"), jerry_number (5.0), false), T (JERRY_BIN_OP_STRICT_EQUAL, jerry_undefined (), jerry_undefined (), true), T (JERRY_BIN_OP_STRICT_EQUAL, jerry_undefined (), jerry_null (), false), T (JERRY_BIN_OP_STRICT_EQUAL, jerry_null (), jerry_null (), true), @@ -66,17 +66,17 @@ main (void) T (JERRY_BIN_OP_STRICT_EQUAL, jerry_value_copy (obj1), jerry_boolean (true), false), T (JERRY_BIN_OP_STRICT_EQUAL, jerry_value_copy (obj1), jerry_boolean (false), false), T (JERRY_BIN_OP_STRICT_EQUAL, jerry_value_copy (obj1), jerry_number (5.0), false), - T (JERRY_BIN_OP_STRICT_EQUAL, jerry_value_copy (obj1), jerry_string_sz ("example string"), false), + T (JERRY_BIN_OP_STRICT_EQUAL, jerry_value_copy (obj1), jerry_sz_ascii ("example string"), false), /* Testing equal comparison */ T (JERRY_BIN_OP_EQUAL, jerry_number (5.0), jerry_number (5.0), true), T (JERRY_BIN_OP_EQUAL, jerry_number (3.1), jerry_number (10), false), T (JERRY_BIN_OP_EQUAL, jerry_number (3.1), jerry_undefined (), false), T (JERRY_BIN_OP_EQUAL, jerry_number (3.1), jerry_boolean (true), false), - T (JERRY_BIN_OP_EQUAL, jerry_string_sz ("example string"), jerry_string_sz ("example string"), true), - T (JERRY_BIN_OP_EQUAL, jerry_string_sz ("example string"), jerry_undefined (), false), - T (JERRY_BIN_OP_EQUAL, jerry_string_sz ("example string"), jerry_null (), false), - T (JERRY_BIN_OP_EQUAL, jerry_string_sz ("example string"), jerry_number (5.0), false), + T (JERRY_BIN_OP_EQUAL, jerry_sz_ascii ("example string"), jerry_sz_ascii ("example string"), true), + T (JERRY_BIN_OP_EQUAL, jerry_sz_ascii ("example string"), jerry_undefined (), false), + T (JERRY_BIN_OP_EQUAL, jerry_sz_ascii ("example string"), jerry_null (), false), + T (JERRY_BIN_OP_EQUAL, jerry_sz_ascii ("example string"), jerry_number (5.0), false), T (JERRY_BIN_OP_EQUAL, jerry_undefined (), jerry_undefined (), true), T (JERRY_BIN_OP_EQUAL, jerry_undefined (), jerry_null (), true), T (JERRY_BIN_OP_EQUAL, jerry_null (), jerry_null (), true), @@ -92,17 +92,17 @@ main (void) T (JERRY_BIN_OP_EQUAL, jerry_value_copy (obj1), jerry_boolean (true), false), T (JERRY_BIN_OP_EQUAL, jerry_value_copy (obj1), jerry_boolean (false), false), T (JERRY_BIN_OP_EQUAL, jerry_value_copy (obj1), jerry_number (5.0), false), - T (JERRY_BIN_OP_EQUAL, jerry_value_copy (obj1), jerry_string_sz ("example string"), false), + T (JERRY_BIN_OP_EQUAL, jerry_value_copy (obj1), jerry_sz_ascii ("example string"), false), /* Testing less comparison */ T (JERRY_BIN_OP_LESS, jerry_number (5.0), jerry_number (5.0), false), T (JERRY_BIN_OP_LESS, jerry_number (3.1), jerry_number (10), true), T (JERRY_BIN_OP_LESS, jerry_number (3.1), jerry_undefined (), false), T (JERRY_BIN_OP_LESS, jerry_number (3.1), jerry_boolean (true), false), - T (JERRY_BIN_OP_LESS, jerry_string_sz ("1"), jerry_string_sz ("2"), true), - T (JERRY_BIN_OP_LESS, jerry_string_sz ("1"), jerry_undefined (), false), - T (JERRY_BIN_OP_LESS, jerry_string_sz ("1"), jerry_null (), false), - T (JERRY_BIN_OP_LESS, jerry_string_sz ("1"), jerry_number (5.0), true), + T (JERRY_BIN_OP_LESS, jerry_sz_ascii ("1"), jerry_sz_ascii ("2"), true), + T (JERRY_BIN_OP_LESS, jerry_sz_ascii ("1"), jerry_undefined (), false), + T (JERRY_BIN_OP_LESS, jerry_sz_ascii ("1"), jerry_null (), false), + T (JERRY_BIN_OP_LESS, jerry_sz_ascii ("1"), jerry_number (5.0), true), T (JERRY_BIN_OP_LESS, jerry_undefined (), jerry_undefined (), false), T (JERRY_BIN_OP_LESS, jerry_undefined (), jerry_null (), false), T (JERRY_BIN_OP_LESS, jerry_null (), jerry_null (), false), @@ -117,12 +117,12 @@ main (void) T (JERRY_BIN_OP_LESS_EQUAL, jerry_number (3.1), jerry_number (10), true), T (JERRY_BIN_OP_LESS_EQUAL, jerry_number (3.1), jerry_undefined (), false), T (JERRY_BIN_OP_LESS_EQUAL, jerry_number (3.1), jerry_boolean (true), false), - T (JERRY_BIN_OP_LESS_EQUAL, jerry_string_sz ("1"), jerry_string_sz ("2"), true), - T (JERRY_BIN_OP_LESS_EQUAL, jerry_string_sz ("1"), jerry_string_sz ("1"), true), - T (JERRY_BIN_OP_LESS_EQUAL, jerry_string_sz ("1"), jerry_undefined (), false), - T (JERRY_BIN_OP_LESS_EQUAL, jerry_string_sz ("1"), jerry_null (), false), - T (JERRY_BIN_OP_LESS_EQUAL, jerry_string_sz ("1"), jerry_number (5.0), true), - T (JERRY_BIN_OP_LESS_EQUAL, jerry_string_sz ("5.0"), jerry_number (5.0), true), + T (JERRY_BIN_OP_LESS_EQUAL, jerry_sz_ascii ("1"), jerry_sz_ascii ("2"), true), + T (JERRY_BIN_OP_LESS_EQUAL, jerry_sz_ascii ("1"), jerry_sz_ascii ("1"), true), + T (JERRY_BIN_OP_LESS_EQUAL, jerry_sz_ascii ("1"), jerry_undefined (), false), + T (JERRY_BIN_OP_LESS_EQUAL, jerry_sz_ascii ("1"), jerry_null (), false), + T (JERRY_BIN_OP_LESS_EQUAL, jerry_sz_ascii ("1"), jerry_number (5.0), true), + T (JERRY_BIN_OP_LESS_EQUAL, jerry_sz_ascii ("5.0"), jerry_number (5.0), true), T (JERRY_BIN_OP_LESS_EQUAL, jerry_undefined (), jerry_undefined (), false), T (JERRY_BIN_OP_LESS_EQUAL, jerry_undefined (), jerry_null (), false), T (JERRY_BIN_OP_LESS_EQUAL, jerry_null (), jerry_null (), true), @@ -136,11 +136,11 @@ main (void) T (JERRY_BIN_OP_GREATER, jerry_number (10), jerry_number (3.1), true), T (JERRY_BIN_OP_GREATER, jerry_number (3.1), jerry_undefined (), false), T (JERRY_BIN_OP_GREATER, jerry_number (3.1), jerry_boolean (true), true), - T (JERRY_BIN_OP_GREATER, jerry_string_sz ("2"), jerry_string_sz ("1"), true), - T (JERRY_BIN_OP_GREATER, jerry_string_sz ("1"), jerry_string_sz ("2"), false), - T (JERRY_BIN_OP_GREATER, jerry_string_sz ("1"), jerry_undefined (), false), - T (JERRY_BIN_OP_GREATER, jerry_string_sz ("1"), jerry_null (), true), - T (JERRY_BIN_OP_GREATER, jerry_number (5.0), jerry_string_sz ("1"), true), + T (JERRY_BIN_OP_GREATER, jerry_sz_ascii ("2"), jerry_sz_ascii ("1"), true), + T (JERRY_BIN_OP_GREATER, jerry_sz_ascii ("1"), jerry_sz_ascii ("2"), false), + T (JERRY_BIN_OP_GREATER, jerry_sz_ascii ("1"), jerry_undefined (), false), + T (JERRY_BIN_OP_GREATER, jerry_sz_ascii ("1"), jerry_null (), true), + T (JERRY_BIN_OP_GREATER, jerry_number (5.0), jerry_sz_ascii ("1"), true), T (JERRY_BIN_OP_GREATER, jerry_undefined (), jerry_undefined (), false), T (JERRY_BIN_OP_GREATER, jerry_undefined (), jerry_null (), false), T (JERRY_BIN_OP_GREATER, jerry_null (), jerry_null (), false), @@ -155,12 +155,12 @@ main (void) T (JERRY_BIN_OP_GREATER_EQUAL, jerry_number (10), jerry_number (3.1), true), T (JERRY_BIN_OP_GREATER_EQUAL, jerry_number (3.1), jerry_undefined (), false), T (JERRY_BIN_OP_GREATER_EQUAL, jerry_number (3.1), jerry_boolean (true), true), - T (JERRY_BIN_OP_GREATER_EQUAL, jerry_string_sz ("2"), jerry_string_sz ("1"), true), - T (JERRY_BIN_OP_GREATER_EQUAL, jerry_string_sz ("1"), jerry_string_sz ("1"), true), - T (JERRY_BIN_OP_GREATER_EQUAL, jerry_string_sz ("1"), jerry_undefined (), false), - T (JERRY_BIN_OP_GREATER_EQUAL, jerry_string_sz ("1"), jerry_null (), true), - T (JERRY_BIN_OP_GREATER_EQUAL, jerry_number (5.0), jerry_string_sz ("1"), true), - T (JERRY_BIN_OP_GREATER_EQUAL, jerry_string_sz ("5.0"), jerry_number (5.0), true), + T (JERRY_BIN_OP_GREATER_EQUAL, jerry_sz_ascii ("2"), jerry_sz_ascii ("1"), true), + T (JERRY_BIN_OP_GREATER_EQUAL, jerry_sz_ascii ("1"), jerry_sz_ascii ("1"), true), + T (JERRY_BIN_OP_GREATER_EQUAL, jerry_sz_ascii ("1"), jerry_undefined (), false), + T (JERRY_BIN_OP_GREATER_EQUAL, jerry_sz_ascii ("1"), jerry_null (), true), + T (JERRY_BIN_OP_GREATER_EQUAL, jerry_number (5.0), jerry_sz_ascii ("1"), true), + T (JERRY_BIN_OP_GREATER_EQUAL, jerry_sz_ascii ("5.0"), jerry_number (5.0), true), T (JERRY_BIN_OP_GREATER_EQUAL, jerry_undefined (), jerry_undefined (), false), T (JERRY_BIN_OP_GREATER_EQUAL, jerry_undefined (), jerry_null (), false), T (JERRY_BIN_OP_GREATER_EQUAL, jerry_null (), jerry_null (), true), diff --git a/tests/unit-core/test-api-binary-operations-instanceof.c b/tests/unit-core/test-api-binary-operations-instanceof.c index 2a2043961d..1e59ce362c 100644 --- a/tests/unit-core/test-api-binary-operations-instanceof.c +++ b/tests/unit-core/test-api-binary-operations-instanceof.c @@ -52,7 +52,7 @@ main (void) jerry_value_t no_proto_instance_val = jerry_construct (constructor, NULL, 0); - jerry_value_t prototype_str = jerry_string_sz ("prototype"); + jerry_value_t prototype_str = jerry_sz_ascii ("prototype"); jerry_value_t res = jerry_object_set (constructor, prototype_str, base_obj); jerry_value_free (prototype_str); TEST_ASSERT (!jerry_value_is_exception (res)); @@ -82,14 +82,14 @@ main (void) T (jerry_value_copy (instance_val), jerry_value_copy (base_obj), true), T (jerry_value_copy (error), jerry_value_copy (constructor), true), T (jerry_value_copy (instance_val), jerry_value_copy (error), true), - T (jerry_string_sz (""), jerry_string_sz (""), true), - T (jerry_string_sz (""), jerry_number (5.0), true), - T (jerry_number (5.0), jerry_string_sz (""), true), + T (jerry_sz_ascii (""), jerry_sz_ascii (""), true), + T (jerry_sz_ascii (""), jerry_number (5.0), true), + T (jerry_number (5.0), jerry_sz_ascii (""), true), T (jerry_array (1), jerry_array (1), true), T (jerry_array (1), jerry_object (), true), T (jerry_object (), jerry_array (1), true), T (jerry_null (), jerry_object (), true), - T (jerry_object (), jerry_string_sz (""), true) }; + T (jerry_object (), jerry_sz_ascii (""), true) }; for (uint32_t idx = 0; idx < sizeof (error_tests) / sizeof (test_entry_t); idx++) { diff --git a/tests/unit-core/test-api-errortype.c b/tests/unit-core/test-api-errortype.c index 43327e8f2b..5125ca2f63 100644 --- a/tests/unit-core/test-api-errortype.c +++ b/tests/unit-core/test-api-errortype.c @@ -29,7 +29,7 @@ main (void) for (size_t idx = 0; idx < sizeof (errors) / sizeof (errors[0]); idx++) { - jerry_value_t error_obj = jerry_throw_sz (errors[idx], "test"); + jerry_value_t error_obj = jerry_throw_sz (errors[idx], jerry_sz_ascii ("test")); TEST_ASSERT (jerry_value_is_exception (error_obj)); TEST_ASSERT (jerry_error_type (error_obj) == errors[idx]); @@ -42,7 +42,7 @@ main (void) jerry_value_t test_values[] = { jerry_number (11), - jerry_string_sz ("message"), + jerry_sz_ascii ("message"), jerry_boolean (true), jerry_object (), }; diff --git a/tests/unit-core/test-api-functiontype.c b/tests/unit-core/test-api-functiontype.c index 6792e5a6fa..de3989c5e6 100644 --- a/tests/unit-core/test-api-functiontype.c +++ b/tests/unit-core/test-api-functiontype.c @@ -70,8 +70,8 @@ main (void) ENTRY (JERRY_FUNCTION_TYPE_NONE, jerry_boolean (true)), ENTRY (JERRY_FUNCTION_TYPE_NONE, jerry_undefined ()), ENTRY (JERRY_FUNCTION_TYPE_NONE, jerry_null ()), - ENTRY (JERRY_FUNCTION_TYPE_NONE, jerry_string_sz ("foo")), - ENTRY (JERRY_FUNCTION_TYPE_NONE, jerry_throw_sz (JERRY_ERROR_TYPE, "error")), + ENTRY (JERRY_FUNCTION_TYPE_NONE, jerry_sz_ascii ("foo")), + ENTRY (JERRY_FUNCTION_TYPE_NONE, jerry_throw_sz (JERRY_ERROR_TYPE, jerry_sz_ascii ("error"))), ENTRY (JERRY_FUNCTION_TYPE_NONE, jerry_object ()), ENTRY (JERRY_FUNCTION_TYPE_NONE, jerry_array (10)), diff --git a/tests/unit-core/test-api-iteratortype.c b/tests/unit-core/test-api-iteratortype.c index 262212ec15..21a38c072e 100644 --- a/tests/unit-core/test-api-iteratortype.c +++ b/tests/unit-core/test-api-iteratortype.c @@ -69,8 +69,8 @@ main (void) ENTRY (JERRY_ITERATOR_TYPE_NONE, jerry_boolean (true)), ENTRY (JERRY_ITERATOR_TYPE_NONE, jerry_undefined ()), ENTRY (JERRY_ITERATOR_TYPE_NONE, jerry_null ()), - ENTRY (JERRY_ITERATOR_TYPE_NONE, jerry_string_sz ("foo")), - ENTRY (JERRY_ITERATOR_TYPE_NONE, jerry_throw_sz (JERRY_ERROR_TYPE, "error")), + ENTRY (JERRY_ITERATOR_TYPE_NONE, jerry_sz_ascii ("foo")), + ENTRY (JERRY_ITERATOR_TYPE_NONE, jerry_throw_sz (JERRY_ERROR_TYPE, jerry_sz_ascii ("error"))), ENTRY (JERRY_ITERATOR_TYPE_NONE, jerry_object ()), ENTRY (JERRY_ITERATOR_TYPE_NONE, jerry_array (10)), diff --git a/tests/unit-core/test-api-object-property-names.c b/tests/unit-core/test-api-object-property-names.c index 2bbe08bb97..b5c735459b 100644 --- a/tests/unit-core/test-api-object-property-names.c +++ b/tests/unit-core/test-api-object-property-names.c @@ -18,14 +18,12 @@ #include "test-common.h" -static const char *prop_names[] = { "val1", "val2", "val3", "val4", "val5", "37", "symbol" }; - static jerry_char_t buffer[256] = { 0 }; +static jerry_char_t buffer_prop_name[256] = { 0 }; static void -create_and_set_property (const jerry_value_t object, const char *prop_name) +create_and_set_property (const jerry_value_t object, jerry_value_t jprop_name) { - jerry_value_t jprop_name = jerry_string_sz (prop_name); jerry_value_t ret_val = jerry_object_set (object, jprop_name, jerry_undefined ()); jerry_value_free (jprop_name); @@ -33,8 +31,16 @@ create_and_set_property (const jerry_value_t object, const char *prop_name) } /* create_and_set_property */ static void -compare_prop_name (const jerry_value_t object, const char *prop_name, uint32_t idx) +compare_prop_name (const jerry_value_t object, const jerry_value_t prop_name_value, uint32_t idx) { + { + jerry_size_t prop_name_size = jerry_string_size (prop_name_value, JERRY_ENCODING_CESU8); + TEST_ASSERT (prop_name_size < sizeof (buffer_prop_name)); + jerry_size_t ret_size = + jerry_string_to_buffer (prop_name_value, JERRY_ENCODING_CESU8, buffer_prop_name, prop_name_size); + TEST_ASSERT (prop_name_size == ret_size); + buffer_prop_name[prop_name_size] = '\0'; + } jerry_value_t name = jerry_object_get_index (object, idx); TEST_ASSERT (jerry_value_is_string (name) || jerry_value_is_number (name)); if (jerry_value_is_string (name)) @@ -44,23 +50,22 @@ compare_prop_name (const jerry_value_t object, const char *prop_name, uint32_t i jerry_size_t ret_size = jerry_string_to_buffer (name, JERRY_ENCODING_CESU8, buffer, sizeof (buffer)); TEST_ASSERT (name_size == ret_size); buffer[name_size] = '\0'; - TEST_ASSERT (strcmp ((const char *) buffer, prop_name) == 0); + TEST_ASSERT (strcmp ((const char *) buffer, (const char *) buffer_prop_name) == 0); } else { - TEST_ASSERT ((int) jerry_value_as_number (name) == atoi (prop_name)); + TEST_ASSERT ((int) jerry_value_as_number (name) == atoi ((const char *) buffer_prop_name)); } jerry_value_free (name); } /* compare_prop_name */ static void -define_property (const jerry_value_t object, - const char *prop_name, +define_property (const jerry_value_t object, /**< object value */ + jerry_value_t jname, /**< property name that will be free/take*/ jerry_property_descriptor_t *prop_desc_p, bool is_symbol) { - jerry_value_t jname = jerry_string_sz (prop_name); jerry_value_t ret_val; if (is_symbol) { @@ -87,6 +92,10 @@ main (void) TEST_ASSERT (jerry_value_is_exception (error_value) && jerry_error_type (error_value) == JERRY_ERROR_TYPE); jerry_value_free (error_value); + jerry_value_t prop_names[] = { jerry_sz_ascii ("val1"), jerry_sz_ascii ("val2"), jerry_sz_ascii ("val3"), + jerry_sz_ascii ("val4"), jerry_sz_ascii ("val5"), jerry_sz_ascii ("37"), + jerry_sz_ascii ("symbol") }; + jerry_value_t test_object = jerry_object (); create_and_set_property (test_object, prop_names[0]); create_and_set_property (test_object, prop_names[1]); diff --git a/tests/unit-core/test-api-objecttype.c b/tests/unit-core/test-api-objecttype.c index 0e35c8fc78..734699a03f 100644 --- a/tests/unit-core/test-api-objecttype.c +++ b/tests/unit-core/test-api-objecttype.c @@ -112,8 +112,8 @@ main (void) ENTRY (JERRY_OBJECT_TYPE_NONE, jerry_boolean (true)), ENTRY (JERRY_OBJECT_TYPE_NONE, jerry_undefined ()), ENTRY (JERRY_OBJECT_TYPE_NONE, jerry_null ()), - ENTRY (JERRY_OBJECT_TYPE_NONE, jerry_string_sz ("foo")), - ENTRY (JERRY_OBJECT_TYPE_NONE, jerry_throw_sz (JERRY_ERROR_TYPE, "error")), + ENTRY (JERRY_OBJECT_TYPE_NONE, jerry_sz_ascii ("foo")), + ENTRY (JERRY_OBJECT_TYPE_NONE, jerry_throw_sz (JERRY_ERROR_TYPE, jerry_sz_ascii ("error"))), ENTRY (JERRY_OBJECT_TYPE_GENERIC, jerry_object ()), ENTRY_IF (JERRY_OBJECT_TYPE_MODULE_NAMESPACE, test_namespace (module_parse_options), JERRY_FEATURE_MODULE), diff --git a/tests/unit-core/test-api-promise.c b/tests/unit-core/test-api-promise.c index 1b8d86bd8a..3612984216 100644 --- a/tests/unit-core/test-api-promise.c +++ b/tests/unit-core/test-api-promise.c @@ -36,7 +36,7 @@ test_promise_resolve_success (void) jerry_value_t resolve_value = jerry_object (); { - jerry_value_t obj_key = jerry_string_sz ("key_one"); + jerry_value_t obj_key = jerry_sz_ascii ("key_one"); jerry_value_t set_result = jerry_object_set (resolve_value, obj_key, jerry_number (3)); TEST_ASSERT (jerry_value_is_boolean (set_result) && (jerry_value_is_true (set_result))); jerry_value_free (set_result); @@ -53,7 +53,7 @@ test_promise_resolve_success (void) jerry_value_t promise_result = jerry_promise_result (my_promise); { TEST_ASSERT (jerry_value_is_object (promise_result)); - jerry_value_t obj_key = jerry_string_sz ("key_one"); + jerry_value_t obj_key = jerry_sz_ascii ("key_one"); jerry_value_t get_result = jerry_object_get (promise_result, obj_key); TEST_ASSERT (jerry_value_is_number (get_result)); TEST_ASSERT (jerry_value_as_number (get_result) == 3.0); @@ -77,7 +77,7 @@ test_promise_resolve_success (void) jerry_value_t promise_result = jerry_promise_result (my_promise); { TEST_ASSERT (jerry_value_is_object (promise_result)); - jerry_value_t obj_key = jerry_string_sz ("key_one"); + jerry_value_t obj_key = jerry_sz_ascii ("key_one"); jerry_value_t get_result = jerry_object_get (promise_result, obj_key); TEST_ASSERT (jerry_value_is_number (get_result)); TEST_ASSERT (jerry_value_as_number (get_result) == 3.0); @@ -115,7 +115,7 @@ test_promise_resolve_fail (void) // A resolved promise should have the result of from the resolve call and a fulfilled state { - jerry_value_t error_obj = jerry_error_sz (JERRY_ERROR_TYPE, "resolve_fail"); + jerry_value_t error_obj = jerry_error_sz (JERRY_ERROR_TYPE, jerry_sz_ascii ("resolve_fail")); jerry_value_t resolve_result = jerry_promise_reject (my_promise, error_obj); jerry_value_free (error_obj); diff --git a/tests/unit-core/test-api-property.c b/tests/unit-core/test-api-property.c index 2315003bb9..5019abd190 100644 --- a/tests/unit-core/test-api-property.c +++ b/tests/unit-core/test-api-property.c @@ -33,7 +33,7 @@ main (void) /* Test: define own properties */ jerry_value_t global_obj_val = jerry_current_realm (); - jerry_value_t prop_name = jerry_string_sz ("my_defined_property"); + jerry_value_t prop_name = jerry_sz_ascii ("my_defined_property"); prop_desc.flags |= JERRY_PROP_IS_VALUE_DEFINED; prop_desc.value = jerry_value_copy (prop_name); jerry_value_t res = jerry_object_define_own_prop (global_obj_val, prop_name, &prop_desc); @@ -96,7 +96,7 @@ main (void) /* Test: define and get own property descriptor */ prop_desc.flags |= JERRY_PROP_IS_ENUMERABLE; - prop_name = jerry_string_sz ("enumerable-property"); + prop_name = jerry_sz_ascii ("enumerable-property"); res = jerry_object_define_own_prop (global_obj_val, prop_name, &prop_desc); TEST_ASSERT (!jerry_value_is_exception (res)); TEST_ASSERT (jerry_value_is_boolean (res)); @@ -119,7 +119,7 @@ main (void) prop_desc.value = jerry_number (11); jerry_value_t obj_val = jerry_object (); - prop_name = jerry_string_sz ("property_key"); + prop_name = jerry_sz_ascii ("property_key"); res = jerry_object_define_own_prop (obj_val, prop_name, &prop_desc); TEST_ASSERT (!jerry_value_is_exception (res)); jerry_value_free (res); diff --git a/tests/unit-core/test-api-set-and-clear-error-flag.c b/tests/unit-core/test-api-set-and-clear-error-flag.c index e4fdb5a407..397c84d01d 100644 --- a/tests/unit-core/test-api-set-and-clear-error-flag.c +++ b/tests/unit-core/test-api-set-and-clear-error-flag.c @@ -44,17 +44,17 @@ main (void) jerry_value_free (err_val); jerry_value_free (obj_val); - const char pterodactylus[] = "Pterodactylus"; +#define pterodactylus "Pterodactylus" const size_t pterodactylus_size = sizeof (pterodactylus) - 1; - jerry_value_t str = jerry_string_sz (pterodactylus); + jerry_value_t str = jerry_sz_ascii (pterodactylus); jerry_value_t error = jerry_throw_value (str, true); str = jerry_exception_value (error, true); compare_str (str, pterodactylus, pterodactylus_size); jerry_value_free (str); - str = jerry_string_sz (pterodactylus); + str = jerry_sz_ascii (pterodactylus); error = jerry_throw_value (str, false); jerry_value_free (str); str = jerry_exception_value (error, true); @@ -62,14 +62,14 @@ main (void) compare_str (str, pterodactylus, pterodactylus_size); jerry_value_free (str); - str = jerry_string_sz (pterodactylus); + str = jerry_sz_ascii (pterodactylus); error = jerry_throw_abort (str, true); str = jerry_exception_value (error, true); compare_str (str, pterodactylus, pterodactylus_size); jerry_value_free (str); - str = jerry_string_sz (pterodactylus); + str = jerry_sz_ascii (pterodactylus); error = jerry_throw_abort (str, false); jerry_value_free (str); str = jerry_exception_value (error, true); @@ -77,7 +77,7 @@ main (void) compare_str (str, pterodactylus, pterodactylus_size); jerry_value_free (str); - str = jerry_string_sz (pterodactylus); + str = jerry_sz_ascii (pterodactylus); error = jerry_throw_value (str, true); error = jerry_throw_abort (error, true); TEST_ASSERT (jerry_value_is_abort (error)); @@ -86,7 +86,7 @@ main (void) compare_str (str, pterodactylus, pterodactylus_size); jerry_value_free (str); - str = jerry_string_sz (pterodactylus); + str = jerry_sz_ascii (pterodactylus); error = jerry_throw_value (str, true); jerry_value_t error2 = jerry_throw_abort (error, false); TEST_ASSERT (jerry_value_is_abort (error2)); diff --git a/tests/unit-core/test-api-strings.c b/tests/unit-core/test-api-strings.c index 4722a8e60a..876e1ecc06 100644 --- a/tests/unit-core/test-api-strings.c +++ b/tests/unit-core/test-api-strings.c @@ -44,7 +44,7 @@ main (void) jerry_init (JERRY_INIT_EMPTY); /* Test corner case for jerry_string_to_char_buffer */ - args[0] = jerry_string_sz (""); + args[0] = jerry_sz_ascii (""); sz = jerry_string_size (args[0], JERRY_ENCODING_CESU8); TEST_ASSERT (sz == 0); jerry_value_free (args[0]); @@ -143,7 +143,7 @@ main (void) /* Test string: '3' */ { - jerry_value_t test_str = jerry_string_sz ("3"); + jerry_value_t test_str = jerry_sz_ascii ("3"); char result_string[1] = { 'E' }; jerry_size_t copied_utf8 = jerry_string_to_buffer (test_str, JERRY_ENCODING_UTF8, (jerry_char_t *) result_string, sizeof (result_string)); diff --git a/tests/unit-core/test-api-value-type.c b/tests/unit-core/test-api-value-type.c index 9890c1eada..578c2779f9 100644 --- a/tests/unit-core/test-api-value-type.c +++ b/tests/unit-core/test-api-value-type.c @@ -47,7 +47,7 @@ main (void) jerry_init (JERRY_INIT_EMPTY); - const char test_eval_function[] = "function demo(a) { return a + 1; }; demo"; +#define test_eval_function "function demo(a) { return a + 1; }; demo" test_entry_t entries[] = { ENTRY (JERRY_TYPE_NUMBER, jerry_number (-33.0)), @@ -63,7 +63,7 @@ main (void) ENTRY (JERRY_TYPE_OBJECT, jerry_object ()), ENTRY (JERRY_TYPE_OBJECT, jerry_array (10)), - ENTRY (JERRY_TYPE_EXCEPTION, jerry_throw_sz (JERRY_ERROR_TYPE, "error")), + ENTRY (JERRY_TYPE_EXCEPTION, jerry_throw_sz (JERRY_ERROR_TYPE, jerry_sz_ascii ("error"))), ENTRY (JERRY_TYPE_NULL, jerry_null ()), @@ -71,8 +71,8 @@ main (void) jerry_eval ((jerry_char_t *) test_eval_function, sizeof (test_eval_function) - 1, JERRY_PARSE_NO_OPTS)), ENTRY (JERRY_TYPE_FUNCTION, jerry_function_external (test_ext_function)), - ENTRY (JERRY_TYPE_STRING, jerry_string_sz (test_eval_function)), - ENTRY (JERRY_TYPE_STRING, jerry_string_sz ("")), + ENTRY (JERRY_TYPE_STRING, jerry_sz_ascii (test_eval_function)), + ENTRY (JERRY_TYPE_STRING, jerry_sz_ascii ("")), }; for (size_t idx = 0; idx < sizeof (entries) / sizeof (entries[0]); idx++) @@ -85,7 +85,7 @@ main (void) jerry_value_free (entries[idx].value); } - jerry_value_t symbol_desc_value = jerry_string_sz ("foo"); + jerry_value_t symbol_desc_value = jerry_sz_ascii ("foo"); jerry_value_t symbol_value = jerry_symbol_with_description (symbol_desc_value); jerry_type_t type_info = jerry_value_type (symbol_value); diff --git a/tests/unit-core/test-api.c b/tests/unit-core/test-api.c index 6bdc0c4a66..09f2217fd2 100644 --- a/tests/unit-core/test-api.c +++ b/tests/unit-core/test-api.c @@ -85,7 +85,7 @@ handler (const jerry_call_info_t *call_info_p, /**< call information */ TEST_ASSERT (jerry_value_is_boolean (args_p[1])); - return jerry_string_sz ("string from handler"); + return jerry_sz_ascii ("string from handler"); } /* handler */ static jerry_value_t @@ -99,7 +99,7 @@ handler_throw_test (const jerry_call_info_t *call_info_p, /**< call information (void *) args_p, (unsigned int) args_cnt); - return jerry_throw_sz (JERRY_ERROR_TYPE, "error"); + return jerry_throw_sz (JERRY_ERROR_TYPE, jerry_sz_ascii ("error")); } /* handler_throw_test */ static void @@ -161,7 +161,7 @@ handler_construct (const jerry_call_info_t *call_info_p, /**< call information * TEST_ASSERT (jerry_value_is_true (args_p[0])); jerry_value_t this_value = call_info_p->this_value; - jerry_value_t field_name = jerry_string_sz ("value_field"); + jerry_value_t field_name = jerry_sz_ascii ("value_field"); jerry_value_t res = jerry_object_set (this_value, field_name, args_p[0]); TEST_ASSERT (!jerry_value_is_exception (res)); TEST_ASSERT (jerry_value_is_true (res)); @@ -310,9 +310,8 @@ foreach_subset (const jerry_value_t name, /**< field name */ static jerry_value_t get_property (const jerry_value_t obj_val, /**< object value */ - const char *str_p) /**< property name */ + jerry_value_t prop_name_val) /**< property name that will be free/take*/ { - jerry_value_t prop_name_val = jerry_string_sz (str_p); jerry_value_t ret_val = jerry_object_get (obj_val, prop_name_val); jerry_value_free (prop_name_val); return ret_val; @@ -320,10 +319,9 @@ get_property (const jerry_value_t obj_val, /**< object value */ static jerry_value_t set_property (const jerry_value_t obj_val, /**< object value */ - const char *str_p, /**< property name */ + jerry_value_t prop_name_val, /**< property name that will be free/take*/ const jerry_value_t val) /**< value to set */ { - jerry_value_t prop_name_val = jerry_string_sz (str_p); jerry_value_t ret_val = jerry_object_set (obj_val, prop_name_val, val); jerry_value_free (prop_name_val); return ret_val; @@ -394,18 +392,18 @@ main (void) global_obj_val = jerry_current_realm (); /* Get global.boo (non-existing field) */ - val_t = get_property (global_obj_val, "boo"); + val_t = get_property (global_obj_val, jerry_sz_ascii ("boo")); TEST_ASSERT (!jerry_value_is_exception (val_t)); TEST_ASSERT (jerry_value_is_undefined (val_t)); /* Get global.t */ - val_t = get_property (global_obj_val, "t"); + val_t = get_property (global_obj_val, jerry_sz_ascii ("t")); TEST_ASSERT (!jerry_value_is_exception (val_t)); TEST_ASSERT (jerry_value_is_number (val_t) && jerry_value_as_number (val_t) == 1.0); jerry_value_free (val_t); /* Get global.foo */ - val_foo = get_property (global_obj_val, "foo"); + val_foo = get_property (global_obj_val, jerry_sz_ascii ("foo")); TEST_ASSERT (!jerry_value_is_exception (val_foo)); TEST_ASSERT (jerry_value_is_object (val_foo)); @@ -418,7 +416,7 @@ main (void) jerry_value_free (res); /* Get global.bar */ - val_bar = get_property (global_obj_val, "bar"); + val_bar = get_property (global_obj_val, jerry_sz_ascii ("bar")); TEST_ASSERT (!jerry_value_is_exception (val_bar)); TEST_ASSERT (jerry_value_is_object (val_bar)); @@ -431,8 +429,8 @@ main (void) /* Set global.t = "abcd" */ jerry_value_free (args[0]); - args[0] = jerry_string_sz ("abcd"); - res = set_property (global_obj_val, "t", args[0]); + args[0] = jerry_sz_ascii ("abcd"); + res = set_property (global_obj_val, jerry_sz_ascii ("t"), args[0]); TEST_ASSERT (!jerry_value_is_exception (res)); TEST_ASSERT (jerry_value_is_true (res)); jerry_value_free (res); @@ -451,20 +449,20 @@ main (void) jerry_value_free (args[1]); /* Get global.A */ - val_A = get_property (global_obj_val, "A"); + val_A = get_property (global_obj_val, jerry_sz_ascii ("A")); TEST_ASSERT (!jerry_value_is_exception (val_A)); TEST_ASSERT (jerry_value_is_object (val_A)); /* Get A.prototype */ is_ok = jerry_value_is_constructor (val_A); TEST_ASSERT (is_ok); - val_A_prototype = get_property (val_A, "prototype"); + val_A_prototype = get_property (val_A, jerry_sz_ascii ("prototype")); TEST_ASSERT (!jerry_value_is_exception (val_A_prototype)); TEST_ASSERT (jerry_value_is_object (val_A_prototype)); jerry_value_free (val_A); /* Set A.prototype.foo = global.foo */ - res = set_property (val_A_prototype, "foo", val_foo); + res = set_property (val_A_prototype, jerry_sz_ascii ("foo"), val_foo); TEST_ASSERT (!jerry_value_is_exception (res)); TEST_ASSERT (jerry_value_is_true (res)); jerry_value_free (res); @@ -472,18 +470,18 @@ main (void) jerry_value_free (val_foo); /* Get global.a */ - val_a = get_property (global_obj_val, "a"); + val_a = get_property (global_obj_val, jerry_sz_ascii ("a")); TEST_ASSERT (!jerry_value_is_exception (val_a)); TEST_ASSERT (jerry_value_is_object (val_a)); /* Get a.t */ - res = get_property (val_a, "t"); + res = get_property (val_a, jerry_sz_ascii ("t")); TEST_ASSERT (!jerry_value_is_exception (res)); TEST_ASSERT (jerry_value_is_number (res) && jerry_value_as_number (res) == 12.0); jerry_value_free (res); /* foreach properties */ - val_p = get_property (global_obj_val, "p"); + val_p = get_property (global_obj_val, jerry_sz_ascii ("p")); is_ok = jerry_object_foreach (val_p, foreach, (void *) "user_data"); TEST_ASSERT (is_ok); @@ -495,13 +493,13 @@ main (void) jerry_value_free (val_p); /* foreach with throw test */ - val_np = get_property (global_obj_val, "np"); + val_np = get_property (global_obj_val, jerry_sz_ascii ("np")); is_ok = !jerry_object_foreach (val_np, foreach_exception, NULL); TEST_ASSERT (is_ok); jerry_value_free (val_np); /* Get a.foo */ - val_a_foo = get_property (val_a, "foo"); + val_a_foo = get_property (val_a, jerry_sz_ascii ("foo")); TEST_ASSERT (!jerry_value_is_exception (val_a_foo)); TEST_ASSERT (jerry_value_is_object (val_a_foo)); @@ -518,13 +516,13 @@ main (void) external_func_val = jerry_function_external (handler); TEST_ASSERT (jerry_value_is_function (external_func_val) && jerry_value_is_constructor (external_func_val)); - res = set_property (global_obj_val, "external", external_func_val); + res = set_property (global_obj_val, jerry_sz_ascii ("external"), external_func_val); TEST_ASSERT (!jerry_value_is_exception (res)); TEST_ASSERT (jerry_value_is_true (res)); jerry_value_free (external_func_val); /* Call 'call_external' function that should call external function created above */ - val_call_external = get_property (global_obj_val, "call_external"); + val_call_external = get_property (global_obj_val, jerry_sz_ascii ("call_external")); TEST_ASSERT (!jerry_value_is_exception (val_call_external)); TEST_ASSERT (jerry_value_is_object (val_call_external)); res = jerry_call (val_call_external, global_obj_val, NULL, 0); @@ -542,7 +540,7 @@ main (void) external_construct_val = jerry_function_external (handler_construct); TEST_ASSERT (jerry_value_is_function (external_construct_val) && jerry_value_is_constructor (external_construct_val)); - res = set_property (global_obj_val, "external_construct", external_construct_val); + res = set_property (global_obj_val, jerry_sz_ascii ("external_construct"), external_construct_val); TEST_ASSERT (!jerry_value_is_exception (res)); TEST_ASSERT (jerry_value_is_true (res)); jerry_value_free (res); @@ -552,7 +550,7 @@ main (void) res = jerry_construct (external_construct_val, args, 1); TEST_ASSERT (!jerry_value_is_exception (res)); TEST_ASSERT (jerry_value_is_object (res)); - val_value_field = get_property (res, "value_field"); + val_value_field = get_property (res, jerry_sz_ascii ("value_field")); /* Get 'value_field' of constructed object */ TEST_ASSERT (!jerry_value_is_exception (val_value_field)); @@ -584,13 +582,13 @@ main (void) throw_test_handler_val = jerry_function_external (handler_throw_test); TEST_ASSERT (jerry_value_is_function (throw_test_handler_val)); - res = set_property (global_obj_val, "throw_test", throw_test_handler_val); + res = set_property (global_obj_val, jerry_sz_ascii ("throw_test"), throw_test_handler_val); TEST_ASSERT (!jerry_value_is_exception (res)); TEST_ASSERT (jerry_value_is_true (res)); jerry_value_free (res); jerry_value_free (throw_test_handler_val); - val_t = get_property (global_obj_val, "call_throw_test"); + val_t = get_property (global_obj_val, jerry_sz_ascii ("call_throw_test")); TEST_ASSERT (!jerry_value_is_exception (val_t)); TEST_ASSERT (jerry_value_is_object (val_t)); @@ -600,7 +598,7 @@ main (void) jerry_value_free (res); /* Test: Unhandled exception in called function */ - val_t = get_property (global_obj_val, "throw_reference_error"); + val_t = get_property (global_obj_val, jerry_sz_ascii ("throw_reference_error")); TEST_ASSERT (!jerry_value_is_exception (val_t)); TEST_ASSERT (jerry_value_is_object (val_t)); @@ -627,7 +625,7 @@ main (void) jerry_value_free (obj_val); /* Test: Unhandled exception in function called, as constructor */ - val_t = get_property (global_obj_val, "throw_reference_error"); + val_t = get_property (global_obj_val, jerry_sz_ascii ("throw_reference_error")); TEST_ASSERT (!jerry_value_is_exception (val_t)); TEST_ASSERT (jerry_value_is_object (val_t)); @@ -805,11 +803,11 @@ main (void) jerry_value_free (val_t); /* Test: create function */ - jerry_value_t script_source = jerry_string_sz (" return 5 + a+\nb+c"); + jerry_value_t script_source = jerry_sz_ascii (" return 5 + a+\nb+c"); jerry_parse_options_t parse_options; parse_options.options = JERRY_PARSE_HAS_ARGUMENT_LIST; - parse_options.argument_list = jerry_string_sz ("a , b,c"); + parse_options.argument_list = jerry_sz_ascii ("a , b,c"); jerry_value_t func_val = jerry_parse_value (script_source, &parse_options); @@ -912,7 +910,7 @@ main (void) jerry_value_free (parse_result); jerry_value_t global_obj = jerry_current_realm (); - jerry_value_t prop_name = jerry_string_sz ("foo"); + jerry_value_t prop_name = jerry_sz_ascii ("foo"); jerry_property_descriptor_t prop_desc = jerry_property_descriptor (); prop_desc.flags |= JERRY_PROP_IS_VALUE_DEFINED; @@ -1005,7 +1003,7 @@ main (void) false); parse_options.options = JERRY_PARSE_HAS_SOURCE_NAME; - parse_options.source_name = jerry_string_sz ("filename.js"); + parse_options.source_name = jerry_sz_ascii ("filename.js"); test_syntax_error ("b = 'hello';\nvar a = (;", &parse_options, @@ -1045,9 +1043,9 @@ main (void) jerry_value_free (res); jerry_value_free (parsed_code_val); - /* call jerry_string_sz functions which will returns with the registered external magic strings */ - args[0] = jerry_string_sz ("console"); - args[1] = jerry_string_sz ("\xed\xa0\x80\xed\xb6\x8a"); /**< greek zero sign */ + /* call jerry_sz_ascii functions which will returns with the registered external magic strings */ + args[0] = jerry_sz_ascii ("console"); + args[1] = jerry_sz_cesu8 ("\xed\xa0\x80\xed\xb6\x8a"); /**< greek zero sign */ cesu8_length = jerry_string_length (args[0]); cesu8_sz = jerry_string_size (args[0], JERRY_ENCODING_CESU8); diff --git a/tests/unit-core/test-arraybuffer.c b/tests/unit-core/test-arraybuffer.c index c759d72ef0..a78979d9cc 100644 --- a/tests/unit-core/test-arraybuffer.c +++ b/tests/unit-core/test-arraybuffer.c @@ -22,12 +22,11 @@ * Register a JavaScript value in the global object. */ static void -register_js_value (const char *name_p, /**< name of the function */ +register_js_value (jerry_value_t name_val, /**< name of the function that will be free/take*/ jerry_value_t value) /**< JS value */ { jerry_value_t global_obj_val = jerry_current_realm (); - jerry_value_t name_val = jerry_string_sz (name_p); jerry_value_t result_val = jerry_object_set (global_obj_val, name_val, value); TEST_ASSERT (jerry_value_is_boolean (result_val)); @@ -105,7 +104,7 @@ test_write_with_offset (uint8_t offset) /**< offset for buffer write. */ { { jerry_value_t offset_val = jerry_number (offset); - register_js_value ("offset", offset_val); + register_js_value (jerry_sz_ascii ("offset"), offset_val); jerry_value_free (offset_val); } @@ -215,7 +214,7 @@ main (void) jerry_arraybuffer_allocator (test_allocate_cb, test_free_cb, (void *) &allocate_mode); jerry_value_t function_val = jerry_function_external (assert_handler); - register_js_value ("assert", function_val); + register_js_value (jerry_sz_ascii ("assert"), function_val); jerry_value_free (function_val); /* Test array buffer queries */ @@ -347,7 +346,7 @@ main (void) const uint32_t buffer_size = 20; jerry_value_t input_buffer = jerry_arraybuffer_external (NULL, buffer_size, (void *) &allocate_count); - register_js_value ("input_buffer", input_buffer); + register_js_value (jerry_sz_ascii ("input_buffer"), input_buffer); jerry_value_free (input_buffer); const jerry_char_t eval_arraybuffer_src[] = TEST_STRING_LITERAL ("var array = new Uint8Array(input_buffer);" diff --git a/tests/unit-core/test-backtrace.c b/tests/unit-core/test-backtrace.c index 5e77832b47..08a1ce7381 100644 --- a/tests/unit-core/test-backtrace.c +++ b/tests/unit-core/test-backtrace.c @@ -234,12 +234,11 @@ global_capture_handler (const jerry_call_info_t *call_info_p, /**< call informat static void register_callback (jerry_external_handler_t handler_p, /**< callback function */ - char *name_p) /**< name of the function */ + jerry_value_t name) /**< name of the function that will be free/take */ { jerry_value_t global = jerry_current_realm (); jerry_value_t func = jerry_function_external (handler_p); - jerry_value_t name = jerry_string_sz (name_p); jerry_value_t result = jerry_object_set (global, name, func); TEST_ASSERT (!jerry_value_is_exception (result)); @@ -251,15 +250,15 @@ register_callback (jerry_external_handler_t handler_p, /**< callback function */ } /* register_callback */ static jerry_value_t -run (const char *source_name_p, /**< source name */ +run (jerry_value_t source_name, /**< source name that will be free/take */ const char *source_p) /**< source code */ { jerry_parse_options_t parse_options; parse_options.options = JERRY_PARSE_HAS_SOURCE_NAME; - parse_options.source_name = jerry_string_sz (source_name_p); + parse_options.source_name = source_name; jerry_value_t code = jerry_parse ((const jerry_char_t *) source_p, strlen (source_p), &parse_options); - jerry_value_free (parse_options.source_name); + jerry_value_free (source_name); TEST_ASSERT (!jerry_value_is_exception (code)); jerry_value_t result = jerry_run (code); @@ -298,8 +297,8 @@ test_get_backtrace_api_call (void) { jerry_init (JERRY_INIT_EMPTY); - register_callback (backtrace_handler, "backtrace"); - register_callback (capture_handler, "capture"); + register_callback (backtrace_handler, jerry_sz_ascii ("backtrace")); + register_callback (capture_handler, jerry_sz_ascii ("capture")); const char *source_p = ("function f() {\n" " return backtrace(0);\n" @@ -315,7 +314,7 @@ test_get_backtrace_api_call (void) "\n" "h();\n"); - jerry_value_t backtrace = run ("something.js", source_p); + jerry_value_t backtrace = run (jerry_sz_ascii ("something.js"), source_p); TEST_ASSERT (!jerry_value_is_exception (backtrace) && jerry_value_is_array (backtrace)); @@ -344,7 +343,7 @@ test_get_backtrace_api_call (void) "\n" "h();\n"); - backtrace = run ("something_else.js", source_p); + backtrace = run (jerry_sz_ascii ("something_else.js"), source_p); TEST_ASSERT (!jerry_value_is_exception (backtrace) && jerry_value_is_array (backtrace)); @@ -373,7 +372,7 @@ test_get_backtrace_api_call (void) "\n" "h();\n"); - jerry_value_t result = run ("capture_test.js", source_p); + jerry_value_t result = run (jerry_sz_ascii ("capture_test.js"), source_p); TEST_ASSERT (jerry_value_is_undefined (result)); jerry_value_free (result); @@ -401,7 +400,7 @@ test_get_backtrace_api_call (void) "\n" "g();\n"); - result = run ("async_capture_test.js", source_p); + result = run (jerry_sz_ascii ("async_capture_test.js"), source_p); TEST_ASSERT (jerry_value_is_promise (result)); jerry_value_free (result); @@ -435,7 +434,7 @@ test_get_backtrace_api_call (void) "}\n" "new C;\n"); - result = run ("class_capture_test.js", source_p); + result = run (jerry_sz_ascii ("class_capture_test.js"), source_p); TEST_ASSERT (!jerry_value_is_exception (result)); TEST_ASSERT (frame_index == 2); @@ -447,7 +446,7 @@ test_get_backtrace_api_call (void) jerry_value_free (result); - register_callback (global_capture_handler, "global_capture"); + register_callback (global_capture_handler, jerry_sz_ascii ("global_capture")); frame_index = 0; @@ -483,7 +482,7 @@ test_exception_backtrace (void) "\n" "g();\n"); - jerry_value_t error = run ("bad.js", source); + jerry_value_t error = run (jerry_sz_ascii ("bad.js"), source); TEST_ASSERT (jerry_value_is_exception (error)); @@ -491,7 +490,7 @@ test_exception_backtrace (void) TEST_ASSERT (jerry_value_is_object (error)); - jerry_value_t name = jerry_string_sz ("stack"); + jerry_value_t name = jerry_sz_ascii ("stack"); jerry_value_t backtrace = jerry_object_get (error, name); jerry_value_free (name); @@ -529,7 +528,7 @@ test_large_line_count (void) "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" "g();\n"); - jerry_value_t error = run ("bad.js", source); + jerry_value_t error = run (jerry_sz_ascii ("bad.js"), source); TEST_ASSERT (jerry_value_is_exception (error)); @@ -537,7 +536,7 @@ test_large_line_count (void) TEST_ASSERT (jerry_value_is_object (error)); - jerry_value_t name = jerry_string_sz ("stack"); + jerry_value_t name = jerry_sz_ascii ("stack"); jerry_value_t backtrace = jerry_object_get (error, name); jerry_value_free (name); diff --git a/tests/unit-core/test-bigint.c b/tests/unit-core/test-bigint.c index 640da0bd95..d23f95deb3 100644 --- a/tests/unit-core/test-bigint.c +++ b/tests/unit-core/test-bigint.c @@ -28,7 +28,7 @@ main (void) jerry_init (JERRY_INIT_EMPTY); - jerry_value_t string = jerry_string_sz ("0xfffffff1fffffff2fffffff3"); + jerry_value_t string = jerry_sz_ascii ("0xfffffff1fffffff2fffffff3"); TEST_ASSERT (!jerry_value_is_exception (string)); jerry_value_t bigint = jerry_value_to_bigint (string); diff --git a/tests/unit-core/test-common.h b/tests/unit-core/test-common.h index b6d83491c5..b34166d96c 100644 --- a/tests/unit-core/test-common.h +++ b/tests/unit-core/test-common.h @@ -25,8 +25,6 @@ #include "jerryscript-port.h" -#define JERRY_UNUSED(x) ((void) (x)) - #define TEST_ASSERT(x) \ do \ { \ diff --git a/tests/unit-core/test-container-operation.c b/tests/unit-core/test-container-operation.c index a641ea91a6..4e5e69894a 100644 --- a/tests/unit-core/test-container-operation.c +++ b/tests/unit-core/test-container-operation.c @@ -34,7 +34,7 @@ main (void) jerry_value_t map = jerry_container (JERRY_CONTAINER_TYPE_MAP, NULL, 0); TEST_ASSERT (jerry_container_type (map) == JERRY_CONTAINER_TYPE_MAP); - jerry_value_t key_str = jerry_string_sz ("number"); + jerry_value_t key_str = jerry_sz_ascii ("number"); jerry_value_t number = jerry_number (10); jerry_value_t args[2] = { key_str, number }; jerry_value_t result = jerry_container_op (JERRY_CONTAINER_OP_SET, map, args, 2); @@ -53,7 +53,7 @@ main (void) TEST_ASSERT (jerry_value_as_number (result) == 1); jerry_value_free (result); - key_str = jerry_string_sz ("number2"); + key_str = jerry_sz_ascii ("number2"); number = jerry_number (11); jerry_value_t args2[2] = { key_str, number }; result = jerry_container_op (JERRY_CONTAINER_OP_SET, map, args2, 2); @@ -164,8 +164,7 @@ main (void) jerry_value_free (result); // arguments is a error - const char *const error_message_p = "Random error."; - jerry_value_t error_val = jerry_throw_sz (JERRY_ERROR_RANGE, error_message_p); + jerry_value_t error_val = jerry_throw_sz (JERRY_ERROR_RANGE, jerry_sz_ascii ("Random error.")); jerry_value_t args3[2] = { error_val, error_val }; result = jerry_container_op (JERRY_CONTAINER_OP_SET, map, args3, 2); TEST_ASSERT (jerry_value_is_exception (result)); diff --git a/tests/unit-core/test-container.c b/tests/unit-core/test-container.c index 389a8a6b7b..86d0c3caec 100644 --- a/tests/unit-core/test-container.c +++ b/tests/unit-core/test-container.c @@ -84,17 +84,17 @@ main (void) jerry_value_t instance_check; jerry_value_t global = jerry_current_realm (); - jerry_value_t map_str = jerry_string_sz ("Map"); - jerry_value_t set_str = jerry_string_sz ("Set"); - jerry_value_t weakmap_str = jerry_string_sz ("WeakMap"); - jerry_value_t weakset_str = jerry_string_sz ("WeakSet"); + jerry_value_t map_str = jerry_sz_ascii ("Map"); + jerry_value_t set_str = jerry_sz_ascii ("Set"); + jerry_value_t weakmap_str = jerry_sz_ascii ("WeakMap"); + jerry_value_t weakset_str = jerry_sz_ascii ("WeakSet"); jerry_value_t global_map = jerry_object_get (global, map_str); jerry_value_t global_set = jerry_object_get (global, set_str); jerry_value_t global_weakmap = jerry_object_get (global, weakmap_str); jerry_value_t global_weakset = jerry_object_get (global, weakset_str); jerry_value_t function = jerry_function_external (create_array_from_container_handler); - jerry_value_t name = jerry_string_sz ("create_array_from_container"); + jerry_value_t name = jerry_sz_ascii ("create_array_from_container"); jerry_value_t res = jerry_object_set (global, name, function); TEST_ASSERT (!jerry_value_is_exception (res)); diff --git a/tests/unit-core/test-dataview.c b/tests/unit-core/test-dataview.c index d392bb799f..05f4767c49 100644 --- a/tests/unit-core/test-dataview.c +++ b/tests/unit-core/test-dataview.c @@ -73,8 +73,8 @@ main (void) /* Test getting/setting values */ jerry_value_t global_obj = jerry_current_realm (); - jerry_value_t view1_str = jerry_string_sz ("view1"); - jerry_value_t view2_str = jerry_string_sz ("view2"); + jerry_value_t view1_str = jerry_sz_ascii ("view1"); + jerry_value_t view2_str = jerry_sz_ascii ("view2"); TEST_ASSERT (jerry_object_set (global_obj, view1_str, view1)); TEST_ASSERT (jerry_object_set (global_obj, view2_str, view2)); diff --git a/tests/unit-core/test-error-callback.c b/tests/unit-core/test-error-callback.c index 3fe32d92d9..b9cdcb7a8c 100644 --- a/tests/unit-core/test-error-callback.c +++ b/tests/unit-core/test-error-callback.c @@ -30,8 +30,8 @@ error_object_created_callback (const jerry_value_t error_object_t, /**< new erro error_object_created_callback_is_running = true; error_object_created_callback_count++; - jerry_value_t name = jerry_string_sz ("message"); - jerry_value_t message = jerry_string_sz ("Replaced message!"); + jerry_value_t name = jerry_sz_ascii ("message"); + jerry_value_t message = jerry_sz_ascii ("Replaced message!"); jerry_value_t result = jerry_object_set (error_object_t, name, message); TEST_ASSERT (jerry_value_is_boolean (result) && jerry_value_is_true (result)); @@ -84,7 +84,7 @@ main (void) run_test ("var error = new Error()\n" "error.message === 'Replaced message!'\n"); - jerry_value_free (jerry_error_sz (JERRY_ERROR_COMMON, "Message")); + jerry_value_free (jerry_error_sz (JERRY_ERROR_COMMON, jerry_sz_ascii ("Message"))); TEST_ASSERT (error_object_created_callback_count == 11); diff --git a/tests/unit-core/test-external-string.c b/tests/unit-core/test-external-string.c index 3de28ebfb8..1a9cacfd3e 100644 --- a/tests/unit-core/test-external-string.c +++ b/tests/unit-core/test-external-string.c @@ -19,17 +19,17 @@ static int free_count = 0; -static const char *external_1 = "External string! External string! External string! External string!"; -static const char *external_2 = "Object"; -static const char *external_3 = "x!?:s"; -static const char *external_4 = "Object property external string! Object property external string!"; +#define external_1 "External string! External string! External string! External string!" +#define external_2 "Object" +#define external_3 "x!?:s" +#define external_4 "Object property external string! Object property external string!" static void external_string_free_callback_1 (jerry_char_t *string_p, /**< string pointer */ jerry_size_t string_size, /**< size of the string */ void *user_p) /**< user pointer */ { - TEST_ASSERT ((const char *) string_p == external_1); + TEST_ASSERT (strcmp ((char *) string_p, external_1) == 0); TEST_ASSERT (string_size == strlen (external_1)); TEST_ASSERT (user_p == NULL); free_count++; @@ -40,7 +40,7 @@ external_string_free_callback_2 (jerry_char_t *string_p, /**< string pointer */ jerry_size_t string_size, /**< size of the string */ void *user_p) /**< user pointer */ { - TEST_ASSERT ((const char *) string_p == external_2); + TEST_ASSERT (strcmp ((char *) string_p, external_2) == 0); TEST_ASSERT (string_size == strlen (external_2)); TEST_ASSERT (user_p == (void *) &free_count); free_count++; @@ -51,7 +51,7 @@ external_string_free_callback_3 (jerry_char_t *string_p, /**< string pointer */ jerry_size_t string_size, /**< size of the string */ void *user_p) /**< user pointer */ { - TEST_ASSERT ((const char *) string_p == external_3); + TEST_ASSERT (strcmp ((char *) string_p, external_3) == 0); TEST_ASSERT (string_size == strlen (external_3)); TEST_ASSERT (user_p == (void *) string_p); free_count++; @@ -68,7 +68,7 @@ main (void) /* Test external callback calls. */ jerry_string_external_on_free (external_string_free_callback_1); - jerry_value_t external_string = jerry_string_external_sz (external_1, NULL); + jerry_value_t external_string = jerry_string_external (JERRY_ZSTR_ARG (external_1), NULL); TEST_ASSERT (free_count == 0); TEST_ASSERT (jerry_string_user_ptr (external_string, &is_external) == NULL); TEST_ASSERT (is_external); @@ -77,7 +77,7 @@ main (void) TEST_ASSERT (free_count == 1); jerry_string_external_on_free (NULL); - external_string = jerry_string_external_sz (external_1, (void *) &free_count); + external_string = jerry_string_external (JERRY_ZSTR_ARG (external_1), (void *) &free_count); TEST_ASSERT (free_count == 1); TEST_ASSERT (jerry_string_user_ptr (external_string, &is_external) == (void *) &free_count); TEST_ASSERT (is_external); @@ -86,7 +86,7 @@ main (void) TEST_ASSERT (free_count == 1); jerry_string_external_on_free (external_string_free_callback_2); - external_string = jerry_string_external_sz (external_2, (void *) &free_count); + external_string = jerry_string_external (JERRY_ZSTR_ARG (external_2), (void *) &free_count); TEST_ASSERT (free_count == 2); TEST_ASSERT (jerry_string_user_ptr (external_string, &is_external) == NULL); TEST_ASSERT (!is_external); @@ -94,7 +94,7 @@ main (void) TEST_ASSERT (free_count == 2); jerry_string_external_on_free (NULL); - external_string = jerry_string_external_sz (external_2, (void *) &free_count); + external_string = jerry_string_external (JERRY_ZSTR_ARG (external_2), (void *) &free_count); TEST_ASSERT (free_count == 2); TEST_ASSERT (jerry_string_user_ptr (external_string, &is_external) == NULL); TEST_ASSERT (!is_external); @@ -102,7 +102,8 @@ main (void) TEST_ASSERT (free_count == 2); jerry_string_external_on_free (external_string_free_callback_3); - external_string = jerry_string_external_sz (external_3, (void *) external_3); + jerry_string_t str = { JERRY_ZSTR_ARG (external_3) }; + external_string = jerry_string_external (str.ptr, str.size, (void *) str.ptr); TEST_ASSERT (free_count == 3); TEST_ASSERT (jerry_string_user_ptr (external_string, &is_external) == NULL); TEST_ASSERT (!is_external); @@ -110,7 +111,7 @@ main (void) TEST_ASSERT (free_count == 3); jerry_string_external_on_free (NULL); - external_string = jerry_string_external_sz (external_3, (void *) external_3); + external_string = jerry_string_external (JERRY_ZSTR_ARG (external_3), (void *) external_3); TEST_ASSERT (free_count == 3); TEST_ASSERT (jerry_string_user_ptr (external_string, &is_external) == NULL); TEST_ASSERT (!is_external); @@ -119,8 +120,8 @@ main (void) /* Test string comparison. */ jerry_string_external_on_free (external_string_free_callback_1); - external_string = jerry_string_external_sz (external_1, NULL); - jerry_value_t other_string = jerry_string_sz (external_1); + external_string = jerry_string_external (JERRY_ZSTR_ARG (external_1), NULL); + jerry_value_t other_string = jerry_sz_ascii (external_1); jerry_value_t result = jerry_binary_op (JERRY_BIN_OP_STRICT_EQUAL, external_string, other_string); TEST_ASSERT (jerry_value_is_boolean (result)); @@ -139,7 +140,7 @@ main (void) /* Test getting string. */ jerry_string_external_on_free (external_string_free_callback_1); - external_string = jerry_string_external_sz (external_1, NULL); + external_string = jerry_string_external (JERRY_ZSTR_ARG (external_1), NULL); size_t length = strlen (external_1); TEST_ASSERT (jerry_value_is_string (external_string)); @@ -156,8 +157,8 @@ main (void) /* Test property access. */ jerry_string_external_on_free (NULL); - external_string = jerry_string_external_sz (external_4, NULL); - other_string = jerry_string_sz (external_4); + external_string = jerry_string_external (JERRY_ZSTR_ARG (external_4), NULL); + other_string = jerry_sz_ascii (external_4); jerry_value_t obj = jerry_object (); result = jerry_object_set (obj, external_string, other_string); diff --git a/tests/unit-core/test-from-property-descriptor.c b/tests/unit-core/test-from-property-descriptor.c index 6134c0c7b4..f3382f3cdf 100644 --- a/tests/unit-core/test-from-property-descriptor.c +++ b/tests/unit-core/test-from-property-descriptor.c @@ -26,7 +26,7 @@ main (void) jerry_init (JERRY_INIT_EMPTY); jerry_value_t object = jerry_object (); - jerry_value_t prop_name = jerry_string_sz ("length"); + jerry_value_t prop_name = jerry_sz_ascii ("length"); jerry_value_t value = jerry_boolean (true); TEST_ASSERT (jerry_object_set (object, prop_name, prop_name)); @@ -38,19 +38,19 @@ main (void) jerry_value_t from_object = jerry_property_descriptor_to_object (&prop_desc); - prop_name = jerry_string_sz ("value"); + prop_name = jerry_sz_ascii ("value"); value = jerry_object_get (from_object, prop_name); TEST_ASSERT (value == prop_desc.value); - prop_name = jerry_string_sz ("writable"); + prop_name = jerry_sz_ascii ("writable"); value = jerry_object_get (from_object, prop_name); TEST_ASSERT (jerry_value_is_true (value) == ((prop_desc.flags & JERRY_PROP_IS_WRITABLE) != 0)); - prop_name = jerry_string_sz ("enumerable"); + prop_name = jerry_sz_ascii ("enumerable"); value = jerry_object_get (from_object, prop_name); TEST_ASSERT (jerry_value_is_true (value) == ((prop_desc.flags & JERRY_PROP_IS_ENUMERABLE) != 0)); - prop_name = jerry_string_sz ("configurable"); + prop_name = jerry_sz_ascii ("configurable"); value = jerry_object_get (from_object, prop_name); TEST_ASSERT (jerry_value_is_true (value) == ((prop_desc.flags & JERRY_PROP_IS_CONFIGURABLE) != 0)); diff --git a/tests/unit-core/test-get-own-property.c b/tests/unit-core/test-get-own-property.c index 4802dffc44..40b08ee524 100644 --- a/tests/unit-core/test-get-own-property.c +++ b/tests/unit-core/test-get-own-property.c @@ -47,9 +47,9 @@ main (void) TEST_INIT (); jerry_init (JERRY_INIT_EMPTY); - jerry_value_t pp_string = jerry_string_sz ("pp"); - jerry_value_t qq_string = jerry_string_sz ("qq"); - jerry_value_t rr_string = jerry_string_sz ("rr"); + jerry_value_t pp_string = jerry_sz_ascii ("pp"); + jerry_value_t qq_string = jerry_sz_ascii ("qq"); + jerry_value_t rr_string = jerry_sz_ascii ("rr"); jerry_value_t object = create_object ("'use strict';\n" "({ pp:'A', get qq() { return 'B' } })"); diff --git a/tests/unit-core/test-has-property.c b/tests/unit-core/test-has-property.c index cb1993b30c..3abd35e4df 100644 --- a/tests/unit-core/test-has-property.c +++ b/tests/unit-core/test-has-property.c @@ -34,7 +34,7 @@ main (void) jerry_init (JERRY_INIT_EMPTY); jerry_value_t object = jerry_object (); - jerry_value_t prop_name = jerry_string_sz ("something"); + jerry_value_t prop_name = jerry_sz_ascii ("something"); jerry_value_t prop_value = jerry_boolean (true); jerry_value_t proto_object = jerry_object (); diff --git a/tests/unit-core/test-internal-properties.c b/tests/unit-core/test-internal-properties.c index 2995a91694..dc1c4f4689 100644 --- a/tests/unit-core/test-internal-properties.c +++ b/tests/unit-core/test-internal-properties.c @@ -25,19 +25,19 @@ main (void) jerry_value_t object = jerry_object (); - jerry_value_t prop_name_1 = jerry_string_sz ("foo"); - jerry_value_t prop_name_2 = jerry_string_sz ("non_hidden_prop"); + jerry_value_t prop_name_1 = jerry_sz_ascii ("foo"); + jerry_value_t prop_name_2 = jerry_sz_ascii ("non_hidden_prop"); jerry_value_t prop_name_3; - jerry_value_t prop_name_3_desc = jerry_string_sz ("bar"); + jerry_value_t prop_name_3_desc = jerry_sz_ascii ("bar"); prop_name_3 = jerry_symbol_with_description (prop_name_3_desc); jerry_value_free (prop_name_3_desc); - jerry_value_t internal_prop_name_1 = jerry_string_sz ("hidden_foo"); - jerry_value_t internal_prop_name_2 = jerry_string_sz ("hidden_prop"); + jerry_value_t internal_prop_name_1 = jerry_sz_ascii ("hidden_foo"); + jerry_value_t internal_prop_name_2 = jerry_sz_ascii ("hidden_prop"); jerry_value_t internal_prop_name_3; - jerry_value_t internal_prop_name_3_desc = jerry_string_sz ("bar"); + jerry_value_t internal_prop_name_3_desc = jerry_sz_ascii ("bar"); internal_prop_name_3 = jerry_symbol_with_description (internal_prop_name_3_desc); jerry_value_free (internal_prop_name_3_desc); diff --git a/tests/unit-core/test-is-eval-code.c b/tests/unit-core/test-is-eval-code.c index 13daa21623..bc159c8387 100644 --- a/tests/unit-core/test-is-eval-code.c +++ b/tests/unit-core/test-is-eval-code.c @@ -73,7 +73,7 @@ main (void) jerry_value_t global_object_value = jerry_current_realm (); jerry_value_t function_value = jerry_function_external (check_eval); - jerry_value_t function_name_value = jerry_string_sz ("check_eval"); + jerry_value_t function_name_value = jerry_sz_ascii ("check_eval"); jerry_value_free (jerry_object_set (global_object_value, function_name_value, function_value)); jerry_value_free (function_name_value); @@ -94,7 +94,7 @@ main (void) } parse_options.options = JERRY_PARSE_HAS_ARGUMENT_LIST; - parse_options.argument_list = jerry_string_sz (""); + parse_options.argument_list = jerry_sz_ascii (""); test_parse (source_p, &parse_options); jerry_value_free (parse_options.argument_list); diff --git a/tests/unit-core/test-json.c b/tests/unit-core/test-json.c index 0d1eef8058..1385187e04 100644 --- a/tests/unit-core/test-json.c +++ b/tests/unit-core/test-json.c @@ -27,7 +27,7 @@ custom_to_json (const jerry_call_info_t *call_info_p, /**< call information */ JERRY_UNUSED (args_p); JERRY_UNUSED (args_cnt); - return jerry_throw_sz (JERRY_ERROR_URI, "Error"); + return jerry_throw_sz (JERRY_ERROR_URI, jerry_sz_ascii ("Error")); } /* custom_to_json */ int @@ -43,7 +43,7 @@ main (void) jerry_value_t parsed_json = jerry_json_parse (data, sizeof (data) - 1); /* Check "name" property values */ - jerry_value_t name_key = jerry_string_sz ("name"); + jerry_value_t name_key = jerry_sz_ascii ("name"); jerry_value_t has_name = jerry_object_has (parsed_json, name_key); TEST_ASSERT (jerry_value_is_true (has_name)); @@ -65,7 +65,7 @@ main (void) jerry_value_free (name_key); /* Check "age" property values */ - jerry_value_t age_key = jerry_string_sz ("age"); + jerry_value_t age_key = jerry_sz_ascii ("age"); jerry_value_t has_age = jerry_object_has (parsed_json, age_key); TEST_ASSERT (jerry_value_is_true (has_age)); @@ -89,7 +89,7 @@ main (void) jerry_value_t parsed_cesu8 = jerry_json_parse (cesu8, sizeof (cesu8) - 1); jerry_value_t parsed_utf8 = jerry_json_parse (utf8, sizeof (utf8) - 1); - jerry_value_t key = jerry_string_sz ("ch"); + jerry_value_t key = jerry_sz_ascii ("ch"); jerry_value_t char_cesu8 = jerry_object_get (parsed_cesu8, key); jerry_value_t char_utf8 = jerry_object_get (parsed_utf8, key); jerry_value_free (key); @@ -121,8 +121,8 @@ main (void) jerry_value_t obj = jerry_object (); /* Fill "obj" with data */ { - jerry_value_t name_key = jerry_string_sz ("name"); - jerry_value_t name_value = jerry_string_sz ("John"); + jerry_value_t name_key = jerry_sz_ascii ("name"); + jerry_value_t name_value = jerry_sz_ascii ("John"); jerry_value_t name_set = jerry_object_set (obj, name_key, name_value); TEST_ASSERT (!jerry_value_is_exception (name_set)); TEST_ASSERT (jerry_value_is_boolean (name_set)); @@ -132,7 +132,7 @@ main (void) jerry_value_free (name_set); } { - jerry_value_t age_key = jerry_string_sz ("age"); + jerry_value_t age_key = jerry_sz_ascii ("age"); jerry_value_t age_value = jerry_number (32); jerry_value_t age_set = jerry_object_set (obj, age_key, age_value); TEST_ASSERT (!jerry_value_is_exception (age_set)); @@ -165,7 +165,7 @@ main (void) jerry_value_t obj = jerry_object (); /* Fill "obj" with data */ { - jerry_value_t name_key = jerry_string_sz ("toJSON"); + jerry_value_t name_key = jerry_sz_ascii ("toJSON"); jerry_value_t name_value = jerry_function_external (custom_to_json); jerry_value_t name_set = jerry_object_set (obj, name_key, name_value); TEST_ASSERT (!jerry_value_is_exception (name_set)); diff --git a/tests/unit-core/test-literal-storage.c b/tests/unit-core/test-literal-storage.c index b52863383b..ecdf594f14 100644 --- a/tests/unit-core/test-literal-storage.c +++ b/tests/unit-core/test-literal-storage.c @@ -89,7 +89,7 @@ main (void) lit_magic_string_id_t msi = (lit_magic_string_id_t) (rand () % LIT_NON_INTERNAL_MAGIC_STRING__COUNT); ptrs[j] = lit_get_magic_string_utf8 (msi); TEST_ASSERT (ptrs[j]); - lengths[j] = (lit_utf8_size_t) lit_zt_utf8_string_size (ptrs[j]); + lengths[j] = lit_get_magic_string_size (msi); ecma_find_or_create_literal_string (ptrs[j], lengths[j], false); } else diff --git a/tests/unit-core/test-module-dynamic.c b/tests/unit-core/test-module-dynamic.c index ee99ed3fbf..2d80e42bb1 100644 --- a/tests/unit-core/test-module-dynamic.c +++ b/tests/unit-core/test-module-dynamic.c @@ -38,7 +38,7 @@ register_assert (void) jerry_value_t global_object_value = jerry_current_realm (); jerry_value_t function_value = jerry_function_external (global_assert); - jerry_value_t function_name_value = jerry_string_sz ("assert"); + jerry_value_t function_name_value = jerry_sz_ascii ("assert"); jerry_value_t result_value = jerry_object_set (global_object_value, function_name_value, function_value); jerry_value_free (function_name_value); @@ -90,7 +90,7 @@ module_import_callback (const jerry_value_t specifier, /* string value */ case 0: { compare_specifier (specifier, 1); - return jerry_throw_sz (JERRY_ERROR_RANGE, "Err01"); + return jerry_throw_sz (JERRY_ERROR_RANGE, jerry_sz_ascii ("Err01")); } case 1: { @@ -113,7 +113,7 @@ module_import_callback (const jerry_value_t specifier, /* string value */ compare_specifier (specifier, 28); TEST_ASSERT (jerry_value_is_object (user_value)); - jerry_value_t property_name = jerry_string_sz ("MyProp1"); + jerry_value_t property_name = jerry_sz_ascii ("MyProp1"); jerry_value_t result = jerry_object_get (user_value, property_name); TEST_ASSERT (jerry_value_is_number (result) && jerry_value_as_number (result) == 3.5); jerry_value_free (result); @@ -259,7 +259,7 @@ main (void) run_script (source_p, &parse_options, false); jerry_value_free (global_user_value); - global_user_value = jerry_string_sz ("Any string..."); + global_user_value = jerry_sz_ascii ("Any string..."); source_p = TEST_STRING_LITERAL ("var src = \"import('02_module.mjs').then(\\\n" " function(resolve) { assert(typeof resolve == 'object') },\\\n" " function(reject) { assert(false) }\\\n" @@ -274,7 +274,7 @@ main (void) mode = 3; parse_options.options = JERRY_PARSE_HAS_USER_VALUE | (i == 1 ? JERRY_PARSE_MODULE : 0); parse_options.user_value = jerry_object (); - jerry_value_t property_name = jerry_string_sz ("MyProp1"); + jerry_value_t property_name = jerry_sz_ascii ("MyProp1"); jerry_value_t property_value = jerry_number (3.5); jerry_value_t result = jerry_object_set (parse_options.user_value, property_name, property_value); TEST_ASSERT (jerry_value_is_true (result)); diff --git a/tests/unit-core/test-module-import-meta.c b/tests/unit-core/test-module-import-meta.c index 9adaab0339..957152b00a 100644 --- a/tests/unit-core/test-module-import-meta.c +++ b/tests/unit-core/test-module-import-meta.c @@ -38,7 +38,7 @@ register_assert (void) jerry_value_t global_object_value = jerry_current_realm (); jerry_value_t function_value = jerry_function_external (global_assert); - jerry_value_t function_name_value = jerry_string_sz ("assert"); + jerry_value_t function_name_value = jerry_sz_ascii ("assert"); jerry_value_t result_value = jerry_object_set (global_object_value, function_name_value, function_value); jerry_value_free (function_name_value); @@ -57,7 +57,7 @@ module_import_meta_callback (const jerry_value_t module, /**< module */ TEST_ASSERT (user_p == (void *) &counter); TEST_ASSERT (module == global_module_value); - jerry_value_t property_name_value = jerry_string_sz ("prop"); + jerry_value_t property_name_value = jerry_sz_ascii ("prop"); jerry_value_t result_value = jerry_object_set (meta_object, property_name_value, property_name_value); jerry_value_free (result_value); jerry_value_free (property_name_value); diff --git a/tests/unit-core/test-module.c b/tests/unit-core/test-module.c index cd01dd45ef..c700d3a1e0 100644 --- a/tests/unit-core/test-module.c +++ b/tests/unit-core/test-module.c @@ -42,10 +42,9 @@ compare_specifier (jerry_value_t specifier, /* string value */ static void compare_property (jerry_value_t namespace_object, /**< namespace object */ - const char *name_p, /**< property name */ + jerry_value_t name, /**< property name that will be free/take*/ double expected_value) /**< property value (number for simplicity) */ { - jerry_value_t name = jerry_string_sz (name_p); jerry_value_t result = jerry_object_get (namespace_object, name); TEST_ASSERT (jerry_value_is_number (result)); @@ -118,7 +117,7 @@ resolve_callback2 (const jerry_value_t specifier, /**< module specifier */ { if (terminate_with_error) { - return jerry_throw_sz (JERRY_ERROR_RANGE, "Module not found"); + return jerry_throw_sz (JERRY_ERROR_RANGE, jerry_sz_ascii ("Module not found")); } return create_module (0); @@ -147,10 +146,10 @@ native_module_evaluate (const jerry_value_t native_module) /**< native module */ TEST_ASSERT (jerry_module_state (module) == JERRY_MODULE_STATE_EVALUATING); - jerry_value_t exp_val = jerry_string_sz ("exp"); - jerry_value_t other_exp_val = jerry_string_sz ("other_exp"); + jerry_value_t exp_val = jerry_sz_ascii ("exp"); + jerry_value_t other_exp_val = jerry_sz_ascii ("other_exp"); /* The native module has no such export. */ - jerry_value_t no_exp_val = jerry_string_sz ("no_exp"); + jerry_value_t no_exp_val = jerry_sz_ascii ("no_exp"); jerry_value_t result = jerry_native_module_get (native_module, exp_val); TEST_ASSERT (jerry_value_is_undefined (result)); @@ -171,7 +170,7 @@ native_module_evaluate (const jerry_value_t native_module) /**< native module */ jerry_value_free (result); jerry_value_free (export); - export = jerry_string_sz ("str"); + export = jerry_sz_ascii ("str"); result = jerry_native_module_set (native_module, other_exp_val, export); TEST_ASSERT (jerry_value_is_boolean (result) && jerry_value_is_true (result)); jerry_value_free (result); @@ -197,7 +196,7 @@ native_module_evaluate (const jerry_value_t native_module) /**< native module */ if (counter == 4) { ++counter; - return jerry_throw_sz (JERRY_ERROR_COMMON, "Ooops!"); + return jerry_throw_sz (JERRY_ERROR_COMMON, jerry_sz_ascii ("Ooops!")); } return jerry_undefined (); @@ -213,7 +212,7 @@ resolve_callback4 (const jerry_value_t specifier, /**< module specifier */ ++counter; - jerry_value_t exports[2] = { jerry_string_sz ("exp"), jerry_string_sz ("other_exp") }; + jerry_value_t exports[2] = { jerry_sz_ascii ("exp"), jerry_sz_ascii ("other_exp") }; jerry_value_t native_module = jerry_native_module (native_module_evaluate, exports, 2); TEST_ASSERT (!jerry_value_is_exception (native_module)); @@ -414,8 +413,8 @@ main (void) result = jerry_module_namespace (module); TEST_ASSERT (jerry_value_is_object (result)); - compare_property (result, "a", 6); - compare_property (result, "b", 8.5); + compare_property (result, jerry_sz_ascii ("a"), 6); + compare_property (result, jerry_sz_ascii ("b"), 8.5); jerry_value_free (result); jerry_value_free (module); @@ -439,7 +438,7 @@ main (void) jerry_value_free (module); /* Valid identifier. */ - jerry_value_t export = jerry_string_sz ("\xed\xa0\x83\xed\xb2\x80"); + jerry_value_t export = jerry_sz_cesu8 ("\xed\xa0\x83\xed\xb2\x80"); module = jerry_native_module (NULL, &export, 1); TEST_ASSERT (!jerry_value_is_exception (module)); @@ -457,13 +456,13 @@ main (void) jerry_value_free (export); /* Invalid identifiers. */ - export = jerry_string_sz ("a+"); + export = jerry_sz_ascii ("a+"); module = jerry_native_module (NULL, &export, 1); TEST_ASSERT (jerry_value_is_exception (module)); jerry_value_free (module); jerry_value_free (export); - export = jerry_string_sz ("\xed\xa0\x80"); + export = jerry_sz_cesu8 ("\xed\xa0\x80"); module = jerry_native_module (NULL, &export, 1); TEST_ASSERT (jerry_value_is_exception (module)); jerry_value_free (module); diff --git a/tests/unit-core/test-native-instanceof.c b/tests/unit-core/test-native-instanceof.c index e5fcad0655..fbd2290e6c 100644 --- a/tests/unit-core/test-native-instanceof.c +++ b/tests/unit-core/test-native-instanceof.c @@ -63,7 +63,7 @@ main (void) /* Test for a JS constructor. */ jerry_value_t global = jerry_current_realm (); - jerry_value_t object_name = jerry_string_sz ("Object"); + jerry_value_t object_name = jerry_sz_ascii ("Object"); constructor = jerry_object_get (global, object_name); jerry_value_free (object_name); jerry_value_free (global); diff --git a/tests/unit-core/test-native-pointer.c b/tests/unit-core/test-native-pointer.c index 664aba4616..937b16075e 100644 --- a/tests/unit-core/test-native-pointer.c +++ b/tests/unit-core/test-native-pointer.c @@ -233,8 +233,8 @@ main (void) TEST_ASSERT (!jerry_object_has_native_ptr (object_value, &native_info_3)); /* Test value references */ - jerry_value_t string1_value = jerry_string_sz ("String1"); - jerry_value_t string2_value = jerry_string_sz ("String2"); + jerry_value_t string1_value = jerry_sz_ascii ("String1"); + jerry_value_t string2_value = jerry_sz_ascii ("String2"); jerry_value_t object1_value = jerry_object (); jerry_value_t object2_value = jerry_object (); diff --git a/tests/unit-core/test-newtarget.c b/tests/unit-core/test-newtarget.c index d5b28240fe..38fc577bb9 100644 --- a/tests/unit-core/test-newtarget.c +++ b/tests/unit-core/test-newtarget.c @@ -22,13 +22,12 @@ * Register a JavaScript function in the global object. */ static jerry_value_t -register_js_function (const char *name_p, /**< name of the function */ +register_js_function (jerry_value_t function_name_val, /**< name of the function that will be free/take */ jerry_external_handler_t handler_p) /**< function callback */ { jerry_value_t global_obj_val = jerry_current_realm (); jerry_value_t function_val = jerry_function_external (handler_p); - jerry_value_t function_name_val = jerry_string_sz (name_p); jerry_value_t result_val = jerry_object_set (global_obj_val, function_name_val, function_val); jerry_value_free (function_name_val); @@ -110,7 +109,7 @@ main (void) { jerry_init (JERRY_INIT_EMPTY); - jerry_value_t demo_func = register_js_function ("Demo", construct_handler); + jerry_value_t demo_func = register_js_function (jerry_sz_ascii ("Demo"), construct_handler); { jerry_value_t test_arg = jerry_number (TEST_ID_SIMPLE_CONSTRUCT); diff --git a/tests/unit-core/test-number-converter.c b/tests/unit-core/test-number-converter.c index 9b7116cb27..d827ac70f9 100644 --- a/tests/unit-core/test-number-converter.c +++ b/tests/unit-core/test-number-converter.c @@ -115,7 +115,7 @@ main (void) test_to_interger (-4294967297, -4294967297); // few test-cases which return with error - jerry_value_t error_val = jerry_throw_sz (JERRY_ERROR_TYPE, "error"); + jerry_value_t error_val = jerry_throw_sz (JERRY_ERROR_TYPE, jerry_sz_ascii ("error")); double number = jerry_value_as_integer (error_val); jerry_value_free (error_val); TEST_ASSERT (number == 0); diff --git a/tests/unit-core/test-objects-foreach.c b/tests/unit-core/test-objects-foreach.c index d97cfe6da2..701a5adbd0 100644 --- a/tests/unit-core/test-objects-foreach.c +++ b/tests/unit-core/test-objects-foreach.c @@ -33,7 +33,7 @@ static void test_container (void) { jerry_value_t global = jerry_current_realm (); - jerry_value_t map_str = jerry_string_sz ("Map"); + jerry_value_t map_str = jerry_sz_ascii ("Map"); jerry_value_t map_result = jerry_object_get (global, map_str); jerry_type_t type = jerry_value_type (map_result); @@ -111,7 +111,7 @@ test_internal_prop (void) TEST_ASSERT ((after_object_count - before_object_count) == 1); } - jerry_value_t internal_prop_name = jerry_string_sz ("hidden_foo"); + jerry_value_t internal_prop_name = jerry_sz_ascii ("hidden_foo"); jerry_value_t internal_prop_object = jerry_object (); bool internal_result = jerry_object_set_internal (object, internal_prop_name, internal_prop_object); TEST_ASSERT (internal_result == true); @@ -221,7 +221,7 @@ main (void) /* Create an object and set a property on it. */ object = jerry_object (); - jerry_value_t property_name = jerry_string_sz ("xyzzy"); + jerry_value_t property_name = jerry_sz_ascii ("xyzzy"); jerry_value_t property_value = jerry_number (42); jerry_value_free (jerry_object_set (object, property_name, property_value)); jerry_value_free (property_value); diff --git a/tests/unit-core/test-promise.c b/tests/unit-core/test-promise.c index b66ce791c3..c1b5ae5546 100644 --- a/tests/unit-core/test-promise.c +++ b/tests/unit-core/test-promise.c @@ -30,8 +30,8 @@ static const jerry_char_t test_source[] = TEST_STRING_LITERAL ("var p1 = create_ static int count_in_assert = 0; static jerry_value_t my_promise1; static jerry_value_t my_promise2; -static const char s1[] = "resolved"; -static const char s2[] = "rejected"; +#define s1 "resolved" +#define s2 "rejected" static jerry_value_t create_promise1_handler (const jerry_call_info_t *call_info_p, /**< call information */ @@ -86,13 +86,12 @@ assert_handler (const jerry_call_info_t *call_info_p, /**< call information */ * Register a JavaScript function in the global object. */ static void -register_js_function (const char *name_p, /**< name of the function */ +register_js_function (jerry_value_t function_name_val, /**< name of the function that will be free/take*/ jerry_external_handler_t handler_p) /**< function callback */ { jerry_value_t global_obj_val = jerry_current_realm (); jerry_value_t function_val = jerry_function_external (handler_p); - jerry_value_t function_name_val = jerry_string_sz (name_p); jerry_value_t result_val = jerry_object_set (global_obj_val, function_name_val, function_val); jerry_value_free (function_name_val); @@ -107,9 +106,9 @@ main (void) { jerry_init (JERRY_INIT_EMPTY); - register_js_function ("create_promise1", create_promise1_handler); - register_js_function ("create_promise2", create_promise2_handler); - register_js_function ("assert", assert_handler); + register_js_function (jerry_sz_ascii ("create_promise1"), create_promise1_handler); + register_js_function (jerry_sz_ascii ("create_promise2"), create_promise2_handler); + register_js_function (jerry_sz_ascii ("assert"), assert_handler); jerry_value_t parsed_code_val = jerry_parse (test_source, sizeof (test_source) - 1, NULL); TEST_ASSERT (!jerry_value_is_exception (parsed_code_val)); @@ -127,8 +126,8 @@ main (void) TEST_ASSERT (count_in_assert == 0); /* Test jerry_resolve_or_reject_promise. */ - jerry_value_t str_resolve = jerry_string_sz (s1); - jerry_value_t str_reject = jerry_string_sz (s2); + jerry_value_t str_resolve = jerry_sz_ascii (s1); + jerry_value_t str_reject = jerry_sz_ascii (s2); jerry_promise_resolve (my_promise1, str_resolve); jerry_promise_reject (my_promise2, str_reject); diff --git a/tests/unit-core/test-proxy.c b/tests/unit-core/test-proxy.c index 40c70e53ea..6a015a1d90 100644 --- a/tests/unit-core/test-proxy.c +++ b/tests/unit-core/test-proxy.c @@ -111,10 +111,9 @@ handler_set (const jerry_call_info_t *call_info_p, /**< call information */ static void set_property (jerry_value_t target, /**< target object */ - const char *name_p, /**< name of the property */ + jerry_value_t name_val, /**< name of the property that will be free/take */ jerry_value_t value) /**< value of the property */ { - jerry_value_t name_val = jerry_string_sz (name_p); jerry_value_t result_val = jerry_object_set (target, name_val, value); TEST_ASSERT (jerry_value_is_boolean (result_val)); @@ -124,9 +123,8 @@ set_property (jerry_value_t target, /**< target object */ static jerry_value_t get_property (jerry_value_t target, /**< target object */ - const char *name_p) /**< name of the property */ + jerry_value_t name_val) /**< name of the property that will be free/take */ { - jerry_value_t name_val = jerry_string_sz (name_p); jerry_value_t result_val = jerry_object_get (target, name_val); TEST_ASSERT (!jerry_value_is_exception (result_val)); @@ -136,11 +134,11 @@ get_property (jerry_value_t target, /**< target object */ static void set_function (jerry_value_t target, /**< target object */ - const char *name_p, /**< name of the function */ + jerry_value_t name_val, /**< name of the function that will be free/take */ jerry_external_handler_t handler_p) /**< function callback */ { jerry_value_t function_val = jerry_function_external (handler_p); - set_property (target, name_p, function_val); + set_property (target, name_val, function_val); jerry_value_free (function_val); } /* set_function */ @@ -193,7 +191,7 @@ static void test_proxy_native (void) { jerry_value_t handler = jerry_object (); - set_function (handler, "get", proxy_native_handler_get); + set_function (handler, jerry_sz_ascii ("get"), proxy_native_handler_get); jerry_value_t target = jerry_object (); jerry_value_t proxy = jerry_proxy (target, handler); @@ -233,15 +231,15 @@ main (void) jerry_value_t handler = jerry_object (); { - set_function (handler, "get", handler_get); - set_function (handler, "set", handler_set); + set_function (handler, jerry_sz_ascii ("get"), handler_get); + set_function (handler, jerry_sz_ascii ("set"), handler_set); } jerry_value_t target = jerry_object (); jerry_value_t proxy = jerry_proxy (target, handler); { jerry_value_t global = jerry_current_realm (); - set_property (global, "pdemo", proxy); + set_property (global, jerry_sz_ascii ("pdemo"), proxy); jerry_value_free (global); } @@ -257,7 +255,7 @@ main (void) } { - jerry_value_t res = get_property (proxy, "value"); + jerry_value_t res = get_property (proxy, jerry_sz_ascii ("value")); TEST_ASSERT (jerry_value_is_number (res)); TEST_ASSERT (jerry_value_as_number (res) == 2.0); jerry_value_free (res); @@ -290,12 +288,12 @@ main (void) { jerry_value_t new_value = jerry_number (12); - set_property (proxy, "value", new_value); + set_property (proxy, jerry_sz_ascii ("value"), new_value); jerry_value_free (new_value); } { - jerry_value_t res = get_property (proxy, "value"); + jerry_value_t res = get_property (proxy, jerry_sz_ascii ("value")); TEST_ASSERT (jerry_value_is_number (res)); TEST_ASSERT (jerry_value_as_number (res) == 13.0); jerry_value_free (res); @@ -318,7 +316,7 @@ main (void) jerry_value_free (parsed_has_code_val); TEST_ASSERT (jerry_value_is_proxy (res)); - jerry_value_t name = jerry_string_sz ("key"); + jerry_value_t name = jerry_sz_ascii ("key"); TEST_ASSERT (jerry_value_is_string (name)); jerry_value_t property = jerry_object_has (res, name); jerry_value_free (name); diff --git a/tests/unit-core/test-realm.c b/tests/unit-core/test-realm.c index db088fd5b0..58b39acd39 100644 --- a/tests/unit-core/test-realm.c +++ b/tests/unit-core/test-realm.c @@ -19,10 +19,9 @@ static void create_number_property (jerry_value_t object_value, /**< object value */ - char *name_p, /**< name */ + jerry_value_t name_value, /**< name that will be take/free*/ double number) /**< value */ { - jerry_value_t name_value = jerry_string_sz (name_p); jerry_value_t number_value = jerry_number (number); jerry_value_t result_value = jerry_object_set (object_value, name_value, number_value); TEST_ASSERT (!jerry_value_is_exception (result_value)); @@ -34,9 +33,8 @@ create_number_property (jerry_value_t object_value, /**< object value */ static double get_number_property (jerry_value_t object_value, /**< object value */ - char *name_p) /**< name */ + jerry_value_t name_value) /**< name that will be free/take */ { - jerry_value_t name_value = jerry_string_sz (name_p); jerry_value_t result_value = jerry_object_get (object_value, name_value); TEST_ASSERT (!jerry_value_is_exception (result_value)); TEST_ASSERT (jerry_value_is_number (result_value)); @@ -72,12 +70,12 @@ check_type_error (jerry_value_t result_value) /**< result value */ static void check_array_prototype (jerry_value_t realm_value, jerry_value_t result_value) { - jerry_value_t name_value = jerry_string_sz ("Array"); + jerry_value_t name_value = jerry_sz_ascii ("Array"); jerry_value_t array_value = jerry_object_get (realm_value, name_value); TEST_ASSERT (jerry_value_is_object (array_value)); jerry_value_free (name_value); - name_value = jerry_string_sz ("prototype"); + name_value = jerry_sz_ascii ("prototype"); jerry_value_t prototype_value = jerry_object_get (array_value, name_value); TEST_ASSERT (jerry_value_is_object (prototype_value)); jerry_value_free (name_value); @@ -117,10 +115,10 @@ main (void) jerry_value_t realm_value = jerry_realm (); - create_number_property (global_value, "a", 3.5); - create_number_property (global_value, "b", 7.25); - create_number_property (realm_value, "a", -1.25); - create_number_property (realm_value, "b", -6.75); + create_number_property (global_value, jerry_sz_ascii ("a"), 3.5); + create_number_property (global_value, jerry_sz_ascii ("b"), 7.25); + create_number_property (realm_value, jerry_sz_ascii ("a"), -1.25); + create_number_property (realm_value, jerry_sz_ascii ("b"), -6.75); TEST_ASSERT (eval_and_get_number ("a") == 3.5); @@ -170,15 +168,15 @@ main (void) TEST_ASSERT (jerry_value_is_boolean (result_value) && jerry_value_is_true (result_value)); jerry_value_free (result_value); - create_number_property (object_value, "x", 7.25); - create_number_property (object_value, "y", 1.25); + create_number_property (object_value, jerry_sz_ascii ("x"), 7.25); + create_number_property (object_value, jerry_sz_ascii ("y"), 1.25); result_value = jerry_set_realm (realm_value); TEST_ASSERT (!jerry_value_is_exception (result_value)); TEST_ASSERT (eval_and_get_number ("var z = -5.5; x + this.y") == 8.5); jerry_set_realm (result_value); - TEST_ASSERT (get_number_property (object_value, "z") == -5.5); + TEST_ASSERT (get_number_property (object_value, jerry_sz_ascii ("z")) == -5.5); result_value = jerry_realm_this (realm_value); TEST_ASSERT (result_value == object_value); @@ -203,7 +201,7 @@ main (void) TEST_ASSERT (eval_and_get_number ("var z = 1.5; z") == 1.5); jerry_set_realm (old_realm_value); - TEST_ASSERT (get_number_property (target_value, "z") == 1.5); + TEST_ASSERT (get_number_property (target_value, jerry_sz_ascii ("z")) == 1.5); jerry_value_free (target_value); /* Check isExtensible error. */ @@ -241,7 +239,7 @@ main (void) jerry_set_realm (result_value); /* Script is compiled in another realm. */ - create_number_property (realm_value, "global1", 7.5); + create_number_property (realm_value, jerry_sz_ascii ("global1"), 7.5); result_value = jerry_run (script_value); TEST_ASSERT (!jerry_value_is_exception (result_value)); @@ -250,7 +248,7 @@ main (void) jerry_value_free (result_value); jerry_value_free (script_value); - TEST_ASSERT (get_number_property (realm_value, "global2") == 6.5); + TEST_ASSERT (get_number_property (realm_value, jerry_sz_ascii ("global2")) == 6.5); jerry_value_free (realm_value); diff --git a/tests/unit-core/test-regexp-dotall-unicode.c b/tests/unit-core/test-regexp-dotall-unicode.c index 64453523ca..0e0ad7022f 100644 --- a/tests/unit-core/test-regexp-dotall-unicode.c +++ b/tests/unit-core/test-regexp-dotall-unicode.c @@ -24,17 +24,17 @@ main (void) jerry_init (JERRY_INIT_EMPTY); jerry_value_t undefined_this_arg = jerry_undefined (); - char pattern2[] = "\\u{61}.\\u{62}"; +#define pattern2 "\\u{61}.\\u{62}" uint16_t flags = JERRY_REGEXP_FLAG_DOTALL | JERRY_REGEXP_FLAG_UNICODE | JERRY_REGEXP_FLAG_STICKY; - jerry_value_t regex_obj = jerry_regexp_sz (pattern2, flags); + jerry_value_t regex_obj = jerry_regexp_sz (jerry_sz_ascii (pattern2), flags); TEST_ASSERT (jerry_value_is_object (regex_obj)); const jerry_char_t func_src2[] = "return [regex.exec('a\\nb'), regex.dotAll, regex.sticky, regex.unicode ];"; jerry_parse_options_t parse_options; parse_options.options = JERRY_PARSE_HAS_ARGUMENT_LIST; - parse_options.argument_list = jerry_string_sz ("regex"); + parse_options.argument_list = jerry_sz_ascii ("regex"); jerry_value_t func_val = jerry_parse (func_src2, sizeof (func_src2) - 1, &parse_options); jerry_value_free (parse_options.argument_list); diff --git a/tests/unit-core/test-regexp.c b/tests/unit-core/test-regexp.c index 8cb4e8251d..8211f97e6d 100644 --- a/tests/unit-core/test-regexp.c +++ b/tests/unit-core/test-regexp.c @@ -25,16 +25,16 @@ main (void) jerry_value_t global_obj_val = jerry_current_realm (); - char pattern[] = "[^.]+"; +#define pattern "[^.]+" uint16_t flags = JERRY_REGEXP_FLAG_GLOBAL | JERRY_REGEXP_FLAG_MULTILINE; - jerry_value_t regex_obj = jerry_regexp_sz (pattern, flags); + jerry_value_t regex_obj = jerry_regexp_sz (jerry_sz_ascii (pattern), flags); TEST_ASSERT (jerry_value_is_object (regex_obj)); const jerry_char_t func_src[] = "return [regex.exec('something.domain.com'), regex.multiline, regex.global];"; jerry_parse_options_t parse_options; parse_options.options = JERRY_PARSE_HAS_ARGUMENT_LIST; - parse_options.argument_list = jerry_string_sz ("regex"); + parse_options.argument_list = jerry_sz_ascii ("regex"); jerry_value_t func_val = jerry_parse (func_src, sizeof (func_src) - 1, &parse_options); jerry_value_free (parse_options.argument_list); diff --git a/tests/unit-core/test-regression-3588.c b/tests/unit-core/test-regression-3588.c index 87a2dea198..6d63f6931b 100644 --- a/tests/unit-core/test-regression-3588.c +++ b/tests/unit-core/test-regression-3588.c @@ -43,7 +43,7 @@ main (void) jerry_value_t global_obj_val = jerry_current_realm (); jerry_value_t function_val = jerry_function_external (construct_handler); - jerry_value_t function_name_val = jerry_string_sz ("Demo"); + jerry_value_t function_name_val = jerry_sz_ascii ("Demo"); jerry_value_t result_val = jerry_object_set (global_obj_val, function_name_val, function_val); TEST_ASSERT (!jerry_value_is_exception (result_val)); TEST_ASSERT (jerry_value_is_true (result_val)); diff --git a/tests/unit-core/test-script-user-value.c b/tests/unit-core/test-script-user-value.c index 8effb6ecaa..c0f6aca400 100644 --- a/tests/unit-core/test-script-user-value.c +++ b/tests/unit-core/test-script-user-value.c @@ -59,7 +59,7 @@ test_parse_function (const char *source_p, /**< source code */ bool run_code) /**< run the code after parsing */ { options_p->options |= JERRY_PARSE_HAS_ARGUMENT_LIST; - options_p->argument_list = jerry_string_sz (""); + options_p->argument_list = jerry_sz_ascii (""); for (size_t i = 0; i < USER_VALUES_SIZE; i++) { @@ -101,7 +101,7 @@ main (void) user_values[0] = jerry_object (); user_values[1] = jerry_null (); user_values[2] = jerry_number (5.5); - user_values[3] = jerry_string_sz ("AnyString..."); + user_values[3] = jerry_sz_ascii ("AnyString..."); jerry_parse_options_t parse_options; const char *source_p = TEST_STRING_LITERAL (""); diff --git a/tests/unit-core/test-snapshot.c b/tests/unit-core/test-snapshot.c index 4de459c1a5..3f792386a4 100644 --- a/tests/unit-core/test-snapshot.c +++ b/tests/unit-core/test-snapshot.c @@ -276,7 +276,7 @@ test_function_snapshot (void) jerry_parse_options_t parse_options; parse_options.options = JERRY_PARSE_HAS_ARGUMENT_LIST; - parse_options.argument_list = jerry_string_sz ("a, b"); + parse_options.argument_list = jerry_sz_ascii ("a, b"); jerry_value_t parse_result = jerry_parse (code_to_snapshot, sizeof (code_to_snapshot) - 1, &parse_options); TEST_ASSERT (!jerry_value_is_exception (parse_result)); @@ -409,7 +409,7 @@ test_snapshot_with_user (void) } else { - snapshot_exec_options.user_value = jerry_string_sz ("AnyString..."); + snapshot_exec_options.user_value = jerry_sz_ascii ("AnyString..."); } jerry_value_t result = jerry_exec_snapshot (snapshot_buffer, diff --git a/tests/unit-core/test-source-name.c b/tests/unit-core/test-source-name.c index 9ecd16bdee..4b3807d510 100644 --- a/tests/unit-core/test-source-name.c +++ b/tests/unit-core/test-source-name.c @@ -50,7 +50,7 @@ main (void) /* Register the "sourceName" method. */ { jerry_value_t func = jerry_function_external (source_name_handler); - jerry_value_t name = jerry_string_sz ("sourceName"); + jerry_value_t name = jerry_sz_ascii ("sourceName"); jerry_value_t result = jerry_object_set (global, name, func); jerry_value_free (result); jerry_value_free (name); @@ -70,7 +70,7 @@ main (void) "} \n" "f1();"); - parse_options.source_name = jerry_string_sz ("demo1.js"); + parse_options.source_name = jerry_sz_ascii ("demo1.js"); jerry_value_t program = jerry_parse ((const jerry_char_t *) source_1, strlen (source_1), &parse_options); TEST_ASSERT (!jerry_value_is_exception (program)); @@ -101,7 +101,7 @@ main (void) "} \n" "f2(); \n"); - parse_options.source_name = jerry_string_sz ("demo2.js"); + parse_options.source_name = jerry_sz_ascii ("demo2.js"); program = jerry_parse ((const jerry_char_t *) source_2, strlen (source_2), &parse_options); TEST_ASSERT (!jerry_value_is_exception (program)); @@ -122,11 +122,11 @@ main (void) jerry_value_free (program); if (jerry_feature_enabled (JERRY_FEATURE_MODULE)) { - jerry_value_t anon = jerry_string_sz (""); + jerry_value_t anon = jerry_sz_ascii (""); const char *source_3 = ""; parse_options.options = JERRY_PARSE_MODULE | JERRY_PARSE_HAS_SOURCE_NAME; - parse_options.source_name = jerry_string_sz ("demo3.js"); + parse_options.source_name = jerry_sz_ascii ("demo3.js"); program = jerry_parse ((const jerry_char_t *) source_3, strlen (source_3), &parse_options); TEST_ASSERT (!jerry_value_is_exception (program)); @@ -166,7 +166,7 @@ main (void) "f.bind().bind();"); parse_options.options = JERRY_PARSE_HAS_SOURCE_NAME; - parse_options.source_name = jerry_string_sz ("demo4.js"); + parse_options.source_name = jerry_sz_ascii ("demo4.js"); program = jerry_parse ((const jerry_char_t *) source_4, strlen (source_4), &parse_options); TEST_ASSERT (!jerry_value_is_exception (program)); @@ -189,7 +189,7 @@ main (void) parse_options.options = JERRY_PARSE_HAS_USER_VALUE | JERRY_PARSE_HAS_SOURCE_NAME; parse_options.user_value = jerry_object (); - parse_options.source_name = jerry_string_sz ("demo5.js"); + parse_options.source_name = jerry_sz_ascii ("demo5.js"); program = jerry_parse ((const jerry_char_t *) source_5, strlen (source_5), &parse_options); TEST_ASSERT (!jerry_value_is_exception (program)); @@ -207,7 +207,7 @@ main (void) const char *source_6 = "(class {})"; parse_options.options = JERRY_PARSE_HAS_SOURCE_NAME; - parse_options.source_name = jerry_string_sz ("demo6.js"); + parse_options.source_name = jerry_sz_ascii ("demo6.js"); program = jerry_parse ((const jerry_char_t *) source_6, strlen (source_6), &parse_options); if (!jerry_value_is_exception (program)) diff --git a/tests/unit-core/test-special-proxy.c b/tests/unit-core/test-special-proxy.c index e7e6e2cd06..233cad2c03 100644 --- a/tests/unit-core/test-special-proxy.c +++ b/tests/unit-core/test-special-proxy.c @@ -60,7 +60,7 @@ main (void) jerry_value_t global = jerry_current_realm (); jerry_value_t function = jerry_function_external (create_special_proxy_handler); - jerry_value_t name = jerry_string_sz ("create_special_proxy"); + jerry_value_t name = jerry_sz_ascii ("create_special_proxy"); jerry_value_t result = jerry_object_set (global, name, function); TEST_ASSERT (!jerry_value_is_exception (result)); diff --git a/tests/unit-core/test-string-to-number.c b/tests/unit-core/test-string-to-number.c index 09927534be..b7bdf2e6d1 100644 --- a/tests/unit-core/test-string-to-number.c +++ b/tests/unit-core/test-string-to-number.c @@ -27,14 +27,13 @@ int main (void) { TEST_INIT (); - - const jerry_char_t *strings[] = { - (const jerry_char_t *) "1", (const jerry_char_t *) "0.5", (const jerry_char_t *) "12345", - (const jerry_char_t *) "1e-45", (const jerry_char_t *) "-2.5e+38", (const jerry_char_t *) "-2.5e38", - (const jerry_char_t *) "- 2.5e+38", (const jerry_char_t *) "-2 .5e+38", (const jerry_char_t *) "-2. 5e+38", - (const jerry_char_t *) "-2.5e+ 38", (const jerry_char_t *) "-2.5 e+38", (const jerry_char_t *) "-2.5e +38", - (const jerry_char_t *) "NaN", (const jerry_char_t *) "abc", (const jerry_char_t *) " Infinity ", - (const jerry_char_t *) "-Infinity", (const jerry_char_t *) "0", (const jerry_char_t *) "0", + jerry_string_t strings[] = { + { JERRY_ZSTR_ARG ("1") }, { JERRY_ZSTR_ARG ("0.5") }, { JERRY_ZSTR_ARG ("12345") }, + { JERRY_ZSTR_ARG ("1e-45") }, { JERRY_ZSTR_ARG ("-2.5e+38") }, { JERRY_ZSTR_ARG ("-2.5e38") }, + { JERRY_ZSTR_ARG ("- 2.5e+38") }, { JERRY_ZSTR_ARG ("-2 .5e+38") }, { JERRY_ZSTR_ARG ("-2. 5e+38") }, + { JERRY_ZSTR_ARG ("-2.5e+ 38") }, { JERRY_ZSTR_ARG ("-2.5 e+38") }, { JERRY_ZSTR_ARG ("-2.5e +38") }, + { JERRY_ZSTR_ARG ("NaN") }, { JERRY_ZSTR_ARG ("abc") }, { JERRY_ZSTR_ARG (" Infinity ") }, + { JERRY_ZSTR_ARG ("-Infinity") }, { JERRY_ZSTR_ARG ("0") }, { JERRY_ZSTR_ARG ("0") }, }; const ecma_number_t nums[] = { (ecma_number_t) 1.0, (ecma_number_t) 0.5, (ecma_number_t) 12345.0, @@ -46,7 +45,7 @@ main (void) for (uint32_t i = 0; i < sizeof (nums) / sizeof (nums[0]); i++) { - ecma_number_t num = ecma_utf8_string_to_number (strings[i], lit_zt_utf8_string_size (strings[i]), 0); + ecma_number_t num = ecma_utf8_string_to_number (strings[i].ptr, strings[i].size, 0); if (num != nums[i] && (!ecma_number_is_nan (num) || !ecma_number_is_nan (nums[i]))) { diff --git a/tests/unit-core/test-symbol.c b/tests/unit-core/test-symbol.c index bbbc0c0173..064ee5deac 100644 --- a/tests/unit-core/test-symbol.c +++ b/tests/unit-core/test-symbol.c @@ -19,10 +19,10 @@ #include "test-common.h" /* foo string */ -#define STRING_FOO ("foo") +#define STRING_FOO "foo" /* bar string */ -#define STRING_BAR ("bar") +#define STRING_BAR "bar" /* Symbol(bar) desciptive string */ #define SYMBOL_DESCIPTIVE_STRING_BAR "Symbol(bar)" @@ -38,8 +38,8 @@ main (void) jerry_value_t object = jerry_object (); /* Test for that each symbol is unique independently from their descriptor strings */ - jerry_value_t symbol_desc_1 = jerry_string_sz (STRING_FOO); - jerry_value_t symbol_desc_2 = jerry_string_sz (STRING_FOO); + jerry_value_t symbol_desc_1 = jerry_sz_ascii (STRING_FOO); + jerry_value_t symbol_desc_2 = jerry_sz_ascii (STRING_FOO); jerry_value_t symbol_1 = jerry_symbol_with_description (symbol_desc_1); TEST_ASSERT (!jerry_value_is_exception (symbol_1)); @@ -99,7 +99,7 @@ main (void) /* Modify the descriptor fields */ prop_desc = jerry_property_descriptor (); - jerry_value_t value_3 = jerry_string_sz (STRING_BAR); + jerry_value_t value_3 = jerry_sz_ascii (STRING_BAR); prop_desc.flags |= JERRY_PROP_IS_VALUE_DEFINED | JERRY_PROP_IS_WRITABLE_DEFINED | JERRY_PROP_IS_ENUMERABLE_DEFINED | JERRY_PROP_IS_CONFIGURABLE_DEFINED; @@ -130,7 +130,7 @@ main (void) jerry_value_free (object); /* Test creating symbol with a symbol description */ - jerry_value_t empty_symbol_desc = jerry_string_sz (""); + jerry_value_t empty_symbol_desc = jerry_sz_ascii (""); jerry_value_t empty_symbol = jerry_symbol_with_description (empty_symbol_desc); TEST_ASSERT (!jerry_value_is_exception (empty_symbol)); @@ -150,7 +150,7 @@ main (void) jerry_value_free (empty_symbol); /* Test symbol to string operation with symbol argument */ - jerry_value_t bar_symbol_desc = jerry_string_sz (STRING_BAR); + jerry_value_t bar_symbol_desc = jerry_sz_ascii (STRING_BAR); jerry_value_t bar_symbol = jerry_symbol_with_description (bar_symbol_desc); TEST_ASSERT (!jerry_value_is_exception (bar_symbol)); @@ -224,16 +224,19 @@ main (void) " [Symbol.matchAll]: 13," "})"; - const char *symbols[] = { - "asyncIterator", "hasInstance", "isConcatSpreadable", "iterator", "match", "replace", "search", - "species", "split", "toPrimitive", "toStringTag", "unscopables", "matchAll", + const jerry_value_t symbols[] = { + jerry_sz_ascii ("asyncIterator"), jerry_sz_ascii ("hasInstance"), jerry_sz_ascii ("isConcatSpreadable"), + jerry_sz_ascii ("iterator"), jerry_sz_ascii ("match"), jerry_sz_ascii ("replace"), + jerry_sz_ascii ("search"), jerry_sz_ascii ("species"), jerry_sz_ascii ("split"), + jerry_sz_ascii ("toPrimitive"), jerry_sz_ascii ("toStringTag"), jerry_sz_ascii ("unscopables"), + jerry_sz_ascii ("matchAll"), }; jerry_value_t obj = jerry_eval (obj_src, sizeof (obj_src) - 1, JERRY_PARSE_NO_OPTS); TEST_ASSERT (jerry_value_is_object (obj)); jerry_value_t global_obj = jerry_current_realm (); - jerry_value_t symbol_str = jerry_string_sz ("Symbol"); + jerry_value_t symbol_str = jerry_sz_ascii ("Symbol"); jerry_value_t builtin_symbol = jerry_object_get (global_obj, symbol_str); TEST_ASSERT (jerry_value_is_object (builtin_symbol)); @@ -245,9 +248,7 @@ main (void) { jerry_value_t well_known_symbol = jerry_symbol (id); - jerry_value_t prop_str = jerry_string_sz (symbols[prop_index]); - jerry_value_t current_global_symbol = jerry_object_get (builtin_symbol, prop_str); - jerry_value_free (prop_str); + jerry_value_t current_global_symbol = jerry_object_get (builtin_symbol, symbols[prop_index]); jerry_value_t relation = jerry_binary_op (JERRY_BIN_OP_STRICT_EQUAL, well_known_symbol, current_global_symbol); @@ -272,6 +273,7 @@ main (void) jerry_value_free (well_known_symbol); } + jerry_value_list_free (symbols, sizeof (symbols) / symbols[0]); jerry_value_free (builtin_symbol); /* Deletion of the 'Symbol' builtin makes the well-known symbols unaccessible from JS context diff --git a/tests/unit-core/test-to-property-descriptor.c b/tests/unit-core/test-to-property-descriptor.c index 53821c27c1..f86ed548d7 100644 --- a/tests/unit-core/test-to-property-descriptor.c +++ b/tests/unit-core/test-to-property-descriptor.c @@ -29,9 +29,8 @@ create_property_descriptor (const char *script_p) /**< source code */ static void check_attribute (jerry_value_t attribute, /**< attribute to be checked */ jerry_value_t object, /**< original object */ - const char *name_p) /**< name of the attribute */ + jerry_value_t prop_name) /**< name of the attribute that will be free/take */ { - jerry_value_t prop_name = jerry_string_sz (name_p); jerry_value_t value = jerry_object_get (object, prop_name); if (jerry_value_is_undefined (value)) @@ -74,7 +73,7 @@ main (void) jerry_property_descriptor_t prop_desc = to_property_descriptor (object); - check_attribute (prop_desc.value, object, "value"); + check_attribute (prop_desc.value, object, jerry_sz_ascii ("value")); TEST_ASSERT (prop_desc.flags & JERRY_PROP_IS_VALUE_DEFINED); TEST_ASSERT (!(prop_desc.flags & JERRY_PROP_IS_GET_DEFINED)); @@ -115,8 +114,8 @@ main (void) prop_desc = to_property_descriptor (object); - check_attribute (prop_desc.getter, object, "get"); - check_attribute (prop_desc.setter, object, "set"); + check_attribute (prop_desc.getter, object, jerry_sz_ascii ("get")); + check_attribute (prop_desc.setter, object, jerry_sz_ascii ("set")); TEST_ASSERT (!(prop_desc.flags & JERRY_PROP_IS_VALUE_DEFINED)); TEST_ASSERT (!(prop_desc.flags & JERRY_PROP_IS_WRITABLE_DEFINED)); @@ -135,7 +134,7 @@ main (void) prop_desc = to_property_descriptor (object); - check_attribute (prop_desc.getter, object, "get"); + check_attribute (prop_desc.getter, object, jerry_sz_ascii ("get")); TEST_ASSERT (!(prop_desc.flags & JERRY_PROP_IS_VALUE_DEFINED)); TEST_ASSERT (!(prop_desc.flags & JERRY_PROP_IS_WRITABLE_DEFINED)); @@ -154,7 +153,7 @@ main (void) prop_desc = to_property_descriptor (object); - check_attribute (prop_desc.setter, object, "set"); + check_attribute (prop_desc.setter, object, jerry_sz_ascii ("set")); TEST_ASSERT (!(prop_desc.flags & JERRY_PROP_IS_VALUE_DEFINED)); TEST_ASSERT (!(prop_desc.flags & JERRY_PROP_IS_WRITABLE_DEFINED)); diff --git a/tests/unit-core/test-typedarray.c b/tests/unit-core/test-typedarray.c index 78535b3e78..4c461005bb 100644 --- a/tests/unit-core/test-typedarray.c +++ b/tests/unit-core/test-typedarray.c @@ -26,7 +26,7 @@ typedef struct { jerry_typedarray_type_t typedarray_type; /**< what kind of TypedArray */ - char *constructor_name; /**< JS constructor name for TypedArray */ + jerry_value_t constructor_name; /**< JS constructor name for TypedArray */ uint32_t element_count; /**< number of elements for the TypedArray */ uint32_t bytes_per_element; /**< bytes per elment of the given typedarray_type */ } test_entry_t; @@ -35,12 +35,11 @@ typedef struct * Register a JavaScript value in the global object. */ static void -register_js_value (const char *name_p, /**< name of the function */ +register_js_value (jerry_value_t name_val, /**< name of the function that will be free/take */ jerry_value_t value) /**< function callback */ { jerry_value_t global_obj_val = jerry_current_realm (); - jerry_value_t name_val = jerry_string_sz (name_p); jerry_value_t result_val = jerry_object_set (global_obj_val, name_val, value); jerry_value_free (name_val); @@ -108,18 +107,16 @@ test_typedarray_queries (test_entry_t test_entries[]) /**< test cases */ { jerry_value_t global_obj_val = jerry_current_realm (); - for (uint32_t i = 0; test_entries[i].constructor_name != NULL; i++) + for (uint32_t i = 0; !jerry_value_is_undefined (test_entries[i].constructor_name); i++) { /* Create TypedArray via construct call */ { - jerry_value_t prop_name = jerry_string_sz (test_entries[i].constructor_name); - jerry_value_t prop_value = jerry_object_get (global_obj_val, prop_name); + jerry_value_t prop_value = jerry_object_get (global_obj_val, test_entries[i].constructor_name); TEST_ASSERT (!jerry_value_is_exception (prop_value)); jerry_value_t length_arg = jerry_number (test_entries[i].element_count); jerry_value_t typedarray = jerry_construct (prop_value, &length_arg, 1); - jerry_value_free (prop_name); jerry_value_free (prop_value); jerry_value_free (length_arg); @@ -223,7 +220,7 @@ test_typedarray_complex_creation (test_entry_t test_entries[], /**< test cases * { const uint32_t arraybuffer_size = 256; - for (uint32_t i = 0; test_entries[i].constructor_name != NULL; i++) + for (uint32_t i = 0; !jerry_value_is_undefined (test_entries[i].constructor_name); i++) { const uint32_t offset = 8; uint32_t element_count = test_entries[i].element_count; @@ -248,8 +245,8 @@ test_typedarray_complex_creation (test_entry_t test_entries[], /**< test cases * jerry_value_t js_offset = jerry_number (offset); jerry_value_t js_element_count = jerry_number (element_count); - register_js_value ("expected_offset", js_offset); - register_js_value ("expected_length", js_element_count); + register_js_value (jerry_sz_ascii ("expected_offset"), js_offset); + register_js_value (jerry_sz_ascii ("expected_length"), js_element_count); typedarray = jerry_typedarray_with_buffer_span (test_entries[i].typedarray_type, arraybuffer, offset, element_count); @@ -260,7 +257,7 @@ test_typedarray_complex_creation (test_entry_t test_entries[], /**< test cases * jerry_value_free (arraybuffer); } - register_js_value ("array", typedarray); + register_js_value (jerry_sz_ascii ("array"), typedarray); const jerry_char_t test_exptected_src[] = TEST_STRING_LITERAL ("assert (array.length == expected_length," @@ -324,7 +321,7 @@ test_property_by_index (test_entry_t test_entries[]) uint64_t test_uint64_numbers[5] = { 83, 0, 1, UINT32_MAX, UINT64_MAX }; int64_t test_int64_numbers[5] = { INT64_MAX, INT64_MIN, 0, INT32_MAX, INT32_MIN }; - for (uint32_t i = 0; test_entries[i].constructor_name != NULL; i++) + for (uint32_t i = 0; !jerry_value_is_undefined (test_entries[i].constructor_name); i++) { jerry_value_t test_number; uint32_t test_numbers_length = sizeof (test_int_numbers) / sizeof (int); @@ -496,6 +493,18 @@ test_property_by_index (test_entry_t test_entries[]) } } /* test_property_by_index */ +/** + * Test entries free + */ +static void +test_entries_free (test_entry_t test_entries[]) +{ + for (uint32_t i = 0; !jerry_value_is_undefined (test_entries[i].constructor_name); i++) + { + jerry_value_free (test_entries[i].constructor_name); + } +} /* test_entries_free */ + static void test_detached_arraybuffer (void) { @@ -575,11 +584,12 @@ main (void) } jerry_value_t function_val = jerry_function_external (assert_handler); - register_js_value ("assert", function_val); + register_js_value (jerry_sz_ascii ("assert"), function_val); jerry_value_free (function_val); test_entry_t test_entries[] = { -#define TEST_ENTRY(TYPE, CONSTRUCTOR, COUNT, BYTES_PER_ELEMENT) { TYPE, CONSTRUCTOR, COUNT, BYTES_PER_ELEMENT } +#define TEST_ENTRY(TYPE, CONSTRUCTOR, COUNT, BYTES_PER_ELEMENT) \ + { TYPE, jerry_sz_ascii (CONSTRUCTOR), COUNT, BYTES_PER_ELEMENT } TEST_ENTRY (JERRY_TYPEDARRAY_UINT8, "Uint8Array", 12, 1), TEST_ENTRY (JERRY_TYPEDARRAY_UINT8CLAMPED, "Uint8ClampedArray", 12, 1), @@ -595,7 +605,7 @@ main (void) TEST_ENTRY (JERRY_TYPEDARRAY_BIGINT64, "BigInt64Array", 12, 8), TEST_ENTRY (JERRY_TYPEDARRAY_BIGUINT64, "BigUint64Array", 12, 8), - TEST_ENTRY (JERRY_TYPEDARRAY_INVALID, NULL, 0, 0) + { JERRY_TYPEDARRAY_INVALID, jerry_undefined (), 0, 0 } #undef TEST_ENTRY }; @@ -624,9 +634,9 @@ main (void) jerry_value_t js_element_count = jerry_number (element_count); jerry_value_t js_expected_value = jerry_number (expected_value); - register_js_value ("array", array); - register_js_value ("expected_length", js_element_count); - register_js_value ("expected_value", js_expected_value); + register_js_value (jerry_sz_ascii ("array"), array); + register_js_value (jerry_sz_ascii ("expected_length"), js_element_count); + register_js_value (jerry_sz_ascii ("expected_value"), js_expected_value); jerry_value_free (js_element_count); jerry_value_free (js_expected_value); @@ -673,16 +683,18 @@ main (void) test_property_by_index (test_entries); + test_entries_free (test_entries); + /* test invalid things */ { jerry_value_t values[] = { jerry_number (11), jerry_boolean (false), - jerry_string_sz ("test"), + jerry_sz_ascii ("test"), jerry_object (), jerry_null (), jerry_arraybuffer (16), - jerry_error_sz (JERRY_ERROR_TYPE, "error"), + jerry_error_sz (JERRY_ERROR_TYPE, jerry_sz_ascii ("error")), jerry_undefined (), jerry_promise (), }; diff --git a/tests/unit-core/test-vm-exec-stop.c b/tests/unit-core/test-vm-exec-stop.c index 41cadba354..8492e76a17 100644 --- a/tests/unit-core/test-vm-exec-stop.c +++ b/tests/unit-core/test-vm-exec-stop.c @@ -30,7 +30,7 @@ vm_exec_stop_callback (void *user_p) return jerry_undefined (); } - return jerry_string_sz ("Abort script"); + return jerry_sz_ascii ("Abort script"); } /* vm_exec_stop_callback */ int diff --git a/tests/unit-core/test-vm-throw.c b/tests/unit-core/test-vm-throw.c index b4f2d20861..4115453176 100644 --- a/tests/unit-core/test-vm-throw.c +++ b/tests/unit-core/test-vm-throw.c @@ -100,7 +100,7 @@ native_handler (const jerry_call_info_t *call_info_p, /**< call info */ if (mode == 7) { - jerry_value_t result = jerry_throw_sz (JERRY_ERROR_COMMON, "Error!"); + jerry_value_t result = jerry_throw_sz (JERRY_ERROR_COMMON, jerry_sz_ascii ("Error!")); TEST_ASSERT (!jerry_exception_is_captured (result)); jerry_exception_allow_capture (result, false); @@ -185,7 +185,7 @@ main (void) /* Native functions may trigger the call twice: */ jerry_value_t global_object_value = jerry_current_realm (); jerry_value_t function_value = jerry_function_external (native_handler); - jerry_value_t function_name_value = jerry_string_sz ("native"); + jerry_value_t function_name_value = jerry_sz_ascii ("native"); jerry_value_free (jerry_object_set (global_object_value, function_name_value, function_value)); jerry_value_free (function_name_value); diff --git a/tests/unit-ext/module/jerry-module-test.c b/tests/unit-ext/module/jerry-module-test.c index d8352ed84c..435698f11a 100644 --- a/tests/unit-ext/module/jerry-module-test.c +++ b/tests/unit-ext/module/jerry-module-test.c @@ -202,7 +202,7 @@ main (int argc, char **argv) js_global = jerry_current_realm (); js_function = jerry_function_external (handle_require); - js_property_name = jerry_string_sz ("require"); + js_property_name = jerry_sz_ascii ("require"); res = jerry_object_set (js_global, js_property_name, js_function); TEST_ASSERT (!jerry_value_is_exception (res)); TEST_ASSERT (jerry_value_is_boolean (res) && jerry_value_is_true (res)); @@ -211,7 +211,7 @@ main (int argc, char **argv) jerry_value_free (js_function); js_function = jerry_function_external (handle_clear_require_cache); - js_property_name = jerry_string_sz ("clear_require_cache"); + js_property_name = jerry_sz_ascii ("clear_require_cache"); res = jerry_object_set (js_global, js_property_name, js_function); TEST_ASSERT (!jerry_value_is_exception (res)); TEST_ASSERT (jerry_value_is_boolean (res) && jerry_value_is_true (res)); diff --git a/tests/unit-ext/module/my-custom-module.c b/tests/unit-ext/module/my-custom-module.c index 2395bfcf00..0809f7b505 100644 --- a/tests/unit-ext/module/my-custom-module.c +++ b/tests/unit-ext/module/my-custom-module.c @@ -20,9 +20,10 @@ #define MODULE_NAME my_custom_module static void -jobject_set_property_jval (jerry_value_t jobj, const char *name, jerry_value_t value) +jobject_set_property_jval (jerry_value_t jobj, + jerry_value_t prop_name, /**< property name value that will be free/take */ + jerry_value_t value) { - jerry_value_t prop_name = jerry_string_sz (name); jerry_value_t ret_val = jerry_object_set (jobj, prop_name, value); jerry_value_free (prop_name); jerry_value_free (ret_val); @@ -43,11 +44,11 @@ my_custom_module_on_resolve (void) { jerry_value_t mymodule = jerry_object (); jerry_value_t val = jerry_number (42); - jobject_set_property_jval (mymodule, "number_value", val); + jobject_set_property_jval (mymodule, jerry_sz_ascii ("number_value"), val); jerry_value_free (val); jerry_value_t jfunc = jerry_function_external (call_function_with_callback); - jobject_set_property_jval (mymodule, "call_function_with_callback", jfunc); + jobject_set_property_jval (mymodule, jerry_sz_ascii ("call_function_with_callback"), jfunc); jerry_value_free (jfunc); return mymodule; diff --git a/tests/unit-ext/test-common.h b/tests/unit-ext/test-common.h index 5941f46184..562e9470cc 100644 --- a/tests/unit-ext/test-common.h +++ b/tests/unit-ext/test-common.h @@ -20,8 +20,6 @@ #define ARRAY_SIZE(array) ((unsigned long) (sizeof (array) / sizeof ((array)[0]))) -#define JERRY_UNUSED(x) ((void) (x)) - #define TEST_ASSERT(x) \ do \ { \ diff --git a/tests/unit-ext/test-ext-arg.c b/tests/unit-ext/test-ext-arg.c index d32b865833..44a6917573 100644 --- a/tests/unit-ext/test-ext-arg.c +++ b/tests/unit-ext/test-ext-arg.c @@ -183,7 +183,7 @@ my_custom_transform (jerryx_arg_js_iterator_t *js_arg_iter_p, /**< available JS { jerry_value_free (to_number); - return jerry_throw_sz (JERRY_ERROR_TYPE, "It can not be converted to a number."); + return jerry_throw_sz (JERRY_ERROR_TYPE, jerry_sz_ascii ("It can not be converted to a number.")); } int expected_num = (int) c_arg_p->extra_info; @@ -191,7 +191,7 @@ my_custom_transform (jerryx_arg_js_iterator_t *js_arg_iter_p, /**< available JS if (get_num != expected_num) { - return jerry_throw_sz (JERRY_ERROR_TYPE, "Number value is not expected."); + return jerry_throw_sz (JERRY_ERROR_TYPE, jerry_sz_ascii ("Number value is not expected.")); } return jerry_undefined (); @@ -311,18 +311,16 @@ test_validator_prop1_handler (const jerry_call_info_t *call_info_p, /**< call in double native2 = 0; double native3 = 3; - const char *name_p[] = { "prop1", "prop2", "prop3" }; + const jerry_value_t name_p[] = { jerry_sz_ascii ("prop1"), jerry_sz_ascii ("prop2"), jerry_sz_ascii ("prop3") }; jerryx_arg_t mapping[] = { jerryx_arg_boolean (&native1, JERRYX_ARG_COERCE, JERRYX_ARG_REQUIRED), jerryx_arg_number (&native2, JERRYX_ARG_COERCE, JERRYX_ARG_REQUIRED), jerryx_arg_number (&native3, JERRYX_ARG_COERCE, JERRYX_ARG_OPTIONAL) }; - jerry_value_t is_ok = jerryx_arg_transform_object_properties (args_p[0], - (const jerry_char_t **) name_p, - ARRAY_SIZE (name_p), - mapping, - ARRAY_SIZE (mapping)); + jerry_value_t is_ok = + jerryx_arg_transform_object_properties (args_p[0], name_p, ARRAY_SIZE (name_p), mapping, ARRAY_SIZE (mapping)); + jerry_value_list_free (name_p, ARRAY_SIZE (name_p)); TEST_ASSERT (!jerry_value_is_exception (is_ok)); TEST_ASSERT (native1); TEST_ASSERT (native2 == 1.5); @@ -350,13 +348,13 @@ test_validator_prop2_handler (const jerry_call_info_t *call_info_p, /**< call in jerryx_arg_object_props_t prop_info; - const char *name_p[] = { "prop1", "prop2", "prop3" }; + const jerry_value_t name_p[] = { jerry_sz_ascii ("prop1"), jerry_sz_ascii ("prop2"), jerry_sz_ascii ("prop3") }; jerryx_arg_t prop_mapping[] = { jerryx_arg_boolean (&native1, JERRYX_ARG_COERCE, JERRYX_ARG_REQUIRED), jerryx_arg_number (&native2, JERRYX_ARG_COERCE, JERRYX_ARG_REQUIRED), jerryx_arg_number (&native3, JERRYX_ARG_COERCE, JERRYX_ARG_OPTIONAL) }; - prop_info.name_p = (const jerry_char_t **) name_p; + prop_info.name_p = name_p; prop_info.name_cnt = 3; prop_info.c_arg_p = prop_mapping; prop_info.c_arg_cnt = 3; @@ -367,6 +365,7 @@ test_validator_prop2_handler (const jerry_call_info_t *call_info_p, /**< call in jerry_value_t is_ok = jerryx_arg_transform_args (args_p, args_cnt, mapping, ARRAY_SIZE (mapping)); + jerry_value_list_free (name_p, ARRAY_SIZE (name_p)); TEST_ASSERT (!jerry_value_is_exception (is_ok)); if (validator_prop_count == 1) @@ -392,19 +391,17 @@ test_validator_prop3_handler (const jerry_call_info_t *call_info_p, /**< call in bool native1 = false; bool native2 = true; - const char *name_p[] = { "prop1", "prop2" }; + const jerry_value_t name_p[] = { jerry_sz_ascii ("prop1"), jerry_sz_ascii ("prop2") }; jerryx_arg_t mapping[] = { jerryx_arg_boolean (&native1, JERRYX_ARG_COERCE, JERRYX_ARG_REQUIRED), jerryx_arg_boolean (&native2, JERRYX_ARG_COERCE, JERRYX_ARG_REQUIRED), }; - jerry_value_t is_ok = jerryx_arg_transform_object_properties (args_p[0], - (const jerry_char_t **) name_p, - ARRAY_SIZE (name_p), - mapping, - ARRAY_SIZE (mapping)); + jerry_value_t is_ok = + jerryx_arg_transform_object_properties (args_p[0], name_p, ARRAY_SIZE (name_p), mapping, ARRAY_SIZE (mapping)); + jerry_value_list_free (name_p, ARRAY_SIZE (name_p)); TEST_ASSERT (jerry_value_is_exception (is_ok)); TEST_ASSERT (!native1); TEST_ASSERT (native2); @@ -718,7 +715,7 @@ jerry_arg_to_double_or_bool_t (jerryx_arg_js_iterator_t *js_arg_iter_p, const je /* Fall through indicates that whatever they gave us, it wasn't * one of the types we were expecting... */ jerry_value_free (conversion_result); - return jerry_throw_sz (JERRY_ERROR_TYPE, "double_or_bool-type error."); + return jerry_throw_sz (JERRY_ERROR_TYPE, jerry_sz_ascii ("double_or_bool-type error.")); } /* jerry_arg_to_double_or_bool_t */ /** @@ -758,7 +755,7 @@ static void test_utf8_string (void) { /* test string: 'str: {DESERET CAPITAL LETTER LONG I}' */ - jerry_value_t str = jerry_string_sz ("\x73\x74\x72\x3a \xed\xa0\x81\xed\xb0\x80"); + jerry_value_t str = jerry_sz_cesu8 ("\x73\x74\x72\x3a \xed\xa0\x81\xed\xb0\x80"); char expect_utf8_buf[] = "\x73\x74\x72\x3a \xf0\x90\x90\x80"; size_t buf_len = sizeof (expect_utf8_buf) - 1; JERRY_VLA (char, buf, buf_len + 1); @@ -811,13 +808,12 @@ create_object_b_handler (const jerry_call_info_t *call_info_p, /**< call informa * Register a JavaScript function in the global object. */ static void -register_js_function (const char *name_p, /**< name of the function */ +register_js_function (jerry_value_t function_name_val, /**< name of the function that will be free/take */ jerry_external_handler_t handler_p) /**< function callback */ { jerry_value_t global_obj_val = jerry_current_realm (); jerry_value_t function_val = jerry_function_external (handler_p); - jerry_value_t function_name_val = jerry_string_sz (name_p); jerry_value_t result_val = jerry_object_set (global_obj_val, function_name_val, function_val); jerry_value_free (function_name_val); @@ -834,20 +830,20 @@ main (void) test_utf8_string (); - register_js_function ("test_validator1", test_validator1_handler); - register_js_function ("test_validator2", test_validator2_handler); - register_js_function ("test_validator3", test_validator3_handler); - register_js_function ("test_validator_int1", test_validator_int1_handler); - register_js_function ("test_validator_int2", test_validator_int2_handler); - register_js_function ("test_validator_int3", test_validator_int3_handler); - register_js_function ("MyObjectA", create_object_a_handler); - register_js_function ("MyObjectB", create_object_b_handler); - register_js_function ("test_validator_prop1", test_validator_prop1_handler); - register_js_function ("test_validator_prop2", test_validator_prop2_handler); - register_js_function ("test_validator_prop3", test_validator_prop3_handler); - register_js_function ("test_validator_array1", test_validator_array1_handler); - register_js_function ("test_validator_array2", test_validator_array2_handler); - register_js_function ("test_validator_restore", test_validator_restore_handler); + register_js_function (jerry_sz_ascii ("test_validator1"), test_validator1_handler); + register_js_function (jerry_sz_ascii ("test_validator2"), test_validator2_handler); + register_js_function (jerry_sz_ascii ("test_validator3"), test_validator3_handler); + register_js_function (jerry_sz_ascii ("test_validator_int1"), test_validator_int1_handler); + register_js_function (jerry_sz_ascii ("test_validator_int2"), test_validator_int2_handler); + register_js_function (jerry_sz_ascii ("test_validator_int3"), test_validator_int3_handler); + register_js_function (jerry_sz_ascii ("MyObjectA"), create_object_a_handler); + register_js_function (jerry_sz_ascii ("MyObjectB"), create_object_b_handler); + register_js_function (jerry_sz_ascii ("test_validator_prop1"), test_validator_prop1_handler); + register_js_function (jerry_sz_ascii ("test_validator_prop2"), test_validator_prop2_handler); + register_js_function (jerry_sz_ascii ("test_validator_prop3"), test_validator_prop3_handler); + register_js_function (jerry_sz_ascii ("test_validator_array1"), test_validator_array1_handler); + register_js_function (jerry_sz_ascii ("test_validator_array2"), test_validator_array2_handler); + register_js_function (jerry_sz_ascii ("test_validator_restore"), test_validator_restore_handler); jerry_value_t parsed_code_val = jerry_parse (test_source, sizeof (test_source) - 1, NULL); TEST_ASSERT (!jerry_value_is_exception (parsed_code_val)); diff --git a/tests/unit-ext/test-ext-method-register.c b/tests/unit-ext/test-ext-method-register.c index 566ea9c6c7..ca39832da4 100644 --- a/tests/unit-ext/test-ext-method-register.c +++ b/tests/unit-ext/test-ext-method-register.c @@ -39,13 +39,12 @@ method_hello (const jerry_call_info_t *call_info_p, /**< call information */ */ static void freeze_property (jerry_value_t target_obj, /**< target object */ - const char *target_prop) /**< target property name */ + jerry_value_t prop_name) /**< target property name that will be free/take */ { // "freeze" property jerry_property_descriptor_t prop_desc = jerry_property_descriptor (); prop_desc.flags |= JERRY_PROP_IS_CONFIGURABLE_DEFINED; - jerry_value_t prop_name = jerry_string_sz (target_prop); jerry_value_t return_value = jerry_object_define_own_prop (target_obj, prop_name, &prop_desc); TEST_ASSERT (jerry_value_is_boolean (return_value)); jerry_value_free (return_value); @@ -81,7 +80,7 @@ test_simple_registration (void) jerry_value_free (register_result.result); jerry_value_t global_obj = jerry_current_realm (); - jerry_object_set_sz (global_obj, "test", target_object); + jerry_object_set_sz (global_obj, jerry_sz_ascii ("test"), target_object); jerry_value_free (target_object); jerry_value_free (global_obj); @@ -162,12 +161,12 @@ test_error_setvalue (void) { jerry_init (JERRY_INIT_EMPTY); - const char *target_prop = "test_err"; +#define target_prop "test_err" jerry_value_t global_obj = jerry_current_realm (); - freeze_property (global_obj, target_prop); + freeze_property (global_obj, jerry_sz_ascii (target_prop)); jerry_value_t new_object = jerry_object (); - jerry_value_t set_result = jerry_object_set_sz (global_obj, target_prop, new_object); + jerry_value_t set_result = jerry_object_set_sz (global_obj, jerry_sz_ascii (target_prop), new_object); TEST_ASSERT (jerry_value_is_exception (set_result)); jerry_value_free (set_result); @@ -188,9 +187,9 @@ test_error_single_function (void) { jerry_init (JERRY_INIT_EMPTY); - const char *target_prop = "test_err"; +#define target_prop "test_err" jerry_value_t target_object = jerry_object (); - freeze_property (target_object, target_prop); + freeze_property (target_object, jerry_sz_ascii (target_prop)); jerryx_property_entry methods[] = { JERRYX_PROPERTY_FUNCTION (target_prop, method_hello), // This registration should fail @@ -217,11 +216,11 @@ test_error_multiple_functions (void) { jerry_init (JERRY_INIT_EMPTY); - const char *prop_ok = "prop_ok"; - const char *prop_err = "prop_err"; - const char *prop_not = "prop_not"; +#define prop_ok "prop_ok" +#define prop_err "prop_err" +#define prop_not "prop_not" jerry_value_t target_object = jerry_object (); - freeze_property (target_object, prop_err); + freeze_property (target_object, jerry_sz_ascii (prop_err)); jerryx_property_entry methods[] = { JERRYX_PROPERTY_FUNCTION (prop_ok, method_hello), // This registration is ok @@ -240,7 +239,7 @@ test_error_multiple_functions (void) { // Test if property "prop_ok" is correctly registered. - jerry_value_t prop_ok_val = jerry_string_sz (prop_ok); + jerry_value_t prop_ok_val = jerry_sz_ascii (prop_ok); jerry_value_t prop_ok_exists = jerry_object_has_own (target_object, prop_ok_val); TEST_ASSERT (jerry_value_is_true (prop_ok_exists)); jerry_value_free (prop_ok_exists); @@ -267,7 +266,7 @@ test_error_multiple_functions (void) { // The "prop_err" should exist - as it was "freezed" - but it should not be a function - jerry_value_t prop_err_val = jerry_string_sz (prop_err); + jerry_value_t prop_err_val = jerry_sz_ascii (prop_err); jerry_value_t prop_err_exists = jerry_object_has_own (target_object, prop_err_val); TEST_ASSERT (jerry_value_is_true (prop_err_exists)); jerry_value_free (prop_err_exists); @@ -278,7 +277,7 @@ test_error_multiple_functions (void) } { // The "prop_not" is not available on the target object - jerry_value_t prop_not_val = jerry_string_sz (prop_not); + jerry_value_t prop_not_val = jerry_sz_ascii (prop_not); jerry_value_t prop_not_exists = jerry_object_has_own (target_object, prop_not_val); TEST_ASSERT (jerry_value_is_true (prop_not_exists) == false); jerry_value_free (prop_not_exists); diff --git a/tests/unit-ext/test-ext-module-canonical.c b/tests/unit-ext/test-ext-module-canonical.c index 8bad59f8ca..9e95a18463 100644 --- a/tests/unit-ext/test-ext-module-canonical.c +++ b/tests/unit-ext/test-ext-module-canonical.c @@ -37,7 +37,7 @@ get_canonical_name (const jerry_value_t name) } else if (!strcmp ((char *) name_string, ALIAS_NAME)) { - return jerry_string_sz (ACTUAL_NAME); + return jerry_sz_ascii (ACTUAL_NAME); } else { @@ -76,8 +76,8 @@ main (int argc, char **argv) jerry_init (JERRY_INIT_EMPTY); - jerry_value_t actual_name = jerry_string_sz (ACTUAL_NAME); - jerry_value_t alias_name = jerry_string_sz (ALIAS_NAME); + jerry_value_t actual_name = jerry_sz_ascii (ACTUAL_NAME); + jerry_value_t alias_name = jerry_sz_ascii (ALIAS_NAME); /* It's important that we resolve by the non-canonical name first. */ jerry_value_t result2 = jerryx_module_resolve (alias_name, &resolver, 1); @@ -86,7 +86,7 @@ main (int argc, char **argv) jerry_value_free (alias_name); /* An elaborate way of doing strict equal - set a property on one object and it "magically" appears on the other. */ - jerry_value_t prop_name = jerry_string_sz ("something"); + jerry_value_t prop_name = jerry_sz_ascii ("something"); jerry_value_t prop_value = jerry_number (TEST_VALUE); jerry_value_free (jerry_object_set (result1, prop_name, prop_value)); jerry_value_free (prop_value); diff --git a/tests/unit-ext/test-ext-module-empty.c b/tests/unit-ext/test-ext-module-empty.c index 353b2fa266..bf1b75c42c 100644 --- a/tests/unit-ext/test-ext-module-empty.c +++ b/tests/unit-ext/test-ext-module-empty.c @@ -33,7 +33,7 @@ main (int argc, char **argv) jerry_init (JERRY_INIT_EMPTY); /* Attempt to load a non-existing module. */ - module_name = jerry_string_sz ("some-unknown-module-name"); + module_name = jerry_sz_ascii ("some-unknown-module-name"); jerry_value_t module = jerryx_module_resolve (module_name, &resolver, 1); jerry_value_free (module_name); @@ -41,7 +41,7 @@ main (int argc, char **argv) /* Retrieve the error message. */ module = jerry_exception_value (module, true); - jerry_value_t prop_name = jerry_string_sz ("message"); + jerry_value_t prop_name = jerry_sz_ascii ("message"); jerry_value_t prop = jerry_object_get (module, prop_name); /* Assert that the error message is a string with specific contents. */ @@ -56,7 +56,7 @@ main (int argc, char **argv) jerry_value_free (prop_name); /* Retrieve the moduleName property. */ - prop_name = jerry_string_sz ("moduleName"); + prop_name = jerry_sz_ascii ("moduleName"); prop = jerry_object_get (module, prop_name); /* Assert that the moduleName property is a string containing the requested module name. */