Skip to content

Commit 466caea

Browse files
committed
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 [email protected]
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);

docs/06.REFERENCE-COUNTING.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ jerry_value_t my_external_handler (const jerry_value_t function_obj,
135135
136136
/* If the value to be returned is needed for other purposes the
137137
* jerry_value_copy () can be used to create new references. */
138-
return jerry_string (...);
138+
return jerry_string_utf8 (...);
139139
}
140140
```
141141

docs/07.DEBUGGER.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,8 @@ wait_for_source_callback (const jerry_char_t *source_name_p, /**< source name */
320320
321321
jerry_parse_options_t parse_options;
322322
parse_options.options = JERRY_PARSE_HAS_SOURCE_NAME;
323-
parse_options.source_name = jerry_string ((const jerry_char_t *) source_name_p,
324-
(jerry_size_t) source_name_size,
325-
JERRY_ENCODING_UTF8);
323+
parse_options.source_name = jerry_string_utf8 ((const jerry_char_t *) source_name_p,
324+
(jerry_size_t) source_name_size);
326325
327326
jerry_value_t ret_val = jerry_parse (source_p,
328327
source_size,

docs/09.EXT-REFERENCE-ARG.md

+17-25
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,7 @@ jerryx_arg_object_properties (const jerryx_arg_object_props_t *obj_prop_p,
612612
613613
```c
614614
#include "jerryscript.h"
615+
615616
#include "jerryscript-ext/arg.h"
616617
617618
/**
@@ -628,39 +629,31 @@ my_external_handler (const jerry_value_t function_obj,
628629
{
629630
bool required_bool;
630631
double required_num;
631-
double optional_num = 1234.567; // default value
632+
double optional_num = 1234.567; // default value
632633
633634
/* "prop_name_p" defines the name list of the expected properties' names. */
634-
const char *prop_name_p[] = { "enable", "data", "extra_data" };
635+
const jerry_value_t prop_name_p[] = { jerry_string_sz ("enable"),
636+
jerry_string_sz ("data"),
637+
jerry_string_sz ("extra_data") };
635638
636639
/* "prop_mapping" defines the steps to transform properties to C variables. */
637-
const jerryx_arg_t prop_mapping[] =
638-
{
639-
jerryx_arg_boolean (&required_bool, JERRYX_ARG_COERCE, JERRYX_ARG_REQUIRED),
640-
jerryx_arg_number (&required_num, JERRYX_ARG_COERCE, JERRYX_ARG_REQUIRED),
641-
jerryx_arg_number (&optional_num, JERRYX_ARG_COERCE, JERRYX_ARG_OPTIONAL)
642-
};
640+
const jerryx_arg_t prop_mapping[] = { jerryx_arg_boolean (&required_bool, JERRYX_ARG_COERCE, JERRYX_ARG_REQUIRED),
641+
jerryx_arg_number (&required_num, JERRYX_ARG_COERCE, JERRYX_ARG_REQUIRED),
642+
jerryx_arg_number (&optional_num, JERRYX_ARG_COERCE, JERRYX_ARG_OPTIONAL) };
643643
644644
/* Prepare the jerryx_arg_object_props_t instance. */
645-
const jerryx_arg_object_props_t prop_info =
646-
{
647-
.name_p = (const jerry_char_t **) prop_name_p,
648-
.name_cnt = 3,
649-
.c_arg_p = prop_mapping,
650-
.c_arg_cnt = 3
651-
};
645+
const jerryx_arg_object_props_t prop_info = { .name_p = prop_name_p,
646+
.name_cnt = 3,
647+
.c_arg_p = prop_mapping,
648+
.c_arg_cnt = 3 };
652649
653650
/* It is the mapping used in the jerryx_arg_transform_args. */
654-
const jerryx_arg_t mapping[] =
655-
{
656-
jerryx_arg_object_properties (&prop_info, JERRYX_ARG_REQUIRED)
657-
};
651+
const jerryx_arg_t mapping[] = { jerryx_arg_object_properties (&prop_info, JERRYX_ARG_REQUIRED) };
652+
653+
jerry_value_list_free (prop_name_p, sizeof (prop_name_p) / sizeof (prop_name_p[0]));
658654
659655
/* Validate and transform. */
660-
const jerry_value_t rv = jerryx_arg_transform_args (args_p,
661-
args_count,
662-
mapping,
663-
1);
656+
const jerry_value_t rv = jerryx_arg_transform_args (args_p, args_count, mapping, 1);
664657
665658
if (jerry_value_is_exception (rv))
666659
{
@@ -673,9 +666,8 @@ my_external_handler (const jerry_value_t function_obj,
673666
* required_bool, required_num and optional_num can now be used.
674667
*/
675668
676-
return jerry_undefined (); /* Or return something more meaningful. */
669+
return jerry_undefined (); /* Or return something more meaningful. */
677670
}
678-
679671
```
680672

681673
**See also**

docs/10.EXT-REFERENCE-HANDLER.md

+10-9
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ main (int argc, char **argv)
120120
121121
jerryx_property_entry methods[] =
122122
{
123-
{ "demo", jerry_function_external (handler) },
124-
{ NULL, 0 },
123+
JERRYX_PROPERTY_FUNCTION("demo", handler),
124+
JERRYX_PROPERTY_LIST_END(),
125125
};
126126
127127
jerry_value_t global = jerry_current_realm ();
@@ -362,7 +362,7 @@ longer needed.
362362

363363
```c
364364
jerry_value_t
365-
jerryx_register_global (const char *name_p,
365+
jerryx_register_global (jerry_value_t function_name_val,
366366
jerry_external_handler_t handler_p);
367367
```
368368
@@ -381,14 +381,15 @@ jerryx_register_global (const char *name_p,
381381
#include "jerryscript-ext/properties.h"
382382
383383
static const struct {
384-
const char *name_p;
384+
const jerry_char_t *name_p;
385+
jerry_size_t name_size;
385386
jerry_external_handler_t handler_p;
386387
} common_functions[] =
387388
{
388-
{ "assert", jerryx_handler_assert },
389-
{ "gc", jerryx_handler_gc },
390-
{ "print", jerryx_handler_print },
391-
{ NULL, NULL }
389+
{ JERRY_ZSTR_ARG ("assert"), jerryx_handler_assert },
390+
{ JERRY_ZSTR_ARG ("gc"), jerryx_handler_gc },
391+
{ JERRY_ZSTR_ARG ("print"), jerryx_handler_print },
392+
{ NULL, 0, NULL }
392393
};
393394
394395
static void
@@ -398,7 +399,7 @@ register_common_functions (void)
398399
399400
for (int i = 0; common_functions[i].name_p != NULL && !jerry_value_is_exception (ret); i++)
400401
{
401-
ret = jerryx_register_global (common_functions[i].name_p,
402+
ret = jerryx_register_global (jerry_string_utf8 (common_functions[i].name_p, common_functions[i].name_size),
402403
common_functions[i].handler_p);
403404
}
404405

docs/12.EXT-REFERENCE-MODULE.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ canonicalize_file_path (const jerry_value_t name)
205205
/**
206206
* Since a file on the file system can be referred to by multiple relative paths, but only by one absolute path, the
207207
* absolute path becomes the canonical name for the module. Thus, to establish this canonical name, we must search
208-
* name for "./" and "../", follow symlinks, etc., then create absolute_path via jerry_string () and return
208+
* name for "./" and "../", follow symlinks, etc., then create absolute_path via jerry_string_utf8 () and return
209209
* it, because it is the canonical name for this module. Thus, we avoid loading the same JavaScript file twice.
210210
*/
211211

jerry-core/api/jerry-module.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include "jerryscript-core.h"
1717
#include "jerryscript-port.h"
1818

19-
#include "ecma-errors.h"
19+
#include "ecma-globals.h"
2020

2121
#include "lit-magic-strings.h"
2222
#include "lit-strings.h"
@@ -162,7 +162,7 @@ jerry_module_resolve (const jerry_value_t specifier, /**< module specifier strin
162162

163163
if (path_p == NULL)
164164
{
165-
return jerry_throw_sz (JERRY_ERROR_SYNTAX, "Failed to resolve module");
165+
return jerry_throw_sz (JERRY_ERROR_SYNTAX, jerry_string_sz ("Failed to resolve module"));
166166
}
167167

168168
jerry_value_t realm = jerry_current_realm ();
@@ -192,7 +192,7 @@ jerry_module_resolve (const jerry_value_t specifier, /**< module specifier strin
192192
jerry_value_free (realm);
193193
jerry_port_path_free (path_p);
194194

195-
return jerry_throw_sz (JERRY_ERROR_SYNTAX, "Module file not found");
195+
return jerry_throw_sz (JERRY_ERROR_SYNTAX, jerry_string_sz ("Module file not found"));
196196
}
197197

198198
jerry_parse_options_t parse_options;

jerry-core/api/jerry-snapshot.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ snapshot_add_compiled_code (const ecma_compiled_code_t *compiled_code_p, /**< co
140140
size_t snapshot_buffer_size, /**< snapshot buffer size */
141141
snapshot_globals_t *globals_p) /**< snapshot globals */
142142
{
143-
const char *error_buffer_too_small_p = "Snapshot buffer too small";
143+
#define error_buffer_too_small_p "Snapshot buffer too small"
144144

145145
if (!ecma_is_value_empty (globals_p->snapshot_error))
146146
{
@@ -180,7 +180,7 @@ snapshot_add_compiled_code (const ecma_compiled_code_t *compiled_code_p, /**< co
180180
/* Regular expression. */
181181
if (globals_p->snapshot_buffer_write_offset + sizeof (ecma_compiled_code_t) > snapshot_buffer_size)
182182
{
183-
globals_p->snapshot_error = jerry_throw_sz (JERRY_ERROR_RANGE, error_buffer_too_small_p);
183+
globals_p->snapshot_error = jerry_throw_sz (JERRY_ERROR_RANGE, jerry_string_sz (error_buffer_too_small_p));
184184
return 0;
185185
}
186186

@@ -201,7 +201,7 @@ snapshot_add_compiled_code (const ecma_compiled_code_t *compiled_code_p, /**< co
201201
buffer_p,
202202
buffer_size))
203203
{
204-
globals_p->snapshot_error = jerry_throw_sz (JERRY_ERROR_RANGE, error_buffer_too_small_p);
204+
globals_p->snapshot_error = jerry_throw_sz (JERRY_ERROR_RANGE, jerry_string_sz (error_buffer_too_small_p));
205205
/* cannot return inside ECMA_FINALIZE_UTF8_STRING */
206206
}
207207

@@ -235,7 +235,7 @@ snapshot_add_compiled_code (const ecma_compiled_code_t *compiled_code_p, /**< co
235235
compiled_code_p,
236236
((size_t) compiled_code_p->size) << JMEM_ALIGNMENT_LOG))
237237
{
238-
globals_p->snapshot_error = jerry_throw_sz (JERRY_ERROR_RANGE, error_buffer_too_small_p);
238+
globals_p->snapshot_error = jerry_throw_sz (JERRY_ERROR_RANGE, jerry_string_sz (error_buffer_too_small_p));
239239
return 0;
240240
}
241241

0 commit comments

Comments
 (0)