Skip to content

Commit a25ac0c

Browse files
committed
Reduce or even remove the usage of strlen in jerry-core and jerry-ext
Improving the api of jerry-port, so that we can reduce the usage of strlen. JerryScript-DCO-1.0-Signed-off-by: Yonggang Luo [email protected]
1 parent 43f5026 commit a25ac0c

File tree

11 files changed

+271
-118
lines changed

11 files changed

+271
-118
lines changed

docs/05.PORT-API.md

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,12 @@ void jerry_port_line_free (jerry_char_t *buffer_p);
193193
*
194194
* @param path_p: zero-terminated string containing the input path
195195
* @param path_size: size of the input path string in bytes, excluding terminating zero
196+
* @param out_size_p: The normalized path's size in bytes.
196197
*
197198
* @return buffer with the normalized path if the operation is successful,
198199
* NULL otherwise
199200
*/
200-
jerry_char_t *jerry_port_path_normalize (const jerry_char_t *path_p, jerry_size_t path_size);
201+
jerry_char_t *jerry_port_path_normalize (const jerry_char_t *path_p, jerry_size_t path_size, jerry_size_t *out_size_p);
201202
```
202203
203204
```c
@@ -211,31 +212,43 @@ void jerry_port_path_free (jerry_char_t *path_p);
211212

212213
```c
213214
/**
214-
* Get the offset of the basename component in the input path.
215+
* Check if a char of a path is a path separator.
215216
*
216-
* The implementation should return the offset of the first character after the last path separator found in the path.
217-
* This is used by the caller to split the path into a directory name and a file name.
217+
* @param path_c: The char to check
218+
*
219+
* @return if a char of a path is a path separator.
220+
*/
221+
bool jerry_port_path_is_separator (jerry_char_t path_c);
222+
```
223+
224+
```c
225+
/**
226+
* Evalute the length of root component of a path
218227
*
219228
* @param path_p: input zero-terminated path string
229+
* @param path_size: length of the path string
220230
*
221-
* @return offset of the basename component in the input path
231+
* @return length of root component of a path if it's a absolute path
232+
* 0 otherwise
222233
*/
223-
jerry_size_t jerry_port_path_base (const jerry_char_t *path_p);
234+
jerry_size_t jerry_port_path_root (const jerry_char_t *path_p, jerry_size_t path_size);
224235
```
225236

226237
```c
227238
/**
228-
* Open a source file and read its contents into a buffer.
239+
* Open a source file and read the content into a buffer.
229240
*
230241
* When the source file is no longer needed by the caller, the returned pointer will be passed to
231242
* `jerry_port_source_free`, which can be used to finalize the buffer.
232243
*
233244
* @param file_name_p: Path that points to the source file in the filesystem.
245+
* @param file_name_size length of source file path
234246
* @param out_size_p: The opened file's size in bytes.
235247
*
236248
* @return pointer to the buffer which contains the content of the file.
237249
*/
238-
jerry_char_t *jerry_port_source_read (const char *file_name_p, jerry_size_t *out_size_p);
250+
jerry_char_t *
251+
jerry_port_source_read (const jerry_char_t *file_name_p, jerry_size_t file_name_size, jerry_size_t *out_size_p);
239252
```
240253
241254
```c

jerry-core/api/jerry-module.c

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,35 @@ static const jerry_context_data_manager_t jerry_module_manager JERRY_ATTR_CONST_
123123
.bytes_needed = sizeof (jerry_module_manager_t)
124124
};
125125

126+
/**
127+
* Get the offset of the basename component in the input path.
128+
*
129+
* The implementation should return the offset of the first character after the last path separator found in the path.
130+
* This is used by the caller to split the path into a directory name and a file name.
131+
*
132+
* @param path_p: input zero-terminated path string
133+
* @param path_size: size of the input path string in bytes, excluding terminating zero
134+
*
135+
* @return offset of the basename component in the input path
136+
*/
137+
static jerry_size_t
138+
jerry_module_path_base (const jerry_char_t *path_p, jerry_size_t path_size)
139+
{
140+
const jerry_char_t *end_p = path_p + path_size;
141+
142+
while (end_p > path_p)
143+
{
144+
if (jerry_port_path_is_separator (end_p[-1]))
145+
{
146+
return (jerry_size_t) (end_p - path_p);
147+
}
148+
149+
end_p--;
150+
}
151+
152+
return 0;
153+
} /* jerry_module_path_base */
154+
126155
#endif /* JERRY_MODULE_SYSTEM */
127156

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

160-
jerry_char_t *path_p = jerry_port_path_normalize (reference_path_p, reference_size);
189+
jerry_char_t *specifier_path_p = reference_path_p + directory_size;
190+
191+
jerry_size_t path_size;
192+
jerry_char_t *path_p;
193+
if (jerry_port_path_root (specifier_path_p, specifier_size) > 0)
194+
{
195+
path_p = jerry_port_path_normalize (specifier_path_p, specifier_size, &path_size);
196+
}
197+
else
198+
{
199+
path_p = jerry_port_path_normalize (reference_path_p, reference_size, &path_size);
200+
}
201+
161202
jerry_heap_free (reference_path_p, reference_size + 1);
162203

163204
if (path_p == NULL)
@@ -185,7 +226,7 @@ jerry_module_resolve (const jerry_value_t specifier, /**< module specifier strin
185226
}
186227

187228
jerry_size_t source_size;
188-
jerry_char_t *source_p = jerry_port_source_read ((const char *) path_p, &source_size);
229+
jerry_char_t *source_p = jerry_port_source_read (path_p, path_size, &source_size);
189230

190231
if (source_p == NULL)
191232
{
@@ -215,7 +256,7 @@ jerry_module_resolve (const jerry_value_t specifier, /**< module specifier strin
215256

216257
module_p->next_p = manager_p->module_head_p;
217258
module_p->path_p = path_p;
218-
module_p->basename_offset = jerry_port_path_base (module_p->path_p);
259+
module_p->basename_offset = jerry_module_path_base (module_p->path_p, path_size);
219260
module_p->realm = realm;
220261
module_p->module = jerry_value_copy (ret_value);
221262

jerry-core/include/jerryscript-port.h

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,12 @@ void jerry_port_line_free (jerry_char_t *buffer_p);
217217
*
218218
* @param path_p: zero-terminated string containing the input path
219219
* @param path_size: size of the input path string in bytes, excluding terminating zero
220+
* @param out_size_p: The normalized path's size in bytes.
220221
*
221222
* @return buffer with the normalized path if the operation is successful,
222223
* NULL otherwise
223224
*/
224-
jerry_char_t *jerry_port_path_normalize (const jerry_char_t *path_p, jerry_size_t path_size);
225+
jerry_char_t *jerry_port_path_normalize (const jerry_char_t *path_p, jerry_size_t path_size, jerry_size_t *out_size_p);
225226

226227
/**
227228
* Free a path buffer returned by jerry_port_path_normalize.
@@ -231,16 +232,24 @@ jerry_char_t *jerry_port_path_normalize (const jerry_char_t *path_p, jerry_size_
231232
void jerry_port_path_free (jerry_char_t *path_p);
232233

233234
/**
234-
* Get the offset of the basename component in the input path.
235+
* Check if a char of a path is a path separator.
235236
*
236-
* The implementation should return the offset of the first character after the last path separator found in the path.
237-
* This is used by the caller to split the path into a directory name and a file name.
237+
* @param path_c: The char to check
238+
*
239+
* @return if a char of a path is a path separator.
240+
*/
241+
bool jerry_port_path_is_separator (jerry_char_t path_c);
242+
243+
/**
244+
* Evalute the length of root component of a path
238245
*
239246
* @param path_p: input zero-terminated path string
247+
* @param path_size: length of the path string
240248
*
241-
* @return offset of the basename component in the input path
249+
* @return length of root component of a path if it's a absolute path
250+
* 0 otherwise
242251
*/
243-
jerry_size_t jerry_port_path_base (const jerry_char_t *path_p);
252+
jerry_size_t jerry_port_path_root (const jerry_char_t *path_p, jerry_size_t path_size);
244253

245254
/**
246255
* Open a source file and read the content into a buffer.
@@ -249,11 +258,13 @@ jerry_size_t jerry_port_path_base (const jerry_char_t *path_p);
249258
* `jerry_port_source_free`, which can be used to finalize the buffer.
250259
*
251260
* @param file_name_p: Path that points to the source file in the filesystem.
261+
* @param file_name_size length of source file path
252262
* @param out_size_p: The opened file's size in bytes.
253263
*
254264
* @return pointer to the buffer which contains the content of the file.
255265
*/
256-
jerry_char_t *jerry_port_source_read (const char *file_name_p, jerry_size_t *out_size_p);
266+
jerry_char_t *
267+
jerry_port_source_read (const jerry_char_t *file_name_p, jerry_size_t file_name_size, jerry_size_t *out_size_p);
257268

258269
/**
259270
* Free a source file buffer.

jerry-ext/include/jerryscript-ext/sources.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020

2121
JERRY_C_API_BEGIN
2222

23-
jerry_value_t jerryx_source_parse_script (const char* path);
24-
jerry_value_t jerryx_source_exec_script (const char* path);
25-
jerry_value_t jerryx_source_exec_module (const char* path);
26-
jerry_value_t jerryx_source_exec_snapshot (const char* path, size_t function_index);
23+
jerry_value_t jerryx_source_parse_script (const jerry_char_t* path_p, jerry_size_t path_size);
24+
jerry_value_t jerryx_source_exec_script (const jerry_char_t* path_p, jerry_size_t path_size);
25+
jerry_value_t jerryx_source_exec_module (const jerry_char_t* path_p, jerry_size_t path_size);
26+
jerry_value_t jerryx_source_exec_snapshot (const jerry_char_t* path_p, jerry_size_t path_size, size_t function_index);
2727
jerry_value_t jerryx_source_exec_stdin (void);
2828

2929
JERRY_C_API_END

jerry-ext/util/print.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,8 @@ jerryx_print_unhandled_exception (jerry_value_t exception) /**< exception value
263263
*path_str_end_p = '\0';
264264

265265
jerry_size_t source_size;
266-
jerry_char_t *source_p = jerry_port_source_read (path_str_p, &source_size);
266+
jerry_size_t path_size = (jerry_size_t) (path_str_end_p - path_str_p);
267+
jerry_char_t *source_p = jerry_port_source_read ((const jerry_char_t *) path_str_p, path_size, &source_size);
267268

268269
/* Revert the error message. */
269270
*path_str_end_p = ':';

jerry-ext/util/sources.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
#include "jerryscript-ext/print.h"
2828

2929
jerry_value_t
30-
jerryx_source_parse_script (const char *path_p)
30+
jerryx_source_parse_script (const jerry_char_t *path_p, jerry_size_t path_size)
3131
{
3232
jerry_size_t source_size;
33-
jerry_char_t *source_p = jerry_port_source_read (path_p, &source_size);
33+
jerry_char_t *source_p = jerry_port_source_read (path_p, path_size, &source_size);
3434

3535
if (source_p == NULL)
3636
{
@@ -46,8 +46,7 @@ jerryx_source_parse_script (const char *path_p)
4646

4747
jerry_parse_options_t parse_options;
4848
parse_options.options = JERRY_PARSE_HAS_SOURCE_NAME;
49-
parse_options.source_name =
50-
jerry_string ((const jerry_char_t *) path_p, (jerry_size_t) strlen (path_p), JERRY_ENCODING_UTF8);
49+
parse_options.source_name = jerry_string (path_p, path_size, JERRY_ENCODING_UTF8);
5150

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

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

6059
jerry_value_t
61-
jerryx_source_exec_script (const char *path_p)
60+
jerryx_source_exec_script (const jerry_char_t *path_p, jerry_size_t path_size)
6261
{
63-
jerry_value_t result = jerryx_source_parse_script (path_p);
62+
jerry_value_t result = jerryx_source_parse_script (path_p, path_size);
6463

6564
if (!jerry_value_is_exception (result))
6665
{
@@ -73,10 +72,9 @@ jerryx_source_exec_script (const char *path_p)
7372
} /* jerryx_source_exec_script */
7473

7574
jerry_value_t
76-
jerryx_source_exec_module (const char *path_p)
75+
jerryx_source_exec_module (const jerry_char_t *path_p, jerry_size_t path_size)
7776
{
78-
jerry_value_t specifier =
79-
jerry_string ((const jerry_char_t *) path_p, (jerry_size_t) strlen (path_p), JERRY_ENCODING_UTF8);
77+
jerry_value_t specifier = jerry_string (path_p, path_size, JERRY_ENCODING_UTF8);
8078
jerry_value_t referrer = jerry_undefined ();
8179

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

112110
jerry_value_t
113-
jerryx_source_exec_snapshot (const char *path_p, size_t function_index)
111+
jerryx_source_exec_snapshot (const jerry_char_t *path_p, jerry_size_t path_size, size_t function_index)
114112
{
115113
jerry_size_t source_size;
116-
jerry_char_t *source_p = jerry_port_source_read (path_p, &source_size);
114+
jerry_char_t *source_p = jerry_port_source_read (path_p, path_size, &source_size);
117115

118116
if (source_p == NULL)
119117
{

jerry-main/main-desktop.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,18 +129,19 @@ main (int argc, char **argv)
129129
for (uint32_t source_index = 0; source_index < arguments.source_count; source_index++)
130130
{
131131
main_source_t *source_file_p = sources_p + source_index;
132-
const char *file_path_p = argv[source_file_p->path_index];
132+
const jerry_char_t *file_path_p = (const jerry_char_t *) argv[source_file_p->path_index];
133+
jerry_size_t file_path_size = (jerry_size_t) strlen ((const char *) file_path_p);
133134

134135
switch (source_file_p->type)
135136
{
136137
case SOURCE_MODULE:
137138
{
138-
result = jerryx_source_exec_module (file_path_p);
139+
result = jerryx_source_exec_module (file_path_p, file_path_size);
139140
break;
140141
}
141142
case SOURCE_SNAPSHOT:
142143
{
143-
result = jerryx_source_exec_snapshot (file_path_p, source_file_p->snapshot_index);
144+
result = jerryx_source_exec_snapshot (file_path_p, file_path_size, source_file_p->snapshot_index);
144145
break;
145146
}
146147
default:
@@ -149,11 +150,11 @@ main (int argc, char **argv)
149150

150151
if ((arguments.option_flags & OPT_FLAG_PARSE_ONLY) != 0)
151152
{
152-
result = jerryx_source_parse_script (file_path_p);
153+
result = jerryx_source_parse_script (file_path_p, file_path_size);
153154
}
154155
else
155156
{
156-
result = jerryx_source_exec_script (file_path_p);
157+
result = jerryx_source_exec_script (file_path_p, file_path_size);
157158
}
158159

159160
break;

0 commit comments

Comments
 (0)