Skip to content

Commit

Permalink
Reduce the usage of strlen
Browse files Browse the repository at this point in the history
* 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_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);
```

Rename jerry_port_log to jerry_port_log_buffer

Add buffer_size parameter for function jerry_port_log_buffer, so that jerry_port_log_buffer
won't need calculate strlen when printing

JerryScript-DCO-1.0-Signed-off-by: Yonggang Luo [email protected]
  • Loading branch information
lygstate committed Dec 11, 2024
1 parent b151443 commit 91a8163
Show file tree
Hide file tree
Showing 77 changed files with 821 additions and 633 deletions.
171 changes: 133 additions & 38 deletions docs/02.API-REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -3750,7 +3750,7 @@ jerry_error_type (const jerry_value_t value);

```c
{
jerry_value_t error_obj = jerry_error_sz (JERRY_ERROR_RANGE, "error msg");
jerry_value_t error_obj = jerry_error_sz (JERRY_ERROR_RANGE, jerry_string_sz ("error msg"));
jerry_error_t error_type = jerry_error_type (error_obj);

// error_type is now JERRY_ERROR_RANGE.
Expand Down Expand Up @@ -3868,7 +3868,7 @@ void main(void)

jerry_error_on_created (error_object_created_callback, NULL);

jerry_value_free (jerry_error_sz (JERRY_ERROR_COMMON, "Message"));
jerry_value_free (jerry_error_sz (JERRY_ERROR_COMMON, jerry_string_sz ("Message")));

jerry_cleanup ();
} /* main */
Expand Down Expand Up @@ -4062,7 +4062,7 @@ throw_exception (const jerry_call_info_t *call_info_p, /**< call info */
(void) argv;
(void) argc;

jerry_value_t result_value = jerry_throw_sz (JERRY_ERROR_COMMON, "Error!");
jerry_value_t result_value = jerry_throw_sz (JERRY_ERROR_COMMON, jerry_string_sz ("Error!"));

/* Ignore calling the vm_throw_callback function. */
jerry_exception_allow_capture (result_value, false);
Expand Down Expand Up @@ -4353,7 +4353,7 @@ main (void)

jerry_string_external_on_free (external_string_free_callback);

const char *string_p = "This is a long external string, should not be duplicated!";
#define string_p "This is a long external string, should not be duplicated!"
jerry_value_t external_string = jerry_string_external_sz (string_p, NULL);
/* The external_string_free_callback is called. */
jerry_value_free (external_string);
Expand Down Expand Up @@ -4414,7 +4414,7 @@ main (void)
{
jerry_init (JERRY_INIT_EMPTY);

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

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

Expand Down Expand Up @@ -6903,13 +6903,13 @@ jerry_boolean (bool value);

**Summary**

Create new JavaScript Error object with the specified error message.
Create an Error object with the provided `message` string value as the error `message` property.
If the `message` value is not a string, the created error will not have a `message` property.

Important! The `error_type` argument *must not be* `JERRY_ERROR_NONE`.
Creating an Error object with no error type is not valid.

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

**Prototype**

Expand Down Expand Up @@ -6948,27 +6948,31 @@ jerry_error (jerry_error_t error_type, jerry_value_t message);

**Summary**

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

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

**Prototype**

```c
jerry_value_t
jerry_error_sz (jerry_error_t error_type, const char *message_p);
jerry_error_sz (jerry_error_t error_type, const jerry_value_t message_sz);
```

- `error_type` - type of the error
- `message_p` - value of 'message' property of the constructed error object
- `message_sz` - message of the error that will be free/take
- return value - constructed error object

**Example**

```c
{
jerry_value_t error_obj = jerry_error_sz (JERRY_ERROR_TYPE, "error");
jerry_value_t error_obj = jerry_error_sz (JERRY_ERROR_TYPE, jerry_string_sz ("error"));

... // usage of error_obj

Expand Down Expand Up @@ -7472,26 +7476,28 @@ main (void)

**Summary**

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

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

**Prototype**

```c
jerry_value_t
jerry_string_sz (const char *str_p);
#define jerry_string_sz(str)
```

- `str_p` - non-null pointer to string
- `str` - An zero-terminated UTF-8 encoded literal string
- return value - created string

**Example**

```c
{
const char char_array[] = "a string";
#define char_array "a string"
jerry_value_t string_value = jerry_string_sz (char_array);

... // usage of string_value
Expand All @@ -7502,7 +7508,7 @@ jerry_string_sz (const char *str_p);

**See also**

- [jerry_string](#jerry_string)
- [jerry_string_utf8](#jerry_string_utf8)


## jerry_string
Expand Down Expand Up @@ -7548,31 +7554,120 @@ jerry_string (const jerry_char_t *buffer_p,

- [jerry_validate_string](#jerry_validate_string)
- [jerry_string_sz](#jerry_string_sz)
- [jerry_string_utf8](#jerry_string_utf8)
- [jerry_string_cesu8](#jerry_string_cesu8)


## jerry_string_external_sz
## jerry_string_utf8

**Summary**

Create an external string from a zero-terminated ASCII string. The string buffer passed to the function
should not be modified until the free callback is called. This function can be used to avoid
the duplication of large strings.
Create a string value from the input buffer using the UTF-8 encoding.
The content of the buffer is assumed to be valid in the UTF-8 encoding,
it's the callers responsibility to validate the input.

*Note*:
- The free callback can be set by [jerry_string_external_on_free](#jerry_string_external_on_free)
- Returned value must be freed with [jerry_value_free](#jerry_value_free)
when it is no longer needed.
- Returned value must be freed with [jerry_value_free](#jerry_value_free) when it is no longer needed.

**Prototype**

```c
jerry_value_t
jerry_string_external_sz (const char *str_p, void *user_p);
jerry_string_utf8 (const jerry_char_t *buffer_p,
jerry_size_t buf_size)
```

- `str_p` - non-null pointer to string
- `buffer_p` - non-null pointer to buffer
- `buf_size` - size of the buffer

**Example**

```c
{
const jerry_char_t char_array[] = "a string";
jerry_value_t string_value = jerry_string_utf8 (char_array,
sizeof (char_array) - 1);

... // usage of string_value

jerry_value_free (string_value);
}

```

**See also**

- [jerry_validate_string](#jerry_validate_string)
- [jerry_string_sz](#jerry_string_sz)
- [jerry_string](#jerry_string)


## jerry_string_cesu8

**Summary**

Create a string value from the input buffer using the CESU-8 encoding.
The content of the buffer is assumed to be valid in the CESU-8 encoding,
it's the callers responsibility to validate the input.

*Note*:
- Returned value must be freed with [jerry_value_free](#jerry_value_free) when it is no longer needed.

**Prototype**

```c
jerry_value_t
jerry_string_cesu8 (const jerry_char_t *buffer_p,
jerry_size_t buf_size)
```

- `buffer_p` - non-null pointer to buffer
- `buf_size` - size of the buffer

**Example**

```c
{
const jerry_char_t char_array[] = "\xed\xa0\x83\xed\xb2\x80";
jerry_value_t string_value = jerry_string_cesu8 (char_array,
sizeof (char_array) - 1);

... // usage of string_value

jerry_value_free (string_value);
}

```

**See also**

- [jerry_validate_string](#jerry_validate_string)
- [jerry_string](#jerry_string)


## jerry_string_external_sz

**Summary**

Create external string from the zero-terminated ASCII encoded literal string.
The content of the buffer is assumed be encoded correctly, it's the callers
responsibility to validate the input.

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

**Prototype**

```c
#define jerry_string_external_sz(str, user_p)
```

- `str_p` - zero-terminated ASCII encoded literal string
- `user_p` - user pointer passed to the callback when the string is freed
- return value - value of the created string
- return value - created external string

*New in version 2.4*.

Expand Down Expand Up @@ -7794,10 +7889,10 @@ jerry_regexp_sz (const jerry_char_t *pattern_p, uint16_t flags);

```c
{
jerry_char_t pattern_p = "[cgt]gggtaaa|tttaccc[acg]";
#define pattern "[cgt]gggtaaa|tttaccc[acg]"
uint16_t pattern_flags = JERRY_REGEXP_FLAG_IGNORE_CASE;

jerry_value_t regexp = jerry_regexp_sz (pattern_p, pattern_flags);
jerry_value_t regexp = jerry_regexp_sz (jerry_string_sz (pattern), pattern_flags);

...

Expand Down Expand Up @@ -7836,7 +7931,7 @@ jerry_regexp (const jerry_value_t pattern, uint16_t flags);
{
jerry_char_t pattern_p = "[cgt]gggtaaa|tttaccc[acg]";
jerry_size_t pattern_size = sizeof (pattern_p) - 1;
jerry_value_t pattern_str = jerry_string (pattern_p, pattern_size, JERRY_ENCODING_UTF8);
jerry_value_t pattern_str = jerry_string_utf8 (pattern_p, pattern_size);

uint16_t pattern_flags = JERRY_REGEXP_FLAG_IGNORE_CASE;

Expand Down
Loading

0 comments on commit 91a8163

Please sign in to comment.