Add custom configuration to jerry_parse and its variants (#4620)

JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
Zoltan Herczeg
2021-03-10 09:56:48 +01:00
committed by GitHub
parent 7a2665621b
commit 546422161e
45 changed files with 658 additions and 596 deletions
+116 -73
View File
@@ -229,18 +229,21 @@ RegExp object optional flags:
*Changed in version 2.4*: Added `JERRY_REGEXP_FLAG_STICKY`, `JERRY_REGEXP_FLAG_UNICODE` , `JERRY_REGEXP_FLAG_DOTALL` values.
## jerry_parse_opts_t
## jerry_parse_option_enable_feature_t
Option bits for [jerry_parse](#jerry_parse) and
[jerry_parse_function](#jerry_parse_function) functions:
Option bits for [jerry_parse_options_t](#jerry_parse_options_t).
- JERRY_PARSE_NO_OPTS - no options passed
- JERRY_PARSE_STRICT_MODE - enable strict mode
- JERRY_PARSE_MODULE - parse source as an ECMAScript module
- JERRY_PARSE_NO_OPTS - No options passed
- JERRY_PARSE_STRICT_MODE - Enable strict mode
- JERRY_PARSE_MODULE - Parse source as an ECMAScript module
- JERRY_PARSE_HAS_RESOURCE - `resource_name_p` and `resource_name_length` fields are valid
- JERRY_PARSE_HAS_START - `start_line` and `start_column` fields are valid
*New in version 2.0*.
*New in version [[NEXT_RELEASE]]*.
*Changed in version 2.4: Added `JERRY_PARSE_MODULE`.*
**See also**
- [jerry_parse_options_t](#jerry_parse_options_t)
## jerry_gc_mode_t
@@ -271,7 +274,6 @@ Flags for [jerry_generate_snapshot](#jerry_generate_snapshot) and
[jerry_generate_function_snapshot](#jerry_generate_function_snapshot) functions:
- JERRY_SNAPSHOT_SAVE_STATIC - generate static snapshot (see below)
- JERRY_SNAPSHOT_SAVE_STRICT - strict source code provided
**Generate static snapshots**
Snapshots contain literal pools, and these literal pools contain references
@@ -294,6 +296,7 @@ when the snapshot is generated and executed. Furthermore the
`JERRY_SNAPSHOT_EXEC_COPY_DATA` option is not allowed.
*New in version 2.0*.
*Changed in version [[NEXT_RELEASE]]*: The `JERRY_SNAPSHOT_SAVE_STRICT` value is removed, `JERRY_PARSE_STRICT_MODE` should be used instead.
## jerry_exec_snapshot_opts_t
@@ -506,6 +509,38 @@ Enum that contains the flags of property descriptors.
- [jerry_property_descriptor_t](#jerry_property_descriptor_t)
## jerry_parse_options_t
**Summary**
Various configuration options for parsing functions such as [jerry_parse](#jerry_parse)
or [jerry_parse_function](#jerry_parse_function)
**Prototype**
```c
typedef struct
{
uint32_t options; /**< combination of jerry_parse_option_enable_feature_t values */
const jerry_char_t *resource_name_p; /**< resource name (usually a file name)
* if JERRY_PARSE_HAS_RESOURCE is set in options */
size_t resource_name_length; /**< length of resource name
* if JERRY_PARSE_HAS_RESOURCE is set in options */
uint32_t start_line; /**< start line of the source code if JERRY_PARSE_HAS_START is set in options */
uint32_t start_column; /**< start column of the source code if JERRY_PARSE_HAS_START is set in options */
} jerry_parse_options_t;
```
*New in version [[NEXT_RELEASE]]*.
**See also**
- [jerry_parse](#jerry_parse)
- [jerry_parse_function](#jerry_parse_function)
- [jerry_generate_snapshot](#jerry_generate_snapshot)
- [jerry_generate_function_snapshot](#jerry_generate_function_snapshot)
- [jerry_parse_option_enable_feature_t](#jerry_parse_option_enable_feature_t)
## jerry_property_descriptor_t
**Summary**
@@ -1408,23 +1443,20 @@ is no longer needed.
```c
jerry_value_t
jerry_parse (const jerry_char_t *resource_name_p, /**< resource name (usually a file name) */
size_t resource_name_length, /**< length of resource name */
const jerry_char_t *source_p,
jerry_parse (const jerry_char_t *source_p,
size_t source_size,
uint32_t parse_opts);
const jerry_parse_options_t *options_p);
```
- `resource_name_p` - resource name, usually a file name (must be a valid UTF8 string).
- `resource_name_length` - size of the resource name, in bytes.
- `source_p` - string, containing source code to parse (must be a valid UTF8 string).
- `source_size` - size of the string, in bytes.
- `parse_opts` - any combination of [jerry_parse_opts_t](#jerry_parse_opts_t) flags.
- `options_p` - additional parsing options, can be NULL if not used
- return value
- function object value, if script was parsed successfully,
- thrown error, otherwise
*Changed in version 2.0*: Added `resource_name_p`, and `resource_name_length` arguments.
*Changed in version [[NEXT_RELEASE]]*: The `resource_name_p`, `resource_name_length`, and `parse_opts` arguments are replaced by `options_p`.
**Example**
@@ -1439,8 +1471,17 @@ main (void)
jerry_init (JERRY_INIT_EMPTY);
const jerry_char_t script[] = "print ('Hello, World!');";
const jerry_char_t file[] = "hello.js";
jerry_value_t parsed_code = jerry_parse (NULL, 0, script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);
jerry_parse_options_t parse_options;
parse_options.options = JERRY_PARSE_STRICT_MODE | JERRY_PARSE_HAS_RESOURCE | JERRY_PARSE_HAS_START;
parse_options.resource_name_p = file;
parse_options.resource_name_length = sizeof(file) - 1;
/* This example script is extracted from the middle of a file. */
parse_options.start_line = 10;
parse_options.start_column = 1;
jerry_value_t parsed_code = jerry_parse (script, sizeof (script) - 1, &parse_options);
jerry_release_value (parsed_code);
jerry_cleanup ();
@@ -1452,6 +1493,7 @@ main (void)
- [jerry_run](#jerry_run)
- [jerry_parse_function](#jerry_parse_function)
- [jerry_parse_options_t](#jerry_parse_options_t)
## jerry_parse_function
@@ -1471,27 +1513,24 @@ is no longer needed.
```c
jerry_value_t
jerry_parse_function (const jerry_char_t *resource_name_p, /**< resource name (usually a file name) */
size_t resource_name_length, /**< length of resource name */
const jerry_char_t *arg_list_p, /**< script source */
size_t arg_list_size, /**< script source size */
const jerry_char_t *source_p, /**< script source */
size_t source_size, /**< script source size */
uint32_t parse_opts) /**< strict mode */
jerry_parse_function (const jerry_char_t *arg_list_p,
size_t arg_list_size,
const jerry_char_t *source_p,
size_t source_size,
const jerry_parse_options_t *options_p);
```
- `resource_name_p` - resource name, usually a file name (must be a valid UTF8 string).
- `resource_name_length` - size of the resource name, in bytes.
- `arg_list_p` - argument list of the function (must be a valid UTF8 string).
- `arg_list_size` - size of the argument list, in bytes.
- `source_p` - string, containing source code to parse (must be a valid UTF8 string).
- `source_size` - size of the string, in bytes.
- `parse_opts` - any combination of [jerry_parse_opts_t](#jerry_parse_opts_t) flags.
- `options_p` - additional parsing options, can be NULL if not used
- return value
- function object value, if script was parsed successfully,
- thrown error, otherwise
*New in version 2.0*.
*Changed in version [[NEXT_RELEASE]]*: The `resource_name_p`, `resource_name_length`, and `parse_opts` arguments are replaced by `options_p`.
**Example**
@@ -1514,13 +1553,11 @@ main (void)
const char function_args[] = "a, b";
const char function_source[] = "return a + b";
jerry_value_t parsed_function = jerry_parse_function (NULL,
0,
(const jerry_char_t *) function_args,
jerry_value_t parsed_function = jerry_parse_function ((const jerry_char_t *) function_args,
strlen (function_args),
(const jerry_char_t *) function_source,
strlen (function_source),
JERRY_PARSE_NO_OPTS);
NULL);
if (!jerry_value_is_error (parsed_function))
{
@@ -1565,6 +1602,7 @@ main (void)
**See also**
- [jerry_call_function](#jerry_call_function)
- [jerry_parse_options_t](#jerry_parse_options_t)
## jerry_run
@@ -1606,7 +1644,7 @@ main (void)
jerry_init (JERRY_INIT_EMPTY);
/* Setup Global scope code */
jerry_value_t parsed_code = jerry_parse (NULL, 0, script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);
jerry_value_t parsed_code = jerry_parse (script, sizeof (script) - 1, NULL);
if (!jerry_value_is_error (parsed_code))
{
@@ -1650,7 +1688,8 @@ jerry_eval (const jerry_char_t *source_p,
- `source_p` - source code to evaluate, it must be a valid utf8 string.
- `source_size` - length of the source code
- `parse_opts` - any combination of [jerry_parse_opts_t](#jerry_parse_opts_t) flags.
- `parse_opts` - combination of [jerry_parse_option_enable_feature_t](#jerry_parse_option_enable_feature_t) flags.
The following flags are allowed: JERRY_PARSE_STRICT_MODE
- return value - result of eval, may be an error value.
**Example**
@@ -1705,7 +1744,7 @@ main (void)
const jerry_char_t script[] = "new Promise(function(f,r) { f('Hello, World!'); }).then(function(x) { print(x); });";
jerry_value_t parsed_code = jerry_parse (NULL, 0, script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);
jerry_value_t parsed_code = jerry_parse (script, sizeof (script) - 1, NULL);
jerry_value_t script_value = jerry_run (parsed_code);
jerry_value_t job_value;
@@ -8878,19 +8917,17 @@ Generate snapshot from the specified source code.
```c
jerry_value_t
jerry_generate_snapshot (const jerry_char_t *resource_name_p,
size_t resource_name_length,
const jerry_char_t *source_p,
jerry_generate_snapshot (const jerry_char_t *source_p,
size_t source_size,
const jerry_parse_options_t *options_p,
uint32_t generate_snapshot_opts,
uint32_t *buffer_p,
size_t buffer_size);
```
- `resource_name_p` - resource (file) name of the source code. Currently unused, the debugger may use it in the future.
- `resource_name_length` - length of resource name.
- `source_p` - script source, it must be a valid utf8 string.
- `source_size` - script source size, in bytes.
- `options_p` - additional parsing options, can be NULL if not used
- `generate_snapshot_opts` - any combination of [jerry_generate_snapshot_opts_t](#jerry_generate_snapshot_opts_t) flags.
- `buffer_p` - output buffer (aligned to 4 bytes) to save snapshot to.
- `buffer_size` - the output buffer's size in bytes.
@@ -8901,6 +8938,7 @@ jerry_generate_snapshot (const jerry_char_t *resource_name_p,
- thrown error, otherwise.
*New in version 2.0*.
*Changed in version [[NEXT_RELEASE]]*: The `resource_name_p`, and `resource_name_length` arguments are replaced by `options_p`.
**Example**
@@ -8918,10 +8956,9 @@ main (void)
const jerry_char_t script_to_snapshot[] = "(function () { return 'string from snapshot'; }) ();";
jerry_value_t generate_result;
generate_result = jerry_generate_snapshot (NULL,
0,
script_to_snapshot,
generate_result = jerry_generate_snapshot (script_to_snapshot,
sizeof (script_to_snapshot) - 1,
NULL,
0,
global_mode_snapshot_buffer,
sizeof (global_mode_snapshot_buffer) / sizeof (uint32_t));
@@ -8944,6 +8981,7 @@ main (void)
- [jerry_cleanup](#jerry_cleanup)
- [jerry_generate_function_snapshot](#jerry_generate_function_snapshot)
- [jerry_exec_snapshot](#jerry_exec_snapshot)
- [jerry_parse_options_t](#jerry_parse_options_t)
## jerry_generate_function_snapshot
@@ -8967,23 +9005,21 @@ passed as separated arguments.
```c
jerry_value_t
jerry_generate_function_snapshot (const jerry_char_t *resource_name_p,
size_t resource_name_length,
const jerry_char_t *source_p,
jerry_generate_function_snapshot (const jerry_char_t *source_p,
size_t source_size,
const jerry_char_t *args_p,
size_t args_size,
const jerry_parse_options_t *options_p,
uint32_t generate_snapshot_opts,
uint32_t *buffer_p,
size_t buffer_size)
```
- `resource_name_p` - resource (file) name of the source code. Currently unused, the debugger may use it in the future.
- `resource_name_length` - length of resource name.
- `source_p` - script source, it must be a valid utf8 string.
- `source_size` - script source size, in bytes.
- `args_p` - function arguments, it must be a valid utf8 string.
- `args_size` - function argument size, in bytes.
- `options_p` - additional parsing options, can be NULL if not used
- `generate_snapshot_opts` - any combination of [jerry_generate_snapshot_opts_t](#jerry_generate_snapshot_opts_t) flags.
- `buffer_p` - buffer (aligned to 4 bytes) to save snapshot to.
- `buffer_size` - the buffer's size in bytes.
@@ -8994,6 +9030,7 @@ jerry_generate_function_snapshot (const jerry_char_t *resource_name_p,
- thrown error, otherwise.
*New in version 2.0*.
*Changed in version [[NEXT_RELEASE]]*: The `resource_name_p`, and `resource_name_length` arguments are replaced by `options_p`.
**Example**
@@ -9012,12 +9049,11 @@ main (void)
const jerry_char_t src[] = "return a + b;";
jerry_value_t generate_result;
generate_result = jerry_generate_function_snapshot (NULL,
0,
src,
generate_result = jerry_generate_function_snapshot (src,
sizeof (src) - 1,
args,
sizeof (args) - 1,
NULL,
0,
func_snapshot_buffer,
sizeof (func_snapshot_buffer) / sizeof (uint32_t));
@@ -9040,6 +9076,7 @@ main (void)
- [jerry_cleanup](#jerry_cleanup)
- [jerry_generate_snapshot](#jerry_generate_snapshot)
- [jerry_load_function_snapshot_at](#jerry_load_function_snapshot_at)
- [jerry_parse_options_t](#jerry_parse_options_t)
## jerry_exec_snapshot
@@ -9091,10 +9128,9 @@ main (void)
jerry_init (JERRY_INIT_EMPTY);
jerry_value_t generate_result;
generate_result = jerry_generate_snapshot (NULL,
0,
script_to_snapshot,
generate_result = jerry_generate_snapshot (script_to_snapshot,
sizeof (script_to_snapshot) - 1,
NULL,
0,
global_mode_snapshot_buffer,
sizeof (global_mode_snapshot_buffer) / sizeof (uint32_t));
@@ -9180,13 +9216,12 @@ main (void)
jerry_init (JERRY_INIT_EMPTY);
jerry_value_t generate_result;
generate_result = jerry_generate_function_snapshot (NULL,
0,
func_src,
generate_result = jerry_generate_function_snapshot (func_src,
sizeof (func_src) - 1,
func_args,
sizeof (func_args) - 1,
false,
NULL,
0,
snapshot_buffer,
sizeof (snapshot_buffer) / sizeof (uint32_t));
@@ -9281,10 +9316,9 @@ main (void)
static uint32_t snapshot_buffer[256];
const jerry_char_t script_for_literal_save[] = "var obj = { a:'aa', bb:'Bb' }";
jerry_value_t generate_result = jerry_generate_snapshot (NULL,
0,
script_for_literal_save,
jerry_value_t generate_result = jerry_generate_snapshot (script_for_literal_save,
sizeof (script_for_literal_save) - 1,
NULL,
0,
snapshot_buffer,
256);
@@ -9418,11 +9452,14 @@ main (void)
"f ();\n");
const char *resource = "demo_memoryjs";
jerry_value_t program = jerry_parse ((const jerry_char_t *) resource,
strlen (resource),
(const jerry_char_t *) source,
jerry_parse_options_t parse_options;
parse_options.options = JERRY_PARSE_HAS_RESOURCE;
parse_options.resource_name_p = (jerry_char_t *) resource;
parse_options.resource_name_length = (size_t) strlen (resource);
jerry_value_t program = jerry_parse ((const jerry_char_t *) source,
strlen (source),
JERRY_PARSE_NO_OPTS);
&parse_options);
if (!jerry_value_is_error (program))
{
jerry_value_t run_result = jerry_run (program);
@@ -9521,11 +9558,14 @@ main (void)
"f ();\n");
const char *resource = "demo_backtrace.js";
jerry_value_t program = jerry_parse ((const jerry_char_t *) resource,
strlen (resource),
(const jerry_char_t *) source,
jerry_parse_options_t parse_options;
parse_options.options = JERRY_PARSE_HAS_RESOURCE;
parse_options.resource_name_p = (jerry_char_t *) resource;
parse_options.resource_name_length = (size_t) strlen (resource);
jerry_value_t program = jerry_parse ((const jerry_char_t *) source,
strlen (source),
JERRY_PARSE_NO_OPTS);
&parse_options);
if (!jerry_value_is_error (program))
{
jerry_value_t run_result = jerry_run (program);
@@ -9843,7 +9883,7 @@ main (void)
// Infinite loop.
const jerry_char_t script[] = "while(true) {}";
jerry_value_t parsed_code = jerry_parse (NULL, 0, script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);
jerry_value_t parsed_code = jerry_parse (script, sizeof (script) - 1, NULL);
jerry_release_value (jerry_run (parsed_code));
jerry_release_value (parsed_code);
jerry_cleanup ();
@@ -9930,11 +9970,14 @@ main (void)
const jerry_char_t source[] = "function myFunction() { return resourceName() }; myFunction()";
const jerry_char_t resource[] = "demo.js";
jerry_value_t program = jerry_parse (resource,
sizeof (resource) - 1,
source,
jerry_parse_options_t parse_options;
parse_options.options = JERRY_PARSE_HAS_RESOURCE;
parse_options.resource_name_p = resource;
parse_options.resource_name_length = sizeof (resource) - 1;
jerry_value_t program = jerry_parse (source,
sizeof (source) - 1,
JERRY_PARSE_NO_OPTS);
&parse_options);
if (!jerry_value_is_error (program))
{
+4 -4
View File
@@ -153,7 +153,7 @@ main (void)
jerry_init (JERRY_INIT_EMPTY);
/* Setup Global scope code */
jerry_value_t parsed_code = jerry_parse (NULL, 0, script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);
jerry_value_t parsed_code = jerry_parse (script, sizeof (script) - 1, NULL);
/* Check if there is any JS code parse error */
if (!jerry_value_is_error (parsed_code))
@@ -259,7 +259,7 @@ main (void)
}
/* Setup Global scope code */
jerry_value_t parsed_code = jerry_parse (NULL, 0, script, script_size, JERRY_PARSE_NO_OPTS);
jerry_value_t parsed_code = jerry_parse (script, script_size, NULL);
if (!jerry_value_is_error (parsed_code))
{
@@ -378,7 +378,7 @@ main (void)
}
/* Setup Global scope code */
jerry_value_t parsed_code = jerry_parse (NULL, 0, script, script_size, JERRY_PARSE_NO_OPTS);
jerry_value_t parsed_code = jerry_parse (script, script_size, NULL);
if (!jerry_value_is_error (parsed_code))
{
@@ -444,7 +444,7 @@ main (void)
jerryx_handler_print);
/* Setup Global scope code */
jerry_value_t parsed_code = jerry_parse (NULL, 0, script, script_size, JERRY_PARSE_NO_OPTS);
jerry_value_t parsed_code = jerry_parse (script, script_size, NULL);
if (!jerry_value_is_error (parsed_code))
{
+7 -4
View File
@@ -314,11 +314,14 @@ wait_for_source_callback (const jerry_char_t *resource_name_p, /**< resource nam
{
(void) user_p;
jerry_value_t ret_val = jerry_parse (resource_name_p,
resource_name_size,
source_p,
jerry_parse_options_t parse_options;
parse_options.options = JERRY_PARSE_HAS_RESOURCE;
parse_options.resource_name_p = resource_name_p;
parse_options.resource_name_length = resource_name_size;
jerry_value_t ret_val = jerry_parse (source_p,
source_size,
JERRY_PARSE_NO_OPTS);
&parse_options);
if (!jerry_value_is_error (ret_val))
{