diff --git a/doc/api/errors.md b/doc/api/errors.md index 2adc69eadc58dc..a7d4583ea15ed4 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -1999,6 +1999,16 @@ the following reasons: * It is being linked (`linkingStatus` is `'linking'`) * Linking has failed for this module (`linkingStatus` is `'errored'`) + +### `ERR_VM_MODULE_CACHED_DATA_REJECTED` + +The `cachedData` option passed to a module constructor is invalid. + + +### `ERR_VM_MODULE_CANNOT_CREATE_CACHED_DATA` + +Cached data cannot be created for modules which have already been evaluated. + ### `ERR_VM_MODULE_DIFFERENT_CONTEXT` diff --git a/doc/api/vm.md b/doc/api/vm.md index 595f4fdfa40ccc..98f422cce3656c 100644 --- a/doc/api/vm.md +++ b/doc/api/vm.md @@ -563,6 +563,10 @@ defined in the ECMAScript specification. * `identifier` {string} String used in stack traces. **Default:** `'vm:module(i)'` where `i` is a context-specific ascending index. + * `cachedData` {Buffer|TypedArray|DataView} Provides an optional `Buffer` or + `TypedArray`, or `DataView` with V8's code cache data for the supplied + source. The `code` must be the same as the module from which this + `cachedData` was created. * `context` {Object} The [contextified][] object as returned by the `vm.createContext()` method, to compile and evaluate this `Module` in. * `lineOffset` {integer} Specifies the line number offset that is displayed @@ -618,6 +622,28 @@ const contextifiedObject = vm.createContext({ secret: 42 }); })(); ``` +### `sourceTextModule.createCachedData()` + + +* Returns: {Buffer} + +Creates a code cache that can be used with the SourceTextModule constructor's +`cachedData` option. Returns a Buffer. This method may be called any number +of times before the module has been evaluated. + +```js +// Create an initial module +const module = new vm.SourceTextModule('const a = 1;'); + +// Create cached data from this module +const cachedData = module.createCachedData(); + +// Create a new module using the cached data. The code must be the same. +const module2 = new vm.SourceTextModule('const a = 1;', { cachedData }); +``` + ## Class: `vm.SyntheticModule`