Skip to content

Commit 80dba58

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 a1e4bd8 commit 80dba58

File tree

78 files changed

+808
-627
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

+808
-627
lines changed

docs/02.API-REFERENCE.md

+125-31
Original file line numberDiff line numberDiff line change
@@ -3836,7 +3836,7 @@ jerry_error_type (const jerry_value_t value);
38363836

38373837
```c
38383838
{
3839-
jerry_value_t error_obj = jerry_error_sz (JERRY_ERROR_RANGE, "error msg");
3839+
jerry_value_t error_obj = jerry_error_sz (JERRY_ERROR_RANGE, jerry_string_sz ("error msg"));
38403840
jerry_error_t error_type = jerry_error_type (error_obj);
38413841

38423842
// error_type is now JERRY_ERROR_RANGE.
@@ -3958,7 +3958,7 @@ void main(void)
39583958

39593959
jerry_error_on_created (error_object_created_callback, NULL);
39603960

3961-
jerry_value_free (jerry_error_sz (JERRY_ERROR_COMMON, "Message"));
3961+
jerry_value_free (jerry_error_sz (JERRY_ERROR_COMMON, jerry_string_sz ("Message")));
39623962

39633963
jerry_cleanup ();
39643964
} /* main */
@@ -4153,7 +4153,7 @@ throw_exception (const jerry_call_info_t *call_info_p, /**< call info */
41534153
(void) argv;
41544154
(void) argc;
41554155

4156-
jerry_value_t result_value = jerry_throw_sz (JERRY_ERROR_COMMON, "Error!");
4156+
jerry_value_t result_value = jerry_throw_sz (JERRY_ERROR_COMMON, jerry_string_sz ("Error!"));
41574157

41584158
/* Ignore calling the vm_throw_callback function. */
41594159
jerry_exception_allow_capture (result_value, false);
@@ -4541,7 +4541,7 @@ main (void)
45414541

45424542
jerry_string_external_on_free (external_string_free_callback);
45434543

4544-
const char *string_p = "This is a long external string, should not be duplicated!";
4544+
#define string_p "This is a long external string, should not be duplicated!"
45454545
jerry_value_t external_string = jerry_string_external_sz (string_p, NULL);
45464546
/* The external_string_free_callback is called. */
45474547
jerry_value_free (external_string);
@@ -4602,7 +4602,7 @@ main (void)
46024602
{
46034603
jerry_init (JERRY_INIT_EMPTY);
46044604

4605-
const char *string_p = "This is a long external string, should not be duplicated!";
4605+
#define string_p "This is a long external string, should not be duplicated!"
46064606

46074607
jerry_value_t external_string = jerry_string_external_sz (string_p, (void *) &user_value);
46084608

@@ -7158,12 +7158,12 @@ jerry_boolean (bool value);
71587158

71597159
**Summary**
71607160

7161-
Create new JavaScript Error object with the specified error message.
7162-
7163-
Important! The `error_type` argument *must not be* `JERRY_ERROR_NONE`.
7164-
Creating an Error object with no error type is not valid.
7161+
Create an Error object with the provided `message` string value as the error `message` property.
7162+
If the `message` value is not a string, the created error will not have a `message` property.
71657163

71667164
*Note*:
7165+
- Important! The `error_type` argument *must not be* `JERRY_ERROR_NONE`.
7166+
Creating an Error object with no error type is not valid.
71677167
- Returned value must be freed with [jerry_value_free](#jerry_value_free) when it
71687168
is no longer needed.
71697169

@@ -7206,21 +7206,25 @@ jerry_error (jerry_error_t error_type, jerry_value_t message);
72067206

72077207
**Summary**
72087208

7209-
Create new JavaScript Error object, using the a zero-terminated string as the error message.
7209+
Create an Error object with the provided `message_sz` string value as the error `message` property.
7210+
If the `message_sz` value is not a string, the created error will not have a `message` property.
72107211

72117212
*Note*:
7213+
- Important! The `error_type` argument *must not be* `JERRY_ERROR_NONE`.
7214+
Creating an Error object with no error type is not valid.
72127215
- Returned value must be freed with [jerry_value_free](#jerry_value_free) when it
72137216
is no longer needed.
7217+
- The `message_sz` value will be freed in this function.
72147218

72157219
**Prototype**
72167220

72177221
```c
72187222
jerry_value_t
7219-
jerry_error_sz (jerry_error_t error_type, const char *message_p);
7223+
jerry_error_sz (jerry_error_t error_type, const jerry_value_t message_sz);
72207224
```
72217225

72227226
- `error_type` - type of the error
7223-
- `message_p` - value of 'message' property of the constructed error object
7227+
- `message_sz` - message of the error that will be free/take
72247228
- return value - constructed error object
72257229

72267230
*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);
72297233

72307234
```c
72317235
{
7232-
jerry_value_t error_obj = jerry_error_sz (JERRY_ERROR_TYPE, "error");
7236+
jerry_value_t error_obj = jerry_error_sz (JERRY_ERROR_TYPE, jerry_string_sz ("error"));
72337237

72347238
... // usage of error_obj
72357239

@@ -7757,20 +7761,21 @@ main (void)
77577761

77587762
**Summary**
77597763

7760-
Create string from a zero-terminated ASCII string.
7764+
Create string value from the zero-terminated UTF-8 encoded literal string.
7765+
The content of the buffer is assumed be encoded correctly, it's the callers
7766+
responsibility to validate the input.
77617767

77627768
*Note*:
7763-
- Returned value must be freed with [jerry_value_free](#jerry_value_free) when it
7764-
is no longer needed.
7769+
- This is a macro that only accept literal string
7770+
- Returned value must be freed with [jerry_value_free](#jerry_value_free) when it is no longer needed.
77657771

77667772
**Prototype**
77677773

77687774
```c
7769-
jerry_value_t
7770-
jerry_string_sz (const char *str_p);
7775+
#define jerry_string_sz(str)
77717776
```
77727777

7773-
- `str_p` - non-null pointer to zero-terminated string
7778+
- `str` - A zero-terminated UTF-8 encoded literal string
77747779
- return value - created string
77757780

77767781
*Renamed in version 3.0, it was previously known as `jerry_create_string` in earlier versions.*
@@ -7779,7 +7784,7 @@ jerry_string_sz (const char *str_p);
77797784

77807785
```c
77817786
{
7782-
const char char_array[] = "a string";
7787+
#define char_array "a string"
77837788
jerry_value_t string_value = jerry_string_sz (char_array);
77847789

77857790
... // usage of string_value
@@ -7790,7 +7795,7 @@ jerry_string_sz (const char *str_p);
77907795

77917796
**See also**
77927797

7793-
- [jerry_string](#jerry_string)
7798+
- [jerry_string_utf8](#jerry_string_utf8)
77947799

77957800

77967801
## jerry_string
@@ -7839,31 +7844,120 @@ jerry_string (const jerry_char_t *buffer_p,
78397844

78407845
- [jerry_validate_string](#jerry_validate_string)
78417846
- [jerry_string_sz](#jerry_string_sz)
7847+
- [jerry_string_utf8](#jerry_string_utf8)
7848+
- [jerry_string_cesu8](#jerry_string_cesu8)
7849+
7850+
7851+
## jerry_string_utf8
7852+
7853+
**Summary**
7854+
7855+
Create a string value from the input buffer using the UTF-8 encoding.
7856+
The content of the buffer is assumed to be valid in the UTF-8 encoding,
7857+
it's the callers responsibility to validate the input.
7858+
7859+
*Note*:
7860+
- Returned value must be freed with [jerry_value_free](#jerry_value_free) when it is no longer needed.
7861+
7862+
**Prototype**
7863+
7864+
```c
7865+
jerry_value_t
7866+
jerry_string_utf8 (const jerry_char_t *buffer_p,
7867+
jerry_size_t buf_size)
7868+
```
7869+
7870+
- `buffer_p` - non-null pointer to buffer
7871+
- `buf_size` - size of the buffer
7872+
7873+
**Example**
7874+
7875+
```c
7876+
{
7877+
const jerry_char_t char_array[] = "a string";
7878+
jerry_value_t string_value = jerry_string_utf8 (char_array,
7879+
sizeof (char_array) - 1);
7880+
7881+
... // usage of string_value
7882+
7883+
jerry_value_free (string_value);
7884+
}
7885+
7886+
```
7887+
7888+
**See also**
7889+
7890+
- [jerry_validate_string](#jerry_validate_string)
7891+
- [jerry_string_sz](#jerry_string_sz)
7892+
- [jerry_string](#jerry_string)
7893+
7894+
7895+
## jerry_string_cesu8
7896+
7897+
**Summary**
7898+
7899+
Create a string value from the input buffer using the CESU-8 encoding.
7900+
The content of the buffer is assumed to be valid in the CESU-8 encoding,
7901+
it's the callers responsibility to validate the input.
7902+
7903+
*Note*:
7904+
- Returned value must be freed with [jerry_value_free](#jerry_value_free) when it is no longer needed.
7905+
7906+
**Prototype**
7907+
7908+
```c
7909+
jerry_value_t
7910+
jerry_string_cesu8 (const jerry_char_t *buffer_p,
7911+
jerry_size_t buf_size)
7912+
```
7913+
7914+
- `buffer_p` - non-null pointer to buffer
7915+
- `buf_size` - size of the buffer
7916+
7917+
**Example**
7918+
7919+
```c
7920+
{
7921+
const jerry_char_t char_array[] = "\xed\xa0\x83\xed\xb2\x80";
7922+
jerry_value_t string_value = jerry_string_cesu8 (char_array,
7923+
sizeof (char_array) - 1);
7924+
7925+
... // usage of string_value
7926+
7927+
jerry_value_free (string_value);
7928+
}
7929+
7930+
```
7931+
7932+
**See also**
7933+
7934+
- [jerry_validate_string](#jerry_validate_string)
7935+
- [jerry_string](#jerry_string)
78427936

78437937

78447938
## jerry_string_external_sz
78457939

78467940
**Summary**
78477941

7848-
Create an external string from a zero-terminated ASCII string. The string buffer passed to the function
7849-
should not be modified until the free callback is called. This function can be used to avoid
7850-
the duplication of large strings.
7942+
Create external string from the zero-terminated CESU encoded literal string.
7943+
The content of the buffer is assumed be encoded correctly, it's the callers
7944+
responsibility to validate the input.
78517945

78527946
*Note*:
7947+
- This is a macro that only accept literal string
78537948
- The free callback can be set by [jerry_string_external_on_free](#jerry_string_external_on_free)
78547949
- Returned value must be freed with [jerry_value_free](#jerry_value_free)
78557950
when it is no longer needed.
78567951

78577952
**Prototype**
78587953

78597954
```c
7860-
jerry_value_t
7861-
jerry_string_external_sz (const char *str_p, void *user_p);
7955+
#define jerry_string_external_sz(str, user_p)
78627956
```
78637957

7864-
- `str_p` - non-null pointer to a zero-terminated string
7958+
- `str_p` - A zero-terminated CESU-8 encoded literal string
78657959
- `user_p` - user pointer passed to the callback when the string is freed
7866-
- return value - value of the created string
7960+
- return value - created external string
78677961

78687962
*Introduced in version 2.4*.
78697963

@@ -8096,10 +8190,10 @@ jerry_regexp_sz (const jerry_char_t *pattern_p, uint16_t flags);
80968190

80978191
```c
80988192
{
8099-
jerry_char_t pattern_p = "[cgt]gggtaaa|tttaccc[acg]";
8193+
#define pattern "[cgt]gggtaaa|tttaccc[acg]"
81008194
uint16_t pattern_flags = JERRY_REGEXP_FLAG_IGNORE_CASE;
81018195

8102-
jerry_value_t regexp = jerry_regexp_sz (pattern_p, pattern_flags);
8196+
jerry_value_t regexp = jerry_regexp_sz (jerry_string_sz (pattern), pattern_flags);
81038197

81048198
...
81058199

@@ -8141,7 +8235,7 @@ jerry_regexp (const jerry_value_t pattern, uint16_t flags);
81418235
{
81428236
jerry_char_t pattern_p = "[cgt]gggtaaa|tttaccc[acg]";
81438237
jerry_size_t pattern_size = sizeof (pattern_p) - 1;
8144-
jerry_value_t pattern_str = jerry_string (pattern_p, pattern_size, JERRY_ENCODING_UTF8);
8238+
jerry_value_t pattern_str = jerry_string_utf8 (pattern_p, pattern_size);
81458239

81468240
uint16_t pattern_flags = JERRY_REGEXP_FLAG_IGNORE_CASE;
81478241

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)