Skip to content

Commit faf4916

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 48ac53d commit faf4916

File tree

78 files changed

+820
-639
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

+820
-639
lines changed

docs/02.API-REFERENCE.md

Lines changed: 137 additions & 43 deletions
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

@@ -8574,11 +8668,11 @@ Checks whether the object or its prototype objects have the given property.
85748668
**Prototype**
85758669

85768670
```c
8577-
jerry_value_t jerry_object_has_sz (const jerry_value_t object, const char *key_p);
8671+
jerry_value_t jerry_object_has_sz (const jerry_value_t object, const jerry_value_t key_sz);
85788672
```
85798673

85808674
- `object` - object value
8581-
- `key_p` - property name (zero-terminated string)
8675+
- `key_sz` - property key that will be free/take
85828676
- return value - JavaScript value that evaluates to
85838677
- exception - if the operation fail
85848678
- true/false API value - depend on whether the property exists
@@ -8599,7 +8693,7 @@ main (void)
85998693

86008694
jerry_value_t global_object = jerry_current_realm ();
86018695

8602-
jerry_value_t has_prop_js = jerry_object_has_sz (global_object, "handler_field");
8696+
jerry_value_t has_prop_js = jerry_object_has_sz (global_object, jerry_string_sz ("handler_field"));
86038697
bool has_prop = jerry_value_is_true (has_prop_js);
86048698

86058699
jerry_value_free (has_prop_js);
@@ -8796,11 +8890,11 @@ Delete a property from an object.
87968890
**Prototype**
87978891

87988892
```c
8799-
jerry_value_t jerry_object_delete_sz (const jerry_value_t object, const char *key_p);
8893+
jerry_value_t jerry_object_delete_sz (const jerry_value_t object, const jerry_value_t key_sz);
88008894
```
88018895

88028896
- `obj_val` - object value
8803-
- `key_p` - property name (zero-terminated string)
8897+
- `key_sz` - < property name that will be free/take
88048898
- return value
88058899
- true, if property was deleted successfully
88068900
- exception, otherwise
@@ -8813,7 +8907,7 @@ jerry_value_t jerry_object_delete_sz (const jerry_value_t object, const char *ke
88138907
{
88148908
jerry_value_t global_object = jerry_current_realm ();
88158909

8816-
jerry_value_t delete_result = jerry_object_delete_sz (global_object, "my_prop");
8910+
jerry_value_t delete_result = jerry_object_delete_sz (global_object, jerry_string_sz ("my_prop"));
88178911
/* use "delete_result" */
88188912

88198913
jerry_value_free (delete_result);
@@ -9001,11 +9095,11 @@ is no longer needed.
90019095
**Prototype**
90029096

90039097
```c
9004-
jerry_value_t jerry_object_get_sz (const jerry_value_t object, const char *key_p);
9098+
jerry_value_t jerry_object_get_sz (const jerry_value_t object, const jerry_value_t key_sz);
90059099
```
90069100

90079101
- `obj_val` - object value
9008-
- `key_p` - property name (zero-terminated string)
9102+
- `key_sz` - property key that will be free/take
90099103
- return value
90109104
- value of property, if success
90119105
- thrown exception, otherwise
@@ -9026,7 +9120,7 @@ main (void)
90269120

90279121
jerry_value_t global_object = jerry_current_realm ();
90289122

9029-
jerry_value_t prop_value = jerry_object_get_sz (global_object, "Object");
9123+
jerry_value_t prop_value = jerry_object_get_sz (global_object, jerry_string_sz ("Object"));
90309124

90319125
/* use "prop_value" then release it. */
90329126

@@ -9309,11 +9403,11 @@ is no longer needed.
93099403
**Prototype**
93109404

93119405
```c
9312-
jerry_value_t jerry_object_set_sz (jerry_value_t object, const char *key_p, const jerry_value_t value);
9406+
jerry_value_t jerry_object_set_sz (jerry_value_t object, const jerry_value_t key_sz, const jerry_value_t value);
93139407
```
93149408

93159409
- `obj_val` - object value
9316-
- `key_p` - property name (zero-terminated string)
9410+
- `key_sz` - property key that will be free/take
93179411
- `value_to_set` - value to set
93189412
- return value
93199413
- true, if success
@@ -9331,7 +9425,7 @@ jerry_value_t jerry_object_set_sz (jerry_value_t object, const char *key_p, cons
93319425

93329426
jerry_value_t glob_obj = jerry_current_realm ();
93339427

9334-
jerry_value_t set_result = jerry_object_set_sz (glob_obj, "my_prop", value_to_set);
9428+
jerry_value_t set_result = jerry_object_set_sz (glob_obj, jerry_string_sz ("my_prop"), value_to_set);
93359429

93369430
... // check result of property set call
93379431

0 commit comments

Comments
 (0)