@@ -126,6 +126,15 @@ Creates a code cache that can be used with the `Script` constructor's
126
126
` cachedData ` option. Returns a ` Buffer ` . This method may be called at any
127
127
time and any number of times.
128
128
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
+
129
138
``` js
130
139
const script = new vm.Script (`
131
140
function add(a, b) {
@@ -135,11 +144,13 @@ function add(a, b) {
135
144
const x = add(1, 2);
136
145
` );
137
146
138
- const cacheWithoutX = script .createCachedData ();
147
+ const cache1 = script .createCachedData ();
148
+ // cache1 contains function `add` with lazy compilation stub.
139
149
140
150
script .runInThisContext ();
141
151
142
- const cacheWithX = script .createCachedData ();
152
+ const cache2 = script .createCachedData ();
153
+ // cache2 contains compiled function `add`.
143
154
```
144
155
145
156
### ` script.runInContext(contextifiedObject[, options]) `
@@ -780,6 +791,15 @@ Creates a code cache that can be used with the `SourceTextModule` constructor's
780
791
` cachedData` option . Returns a ` Buffer` . This method may be called any number
781
792
of times before the module has been evaluated.
782
793
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
+
783
803
```js
784
804
// Create an initial module
785
805
const module = new vm.SourceTextModule(' const a = 1 ;' );
0 commit comments