Replace jerry_load_function_snapshot function with a flag (#4719)

JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
Zoltan Herczeg
2021-07-16 16:09:51 +02:00
committed by GitHub
parent 252d68936f
commit 9ff25dbc12
4 changed files with 89 additions and 174 deletions
+74 -111
View File
@@ -305,11 +305,14 @@ when the snapshot is generated and executed. Furthermore the
## jerry_exec_snapshot_opts_t
Flags for [jerry_exec_snapshot](#jerry_exec_snapshot) and
[jerry_load_function_snapshot](#jerry_load_function_snapshot) functions:
Flags for [jerry_exec_snapshot](#jerry_exec_snapshot) functions:
- JERRY_SNAPSHOT_EXEC_COPY_DATA - copy snapshot data into memory (see below)
- JERRY_SNAPSHOT_EXEC_ALLOW_STATIC - allow executing static snapshots
- JERRY_SNAPSHOT_EXEC_LOAD_AS_FUNCTION - load snapshot as function instead of executing it
*Changed in version [[NEXT_RELEASE]]*: The `JERRY_SNAPSHOT_EXEC_LOAD_AS_FUNCTION` value is added,
which replaces the `jerry_load_function_snapshot` function.
**Copy snapshot data into memory**
@@ -10195,7 +10198,6 @@ main (void)
- [jerry_init](#jerry_init)
- [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)
@@ -10203,7 +10205,7 @@ main (void)
**Summary**
Execute snapshot from the specified buffer.
Execute/load snapshot from the specified buffer.
*Notes*:
- Returned value must be freed with [jerry_release_value](#jerry_release_value) when it
@@ -10232,94 +10234,7 @@ jerry_exec_snapshot (const uint32_t *snapshot_p,
*Changed in version 2.0*: Added `func_index` and `exec_snapshot_opts` arguments. Removed the `copy_bytecode` last argument.
**Example**
[doctest]: # ()
```c
#include "jerryscript.h"
int
main (void)
{
static uint32_t global_mode_snapshot_buffer[256];
const jerry_char_t script_to_snapshot[] = "(function () { return 'string from snapshot'; }) ();";
jerry_init (JERRY_INIT_EMPTY);
jerry_value_t generate_result;
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));
// generate_result should be checked if it is an error or not
size_t global_mode_snapshot_size = (size_t) jerry_get_number_value (generate_result);
jerry_release_value (generate_result);
jerry_cleanup ();
jerry_init (JERRY_INIT_EMPTY);
jerry_value_t res = jerry_exec_snapshot (global_mode_snapshot_buffer,
global_mode_snapshot_size,
0,
0);
// check the `res` value for error and process the result.
jerry_release_value (res);
jerry_cleanup ();
return 0;
}
```
**See also**
- [jerry_init](#jerry_init)
- [jerry_cleanup](#jerry_cleanup)
- [jerry_parse_and_save_snapshot](#jerry_parse_and_save_snapshot)
## jerry_load_function_snapshot
**Summary**
Load the selected snapshot function from the specified buffer as a function object.
The lexical environment of the loaded function is always the global lexical environment.
*Notes*:
- Returned value must be freed with [jerry_release_value](#jerry_release_value) when it
is no longer needed.
- This API depends on a build option (`JERRY_SNAPSHOT_EXEC`) and can be checked in runtime with
the `JERRY_FEATURE_SNAPSHOT_EXEC` feature enum value, see [jerry_is_feature_enabled](#jerry_is_feature_enabled).
If the feature is not enabled the function will return an error.
**Prototype**
```c
jerry_value_t
jerry_load_function_snapshot (const uint32_t *snapshot_p,
size_t snapshot_size,
size_t func_index,
uint32_t exec_snapshot_opts);
```
- `snapshot_p` - pointer to snapshot.
- `snapshot_size` - size of snapshot in bytes.
- `func_index` - index of function to load from the snapshot.
- `exec_snapshot_opts` - any combination of [jerry_exec_snapshot_opts_t](#jerry_exec_snapshot_opts_t) flags.
- return value
- function object built from the snapshot.
- thrown error, otherwise.
*New in version 2.0*.
**Example**
**Example 1**
[doctest]: # ()
@@ -10330,33 +10245,80 @@ int
main (void)
{
static uint32_t snapshot_buffer[256];
const jerry_char_t func_args[] = "a, b";
const jerry_char_t func_src[] = "return a + b;";
/* 1st example: global mode snapshot. */
jerry_init (JERRY_INIT_EMPTY);
const jerry_char_t script_to_snapshot[] = "(function () { return 'string from snapshot'; }) ();";
jerry_value_t generate_result;
generate_result = jerry_generate_function_snapshot (func_src,
sizeof (func_src) - 1,
func_args,
sizeof (func_args) - 1,
NULL,
0,
snapshot_buffer,
sizeof (snapshot_buffer) / sizeof (uint32_t));
generate_result = jerry_generate_snapshot (script_to_snapshot,
sizeof (script_to_snapshot) - 1,
NULL,
0,
snapshot_buffer,
sizeof (snapshot_buffer) / sizeof (uint32_t));
/* 'generate_result' variable should be checked whether it contains an error. */
size_t snapshot_size = (size_t) jerry_get_number_value (generate_result);
jerry_release_value (generate_result);
jerry_cleanup ();
jerry_init (JERRY_INIT_EMPTY);
jerry_value_t func = jerry_load_function_snapshot (snapshot_buffer,
snapshot_size,
0,
0);
/* 'func' can be used now as a function object */
jerry_value_t res = jerry_exec_snapshot (snapshot_buffer,
snapshot_size,
0,
0);
/* 'res' now contains 'string from snapshot' */
jerry_release_value (res);
jerry_cleanup ();
return 0;
}
```
**Example 2**
[doctest]: # ()
```c
#include "jerryscript.h"
int
main (void)
{
static uint32_t snapshot_buffer[256];
/* 2nd example: function snapshot. */
jerry_init (JERRY_INIT_EMPTY);
const jerry_char_t function_to_snapshot_args[] = "a, b";
const jerry_char_t function_to_snapshot[] = "return a + b;";
jerry_value_t generate_result;
generate_result = jerry_generate_function_snapshot (function_to_snapshot,
sizeof (function_to_snapshot) - 1,
function_to_snapshot_args,
sizeof (function_to_snapshot_args) - 1,
NULL,
0,
snapshot_buffer,
sizeof (snapshot_buffer) / sizeof (uint32_t));
/* 'generate_result' variable should be checked whether it contains an error. */
size_t snapshot_size = (size_t) jerry_get_number_value (generate_result);
jerry_release_value (generate_result);
jerry_cleanup ();
jerry_init (JERRY_INIT_EMPTY);
jerry_value_t func = jerry_exec_snapshot (snapshot_buffer,
snapshot_size,
0,
JERRY_SNAPSHOT_EXEC_LOAD_AS_FUNCTION);
/* 'func' can be used now as a function object. */
jerry_value_t this_value = jerry_create_undefined ();
jerry_value_t args[2];
@@ -10365,8 +10327,8 @@ main (void)
jerry_value_t res = jerry_call_function (func, this_value, args, 2);
/* 'res' now contains the value 3 as a jerry_value_t */
/* 'res' now contains the value 3 as a jerry_value_t. */
jerry_release_value (res);
jerry_release_value (args[0]);
jerry_release_value (args[1]);
jerry_release_value (this_value);
@@ -10381,7 +10343,8 @@ main (void)
- [jerry_init](#jerry_init)
- [jerry_cleanup](#jerry_cleanup)
- [jerry_parse_and_save_function_snapshot](#jerry_parse_and_save_function_snapshot)
- [jerry_generate_snapshot](#jerry_generate_snapshot)
- [jerry_generate_function_snapshot](#jerry_generate_function_snapshot)
## jerry_get_literals_from_snapshot