From 9f05ccd46c3a82b36b35d2ab2dd8963e3c23423b Mon Sep 17 00:00:00 2001 From: Yonggang Luo Date: Mon, 9 Dec 2024 21:54:19 +0800 Subject: [PATCH] Reduce the usage of strlen * Improve jerry_string_sz only accept UTF-8 string(that's a rare case accept CESU-8 in c/cpp code) * The document about jerry_string_sz only support ASCII(the fact is CESU-8 before this MR), update it to support UTF-8 after this MR * Improve all _sz function to take jerry_value_t that can construct from `jerry_string_sz` * Improve JERRY_ZSTR_ARG can only accept string literal(that's UTF-8) * Add function jerry_value_list_free to free a list of jerry_value_t * All call to jerry_string/jerry_string_cesu8 in core indeed are removed, so when there is no linkage to it, code size is saved The prototype of new/improved function/macros is: ```c 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); #define jerry_string_sz(str) jerry_string_utf8 (JERRY_ZSTR_ARG (str)) jerry_value_t jerry_error_sz (jerry_error_t error_type, const jerry_value_t message_sz); jerry_value_t jerry_throw_sz (jerry_error_t error_type, const jerry_value_t message_sz); jerry_value_t jerry_regexp_sz (const jerry_value_t pattern_sz, uint16_t flags); jerry_value_t jerry_object_delete_sz (const jerry_value_t object, const jerry_value_t key_sz); jerry_value_t jerry_object_get_sz (const jerry_value_t object, const jerry_value_t key_sz); jerry_value_t jerry_object_has_sz (const jerry_value_t object, const jerry_value_t key_sz); jerry_value_t jerry_object_set_sz (jerry_value_t object, const jerry_value_t key_sz, const jerry_value_t value); ``` JerryScript-DCO-1.0-Signed-off-by: Yonggang Luo luoyonggang@gmail.com --- docs/02.API-REFERENCE.md | 198 +++++++++++++---- docs/03.API-EXAMPLE.md | 33 +-- docs/06.REFERENCE-COUNTING.md | 2 +- docs/07.DEBUGGER.md | 5 +- docs/09.EXT-REFERENCE-ARG.md | 42 ++-- docs/10.EXT-REFERENCE-HANDLER.md | 19 +- docs/12.EXT-REFERENCE-MODULE.md | 2 +- jerry-core/api/jerry-module.c | 6 +- jerry-core/api/jerry-snapshot.c | 8 +- jerry-core/api/jerryscript.c | 203 +++++++++++------- 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 | 63 ++++-- jerry-core/ecma/operations/ecma-exceptions.h | 2 + jerry-core/include/jerryscript-core.h | 58 +++-- jerry-core/include/jerryscript-types.h | 16 +- jerry-core/lit/lit-strings.c | 16 -- jerry-core/lit/lit-strings.h | 1 - jerry-ext/arg/arg-transform-functions.c | 23 +- jerry-ext/arg/arg.c | 14 +- jerry-ext/include/jerryscript-ext/arg.h | 4 +- .../include/jerryscript-ext/autorelease.h | 2 +- jerry-ext/include/jerryscript-ext/module.h | 28 +-- .../include/jerryscript-ext/properties.h | 62 +++--- jerry-ext/module/module.c | 22 +- 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 | 10 +- 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 +- .../baremetal-sdk/espressif/main/jerry-main.c | 2 +- targets/os/mbedos/jerry-main.cpp | 9 +- targets/os/nuttx/jerry-main.c | 11 +- targets/os/riot/source/jerry-main.c | 2 +- targets/os/zephyr/src/jerry-main.c | 2 +- .../test-api-binary-operations-arithmetics.c | 2 +- .../test-api-binary-operations-comparisons.c | 2 +- tests/unit-core/test-api-errortype.c | 2 +- tests/unit-core/test-api-functiontype.c | 2 +- tests/unit-core/test-api-iteratortype.c | 2 +- .../test-api-object-property-names.c | 29 ++- tests/unit-core/test-api-objecttype.c | 2 +- tests/unit-core/test-api-promise.c | 2 +- .../test-api-set-and-clear-error-flag.c | 2 +- tests/unit-core/test-api-value-type.c | 4 +- tests/unit-core/test-api.c | 52 +++-- tests/unit-core/test-arraybuffer.c | 9 +- tests/unit-core/test-backtrace.c | 29 ++- tests/unit-core/test-container-operation.c | 4 +- tests/unit-core/test-error-callback.c | 2 +- tests/unit-core/test-external-string.c | 17 +- tests/unit-core/test-json.c | 2 +- tests/unit-core/test-literal-storage.c | 2 +- tests/unit-core/test-module-dynamic.c | 2 +- tests/unit-core/test-module.c | 15 +- tests/unit-core/test-newtarget.c | 5 +- tests/unit-core/test-number-converter.c | 2 +- tests/unit-core/test-promise.c | 13 +- tests/unit-core/test-proxy.c | 24 +-- tests/unit-core/test-realm.c | 26 ++- tests/unit-core/test-regexp-dotall-unicode.c | 4 +- tests/unit-core/test-regexp.c | 4 +- tests/unit-core/test-string-to-number.c | 17 +- tests/unit-core/test-symbol.c | 18 +- tests/unit-core/test-to-property-descriptor.c | 13 +- tests/unit-core/test-typedarray.c | 50 +++-- tests/unit-core/test-vm-throw.c | 2 +- tests/unit-ext/module/my-custom-module.c | 9 +- tests/unit-ext/test-ext-arg.c | 64 +++--- tests/unit-ext/test-ext-method-register.c | 23 +- 78 files changed, 834 insertions(+), 642 deletions(-) diff --git a/docs/02.API-REFERENCE.md b/docs/02.API-REFERENCE.md index 0e3558f80f..0dfdae8393 100644 --- a/docs/02.API-REFERENCE.md +++ b/docs/02.API-REFERENCE.md @@ -3836,7 +3836,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_string_sz ("error msg")); jerry_error_t error_type = jerry_error_type (error_obj); // error_type is now JERRY_ERROR_RANGE. @@ -3958,7 +3958,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_string_sz ("Message"))); jerry_cleanup (); } /* main */ @@ -4153,7 +4153,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_string_sz ("Error!")); /* Ignore calling the vm_throw_callback function. */ jerry_exception_allow_capture (result_value, false); @@ -4541,7 +4541,7 @@ 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!"; +#define string_p "This is a long external string, should not be duplicated!" jerry_value_t external_string = jerry_string_external_sz (string_p, NULL); /* The external_string_free_callback is called. */ jerry_value_free (external_string); @@ -4602,7 +4602,7 @@ 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); @@ -7158,12 +7158,12 @@ jerry_boolean (bool value); **Summary** -Create new JavaScript Error object with the specified error message. - -Important! The `error_type` argument *must not be* `JERRY_ERROR_NONE`. -Creating an Error object with no error type is not valid. +Create an Error object with the provided `message` string value as the error `message` property. +If the `message` value is not a string, the created error will not have a `message` property. *Note*: +- Important! The `error_type` argument *must not be* `JERRY_ERROR_NONE`. + Creating an Error object with no error type is not valid. - Returned value must be freed with [jerry_value_free](#jerry_value_free) when it is no longer needed. @@ -7206,21 +7206,25 @@ 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 `message_sz` string value as the error `message` property. +If the `message_sz` value is not a string, the created error will not have a `message` property. *Note*: +- Important! The `error_type` argument *must not be* `JERRY_ERROR_NONE`. + Creating an Error object with no error type is not valid. - Returned value must be freed with [jerry_value_free](#jerry_value_free) when it is no longer needed. +- The `message_sz` value will be freed in this function. **Prototype** ```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_sz); ``` - `error_type` - type of the error -- `message_p` - value of 'message' property of the constructed error object +- `message_sz` - message of the error that will be free/take - return value - constructed error object *Renamed in version 3.0, it was previously known as `jerry_create_error` in earlier versions.* @@ -7229,7 +7233,7 @@ jerry_error_sz (jerry_error_t error_type, const char *message_p); ```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_string_sz ("error")); ... // usage of error_obj @@ -7757,20 +7761,22 @@ main (void) **Summary** -Create string from a zero-terminated ASCII string. +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. *Note*: +- This is a macro that only accept literal string - 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_sz (const char *str_p); +#define jerry_string_sz(str) ``` -- `str_p` - non-null pointer to zero-terminated string +- `str` - A zero-terminated UTF-8 encoded literal string - return value - created string *Renamed in version 3.0, it was previously known as `jerry_create_string` in earlier versions.* @@ -7779,7 +7785,7 @@ jerry_string_sz (const char *str_p); ```c { - const char char_array[] = "a string"; +#define char_array "a string" jerry_value_t string_value = jerry_string_sz (char_array); ... // usage of string_value @@ -7790,7 +7796,7 @@ jerry_string_sz (const char *str_p); **See also** -- [jerry_string](#jerry_string) +- [jerry_string_utf8](#jerry_string_utf8) ## jerry_string @@ -7839,17 +7845,107 @@ jerry_string (const jerry_char_t *buffer_p, - [jerry_validate_string](#jerry_validate_string) - [jerry_string_sz](#jerry_string_sz) +- [jerry_string_utf8](#jerry_string_utf8) +- [jerry_string_cesu8](#jerry_string_cesu8) + + +## jerry_string_utf8 + +**Summary** + +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. + +*Note*: +- 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_utf8 (const jerry_char_t *buffer_p, + jerry_size_t buf_size) +``` + +- `buffer_p` - non-null pointer to buffer +- `buf_size` - size of the buffer + +**Example** + +```c +{ + const jerry_char_t char_array[] = "a string"; + jerry_value_t string_value = jerry_string_utf8 (char_array, + sizeof (char_array) - 1); + + ... // usage of string_value + + jerry_value_free (string_value); +} + +``` + +**See also** + +- [jerry_validate_string](#jerry_validate_string) +- [jerry_string_sz](#jerry_string_sz) +- [jerry_string](#jerry_string) + + +## jerry_string_cesu8 + +**Summary** + +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. + +*Note*: +- 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_cesu8 (const jerry_char_t *buffer_p, + jerry_size_t buf_size) +``` + +- `buffer_p` - non-null pointer to buffer +- `buf_size` - size of the buffer + +**Example** + +```c +{ + const jerry_char_t char_array[] = "\xed\xa0\x83\xed\xb2\x80"; + jerry_value_t string_value = jerry_string_cesu8 (char_array, + sizeof (char_array) - 1); + + ... // usage of string_value + + jerry_value_free (string_value); +} + +``` + +**See also** + +- [jerry_validate_string](#jerry_validate_string) +- [jerry_string](#jerry_string) ## 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. +Create external string from the zero-terminated CESU encoded literal string. +The content of the buffer is assumed be encoded correctly, it's the callers +responsibility to validate the input. *Note*: +- This is a macro that only accept literal string - 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. @@ -7857,13 +7953,12 @@ the duplication of large strings. **Prototype** ```c -jerry_value_t -jerry_string_external_sz (const char *str_p, void *user_p); +#define jerry_string_external_sz(str, user_p) ``` -- `str_p` - non-null pointer to a zero-terminated string +- `str_p` - A zero-terminated CESU-8 encoded literal string - `user_p` - user pointer passed to the callback when the string is freed -- return value - value of the created string +- return value - created external string *Introduced in version 2.4*. @@ -8096,10 +8191,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_string_sz (pattern), pattern_flags); ... @@ -8141,7 +8236,7 @@ jerry_regexp (const jerry_value_t pattern, uint16_t flags); { jerry_char_t pattern_p = "[cgt]gggtaaa|tttaccc[acg]"; jerry_size_t pattern_size = sizeof (pattern_p) - 1; - jerry_value_t pattern_str = jerry_string (pattern_p, pattern_size, JERRY_ENCODING_UTF8); + jerry_value_t pattern_str = jerry_string_utf8 (pattern_p, pattern_size); uint16_t pattern_flags = JERRY_REGEXP_FLAG_IGNORE_CASE; @@ -8569,16 +8664,18 @@ main (void) Checks whether the object or its prototype objects have the given property. -*Note*: Returned value must be freed with [jerry_value_free](#jerry_value_free) when it is no longer needed. +*Note*: +- Returned value must be freed with [jerry_value_free](#jerry_value_free) when it is no longer needed. +- `key_sz` will be freed in this function **Prototype** ```c -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); ``` - `object` - object value -- `key_p` - property name (zero-terminated string) +- `key_sz` - property key that will be free/take - return value - JavaScript value that evaluates to - exception - if the operation fail - true/false API value - depend on whether the property exists @@ -8599,7 +8696,7 @@ main (void) jerry_value_t global_object = jerry_current_realm (); - jerry_value_t has_prop_js = jerry_object_has_sz (global_object, "handler_field"); + jerry_value_t has_prop_js = jerry_object_has_sz (global_object, jerry_string_sz ("handler_field")); bool has_prop = jerry_value_is_true (has_prop_js); jerry_value_free (has_prop_js); @@ -8747,6 +8844,9 @@ main (void) Delete a property from an object. +*Note*: +- Returned value must be freed with [jerry_value_free](#jerry_value_free) when it is no longer needed. + **Prototype** ```c @@ -8793,14 +8893,18 @@ jerry_object_delete (const jerry_value_t obj_val, Delete a property from an object. +*Note*: +- Returned value must be freed with [jerry_value_free](#jerry_value_free) when it is no longer needed. +- `key_sz` will be freed in this function + **Prototype** ```c -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); ``` - `obj_val` - object value -- `key_p` - property name (zero-terminated string) +- `key_sz` - < property name that will be free/take - return value - true, if property was deleted successfully - exception, otherwise @@ -8813,7 +8917,7 @@ jerry_value_t jerry_object_delete_sz (const jerry_value_t object, const char *ke { jerry_value_t global_object = jerry_current_realm (); - jerry_value_t delete_result = jerry_object_delete_sz (global_object, "my_prop"); + jerry_value_t delete_result = jerry_object_delete_sz (global_object, jerry_string_sz ("my_prop")); /* use "delete_result" */ jerry_value_free (delete_result); @@ -8995,17 +9099,18 @@ main (void) Get value of a property to the specified object with the given name. -*Note*: Returned value must be freed with [jerry_value_free](#jerry_value_free) when it -is no longer needed. +*Note*: +- Returned value must be freed with [jerry_value_free](#jerry_value_free) when it is no longer needed. +- `key_sz` will be freed in this function **Prototype** ```c -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); ``` - `obj_val` - object value -- `key_p` - property name (zero-terminated string) +- `key_sz` - property key that will be free/take - return value - value of property, if success - thrown exception, otherwise @@ -9026,7 +9131,7 @@ main (void) jerry_value_t global_object = jerry_current_realm (); - jerry_value_t prop_value = jerry_object_get_sz (global_object, "Object"); + jerry_value_t prop_value = jerry_object_get_sz (global_object, jerry_string_sz ("Object")); /* use "prop_value" then release it. */ @@ -9303,17 +9408,18 @@ jerry_object_set (const jerry_value_t obj_val, Set a property to the specified object with the given name. -*Note*: Returned value must be freed with [jerry_value_free](#jerry_value_free) when it -is no longer needed. +*Note*: +- Returned value must be freed with [jerry_value_free](#jerry_value_free) when it is no longer needed. +- `key_sz` will be freed in this function **Prototype** ```c -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); ``` - `obj_val` - object value -- `key_p` - property name (zero-terminated string) +- `key_sz` - property key that will be free/take - `value_to_set` - value to set - return value - true, if success @@ -9331,7 +9437,7 @@ jerry_value_t jerry_object_set_sz (jerry_value_t object, const char *key_p, cons jerry_value_t glob_obj = jerry_current_realm (); - jerry_value_t set_result = jerry_object_set_sz (glob_obj, "my_prop", value_to_set); + jerry_value_t set_result = jerry_object_set_sz (glob_obj, jerry_string_sz ("my_prop"), value_to_set); ... // check result of property set call diff --git a/docs/03.API-EXAMPLE.md b/docs/03.API-EXAMPLE.md index e8616cc44e..9c4507deaa 100644 --- a/docs/03.API-EXAMPLE.md +++ b/docs/03.API-EXAMPLE.md @@ -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_string_sz ("print"), jerryx_handler_print); /* Setup Global scope code */ jerry_value_t parsed_code = jerry_parse (script, script_size, NULL); @@ -470,7 +478,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_string_sz ("print"), jerryx_handler_print); /* Getting pointer to the Global object */ jerry_value_t global_object = jerry_current_realm (); @@ -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_string_sz ("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_utf8 (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_string_sz ("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 (); @@ -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_string_sz ("print"), jerryx_handler_print); /* Create a JS object */ const jerry_char_t my_js_object[] = " \ @@ -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_string_sz ("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/06.REFERENCE-COUNTING.md b/docs/06.REFERENCE-COUNTING.md index cb207ea76f..1ea38cff5f 100644 --- a/docs/06.REFERENCE-COUNTING.md +++ b/docs/06.REFERENCE-COUNTING.md @@ -135,7 +135,7 @@ jerry_value_t my_external_handler (const jerry_value_t function_obj, /* If the value to be returned is needed for other purposes the * jerry_value_copy () can be used to create new references. */ - return jerry_string (...); + return jerry_string_utf8 (...); } ``` diff --git a/docs/07.DEBUGGER.md b/docs/07.DEBUGGER.md index 1885f093ac..0028cd1fba 100644 --- a/docs/07.DEBUGGER.md +++ b/docs/07.DEBUGGER.md @@ -320,9 +320,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..d51063eb19 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_string_sz ("enable"), + jerry_string_sz ("data"), + jerry_string_sz ("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..cc4e5253e3 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_utf8 (common_functions[i].name_p, common_functions[i].name_size), common_functions[i].handler_p); } diff --git a/docs/12.EXT-REFERENCE-MODULE.md b/docs/12.EXT-REFERENCE-MODULE.md index 44455d84b6..bf592f6834 100644 --- a/docs/12.EXT-REFERENCE-MODULE.md +++ b/docs/12.EXT-REFERENCE-MODULE.md @@ -205,7 +205,7 @@ canonicalize_file_path (const jerry_value_t name) /** * Since a file on the file system can be referred to by multiple relative paths, but only by one absolute path, the * absolute path becomes the canonical name for the module. Thus, to establish this canonical name, we must search - * name for "./" and "../", follow symlinks, etc., then create absolute_path via jerry_string () and return + * name for "./" and "../", follow symlinks, etc., then create absolute_path via jerry_string_utf8 () and return * it, because it is the canonical name for this module. Thus, we avoid loading the same JavaScript file twice. */ diff --git a/jerry-core/api/jerry-module.c b/jerry-core/api/jerry-module.c index 0e5f6eae02..8e9cac1d20 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_string_sz ("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_string_sz ("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..9d59b456a0 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_string_sz (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_string_sz (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_string_sz (error_buffer_too_small_p)); return 0; } diff --git a/jerry-core/api/jerryscript.c b/jerry-core/api/jerryscript.c index d8174b92c0..45da07124b 100644 --- a/jerry-core/api/jerryscript.c +++ b/jerry-core/api/jerryscript.c @@ -2344,6 +2344,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 * @@ -2375,8 +2388,13 @@ jerry_boolean (bool value) /**< bool value from which a jerry_value_t will be cr } /* jerry_boolean */ /** - * 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. + * Create an Error object with the provided `message` string value as the error `message` property. + * If the `message` value is not a string, the created error will not have a `message` property. + * + * @note + * - Important! The `error_type` argument *must not be* `JERRY_ERROR_NONE`. + * Creating an Error object with no error type is not valid. + * - Returned value must be freed with [jerry_value_free](#jerry_value_free) when it is no longer needed. * * @return Error object */ @@ -2386,44 +2404,37 @@ 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 `message_sz` string value as the error `message` property. + * If the `message_sz` value is not a string, the created error will not have a `message` property. + * + * @note + * - Important! The `error_type` argument *must not be* `JERRY_ERROR_NONE`. + * Creating an Error object with no error type is not valid. + * - Returned value must be freed with [jerry_value_free](#jerry_value_free) when it is no longer needed. + * - The `message_sz` value 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_sz) /**< 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_sz (error_type, message_sz); } /* jerry_error_sz */ /** - * 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. + * Create an exception by constructing an Error object with the specified type and the provided `message` string value + * as the error `message` property. If the `message` value is not a string, the created error will not have a `message` + * property. + * + * @note + * Returned value must be freed with [jerry_value_free](#jerry_value_free) when it is no longer needed. * * @return exception value */ @@ -2435,17 +2446,21 @@ 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 `message_sz` string + * value as the error `message` property. If the `message_sz` value is not a string, the created error will not have a + * `message` property. + * + * @note + * Returned value must be freed with [jerry_value_free](#jerry_value_free) when it is no longer needed. + * The `message_sz` 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_sz) /**< 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_sz), true); } /* jerry_throw_sz */ /** @@ -2652,23 +2667,56 @@ 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 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 + * + * @note + * - Returned value must be freed with [jerry_value_free](#jerry_value_free) when it is no longer needed. + * + * @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 + * + * @note + * - Returned value must be freed with [jerry_value_free](#jerry_value_free) when it is no longer needed. * * @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_cesu8 jerry_string_utf8 + * + * @note + * - Returned value must be freed with [jerry_value_free](#jerry_value_free) when it is no longer needed. * * @return created string */ @@ -2677,50 +2725,33 @@ 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_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 + * + * @note + * - Returned value must be freed with [jerry_value_free](#jerry_value_free) when it is no longer needed. * * @return created external string */ @@ -2731,7 +2762,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 */ @@ -2785,18 +2816,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 */ @@ -3130,18 +3163,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 */ @@ -3231,18 +3266,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 */ @@ -3346,6 +3383,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 @@ -3353,13 +3391,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 */ @@ -3524,6 +3561,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 @@ -3531,14 +3569,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 */ 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..ceb1e5625a 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_cesu8_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..a66c911e99 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_sz (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..b8450e000e 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_sz (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..c3154831c7 100644 --- a/jerry-core/ecma/operations/ecma-exceptions.c +++ b/jerry-core/ecma/operations/ecma-exceptions.c @@ -168,6 +168,53 @@ 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 `message` string value as the error `message` property. + * If the `message` value is not a string, the created error will not have a `message` property. + * + * @note + * - Important! The `error_type` argument *must not be* `JERRY_ERROR_NONE`. + * Creating an Error object with no error type is not valid. + * - Returned value must be freed with [jerry_value_free](#jerry_value_free) when it is no longer needed. + * + * @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 `message_sz` string value as the error `message` property. + * If the `message_sz` value is not a string, the created error will not have a `message` property. + * + * @note + * - Important! The `error_type` argument *must not be* `JERRY_ERROR_NONE`. + * Creating an Error object with no error type is not valid. + * - Returned value must be freed with [jerry_value_free](#jerry_value_free) when it is no longer needed. + * - The `message_sz` value will be freed in this function. + * + * @return Error object + */ +ecma_value_t +ecma_error_value_sz (jerry_error_t error_type, /**< type of error */ + ecma_value_t message_sz) /**< message of the error that will be free/take */ +{ + ecma_value_t error = ecma_error_value (error_type, message_sz); + ecma_free_value (message_sz); + return error; +} /* ecma_error_value_sz */ + /** * aggregate-error object constructor. * @@ -298,21 +345,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_sz (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..652d6e4ea1 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_sz (jerry_error_t error_type, ecma_value_t message_sz); #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-core.h b/jerry-core/include/jerryscript-core.h index b65a509768..677ae65dfe 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_sz); 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,11 +429,44 @@ 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_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); + +/** + * 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. + * + * @note + * - This is a macro that only accept literal string + * - Returned value must be freed with [jerry_value_free](#jerry_value_free) when it is no longer needed. + * + * @return created string + */ +#define jerry_string_sz(str) jerry_string_utf8 (JERRY_ZSTR_ARG (str)) + 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); + +/** + * Create external string 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. + * + * @note + * - This is a macro that only accept literal string + * - 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. + * + * @param str zero-terminated CESU-8 encoded literal string + * @param user_p user pointer passed to the callback when the string is freed + * + * @return created external string + */ +#define jerry_string_external_sz(str, user_p) jerry_string_external (JERRY_ZSTR_ARG (str), user_p) + /** - * jerry-api-string-cotr @} + * jerry-api-string-ctor @} */ /** @@ -550,7 +584,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 +602,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 +615,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 +636,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 +1049,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 +1067,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_sz); /** * jerry-api-error-ctor @} */ diff --git a/jerry-core/include/jerryscript-types.h b/jerry-core/include/jerryscript-types.h index 908382b237..e5b5d354f7 100644 --- a/jerry-core/include/jerryscript-types.h +++ b/jerry-core/include/jerryscript-types.h @@ -152,6 +152,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 zero-terminated ASCII/UTF-8/CESU-8 string */ + jerry_size_t size; /**< size of the string, excluding '\0' terminator */ +} jerry_string_t; + /** * Option bits for jerry_parse_options_t. */ @@ -863,10 +872,15 @@ typedef void (*jerry_arraybuffer_free_cb_t) (jerry_arraybuffer_type_t buffer_typ void *arraybuffer_user_p, void *user_p); +/** + * Helper to generate a string literal with type `const jerry_char_t *` + */ +#define JERRY_ZSTR_LITERAL(str) ((const jerry_char_t *) (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) JERRY_ZSTR_LITERAL (str), ((jerry_size_t) (sizeof (str) - 1)) /** * @} diff --git a/jerry-core/lit/lit-strings.c b/jerry-core/lit/lit-strings.c index edc75f8a2a..a4ca6eec77 100644 --- a/jerry-core/lit/lit-strings.c +++ b/jerry-core/lit/lit-strings.c @@ -251,22 +251,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..2b382e60df 100644 --- a/jerry-core/lit/lit-strings.h +++ b/jerry-core/lit/lit-strings.h @@ -92,7 +92,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..c5a2be8d65 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_string_sz ("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_string_sz ("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_string_sz ("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_string_sz ("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_string_sz ("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_string_sz ("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_string_sz ("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_string_sz ("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_string_sz ("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_string_sz ("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_string_sz ("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..b49c705c45 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_string_sz ("'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_string_sz ("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_string_sz ("Not an array.")); } JERRY_VLA (jerry_value_t, arr, c_arg_cnt); 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/autorelease.h b/jerry-ext/include/jerryscript-ext/autorelease.h index 3a91db23f6..7a068d818d 100644 --- a/jerry-ext/include/jerryscript-ext/autorelease.h +++ b/jerry-ext/include/jerryscript-ext/autorelease.h @@ -27,7 +27,7 @@ JERRY_C_API_BEGIN * Example usage: * static void foo (bool enable) * { - * JERRYX_AR_VALUE_T bar = jerry_string (...); + * JERRYX_AR_VALUE_T bar = jerry_string_utf8 (...); * * if (enable) { * JERRYX_AR_VALUE_T baz = jerry_current_realm (); 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..50b92a539c 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_string_sz (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_string_sz (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_string_sz (NAME), jerry_string_sz (STR) \ } -#define JERRYX_PROPERTY_BOOLEAN(NAME, VALUE) \ - (jerryx_property_entry) \ - { \ - NAME, jerry_boolean (VALUE) \ +#define JERRYX_PROPERTY_BOOLEAN(NAME, VALUE) \ + (jerryx_property_entry) \ + { \ + jerry_string_sz (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_string_sz (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_string_sz (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..d44fc2e23a 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,7 +33,7 @@ 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); @@ -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_string_sz (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_string_sz (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_string_sz (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 82ac7bc52b..b117485b76 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_string_sz ("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..46ccd08483 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_string_sz ("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..99a3e71068 100644 --- a/jerry-ext/util/sources.c +++ b/jerry-ext/util/sources.c @@ -35,13 +35,13 @@ 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_string_sz ("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_string_sz ("Input is not a valid UTF-8 encoded string.")); } jerry_parse_options_t parse_options; @@ -118,7 +118,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_string_sz ("Snapshot file not found")); } jerry_value_t result = @@ -148,7 +148,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_string_sz ("Out of memory.")); } memcpy (source_p + source_size, line_p, line_size); @@ -159,7 +159,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_string_sz ("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..c17741ca34 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_string_sz ("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_string_sz ("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_string_sz ("detachArrayBuffer"), + jerryx_test262_detach_array_buffer); + jerryx_test262_register_function (test262_object, jerry_string_sz ("evalScript"), jerryx_test262_eval_script); + jerryx_test262_register_function (test262_object, jerry_string_sz ("createRealm"), jerryx_test262_create_realm); + jerryx_test262_register_function (test262_object, jerry_string_sz ("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_string_sz ("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_string_sz ("$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..0582ada4a8 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_string_sz ("")); break; } else diff --git a/jerry-main/main-desktop.c b/jerry-main/main-desktop.c index 920ea9ca6f..cfe771b98b 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_string_sz ("assert"), jerryx_handler_assert); + jerryx_register_global (jerry_string_sz ("gc"), jerryx_handler_gc); + jerryx_register_global (jerry_string_sz ("print"), jerryx_handler_print); + jerryx_register_global (jerry_string_sz ("sourceName"), jerryx_handler_source_name); + jerryx_register_global (jerry_string_sz ("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/targets/baremetal-sdk/espressif/main/jerry-main.c b/targets/baremetal-sdk/espressif/main/jerry-main.c index 192bfef661..3fbd05d787 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_string_sz ("print"), jerryx_handler_print); /* Setup Global scope code */ ret_value = jerry_parse (script, sizeof (script) - 1, NULL); diff --git a/targets/os/mbedos/jerry-main.cpp b/targets/os/mbedos/jerry-main.cpp index 4db968513c..4e201f04e4 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_string_sz ("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/nuttx/jerry-main.c b/targets/os/nuttx/jerry-main.c index ab0b2169dc..9acbee5d2c 100644 --- a/targets/os/nuttx/jerry-main.c +++ b/targets/os/nuttx/jerry-main.c @@ -90,10 +90,11 @@ 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 (const jerry_char_t *name_p, /**< name of the function */ + jerry_size_t *name_size, /**< size of the name */ 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_string_utf8 (name_p, name_size), handler_p); if (jerry_value_is_exception (result_val)) { @@ -191,9 +192,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_ZSTR_ARG ("assert"), jerryx_handler_assert); + register_js_function (JERRY_ZSTR_ARG ("gc"), jerryx_handler_gc); + register_js_function (JERRY_ZSTR_ARG ("print"), jerryx_handler_print); jerry_value_t ret_value = jerry_undefined (); int ret_code = JERRY_STANDALONE_EXIT_CODE_OK; diff --git a/targets/os/riot/source/jerry-main.c b/targets/os/riot/source/jerry-main.c index 9ca1583c87..7872ec8054 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_string_sz ("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..7a80d4edee 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_string_sz ("print"), jerryx_handler_print); jerryx_repl (JERRY_ZSTR_ARG ("js> ")); diff --git a/tests/unit-core/test-api-binary-operations-arithmetics.c b/tests/unit-core/test-api-binary-operations-arithmetics.c index 185dc12c01..5e5864597a 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_string_sz ("error")); test_nan_entry_t test_nans[] = { /* Testing addition (+) */ diff --git a/tests/unit-core/test-api-binary-operations-comparisons.c b/tests/unit-core/test-api-binary-operations-comparisons.c index 7feb322e1e..9ec8d1a715 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_string_sz ("error")); test_entry_t tests[] = { /* Testing strict equal comparison */ diff --git a/tests/unit-core/test-api-errortype.c b/tests/unit-core/test-api-errortype.c index 43327e8f2b..99c89dd9f6 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_string_sz ("test")); TEST_ASSERT (jerry_value_is_exception (error_obj)); TEST_ASSERT (jerry_error_type (error_obj) == errors[idx]); diff --git a/tests/unit-core/test-api-functiontype.c b/tests/unit-core/test-api-functiontype.c index 6792e5a6fa..bb5d50c0c7 100644 --- a/tests/unit-core/test-api-functiontype.c +++ b/tests/unit-core/test-api-functiontype.c @@ -71,7 +71,7 @@ main (void) 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_throw_sz (JERRY_ERROR_TYPE, jerry_string_sz ("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..f66111d172 100644 --- a/tests/unit-core/test-api-iteratortype.c +++ b/tests/unit-core/test-api-iteratortype.c @@ -70,7 +70,7 @@ main (void) 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_throw_sz (JERRY_ERROR_TYPE, jerry_string_sz ("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..f295467ee9 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_string_sz ("val1"), jerry_string_sz ("val2"), jerry_string_sz ("val3"), + jerry_string_sz ("val4"), jerry_string_sz ("val5"), jerry_string_sz ("37"), + jerry_string_sz ("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 4c0337d1c1..ac989b1522 100644 --- a/tests/unit-core/test-api-objecttype.c +++ b/tests/unit-core/test-api-objecttype.c @@ -113,7 +113,7 @@ main (void) 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_throw_sz (JERRY_ERROR_TYPE, jerry_string_sz ("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..2567ed1a76 100644 --- a/tests/unit-core/test-api-promise.c +++ b/tests/unit-core/test-api-promise.c @@ -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_string_sz ("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-set-and-clear-error-flag.c b/tests/unit-core/test-api-set-and-clear-error-flag.c index e4fdb5a407..640832f71d 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,7 +44,7 @@ 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); diff --git a/tests/unit-core/test-api-value-type.c b/tests/unit-core/test-api-value-type.c index 9890c1eada..3587b329f2 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_string_sz ("error"))), ENTRY (JERRY_TYPE_NULL, jerry_null ()), diff --git a/tests/unit-core/test-api.c b/tests/unit-core/test-api.c index 6bdc0c4a66..6be1c051ff 100644 --- a/tests/unit-core/test-api.c +++ b/tests/unit-core/test-api.c @@ -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_string_sz ("error")); } /* handler_throw_test */ static void @@ -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_string_sz ("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_string_sz ("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_string_sz ("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_string_sz ("bar")); TEST_ASSERT (!jerry_value_is_exception (val_bar)); TEST_ASSERT (jerry_value_is_object (val_bar)); @@ -432,7 +430,7 @@ 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]); + res = set_property (global_obj_val, jerry_string_sz ("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_string_sz ("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_string_sz ("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_string_sz ("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_string_sz ("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_string_sz ("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_string_sz ("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_string_sz ("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_string_sz ("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_string_sz ("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_string_sz ("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_string_sz ("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_string_sz ("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_string_sz ("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_string_sz ("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_string_sz ("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_string_sz ("throw_reference_error")); TEST_ASSERT (!jerry_value_is_exception (val_t)); TEST_ASSERT (jerry_value_is_object (val_t)); @@ -1047,7 +1045,7 @@ main (void) /* 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 */ + args[1] = jerry_string_cesu8 (JERRY_ZSTR_ARG ("\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..0a3435465e 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_string_sz ("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_string_sz ("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_string_sz ("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..41c5099bd0 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_string_sz ("backtrace")); + register_callback (capture_handler, jerry_string_sz ("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_string_sz ("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_string_sz ("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_string_sz ("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_string_sz ("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_string_sz ("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_string_sz ("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_string_sz ("bad.js"), source); TEST_ASSERT (jerry_value_is_exception (error)); @@ -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_string_sz ("bad.js"), source); TEST_ASSERT (jerry_value_is_exception (error)); diff --git a/tests/unit-core/test-container-operation.c b/tests/unit-core/test-container-operation.c index a641ea91a6..d145632da5 100644 --- a/tests/unit-core/test-container-operation.c +++ b/tests/unit-core/test-container-operation.c @@ -164,8 +164,8 @@ 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); +#define error_message "Random error." + jerry_value_t error_val = jerry_throw_sz (JERRY_ERROR_RANGE, jerry_string_sz (error_message)); 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-error-callback.c b/tests/unit-core/test-error-callback.c index 3fe32d92d9..2cb95b2d4f 100644 --- a/tests/unit-core/test-error-callback.c +++ b/tests/unit-core/test-error-callback.c @@ -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_string_sz ("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..e7705c79ca 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++; @@ -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); diff --git a/tests/unit-core/test-json.c b/tests/unit-core/test-json.c index 0d1eef8058..0aaab91600 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_string_sz ("Error")); } /* custom_to_json */ int 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..7d4b04a4fe 100644 --- a/tests/unit-core/test-module-dynamic.c +++ b/tests/unit-core/test-module-dynamic.c @@ -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_string_sz ("Err01")); } case 1: { diff --git a/tests/unit-core/test-module.c b/tests/unit-core/test-module.c index cd01dd45ef..11b73de786 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_string_sz ("Module not found")); } return create_module (0); @@ -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_string_sz ("Ooops!")); } return jerry_undefined (); @@ -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_string_sz ("a"), 6); + compare_property (result, jerry_string_sz ("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_string_cesu8 (JERRY_ZSTR_ARG ("\xed\xa0\x83\xed\xb2\x80")); module = jerry_native_module (NULL, &export, 1); TEST_ASSERT (!jerry_value_is_exception (module)); @@ -463,7 +462,7 @@ main (void) jerry_value_free (module); jerry_value_free (export); - export = jerry_string_sz ("\xed\xa0\x80"); + export = jerry_string_cesu8 (JERRY_ZSTR_ARG ("\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-newtarget.c b/tests/unit-core/test-newtarget.c index d5b28240fe..127b415a5f 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_string_sz ("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..f4438378fd 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_string_sz ("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-promise.c b/tests/unit-core/test-promise.c index b66ce791c3..25283ecde9 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_string_sz ("create_promise1"), create_promise1_handler); + register_js_function (jerry_string_sz ("create_promise2"), create_promise2_handler); + register_js_function (jerry_string_sz ("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)); diff --git a/tests/unit-core/test-proxy.c b/tests/unit-core/test-proxy.c index 40c70e53ea..ddac15ce66 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_string_sz ("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_string_sz ("get"), handler_get); + set_function (handler, jerry_string_sz ("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_string_sz ("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_string_sz ("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_string_sz ("value"), new_value); jerry_value_free (new_value); } { - jerry_value_t res = get_property (proxy, "value"); + jerry_value_t res = get_property (proxy, jerry_string_sz ("value")); TEST_ASSERT (jerry_value_is_number (res)); TEST_ASSERT (jerry_value_as_number (res) == 13.0); jerry_value_free (res); diff --git a/tests/unit-core/test-realm.c b/tests/unit-core/test-realm.c index db088fd5b0..42fc4a6348 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)); @@ -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_string_sz ("a"), 3.5); + create_number_property (global_value, jerry_string_sz ("b"), 7.25); + create_number_property (realm_value, jerry_string_sz ("a"), -1.25); + create_number_property (realm_value, jerry_string_sz ("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_string_sz ("x"), 7.25); + create_number_property (object_value, jerry_string_sz ("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_string_sz ("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_string_sz ("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_string_sz ("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_string_sz ("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..c4e03ff1a5 100644 --- a/tests/unit-core/test-regexp-dotall-unicode.c +++ b/tests/unit-core/test-regexp-dotall-unicode.c @@ -24,10 +24,10 @@ 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_string_sz (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 ];"; diff --git a/tests/unit-core/test-regexp.c b/tests/unit-core/test-regexp.c index 8cb4e8251d..c0bbe1440a 100644 --- a/tests/unit-core/test-regexp.c +++ b/tests/unit-core/test-regexp.c @@ -25,9 +25,9 @@ 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_string_sz (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];"; 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..871071922e 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)" @@ -224,9 +224,12 @@ 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_string_sz ("asyncIterator"), jerry_string_sz ("hasInstance"), jerry_string_sz ("isConcatSpreadable"), + jerry_string_sz ("iterator"), jerry_string_sz ("match"), jerry_string_sz ("replace"), + jerry_string_sz ("search"), jerry_string_sz ("species"), jerry_string_sz ("split"), + jerry_string_sz ("toPrimitive"), jerry_string_sz ("toStringTag"), jerry_string_sz ("unscopables"), + jerry_string_sz ("matchAll"), }; jerry_value_t obj = jerry_eval (obj_src, sizeof (obj_src) - 1, JERRY_PARSE_NO_OPTS); @@ -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..9972ca1e12 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_string_sz ("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_string_sz ("get")); + check_attribute (prop_desc.setter, object, jerry_string_sz ("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_string_sz ("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_string_sz ("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..94bd650c0a 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_string_sz ("expected_offset"), js_offset); + register_js_value (jerry_string_sz ("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_string_sz ("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_string_sz ("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_string_sz (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_string_sz ("array"), array); + register_js_value (jerry_string_sz ("expected_length"), js_element_count); + register_js_value (jerry_string_sz ("expected_value"), js_expected_value); jerry_value_free (js_element_count); jerry_value_free (js_expected_value); @@ -673,6 +683,8 @@ main (void) test_property_by_index (test_entries); + test_entries_free (test_entries); + /* test invalid things */ { jerry_value_t values[] = { @@ -682,7 +694,7 @@ main (void) jerry_object (), jerry_null (), jerry_arraybuffer (16), - jerry_error_sz (JERRY_ERROR_TYPE, "error"), + jerry_error_sz (JERRY_ERROR_TYPE, jerry_string_sz ("error")), jerry_undefined (), jerry_promise (), }; diff --git a/tests/unit-core/test-vm-throw.c b/tests/unit-core/test-vm-throw.c index b4f2d20861..9bb542030c 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_string_sz ("Error!")); TEST_ASSERT (!jerry_exception_is_captured (result)); jerry_exception_allow_capture (result, false); diff --git a/tests/unit-ext/module/my-custom-module.c b/tests/unit-ext/module/my-custom-module.c index 2395bfcf00..a7b8e7dd5d 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_string_sz ("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_string_sz ("call_function_with_callback"), jfunc); jerry_value_free (jfunc); return mymodule; diff --git a/tests/unit-ext/test-ext-arg.c b/tests/unit-ext/test-ext-arg.c index d32b865833..0c12e88f9a 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_string_sz ("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_string_sz ("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_string_sz ("prop1"), jerry_string_sz ("prop2"), jerry_string_sz ("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_string_sz ("prop1"), jerry_string_sz ("prop2"), jerry_string_sz ("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_string_sz ("prop1"), jerry_string_sz ("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_string_sz ("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_string_cesu8 (JERRY_ZSTR_ARG ("\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_string_sz ("test_validator1"), test_validator1_handler); + register_js_function (jerry_string_sz ("test_validator2"), test_validator2_handler); + register_js_function (jerry_string_sz ("test_validator3"), test_validator3_handler); + register_js_function (jerry_string_sz ("test_validator_int1"), test_validator_int1_handler); + register_js_function (jerry_string_sz ("test_validator_int2"), test_validator_int2_handler); + register_js_function (jerry_string_sz ("test_validator_int3"), test_validator_int3_handler); + register_js_function (jerry_string_sz ("MyObjectA"), create_object_a_handler); + register_js_function (jerry_string_sz ("MyObjectB"), create_object_b_handler); + register_js_function (jerry_string_sz ("test_validator_prop1"), test_validator_prop1_handler); + register_js_function (jerry_string_sz ("test_validator_prop2"), test_validator_prop2_handler); + register_js_function (jerry_string_sz ("test_validator_prop3"), test_validator_prop3_handler); + register_js_function (jerry_string_sz ("test_validator_array1"), test_validator_array1_handler); + register_js_function (jerry_string_sz ("test_validator_array2"), test_validator_array2_handler); + register_js_function (jerry_string_sz ("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..6899b7ac21 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_string_sz ("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_string_sz (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_string_sz (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_string_sz (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_string_sz (prop_err)); jerryx_property_entry methods[] = { JERRYX_PROPERTY_FUNCTION (prop_ok, method_hello), // This registration is ok