Skip to content

Commit 6beff89

Browse files
committed
Support multiple primary functions in a single snapshot.
This patch adds an extension to snapshots which allows storing multiple position independent primary functions in a single snapshot data. A new application called jerry-snapshot is added to the project to manage snapshots. Currently the only option is merging snapshots. JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg [email protected]
1 parent 9f0cf9e commit 6beff89

17 files changed

+1243
-425
lines changed

CMakeLists.txt

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,15 @@ if(NOT CMAKE_BUILD_TYPE)
3838
endif()
3939

4040
# Optional components
41-
set(JERRY_CMDLINE ON CACHE BOOL "Build jerry command line tool?")
42-
set(JERRY_CMDLINE_MINIMAL OFF CACHE BOOL "Build jerry minimal command line tool?")
43-
set(JERRY_PORT_DEFAULT ON CACHE BOOL "Build default jerry port implementation?")
44-
set(JERRY_EXT ON CACHE BOOL "Build jerry-ext?")
45-
set(JERRY_LIBC ON CACHE BOOL "Build and use jerry-libc?")
46-
set(JERRY_LIBM ON CACHE BOOL "Build and use jerry-libm?")
47-
set(UNITTESTS OFF CACHE BOOL "Build unit tests?")
48-
set(DOCTESTS OFF CACHE BOOL "Build doc tests?")
41+
set(JERRY_CMDLINE ON CACHE BOOL "Build jerry command line tool?")
42+
set(JERRY_CMDLINE_MINIMAL OFF CACHE BOOL "Build jerry minimal command line tool?")
43+
set(JERRY_CMDLINE_SNAPSHOT OFF CACHE BOOL "Build jerry snapshot command line tool?")
44+
set(JERRY_PORT_DEFAULT ON CACHE BOOL "Build default jerry port implementation?")
45+
set(JERRY_EXT ON CACHE BOOL "Build jerry-ext?")
46+
set(JERRY_LIBC ON CACHE BOOL "Build and use jerry-libc?")
47+
set(JERRY_LIBM ON CACHE BOOL "Build and use jerry-libm?")
48+
set(UNITTESTS OFF CACHE BOOL "Build unit tests?")
49+
set(DOCTESTS OFF CACHE BOOL "Build doc tests?")
4950

5051
# Optional build settings
5152
set(ENABLE_ALL_IN_ONE OFF CACHE BOOL "Enable all-in-one build?")
@@ -57,7 +58,7 @@ set(ENABLE_STRIP ON CACHE BOOL "Enable stripping all symbols from release
5758
set(FEATURE_INIT_FINI OFF CACHE BOOL "Enable init/fini arrays?")
5859

5960
# Option overrides
60-
if(JERRY_CMDLINE OR JERRY_CMDLINE_MINIMAL OR UNITTESTS OR DOCTESTS)
61+
if(JERRY_CMDLINE OR JERRY_CMDLINE_MINIMAL OR JERRY_CMDLINE_SNAPSHOT OR UNITTESTS OR DOCTESTS)
6162
set(JERRY_PORT_DEFAULT ON)
6263

6364
set(JERRY_PORT_DEFAULT_MESSAGE " (FORCED BY CMDLINE OR TESTS)")
@@ -103,6 +104,7 @@ message(STATUS "ENABLE_STATIC_LINK " ${ENABLE_STATIC_LINK} ${ENABLE_STATI
103104
message(STATUS "ENABLE_STRIP " ${ENABLE_STRIP} ${ENABLE_STRIP_MESSAGE})
104105
message(STATUS "JERRY_CMDLINE " ${JERRY_CMDLINE})
105106
message(STATUS "JERRY_CMDLINE_MINIMAL " ${JERRY_CMDLINE_MINIMAL})
107+
message(STATUS "JERRY_CMDLINE_SNAPSHOT " ${JERRY_CMDLINE_SNAPSHOT})
106108
message(STATUS "JERRY_PORT_DEFAULT " ${JERRY_PORT_DEFAULT} ${JERRY_PORT_DEFAULT_MESSAGE})
107109
message(STATUS "JERRY_EXT " ${JERRY_EXT} ${JERRY_EXT_MESSAGE})
108110
message(STATUS "JERRY_LIBC " ${JERRY_LIBC} ${JERRY_LIBC_MESSAGE})
@@ -268,7 +270,7 @@ if(JERRY_EXT)
268270
endif()
269271

270272
# Jerry command line tool
271-
if(JERRY_CMDLINE OR JERRY_CMDLINE_MINIMAL)
273+
if(JERRY_CMDLINE OR JERRY_CMDLINE_MINIMAL OR JERRY_CMDLINE_SNAPSHOT)
272274
add_subdirectory(jerry-main)
273275
endif()
274276

docs/02.API-REFERENCE.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4236,6 +4236,85 @@ main (void)
42364236

42374237
- [jerry_init](#jerry_init)
42384238
- [jerry_cleanup](#jerry_cleanup)
4239+
- [jerry_exec_snapshot_at](#jerry_exec_snapshot_at)
4240+
- [jerry_parse_and_save_snapshot](#jerry_parse_and_save_snapshot)
4241+
4242+
4243+
## jerry_exec_snapshot_at
4244+
4245+
**Summary**
4246+
4247+
Execute the selected snapshot function from the specified buffer.
4248+
4249+
Same function as [jerry_exec_snapshot](#jerry_exec_snapshot) except
4250+
the executed function index can be specified.
4251+
4252+
*Note*: Returned value must be freed with [jerry_release_value](#jerry_release_value) when it
4253+
is no longer needed.
4254+
4255+
**Prototype**
4256+
4257+
```c
4258+
jerry_value_t
4259+
jerry_exec_snapshot_at (const uint32_t *snapshot_p,
4260+
size_t snapshot_size,
4261+
size_t func_index,
4262+
bool copy_bytecode);
4263+
```
4264+
4265+
- `snapshot_p` - pointer to snapshot
4266+
- `snapshot_size` - size of snapshot
4267+
- `func_index` - index of executed function
4268+
- `copy_bytecode` - flag, indicating whether the passed snapshot buffer should be copied to the
4269+
engine's memory. If set the engine should not reference the buffer after the function returns
4270+
(in this case, the passed buffer could be freed after the call). Otherwise (if the flag is not
4271+
set) - the buffer could only be freed after the engine stops (i.e. after call to jerry_cleanup).
4272+
- return value
4273+
- result of bytecode, if run was successful
4274+
- thrown error, otherwise
4275+
4276+
**Example**
4277+
4278+
[doctest]: # ()
4279+
4280+
```c
4281+
#include <string.h>
4282+
#include "jerryscript.h"
4283+
4284+
int
4285+
main (void)
4286+
{
4287+
static uint32_t global_mode_snapshot_buffer[256];
4288+
const jerry_char_t *code_to_snapshot_p = (const jerry_char_t *) "(function () { return 'string from snapshot'; }) ();";
4289+
4290+
jerry_init (JERRY_INIT_EMPTY);
4291+
size_t global_mode_snapshot_size = jerry_parse_and_save_snapshot (code_to_snapshot_p,
4292+
strlen ((const char *) code_to_snapshot_p),
4293+
true,
4294+
false,
4295+
global_mode_snapshot_buffer,
4296+
sizeof (global_mode_snapshot_buffer) / sizeof (uint32_t));
4297+
jerry_cleanup ();
4298+
4299+
jerry_init (JERRY_INIT_EMPTY);
4300+
4301+
jerry_value_t res = jerry_exec_snapshot_at (global_mode_snapshot_buffer,
4302+
global_mode_snapshot_size,
4303+
0,
4304+
false);
4305+
4306+
jerry_release_value (res);
4307+
4308+
jerry_cleanup ();
4309+
return 0;
4310+
}
4311+
```
4312+
4313+
**See also**
4314+
4315+
- [jerry_init](#jerry_init)
4316+
- [jerry_cleanup](#jerry_cleanup)
4317+
- [jerry_exec_snapshot](#jerry_exec_snapshot)
42394318
- [jerry_parse_and_save_snapshot](#jerry_parse_and_save_snapshot)
42404319

42414320

jerry-core/CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ if(NOT FEATURE_JS_PARSER)
5050
set(FEATURE_PARSER_DUMP_MESSAGE " (FORCED BY DISABLED JS PARSER)")
5151
endif()
5252

53+
if(JERRY_CMDLINE_SNAPSHOT)
54+
set(FEATURE_SNAPSHOT_SAVE ON)
55+
56+
set(FEATURE_SNAPSHOT_SAVE_MESSAGE " (FORCED BY SNAPSHOT TOOL)")
57+
endif()
58+
5359
# Status messages
5460
message(STATUS "FEATURE_CPOINTER_32_BIT " ${FEATURE_CPOINTER_32_BIT} ${FEATURE_CPOINTER_32_BIT_MESSAGE})
5561
message(STATUS "FEATURE_DEBUGGER " ${FEATURE_DEBUGGER})
@@ -62,7 +68,7 @@ message(STATUS "FEATURE_PARSER_DUMP " ${FEATURE_PARSER_DUMP} ${FEATURE_PAR
6268
message(STATUS "FEATURE_PROFILE " ${FEATURE_PROFILE})
6369
message(STATUS "FEATURE_REGEXP_DUMP " ${FEATURE_REGEXP_DUMP})
6470
message(STATUS "FEATURE_SNAPSHOT_EXEC " ${FEATURE_SNAPSHOT_EXEC} ${FEATURE_SNAPSHOT_EXEC_MESSAGE})
65-
message(STATUS "FEATURE_SNAPSHOT_SAVE " ${FEATURE_SNAPSHOT_SAVE})
71+
message(STATUS "FEATURE_SNAPSHOT_SAVE " ${FEATURE_SNAPSHOT_SAVE} ${FEATURE_SNAPSHOT_SAVE_MESSAGE})
6672
message(STATUS "FEATURE_SYSTEM_ALLOCATOR " ${FEATURE_SYSTEM_ALLOCATOR})
6773
message(STATUS "FEATURE_VALGRIND " ${FEATURE_VALGRIND})
6874
message(STATUS "FEATURE_VALGRIND_FREYA " ${FEATURE_VALGRIND_FREYA})

0 commit comments

Comments
 (0)