Skip to content

Commit db3a212

Browse files
committed
Reduce call to strlen when possible
Add buffer_size parameter for function jerry_port_log_buffer, so that jerry_port_log_buffer won't need calculate strlen when printing Introduce jerry_sz_ascii jerry_sz_cesu8 jerry_sz_utf8 and use it Replace all the usage of jerry_string_sz with jerry_sz_ascii,jerry_sz_cesu8 - `jerry_string_external_sz` replaced by `jerry_string_external` - partial `jerry_string` usage replaced by `jerry_sz_ascii`, `jerry_sz_cesu8`, `jerry_sz_utf8` JerryScript-DCO-1.0-Signed-off-by: Yonggang Luo [email protected]
1 parent c509a06 commit db3a212

File tree

118 files changed

+1089
-1046
lines changed

Some content is hidden

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

118 files changed

+1089
-1046
lines changed

docs/01.CONFIGURATION.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ Enabling this feature provides detailed error messages where available, like lin
155155

156156
### Logging
157157

158-
This option can be used to enable log messages during runtime. When enabled the engine will use the `jerry_port_log` port API function to print relevant log messages.
158+
This option can be used to enable log messages during runtime. When enabled the engine will use the `jerry_port_log_buffer` port API function to print relevant log messages.
159159
This feature is disabled by default.
160160

161161
| Options | |

docs/02.API-REFERENCE.md

+90-140
Large diffs are not rendered by default.

docs/03.API-EXAMPLE.md

+30-21
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ This method will be implemented in C and will be called from the JavaScript code
168168
For this a few extra API methods are required:
169169

170170
- `jerry_current_realm`
171-
- `jerry_string_sz`
171+
- `jerry_sz_ascii`
172172
- `jerry_object_set`
173173
- `jerry_function_external`
174174

@@ -207,7 +207,7 @@ main (void)
207207
/* Get the "global" object */
208208
jerry_value_t global_object = jerry_current_realm ();
209209
/* Create a "print" JS string */
210-
jerry_value_t property_name_print = jerry_string_sz ("print");
210+
jerry_value_t property_name_print = jerry_sz_ascii ("print");
211211
/* Create a function from a native C method (this function will be called from JS) */
212212
jerry_value_t property_value_func = jerry_function_external (print_handler);
213213
/* Add the "print" property with the function value to the "global" object */
@@ -326,7 +326,7 @@ main (void)
326326
/* Get the "global" object */
327327
jerry_value_t global_object = jerry_current_realm ();
328328
/* Create a "print" JS string */
329-
jerry_value_t property_name_print = jerry_string_sz ("print");
329+
jerry_value_t property_name_print = jerry_sz_ascii ("print");
330330
/* Create a function from a native C method (this function will be called from JS) */
331331
jerry_value_t property_value_func = jerry_function_external (print_handler);
332332
/* Add the "print" property with the function value to the "global" object */
@@ -393,9 +393,17 @@ In this example the following extension methods are used:
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_sz_ascii ("print"), jerryx_handler_print);
411419

412420
/* Setup Global scope code */
413421
jerry_value_t parsed_code = jerry_parse (script, script_size, NULL);
@@ -470,14 +478,14 @@ 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_sz_ascii ("print"), jerryx_handler_print);
474482

475483
/* Getting pointer to the Global object */
476484
jerry_value_t global_object = jerry_current_realm ();
477485

478486
/* Constructing strings */
479-
jerry_value_t prop_name = jerry_string_sz ("my_var");
480-
jerry_value_t prop_value = jerry_string_sz ("Hello from C!");
487+
jerry_value_t prop_name = jerry_sz_ascii ("my_var");
488+
jerry_value_t prop_value = jerry_sz_ascii ("Hello from C!");
481489

482490
/* Setting the string value as a property of the Global object */
483491
jerry_value_t set_result = jerry_object_set (global_object, prop_name, prop_value);
@@ -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_sz_ascii ("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_ascii (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_sz_ascii ("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 ();
@@ -843,7 +852,7 @@ main (void)
843852
jerry_value_t func_obj = jerry_function_external (get_msg_handler);
844853

845854
/* Set the native function as a property of the empty JS object */
846-
jerry_value_t prop_name = jerry_string_sz ("myFunc");
855+
jerry_value_t prop_name = jerry_sz_ascii ("myFunc");
847856
jerry_value_free (jerry_object_set (object, prop_name, func_obj));
848857
jerry_value_free (prop_name);
849858
jerry_value_free (func_obj);
@@ -852,7 +861,7 @@ main (void)
852861
jerry_value_t global_object = jerry_current_realm ();
853862

854863
/* Add the JS object to the global context */
855-
prop_name = jerry_string_sz ("MyObject");
864+
prop_name = jerry_sz_ascii ("MyObject");
856865
jerry_value_free (jerry_object_set (global_object, prop_name, object));
857866
jerry_value_free (prop_name);
858867
jerry_value_free (object);
@@ -928,7 +937,7 @@ add_handler (const jerry_call_info_t *call_info_p, /**< call information */
928937
/* Note: that the argument count check is ignored for the example's case */
929938

930939
/* Get 'this.x' */
931-
jerry_value_t prop_name = jerry_string_sz ("x");
940+
jerry_value_t prop_name = jerry_sz_ascii ("x");
932941
jerry_value_t x_val = jerry_object_get (call_info_p->this_value, prop_name);
933942

934943
if (!jerry_value_is_exception (x_val))
@@ -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_sz_ascii ("print"), jerryx_handler_print);
962971

963972
/* Create a JS object */
964973
const jerry_char_t my_js_object[] = " \
@@ -983,7 +992,7 @@ main (void)
983992
jerry_value_t add_func_obj = jerry_function_external (add_handler);
984993

985994
/* Set the native function as a property of previously created MyObject */
986-
jerry_value_t prop_name = jerry_string_sz ("add2x");
995+
jerry_value_t prop_name = jerry_sz_ascii ("add2x");
987996
jerry_value_free (jerry_object_set (my_js_obj_val, prop_name, add_func_obj));
988997
jerry_value_free (add_func_obj);
989998
jerry_value_free (prop_name);
@@ -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_sz_ascii ("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/05.PORT-API.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,11 @@ void jerry_port_context_free (void);
112112
*
113113
* The implementation can decide whether error and debug messages are logged to
114114
* the console, or saved to a database or to a file.
115+
*
116+
* @param buffer_p: input buffer
117+
* @param buffer_size: data size
115118
*/
116-
void jerry_port_log (const char *message_p);
119+
void jerry_port_log_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size);
117120
```
118121
119122
```c

docs/07.DEBUGGER.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,8 @@ wait_for_source_callback (const jerry_char_t *source_name_p, /**< source name */
318318
319319
jerry_parse_options_t parse_options;
320320
parse_options.options = JERRY_PARSE_HAS_SOURCE_NAME;
321-
parse_options.source_name = jerry_string ((const jerry_char_t *) source_name_p,
322-
(jerry_size_t) source_name_size,
323-
JERRY_ENCODING_UTF8);
321+
parse_options.source_name = jerry_string_utf8 ((const jerry_char_t *) source_name_p,
322+
(jerry_size_t) source_name_size);
324323
325324
jerry_value_t ret_val = jerry_parse (source_p,
326325
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_sz_ascii ("enable"),
636+
jerry_sz_ascii ("data"),
637+
jerry_sz_ascii ("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_ascii (common_functions[i].name_p, common_functions[i].name_size),
402403
common_functions[i].handler_p);
403404
}
404405

docs/11.EXT-REFERENCE-AUTORELEASE.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ using the `__cleanup__` variable attribute. For other compilers, no support has
2121
static void
2222
foo (bool enable)
2323
{
24-
JERRYX_AR_VALUE_T bar = jerry_string_sz ("...");
24+
JERRYX_AR_VALUE_T bar = jerry_sz_ascii ("...");
2525

2626
if (enable)
2727
{

docs/16.MIGRATION-GUIDE.md

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ For more information on the current API methods please check the [API reference]
3838
- `jerry_get_value_without_error_flag` replaced by `jerry_get_value_from_error`
3939
- `jerry_parse_and_save_snapshot` replaced by `jerry_generate_snapshot`
4040
- `jerry_parse_and_save_function_snapshot` replaced by `jerry_generate_function_snapshot`
41+
- `jerry_string_external_sz` replaced by `jerry_string_external`
42+
- `jerry_string_sz` replaced by `jerry_sz_ascii` or `jerry_string_cesu8`
4143

4244

4345
***Removed unused configuration macros***

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_sz_ascii ("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_sz_ascii ("Module file not found"));
196196
}
197197

198198
jerry_parse_options_t parse_options;

0 commit comments

Comments
 (0)