Skip to content

Commit

Permalink
Reduce call to strlen when possible
Browse files Browse the repository at this point in the history
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]
  • Loading branch information
lygstate committed Dec 9, 2024
1 parent c509a06 commit db3a212
Show file tree
Hide file tree
Showing 118 changed files with 1,089 additions and 1,046 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
230 changes: 90 additions & 140 deletions docs/02.API-REFERENCE.md

Large diffs are not rendered by default.

51 changes: 30 additions & 21 deletions docs/03.API-EXAMPLE.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ This method will be implemented in C and will be called from the JavaScript code
For this a few extra API methods are required:

- `jerry_current_realm`
- `jerry_string_sz`
- `jerry_sz_ascii`
- `jerry_object_set`
- `jerry_function_external`

Expand Down Expand Up @@ -207,7 +207,7 @@ main (void)
/* Get the "global" object */
jerry_value_t global_object = jerry_current_realm ();
/* Create a "print" JS string */
jerry_value_t property_name_print = jerry_string_sz ("print");
jerry_value_t property_name_print = jerry_sz_ascii ("print");
/* Create a function from a native C method (this function will be called from JS) */
jerry_value_t property_value_func = jerry_function_external (print_handler);
/* Add the "print" property with the function value to the "global" object */
Expand Down Expand Up @@ -326,7 +326,7 @@ main (void)
/* Get the "global" object */
jerry_value_t global_object = jerry_current_realm ();
/* Create a "print" JS string */
jerry_value_t property_name_print = jerry_string_sz ("print");
jerry_value_t property_name_print = jerry_sz_ascii ("print");
/* Create a function from a native C method (this function will be called from JS) */
jerry_value_t property_value_func = jerry_function_external (print_handler);
/* Add the "print" property with the function value to the "global" object */
Expand Down 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_sz_ascii ("print"), jerryx_handler_print);

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

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

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

/* Constructing strings */
jerry_value_t prop_name = jerry_string_sz ("my_var");
jerry_value_t prop_value = jerry_string_sz ("Hello from C!");
jerry_value_t prop_name = jerry_sz_ascii ("my_var");
jerry_value_t prop_value = jerry_sz_ascii ("Hello from C!");

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

/* Set the native function as a property of the empty JS object */
jerry_value_t prop_name = jerry_string_sz ("myFunc");
jerry_value_t prop_name = jerry_sz_ascii ("myFunc");
jerry_value_free (jerry_object_set (object, prop_name, func_obj));
jerry_value_free (prop_name);
jerry_value_free (func_obj);
Expand All @@ -852,7 +861,7 @@ main (void)
jerry_value_t global_object = jerry_current_realm ();

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

/* Get 'this.x' */
jerry_value_t prop_name = jerry_string_sz ("x");
jerry_value_t prop_name = jerry_sz_ascii ("x");
jerry_value_t x_val = jerry_object_get (call_info_p->this_value, prop_name);

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

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

/* Set the native function as a property of previously created MyObject */
jerry_value_t prop_name = jerry_string_sz ("add2x");
jerry_value_t prop_name = jerry_sz_ascii ("add2x");
jerry_value_free (jerry_object_set (my_js_obj_val, prop_name, add_func_obj));
jerry_value_free (add_func_obj);
jerry_value_free (prop_name);
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_sz_ascii ("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
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
42 changes: 17 additions & 25 deletions docs/09.EXT-REFERENCE-ARG.md
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,7 @@ jerryx_arg_object_properties (const jerryx_arg_object_props_t *obj_prop_p,
```c
#include "jerryscript.h"
#include "jerryscript-ext/arg.h"
/**
Expand All @@ -628,39 +629,31 @@ my_external_handler (const jerry_value_t function_obj,
{
bool required_bool;
double required_num;
double optional_num = 1234.567; // default value
double optional_num = 1234.567; // default value
/* "prop_name_p" defines the name list of the expected properties' names. */
const char *prop_name_p[] = { "enable", "data", "extra_data" };
const jerry_value_t prop_name_p[] = { jerry_sz_ascii ("enable"),
jerry_sz_ascii ("data"),
jerry_sz_ascii ("extra_data") };
/* "prop_mapping" defines the steps to transform properties to C variables. */
const jerryx_arg_t prop_mapping[] =
{
jerryx_arg_boolean (&required_bool, JERRYX_ARG_COERCE, JERRYX_ARG_REQUIRED),
jerryx_arg_number (&required_num, JERRYX_ARG_COERCE, JERRYX_ARG_REQUIRED),
jerryx_arg_number (&optional_num, JERRYX_ARG_COERCE, JERRYX_ARG_OPTIONAL)
};
const jerryx_arg_t prop_mapping[] = { jerryx_arg_boolean (&required_bool, JERRYX_ARG_COERCE, JERRYX_ARG_REQUIRED),
jerryx_arg_number (&required_num, JERRYX_ARG_COERCE, JERRYX_ARG_REQUIRED),
jerryx_arg_number (&optional_num, JERRYX_ARG_COERCE, JERRYX_ARG_OPTIONAL) };
/* Prepare the jerryx_arg_object_props_t instance. */
const jerryx_arg_object_props_t prop_info =
{
.name_p = (const jerry_char_t **) prop_name_p,
.name_cnt = 3,
.c_arg_p = prop_mapping,
.c_arg_cnt = 3
};
const jerryx_arg_object_props_t prop_info = { .name_p = prop_name_p,
.name_cnt = 3,
.c_arg_p = prop_mapping,
.c_arg_cnt = 3 };
/* It is the mapping used in the jerryx_arg_transform_args. */
const jerryx_arg_t mapping[] =
{
jerryx_arg_object_properties (&prop_info, JERRYX_ARG_REQUIRED)
};
const jerryx_arg_t mapping[] = { jerryx_arg_object_properties (&prop_info, JERRYX_ARG_REQUIRED) };
jerry_value_list_free (prop_name_p, sizeof (prop_name_p) / sizeof (prop_name_p[0]));
/* Validate and transform. */
const jerry_value_t rv = jerryx_arg_transform_args (args_p,
args_count,
mapping,
1);
const jerry_value_t rv = jerryx_arg_transform_args (args_p, args_count, mapping, 1);
if (jerry_value_is_exception (rv))
{
Expand All @@ -673,9 +666,8 @@ my_external_handler (const jerry_value_t function_obj,
* required_bool, required_num and optional_num can now be used.
*/
return jerry_undefined (); /* Or return something more meaningful. */
return jerry_undefined (); /* Or return something more meaningful. */
}
```

**See also**
Expand Down
19 changes: 10 additions & 9 deletions docs/10.EXT-REFERENCE-HANDLER.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ main (int argc, char **argv)
jerryx_property_entry methods[] =
{
{ "demo", jerry_function_external (handler) },
{ NULL, 0 },
JERRYX_PROPERTY_FUNCTION("demo", handler),
JERRYX_PROPERTY_LIST_END(),
};
jerry_value_t global = jerry_current_realm ();
Expand Down Expand Up @@ -362,7 +362,7 @@ longer needed.

```c
jerry_value_t
jerryx_register_global (const char *name_p,
jerryx_register_global (jerry_value_t function_name_val,
jerry_external_handler_t handler_p);
```
Expand All @@ -381,14 +381,15 @@ jerryx_register_global (const char *name_p,
#include "jerryscript-ext/properties.h"
static const struct {
const char *name_p;
const jerry_char_t *name_p;
jerry_size_t name_size;
jerry_external_handler_t handler_p;
} common_functions[] =
{
{ "assert", jerryx_handler_assert },
{ "gc", jerryx_handler_gc },
{ "print", jerryx_handler_print },
{ NULL, NULL }
{ JERRY_ZSTR_ARG ("assert"), jerryx_handler_assert },
{ JERRY_ZSTR_ARG ("gc"), jerryx_handler_gc },
{ JERRY_ZSTR_ARG ("print"), jerryx_handler_print },
{ NULL, 0, NULL }
};
static void
Expand All @@ -398,7 +399,7 @@ register_common_functions (void)
for (int i = 0; common_functions[i].name_p != NULL && !jerry_value_is_exception (ret); i++)
{
ret = jerryx_register_global (common_functions[i].name_p,
ret = jerryx_register_global (jerry_string_ascii (common_functions[i].name_p, common_functions[i].name_size),
common_functions[i].handler_p);
}
Expand Down
2 changes: 1 addition & 1 deletion docs/11.EXT-REFERENCE-AUTORELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ using the `__cleanup__` variable attribute. For other compilers, no support has
static void
foo (bool enable)
{
JERRYX_AR_VALUE_T bar = jerry_string_sz ("...");
JERRYX_AR_VALUE_T bar = jerry_sz_ascii ("...");

if (enable)
{
Expand Down
2 changes: 2 additions & 0 deletions docs/16.MIGRATION-GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ For more information on the current API methods please check the [API reference]
- `jerry_get_value_without_error_flag` replaced by `jerry_get_value_from_error`
- `jerry_parse_and_save_snapshot` replaced by `jerry_generate_snapshot`
- `jerry_parse_and_save_function_snapshot` replaced by `jerry_generate_function_snapshot`
- `jerry_string_external_sz` replaced by `jerry_string_external`
- `jerry_string_sz` replaced by `jerry_sz_ascii` or `jerry_string_cesu8`


***Removed unused configuration macros***
Expand Down
6 changes: 3 additions & 3 deletions jerry-core/api/jerry-module.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "jerryscript-core.h"
#include "jerryscript-port.h"

#include "ecma-errors.h"
#include "ecma-globals.h"

#include "lit-magic-strings.h"
#include "lit-strings.h"
Expand Down Expand Up @@ -162,7 +162,7 @@ jerry_module_resolve (const jerry_value_t specifier, /**< module specifier strin

if (path_p == NULL)
{
return jerry_throw_sz (JERRY_ERROR_SYNTAX, "Failed to resolve module");
return jerry_throw_sz (JERRY_ERROR_SYNTAX, jerry_sz_ascii ("Failed to resolve module"));
}

jerry_value_t realm = jerry_current_realm ();
Expand Down Expand Up @@ -192,7 +192,7 @@ jerry_module_resolve (const jerry_value_t specifier, /**< module specifier strin
jerry_value_free (realm);
jerry_port_path_free (path_p);

return jerry_throw_sz (JERRY_ERROR_SYNTAX, "Module file not found");
return jerry_throw_sz (JERRY_ERROR_SYNTAX, jerry_sz_ascii ("Module file not found"));
}

jerry_parse_options_t parse_options;
Expand Down
Loading

0 comments on commit db3a212

Please sign in to comment.