Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce call to strlen when possible #5192

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
198 changes: 152 additions & 46 deletions docs/02.API-REFERENCE.md

Large diffs are not rendered by default.

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
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 @@ -320,9 +320,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_string_sz ("enable"),
jerry_string_sz ("data"),
jerry_string_sz ("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_utf8 (common_functions[i].name_p, common_functions[i].name_size),
common_functions[i].handler_p);
}

Expand Down
2 changes: 1 addition & 1 deletion docs/12.EXT-REFERENCE-MODULE.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ canonicalize_file_path (const jerry_value_t name)
/**
* Since a file on the file system can be referred to by multiple relative paths, but only by one absolute path, the
* absolute path becomes the canonical name for the module. Thus, to establish this canonical name, we must search
* name for "./" and "../", follow symlinks, etc., then create absolute_path via jerry_string () and return
* name for "./" and "../", follow symlinks, etc., then create absolute_path via jerry_string_utf8 () and return
* it, because it is the canonical name for this module. Thus, we avoid loading the same JavaScript file twice.
*/

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_string_sz ("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_string_sz ("Module file not found"));
}

jerry_parse_options_t parse_options;
Expand Down
8 changes: 4 additions & 4 deletions jerry-core/api/jerry-snapshot.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ snapshot_add_compiled_code (const ecma_compiled_code_t *compiled_code_p, /**< co
size_t snapshot_buffer_size, /**< snapshot buffer size */
snapshot_globals_t *globals_p) /**< snapshot globals */
{
const char *error_buffer_too_small_p = "Snapshot buffer too small";
#define error_buffer_too_small_p "Snapshot buffer too small"

if (!ecma_is_value_empty (globals_p->snapshot_error))
{
Expand Down Expand Up @@ -180,7 +180,7 @@ snapshot_add_compiled_code (const ecma_compiled_code_t *compiled_code_p, /**< co
/* Regular expression. */
if (globals_p->snapshot_buffer_write_offset + sizeof (ecma_compiled_code_t) > snapshot_buffer_size)
{
globals_p->snapshot_error = jerry_throw_sz (JERRY_ERROR_RANGE, error_buffer_too_small_p);
globals_p->snapshot_error = jerry_throw_sz (JERRY_ERROR_RANGE, jerry_string_sz (error_buffer_too_small_p));
return 0;
}

Expand All @@ -201,7 +201,7 @@ snapshot_add_compiled_code (const ecma_compiled_code_t *compiled_code_p, /**< co
buffer_p,
buffer_size))
{
globals_p->snapshot_error = jerry_throw_sz (JERRY_ERROR_RANGE, error_buffer_too_small_p);
globals_p->snapshot_error = jerry_throw_sz (JERRY_ERROR_RANGE, jerry_string_sz (error_buffer_too_small_p));
/* cannot return inside ECMA_FINALIZE_UTF8_STRING */
}

Expand Down Expand Up @@ -235,7 +235,7 @@ snapshot_add_compiled_code (const ecma_compiled_code_t *compiled_code_p, /**< co
compiled_code_p,
((size_t) compiled_code_p->size) << JMEM_ALIGNMENT_LOG))
{
globals_p->snapshot_error = jerry_throw_sz (JERRY_ERROR_RANGE, error_buffer_too_small_p);
globals_p->snapshot_error = jerry_throw_sz (JERRY_ERROR_RANGE, jerry_string_sz (error_buffer_too_small_p));
return 0;
}

Expand Down
Loading
Loading