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 9, 2024
1 parent c509a06 commit 4bf1f07
Show file tree
Hide file tree
Showing 94 changed files with 759 additions and 779 deletions.
2 changes: 1 addition & 1 deletion docs/01.CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ Enabling this feature provides detailed error messages where available, like lin

### Logging

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.
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.
This feature is disabled by default.

| Options | |
Expand Down
101 changes: 28 additions & 73 deletions docs/02.API-REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,6 @@ typedef void (*jerry_value_free_callback_t) (void *native_p);

**See also**

- [jerry_string_external_sz](#jerry_string_external_sz)
- [jerry_string_external](#jerry_string_external)
- [jerry_arraybuffer_external](#jerry_arraybuffer_external)

Expand Down Expand Up @@ -809,7 +808,6 @@ typedef void (*jerry_external_string_free_cb_t) (jerry_char_t *string_p,
**See also**

- [jerry_string_external_on_free](#jerry_string_external_on_free)
- [jerry_string_external_sz](#jerry_string_external_sz)
- [jerry_string_external](#jerry_string_external)

## jerry_error_object_created_cb_t
Expand Down Expand Up @@ -3750,7 +3748,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 +3866,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 +4060,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,8 +4351,8 @@ 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!";
jerry_value_t external_string = jerry_string_external_sz (string_p, NULL);
#define string_p "This is a long external string, should not be duplicated!"
jerry_value_t external_string = jerry_string_external (JERRY_ZSTR_ARG (string_p), NULL);
/* The external_string_free_callback is called. */
jerry_value_free (external_string);

Expand All @@ -4367,7 +4365,6 @@ main (void)

- [jerry_external_string_free_cb_t](#jerry_external_string_free_cb_t)
- [jerry_string_user_ptr](#jerry_string_user_ptr)
- [jerry_string_external_sz](#jerry_string_external_sz)
- [jerry_string_external](#jerry_string_external)


Expand All @@ -4381,7 +4378,7 @@ Returns the user pointer assigned to an external string.
- In some cases (e.g. when the string is also a magic string registered by
[jerry_register_magic_strings](#jerry_register_magic_strings)), the
string is a normal string without a user pointer even if it is created
by [jerry_string_external_sz](#jerry_string_external_sz).
by [jerry_string_external](#jerry_string_external).

**Prototype**

Expand Down Expand Up @@ -4414,9 +4411,9 @@ 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);
jerry_value_t external_string = jerry_string_external (JERRY_ZSTR_ARG (string_p), (void *) &user_value);

bool is_external;
void *user_p = jerry_string_user_ptr (external_string, &is_external);
Expand All @@ -4437,7 +4434,6 @@ main (void)
**See also**

- [jerry_string_set_external_string_free_callback](#jerry_string_set_external_string_free_callback)
- [jerry_string_external_sz](#jerry_string_external_sz)
- [jerry_string_external](#jerry_string_external)


Expand Down Expand Up @@ -6948,7 +6944,13 @@ 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`.
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.
The `message_sz` value will be freed in this function.

*Note*: Returned value must be freed with [jerry_value_free](#jerry_value_free) when it
is no longer needed.
Expand All @@ -6957,18 +6959,18 @@ is no longer needed.

```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,7 +7474,8 @@ main (void)

**Summary**

Create string from a zero-terminated ASCII string.
Create string value from the zero-terminated UTF-8 encoded literal string.
This is a macro that only accept literal string

*Note*: Returned value must be freed with [jerry_value_free](#jerry_value_free) when it
is no longer needed.
Expand All @@ -7481,17 +7484,17 @@ is no longer needed.

```c
jerry_value_t
jerry_string_sz (const char *str_p);
jerry_string_sz (literal_str);
```

- `str_p` - non-null pointer to string
- `literal_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 +7505,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 @@ -7549,54 +7552,6 @@ jerry_string (const jerry_char_t *buffer_p,
- [jerry_validate_string](#jerry_validate_string)
- [jerry_string_sz](#jerry_string_sz)


## jerry_string_external_sz

**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.

*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.

**Prototype**

```c
jerry_value_t
jerry_string_external_sz (const char *str_p, void *user_p);
```

- `str_p` - non-null pointer to string
- `user_p` - user pointer passed to the callback when the string is freed
- return value - value of the created string

*New in version 2.4*.

*Changed in version [[NEXT_RELEASE]]*: `free_cb` is replaced by `user_p`.

**Example**

```c
{
const char* string_p = "a large and immutable string: this is a story about ....";
jerry_value_t string_value = jerry_string_external_sz (string_p, NULL);

... // usage of string_value

jerry_value_free (string_value);
}
```

**See also**

- [jerry_string_external](#jerry_string_external)
- [jerry_string_external_on_free](#jerry_string_external_on_free)


## jerry_string_external

**Summary**
Expand Down Expand Up @@ -7647,7 +7602,7 @@ jerry_string_external (const jerry_char_t *str_p,
**See also**

- [jerry_validate_string](#jerry_validate_string)
- [jerry_string_external_sz](#jerry_string_external_sz)
- [jerry_string_external](#jerry_string_external)
- [jerry_string_external_on_free](#jerry_string_external_on_free)


Expand Down Expand Up @@ -7794,10 +7749,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 +7791,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
33 changes: 21 additions & 12 deletions docs/03.API-EXAMPLE.md
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,17 @@ In this example the following extension methods are used:

In further examples this "print" handler will be used.

The `api-example-6.c` file should contain the following code:

[doctest]: # ()

```c
#include <stdio.h>

#include "jerryscript.h"

#include "jerryscript-ext/handlers.h"
#include "jerryscript-ext/properties.h"

int
main (void)
Expand All @@ -407,7 +415,7 @@ main (void)
jerry_init (JERRY_INIT_EMPTY);

/* Register 'print' function from the extensions to the global object */
jerryx_register_global ("print", jerryx_handler_print);
jerryx_register_global (jerry_string_sz ("print"), jerryx_handler_print);

/* Setup Global scope code */
jerry_value_t parsed_code = jerry_parse (script, script_size, NULL);
Expand Down Expand Up @@ -470,7 +478,7 @@ main (void)
jerry_init (JERRY_INIT_EMPTY);

/* Register 'print' function from the extensions */
jerryx_register_global ("print", jerryx_handler_print);
jerryx_register_global (jerry_string_sz ("print"), jerryx_handler_print);

/* Getting pointer to the Global object */
jerry_value_t global_object = jerry_current_realm ();
Expand Down Expand Up @@ -729,7 +737,7 @@ main (void)
jerry_init (JERRY_INIT_EMPTY);
/* Register 'print' function from the extensions */
jerryx_register_global ("print", jerryx_handler_print);
jerryx_register_global (jerry_string_sz ("print"), jerryx_handler_print);
while (!is_done)
{
Expand Down Expand Up @@ -808,10 +816,8 @@ In this example (`api-example-9.c`) an object with a native function is added to
#include "jerryscript-ext/handlers.h"
#include "jerryscript-ext/properties.h"

struct my_struct
{
const char *msg;
} my_struct;

jerry_string_t my_struct;

/**
* Get a string from a native object
Expand All @@ -821,7 +827,7 @@ get_msg_handler (const jerry_call_info_t *call_info_p, /**< call information */
const jerry_value_t *args_p, /**< function arguments */
const jerry_length_t args_cnt) /**< number of function arguments */
{
return jerry_string_sz (my_struct.msg);
return jerry_string_utf8 (my_struct.ptr, my_struct.size);
} /* get_msg_handler */

int
Expand All @@ -831,10 +837,13 @@ main (void)
jerry_init (JERRY_INIT_EMPTY);

/* Register 'print' function from the extensions */
jerryx_register_global ("print", jerryx_handler_print);
jerryx_register_global (jerry_string_sz ("print"), jerryx_handler_print);

/* Do something with the native object */
my_struct.msg = "Hello, World!";
{
static const jerry_string_t hello = { JERRY_ZSTR_ARG ("Hello, World!") };
my_struct = hello;
}

/* Create an empty JS object */
jerry_value_t object = jerry_object ();
Expand Down Expand Up @@ -958,7 +967,7 @@ main (void)
jerry_init (JERRY_INIT_EMPTY);

/* Register 'print' function from the extensions */
jerryx_register_global ("print", jerryx_handler_print);
jerryx_register_global (jerry_string_sz ("print"), jerryx_handler_print);

/* Create a JS object */
const jerry_char_t my_js_object[] = " \
Expand Down Expand Up @@ -1058,7 +1067,7 @@ main (void)
jerry_init (JERRY_INIT_EMPTY);

/* Register the print function */
jerryx_register_global ("print", jerryx_handler_print);
jerryx_register_global (jerry_string_sz ("print"), jerryx_handler_print);

/* Evaluate the script */
jerry_value_t eval_ret = jerry_eval (script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);
Expand Down
5 changes: 4 additions & 1 deletion docs/05.PORT-API.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,11 @@ void jerry_port_context_free (void);
*
* The implementation can decide whether error and debug messages are logged to
* the console, or saved to a database or to a file.
*
* @param buffer_p: input buffer
* @param buffer_size: data size
*/
void jerry_port_log (const char *message_p);
void jerry_port_log_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size);
```
```c
Expand Down
2 changes: 1 addition & 1 deletion docs/06.REFERENCE-COUNTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ jerry_value_t my_external_handler (const jerry_value_t function_obj,
/* If the value to be returned is needed for other purposes the
* jerry_value_copy () can be used to create new references. */
return jerry_string (...);
return jerry_string_utf8 (...);
}
```

Expand Down
5 changes: 2 additions & 3 deletions docs/07.DEBUGGER.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,8 @@ wait_for_source_callback (const jerry_char_t *source_name_p, /**< source name */
jerry_parse_options_t parse_options;
parse_options.options = JERRY_PARSE_HAS_SOURCE_NAME;
parse_options.source_name = jerry_string ((const jerry_char_t *) source_name_p,
(jerry_size_t) source_name_size,
JERRY_ENCODING_UTF8);
parse_options.source_name = jerry_string_utf8 ((const jerry_char_t *) source_name_p,
(jerry_size_t) source_name_size);
jerry_value_t ret_val = jerry_parse (source_p,
source_size,
Expand Down
Loading

0 comments on commit 4bf1f07

Please sign in to comment.