Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 466caea

Browse files
committedDec 17, 2024·
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
1 parent d2d30df commit 466caea

File tree

78 files changed

+835
-642
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+835
-642
lines changed
 

‎docs/02.API-REFERENCE.md

+152-46
Large diffs are not rendered by default.

‎docs/03.API-EXAMPLE.md

+21-12
Original file line numberDiff line numberDiff line change
@@ -393,9 +393,17 @@ In this example the following extension methods are used:
393393

394394
In further examples this "print" handler will be used.
395395

396+
The `api-example-6.c` file should contain the following code:
397+
398+
[doctest]: # ()
399+
396400
```c
401+
#include <stdio.h>
402+
397403
#include "jerryscript.h"
404+
398405
#include "jerryscript-ext/handlers.h"
406+
#include "jerryscript-ext/properties.h"
399407

400408
int
401409
main (void)
@@ -407,7 +415,7 @@ main (void)
407415
jerry_init (JERRY_INIT_EMPTY);
408416

409417
/* Register 'print' function from the extensions to the global object */
410-
jerryx_register_global ("print", jerryx_handler_print);
418+
jerryx_register_global (jerry_string_sz ("print"), jerryx_handler_print);
411419

412420
/* Setup Global scope code */
413421
jerry_value_t parsed_code = jerry_parse (script, script_size, NULL);
@@ -470,7 +478,7 @@ main (void)
470478
jerry_init (JERRY_INIT_EMPTY);
471479

472480
/* Register 'print' function from the extensions */
473-
jerryx_register_global ("print", jerryx_handler_print);
481+
jerryx_register_global (jerry_string_sz ("print"), jerryx_handler_print);
474482

475483
/* Getting pointer to the Global object */
476484
jerry_value_t global_object = jerry_current_realm ();
@@ -729,7 +737,7 @@ main (void)
729737
jerry_init (JERRY_INIT_EMPTY);
730738
731739
/* Register 'print' function from the extensions */
732-
jerryx_register_global ("print", jerryx_handler_print);
740+
jerryx_register_global (jerry_string_sz ("print"), jerryx_handler_print);
733741
734742
while (!is_done)
735743
{
@@ -808,10 +816,8 @@ In this example (`api-example-9.c`) an object with a native function is added to
808816
#include "jerryscript-ext/handlers.h"
809817
#include "jerryscript-ext/properties.h"
810818

811-
struct my_struct
812-
{
813-
const char *msg;
814-
} my_struct;
819+
820+
jerry_string_t my_struct;
815821

816822
/**
817823
* Get a string from a native object
@@ -821,7 +827,7 @@ get_msg_handler (const jerry_call_info_t *call_info_p, /**< call information */
821827
const jerry_value_t *args_p, /**< function arguments */
822828
const jerry_length_t args_cnt) /**< number of function arguments */
823829
{
824-
return jerry_string_sz (my_struct.msg);
830+
return jerry_string_utf8 (my_struct.ptr, my_struct.size);
825831
} /* get_msg_handler */
826832

827833
int
@@ -831,10 +837,13 @@ main (void)
831837
jerry_init (JERRY_INIT_EMPTY);
832838

833839
/* Register 'print' function from the extensions */
834-
jerryx_register_global ("print", jerryx_handler_print);
840+
jerryx_register_global (jerry_string_sz ("print"), jerryx_handler_print);
835841

836842
/* Do something with the native object */
837-
my_struct.msg = "Hello, World!";
843+
{
844+
static const jerry_string_t hello = { JERRY_ZSTR_ARG ("Hello, World!") };
845+
my_struct = hello;
846+
}
838847

839848
/* Create an empty JS object */
840849
jerry_value_t object = jerry_object ();
@@ -958,7 +967,7 @@ main (void)
958967
jerry_init (JERRY_INIT_EMPTY);
959968

960969
/* Register 'print' function from the extensions */
961-
jerryx_register_global ("print", jerryx_handler_print);
970+
jerryx_register_global (jerry_string_sz ("print"), jerryx_handler_print);
962971

963972
/* Create a JS object */
964973
const jerry_char_t my_js_object[] = " \
@@ -1058,7 +1067,7 @@ main (void)
10581067
jerry_init (JERRY_INIT_EMPTY);
10591068

10601069
/* Register the print function */
1061-
jerryx_register_global ("print", jerryx_handler_print);
1070+
jerryx_register_global (jerry_string_sz ("print"), jerryx_handler_print);
10621071

10631072
/* Evaluate the script */
10641073
jerry_value_t eval_ret = jerry_eval (script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);

0 commit comments

Comments
 (0)
Please sign in to comment.