Skip to content

Commit c8ee3b5

Browse files
committed
doc: fix vm.Script createCachedData example
`Script.createCachedData` and `SourceTextModule.createCachedData` doesn't serialize JavaScript variables.
1 parent d0f73d3 commit c8ee3b5

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

doc/api/vm.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,15 @@ Creates a code cache that can be used with the `Script` constructor's
126126
`cachedData` option. Returns a `Buffer`. This method may be called at any
127127
time and any number of times.
128128

129+
The code cache of the `Script` doesn't contain any JavaScript observable
130+
states. The code cache is safe to be saved along side the script source and
131+
used to construct new `Script` instances multiple times.
132+
133+
Functions in the `Script` source can be marked as lazily compiled and they are
134+
not compiled at construction of the `Script`. These functions are forced to be
135+
compiled when they are invoked the first time. The code cache serializes the
136+
current compilation state.
137+
129138
```js
130139
const script = new vm.Script(`
131140
function add(a, b) {
@@ -135,11 +144,13 @@ function add(a, b) {
135144
const x = add(1, 2);
136145
`);
137146

138-
const cacheWithoutX = script.createCachedData();
147+
const cache1 = script.createCachedData();
148+
// cache1 contains function `add` with lazy compilation stub.
139149

140150
script.runInThisContext();
141151

142-
const cacheWithX = script.createCachedData();
152+
const cache2 = script.createCachedData();
153+
// cache2 contains compiled function `add`.
143154
```
144155

145156
### `script.runInContext(contextifiedObject[, options])`
@@ -780,6 +791,15 @@ Creates a code cache that can be used with the `SourceTextModule` constructor's
780791
`cachedData` option. Returns a `Buffer`. This method may be called any number
781792
of times before the module has been evaluated.
782793

794+
The code cache of the `SourceTextModule` doesn't contain any JavaScript
795+
observable states. The code cache is safe to be saved along side the script
796+
source and used to construct new `SourceTextModule` instances multiple times.
797+
798+
Functions in the `SourceTextModule` source can be marked as lazily compiled and
799+
they are not compiled at construction of the `SourceTextModule`. These
800+
functions are forced to be compiled when they are invoked the first time. The
801+
code cache serializes the current compilation state.
802+
783803
```js
784804
// Create an initial module
785805
const module = new vm.SourceTextModule('const a = 1;');

0 commit comments

Comments
 (0)