Skip to content

Commit

Permalink
Reduce or even remove the usage of strlen in jerry-core and jerry-ext
Browse files Browse the repository at this point in the history
Improving the api of jerry-port, so that we can reduce the usage of strlen.

JerryScript-DCO-1.0-Signed-off-by: Yonggang Luo luoyonggang@gmail.com
  • Loading branch information
lygstate committed Feb 11, 2022
1 parent d00f481 commit b6c061c
Show file tree
Hide file tree
Showing 11 changed files with 271 additions and 118 deletions.
29 changes: 21 additions & 8 deletions docs/05.PORT-API.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,12 @@ void jerry_port_line_free (jerry_char_t *buffer_p);
*
* @param path_p: zero-terminated string containing the input path
* @param path_size: size of the input path string in bytes, excluding terminating zero
* @param out_size_p: The normalized path's size in bytes.
*
* @return buffer with the normalized path if the operation is successful,
* NULL otherwise
*/
jerry_char_t *jerry_port_path_normalize (const jerry_char_t *path_p, jerry_size_t path_size);
jerry_char_t *jerry_port_path_normalize (const jerry_char_t *path_p, jerry_size_t path_size, jerry_size_t *out_size_p);
```
```c
Expand All @@ -211,31 +212,43 @@ void jerry_port_path_free (jerry_char_t *path_p);

```c
/**
* Get the offset of the basename component in the input path.
* Check if a char of a path is a path separator.
*
* The implementation should return the offset of the first character after the last path separator found in the path.
* This is used by the caller to split the path into a directory name and a file name.
* @param path_c: The char to check
*
* @return if a char of a path is a path separator.
*/
bool jerry_port_path_is_separator (jerry_char_t path_c);
```
```c
/**
* Evalute the length of root component of a path
*
* @param path_p: input zero-terminated path string
* @param path_size: length of the path string
*
* @return offset of the basename component in the input path
* @return length of root component of a path if it's a absolute path
* 0 otherwise
*/
jerry_size_t jerry_port_path_base (const jerry_char_t *path_p);
jerry_size_t jerry_port_path_root (const jerry_char_t *path_p, jerry_size_t path_size);
```

```c
/**
* Open a source file and read its contents into a buffer.
* Open a source file and read the content into a buffer.
*
* When the source file is no longer needed by the caller, the returned pointer will be passed to
* `jerry_port_source_free`, which can be used to finalize the buffer.
*
* @param file_name_p: Path that points to the source file in the filesystem.
* @param file_name_size length of source file path
* @param out_size_p: The opened file's size in bytes.
*
* @return pointer to the buffer which contains the content of the file.
*/
jerry_char_t *jerry_port_source_read (const char *file_name_p, jerry_size_t *out_size_p);
jerry_char_t *
jerry_port_source_read (const jerry_char_t *file_name_p, jerry_size_t file_name_size, jerry_size_t *out_size_p);
```
```c
Expand Down
47 changes: 44 additions & 3 deletions jerry-core/api/jerry-module.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,35 @@ static const jerry_context_data_manager_t jerry_module_manager JERRY_ATTR_CONST_
.bytes_needed = sizeof (jerry_module_manager_t)
};

/**
* Get the offset of the basename component in the input path.
*
* The implementation should return the offset of the first character after the last path separator found in the path.
* This is used by the caller to split the path into a directory name and a file name.
*
* @param path_p: input zero-terminated path string
* @param path_size: size of the input path string in bytes, excluding terminating zero
*
* @return offset of the basename component in the input path
*/
static jerry_size_t
jerry_module_path_base (const jerry_char_t *path_p, jerry_size_t path_size)
{
const jerry_char_t *end_p = path_p + path_size;

while (end_p > path_p)
{
if (jerry_port_path_is_separator (end_p[-1]))
{
return (jerry_size_t) (end_p - path_p);
}

end_p--;
}

return 0;
} /* jerry_module_path_base */

#endif /* JERRY_MODULE_SYSTEM */

/**
Expand Down Expand Up @@ -157,7 +186,19 @@ jerry_module_resolve (const jerry_value_t specifier, /**< module specifier strin
jerry_string_to_buffer (specifier, JERRY_ENCODING_UTF8, reference_path_p + directory_size, specifier_size);
reference_path_p[reference_size] = '\0';

jerry_char_t *path_p = jerry_port_path_normalize (reference_path_p, reference_size);
jerry_char_t *specifier_path_p = reference_path_p + directory_size;

jerry_size_t path_size;
jerry_char_t *path_p;
if (jerry_port_path_root (specifier_path_p, specifier_size) > 0)
{
path_p = jerry_port_path_normalize (specifier_path_p, specifier_size, &path_size);
}
else
{
path_p = jerry_port_path_normalize (reference_path_p, reference_size, &path_size);
}

jerry_heap_free (reference_path_p, reference_size + 1);

if (path_p == NULL)
Expand Down Expand Up @@ -185,7 +226,7 @@ jerry_module_resolve (const jerry_value_t specifier, /**< module specifier strin
}

jerry_size_t source_size;
jerry_char_t *source_p = jerry_port_source_read ((const char *) path_p, &source_size);
jerry_char_t *source_p = jerry_port_source_read (path_p, path_size, &source_size);

if (source_p == NULL)
{
Expand Down Expand Up @@ -215,7 +256,7 @@ jerry_module_resolve (const jerry_value_t specifier, /**< module specifier strin

module_p->next_p = manager_p->module_head_p;
module_p->path_p = path_p;
module_p->basename_offset = jerry_port_path_base (module_p->path_p);
module_p->basename_offset = jerry_module_path_base (module_p->path_p, path_size);
module_p->realm = realm;
module_p->module = jerry_value_copy (ret_value);

Expand Down
25 changes: 18 additions & 7 deletions jerry-core/include/jerryscript-port.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,12 @@ void jerry_port_line_free (jerry_char_t *buffer_p);
*
* @param path_p: zero-terminated string containing the input path
* @param path_size: size of the input path string in bytes, excluding terminating zero
* @param out_size_p: The normalized path's size in bytes.
*
* @return buffer with the normalized path if the operation is successful,
* NULL otherwise
*/
jerry_char_t *jerry_port_path_normalize (const jerry_char_t *path_p, jerry_size_t path_size);
jerry_char_t *jerry_port_path_normalize (const jerry_char_t *path_p, jerry_size_t path_size, jerry_size_t *out_size_p);

/**
* Free a path buffer returned by jerry_port_path_normalize.
Expand All @@ -234,16 +235,24 @@ jerry_char_t *jerry_port_path_normalize (const jerry_char_t *path_p, jerry_size_
void jerry_port_path_free (jerry_char_t *path_p);

/**
* Get the offset of the basename component in the input path.
* Check if a char of a path is a path separator.
*
* The implementation should return the offset of the first character after the last path separator found in the path.
* This is used by the caller to split the path into a directory name and a file name.
* @param path_c: The char to check
*
* @return if a char of a path is a path separator.
*/
bool jerry_port_path_is_separator (jerry_char_t path_c);

/**
* Evalute the length of root component of a path
*
* @param path_p: input zero-terminated path string
* @param path_size: length of the path string
*
* @return offset of the basename component in the input path
* @return length of root component of a path if it's a absolute path
* 0 otherwise
*/
jerry_size_t jerry_port_path_base (const jerry_char_t *path_p);
jerry_size_t jerry_port_path_root (const jerry_char_t *path_p, jerry_size_t path_size);

/**
* Open a source file and read the content into a buffer.
Expand All @@ -252,11 +261,13 @@ jerry_size_t jerry_port_path_base (const jerry_char_t *path_p);
* `jerry_port_source_free`, which can be used to finalize the buffer.
*
* @param file_name_p: Path that points to the source file in the filesystem.
* @param file_name_size length of source file path
* @param out_size_p: The opened file's size in bytes.
*
* @return pointer to the buffer which contains the content of the file.
*/
jerry_char_t *jerry_port_source_read (const char *file_name_p, jerry_size_t *out_size_p);
jerry_char_t *
jerry_port_source_read (const jerry_char_t *file_name_p, jerry_size_t file_name_size, jerry_size_t *out_size_p);

/**
* Free a source file buffer.
Expand Down
8 changes: 4 additions & 4 deletions jerry-ext/include/jerryscript-ext/sources.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@

JERRY_C_API_BEGIN

jerry_value_t jerryx_source_parse_script (const char* path);
jerry_value_t jerryx_source_exec_script (const char* path);
jerry_value_t jerryx_source_exec_module (const char* path);
jerry_value_t jerryx_source_exec_snapshot (const char* path, size_t function_index);
jerry_value_t jerryx_source_parse_script (const jerry_char_t* path_p, jerry_size_t path_size);
jerry_value_t jerryx_source_exec_script (const jerry_char_t* path_p, jerry_size_t path_size);
jerry_value_t jerryx_source_exec_module (const jerry_char_t* path_p, jerry_size_t path_size);
jerry_value_t jerryx_source_exec_snapshot (const jerry_char_t* path_p, jerry_size_t path_size, size_t function_index);
jerry_value_t jerryx_source_exec_stdin (void);

JERRY_C_API_END
Expand Down
3 changes: 2 additions & 1 deletion jerry-ext/util/print.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ jerryx_print_unhandled_exception (jerry_value_t exception) /**< exception value
*path_str_end_p = '\0';

jerry_size_t source_size;
jerry_char_t *source_p = jerry_port_source_read (path_str_p, &source_size);
jerry_size_t path_size = (jerry_size_t) (path_str_end_p - path_str_p);
jerry_char_t *source_p = jerry_port_source_read ((const jerry_char_t *) path_str_p, path_size, &source_size);

/* Revert the error message. */
*path_str_end_p = ':';
Expand Down
20 changes: 9 additions & 11 deletions jerry-ext/util/sources.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
#include "jerryscript-ext/print.h"

jerry_value_t
jerryx_source_parse_script (const char *path_p)
jerryx_source_parse_script (const jerry_char_t *path_p, jerry_size_t path_size)
{
jerry_size_t source_size;
jerry_char_t *source_p = jerry_port_source_read (path_p, &source_size);
jerry_char_t *source_p = jerry_port_source_read (path_p, path_size, &source_size);

if (source_p == NULL)
{
Expand All @@ -46,8 +46,7 @@ jerryx_source_parse_script (const char *path_p)

jerry_parse_options_t parse_options;
parse_options.options = JERRY_PARSE_HAS_SOURCE_NAME;
parse_options.source_name =
jerry_string ((const jerry_char_t *) path_p, (jerry_size_t) strlen (path_p), JERRY_ENCODING_UTF8);
parse_options.source_name = jerry_string (path_p, path_size, JERRY_ENCODING_UTF8);

jerry_value_t result = jerry_parse (source_p, source_size, &parse_options);

Expand All @@ -58,9 +57,9 @@ jerryx_source_parse_script (const char *path_p)
} /* jerryx_source_parse_script */

jerry_value_t
jerryx_source_exec_script (const char *path_p)
jerryx_source_exec_script (const jerry_char_t *path_p, jerry_size_t path_size)
{
jerry_value_t result = jerryx_source_parse_script (path_p);
jerry_value_t result = jerryx_source_parse_script (path_p, path_size);

if (!jerry_value_is_exception (result))
{
Expand All @@ -73,10 +72,9 @@ jerryx_source_exec_script (const char *path_p)
} /* jerryx_source_exec_script */

jerry_value_t
jerryx_source_exec_module (const char *path_p)
jerryx_source_exec_module (const jerry_char_t *path_p, jerry_size_t path_size)
{
jerry_value_t specifier =
jerry_string ((const jerry_char_t *) path_p, (jerry_size_t) strlen (path_p), JERRY_ENCODING_UTF8);
jerry_value_t specifier = jerry_string (path_p, path_size, JERRY_ENCODING_UTF8);
jerry_value_t referrer = jerry_undefined ();

jerry_value_t module = jerry_module_resolve (specifier, referrer, NULL);
Expand Down Expand Up @@ -110,10 +108,10 @@ jerryx_source_exec_module (const char *path_p)
} /* jerryx_source_exec_module */

jerry_value_t
jerryx_source_exec_snapshot (const char *path_p, size_t function_index)
jerryx_source_exec_snapshot (const jerry_char_t *path_p, jerry_size_t path_size, size_t function_index)
{
jerry_size_t source_size;
jerry_char_t *source_p = jerry_port_source_read (path_p, &source_size);
jerry_char_t *source_p = jerry_port_source_read (path_p, path_size, &source_size);

if (source_p == NULL)
{
Expand Down
11 changes: 6 additions & 5 deletions jerry-main/main-desktop.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,19 @@ main (int argc, char **argv)
for (uint32_t source_index = 0; source_index < arguments.source_count; source_index++)
{
main_source_t *source_file_p = sources_p + source_index;
const char *file_path_p = argv[source_file_p->path_index];
const jerry_char_t *file_path_p = (const jerry_char_t *) argv[source_file_p->path_index];
jerry_size_t file_path_size = (jerry_size_t) strlen ((const char *) file_path_p);

switch (source_file_p->type)
{
case SOURCE_MODULE:
{
result = jerryx_source_exec_module (file_path_p);
result = jerryx_source_exec_module (file_path_p, file_path_size);
break;
}
case SOURCE_SNAPSHOT:
{
result = jerryx_source_exec_snapshot (file_path_p, source_file_p->snapshot_index);
result = jerryx_source_exec_snapshot (file_path_p, file_path_size, source_file_p->snapshot_index);
break;
}
default:
Expand All @@ -149,11 +150,11 @@ main (int argc, char **argv)

if ((arguments.option_flags & OPT_FLAG_PARSE_ONLY) != 0)
{
result = jerryx_source_parse_script (file_path_p);
result = jerryx_source_parse_script (file_path_p, file_path_size);
}
else
{
result = jerryx_source_exec_script (file_path_p);
result = jerryx_source_exec_script (file_path_p, file_path_size);
}

break;
Expand Down

0 comments on commit b6c061c

Please sign in to comment.