GeezerVison is showing github.com — vm: add code cache support for SourceTextModule · lerouxb/node@b810e67
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/errors.md +10Lines changed: 10 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -1988,6 +1988,16 @@ the following reasons: | 1988 | 1988 | * It is being linked (`linkingStatus` is `'linking'`) | 1989 | 1989 | * Linking has failed for this module (`linkingStatus` is `'errored'`) | 1990 | 1990 |
| | 1991 | +<a id="ERR_VM_MODULE_CACHED_DATA_REJECTED"></a> | | 1992 | +### `ERR_VM_MODULE_CACHED_DATA_REJECTED` | | 1993 | + | | 1994 | +The `cachedData` option passed to a module constructor is invalid. | | 1995 | + | | 1996 | +<a id="ERR_VM_MODULE_CANNOT_CREATE_CACHED_DATA"></a> | | 1997 | +### `ERR_VM_MODULE_CANNOT_CREATE_CACHED_DATA` | | 1998 | + | | 1999 | +Cached data cannot be created for modules which have already been evaluated. | | 2000 | + | 1991 | 2001 | <a id="ERR_VM_MODULE_DIFFERENT_CONTEXT"></a> | 1992 | 2002 | ### `ERR_VM_MODULE_DIFFERENT_CONTEXT` | 1993 | 2003 |
| |
Collapse file Copy file name to clipboardExpand all lines: doc/api/vm.md +26Lines changed: 26 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -566,6 +566,10 @@ defined in the ECMAScript specification. | 566 | 566 | * `identifier` {string} String used in stack traces. | 567 | 567 | **Default:** `'vm:module(i)'` where `i` is a context-specific ascending | 568 | 568 | index. | | 569 | + * `cachedData` {Buffer|TypedArray|DataView} Provides an optional `Buffer` or | | 570 | + `TypedArray`, or `DataView` with V8's code cache data for the supplied | | 571 | + source. The `code` must be the same as the module from which this | | 572 | + `cachedData` was created. | 569 | 573 | * `context` {Object} The [contextified][] object as returned by the | 570 | 574 | `vm.createContext()` method, to compile and evaluate this `Module` in. | 571 | 575 | * `lineOffset` {integer} Specifies the line number offset that is displayed | @@ -621,6 +625,28 @@ const contextifiedObject = vm.createContext({ secret: 42 }); | 621 | 625 | })(); | 622 | 626 | ``` | 623 | 627 |
| | 628 | +### `sourceTextModule.createCachedData()` | | 629 | +<!-- YAML | | 630 | +added: REPLACEME | | 631 | +--> | | 632 | +
| | 633 | +* Returns: {Buffer} | | 634 | +
| | 635 | +Creates a code cache that can be used with the SourceTextModule constructor's | | 636 | +`cachedData` option. Returns a Buffer. This method may be called any number | | 637 | +of times before the module has been evaluated. | | 638 | + | | 639 | +```js | | 640 | +// Create an initial module | | 641 | +const module = new vm.SourceTextModule('const a = 1;'); | | 642 | +
| | 643 | +// Create cached data from this module | | 644 | +const cachedData = module.createCachedData(); | | 645 | +
| | 646 | +// Create a new module using the cached data. The code must be the same. | | 647 | +const module2 = new vm.SourceTextModule('const a = 1;', { cachedData }); | | 648 | +``` | | 649 | + | 624 | 650 | ## Class: `vm.SyntheticModule` | 625 | 651 | <!-- YAML | 626 | 652 | added: v13.0.0 | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/errors.js +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1351,6 +1351,8 @@ E('ERR_VALID_PERFORMANCE_ENTRY_TYPE', | 1351 | 1351 | E('ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING', | 1352 | 1352 | 'A dynamic import callback was not specified.', TypeError); | 1353 | 1353 | E('ERR_VM_MODULE_ALREADY_LINKED', 'Module has already been linked', Error); | | 1354 | +E('ERR_VM_MODULE_CANNOT_CREATE_CACHED_DATA', | | 1355 | + 'Cached data cannot be created for a module which has been evaluated', Error); | 1354 | 1356 | E('ERR_VM_MODULE_DIFFERENT_CONTEXT', | 1355 | 1357 | 'Linked modules must use the same context', Error); | 1356 | 1358 | E('ERR_VM_MODULE_LINKING_ERRORED', | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/vm/module.js +27-2Lines changed: 27 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -11,7 +11,10 @@ const { | 11 | 11 | } = primordials; | 12 | 12 |
| 13 | 13 | const { isContext } = internalBinding('contextify'); | 14 | | -const { isModuleNamespaceObject } = require('internal/util/types'); | | 14 | +const { | | 15 | + isModuleNamespaceObject, | | 16 | + isArrayBufferView, | | 17 | +} = require('internal/util/types'); | 15 | 18 | const { | 16 | 19 | getConstructorOf, | 17 | 20 | customInspectSymbol, | @@ -21,6 +24,7 @@ const { | 21 | 24 | ERR_INVALID_ARG_TYPE, | 22 | 25 | ERR_VM_MODULE_ALREADY_LINKED, | 23 | 26 | ERR_VM_MODULE_DIFFERENT_CONTEXT, | | 27 | + ERR_VM_MODULE_CANNOT_CREATE_CACHED_DATA, | 24 | 28 | ERR_VM_MODULE_LINKING_ERRORED, | 25 | 29 | ERR_VM_MODULE_NOT_MODULE, | 26 | 30 | ERR_VM_MODULE_STATUS, | @@ -107,7 +111,8 @@ class Module { | 107 | 111 |
| 108 | 112 | if (sourceText !== undefined) { | 109 | 113 | this[kWrap] = new ModuleWrap(identifier, context, sourceText, | 110 | | - options.lineOffset, options.columnOffset); | | 114 | + options.lineOffset, options.columnOffset, | | 115 | + options.cachedData); | 111 | 116 |
| 112 | 117 | binding.callbackMap.set(this[kWrap], { | 113 | 118 | initializeImportMeta: options.initializeImportMeta, | @@ -253,6 +258,7 @@ class SourceTextModule extends Module { | 253 | 258 | importModuleDynamically, | 254 | 259 | context, | 255 | 260 | identifier, | | 261 | + cachedData, | 256 | 262 | } = options; | 257 | 263 |
| 258 | 264 | validateInt32(lineOffset, 'options.lineOffset'); | @@ -271,12 +277,21 @@ class SourceTextModule extends Module { | 271 | 277 | importModuleDynamically); | 272 | 278 | } | 273 | 279 |
| | 280 | + if (cachedData !== undefined && !isArrayBufferView(cachedData)) { | | 281 | + throw new ERR_INVALID_ARG_TYPE( | | 282 | + 'options.cachedData', | | 283 | + ['Buffer', 'TypedArray', 'DataView'], | | 284 | + cachedData | | 285 | + ); | | 286 | + } | | 287 | + | 274 | 288 | super({ | 275 | 289 | sourceText, | 276 | 290 | context, | 277 | 291 | identifier, | 278 | 292 | lineOffset, | 279 | 293 | columnOffset, | | 294 | + cachedData, | 280 | 295 | initializeImportMeta, | 281 | 296 | importModuleDynamically, | 282 | 297 | }); | @@ -348,6 +363,16 @@ class SourceTextModule extends Module { | 348 | 363 | } | 349 | 364 | return super.error; | 350 | 365 | } | | 366 | + | | 367 | + createCachedData() { | | 368 | + const { status } = this; | | 369 | + if (status === 'evaluating' || | | 370 | + status === 'evaluated' || | | 371 | + status === 'errored') { | | 372 | + throw new ERR_VM_MODULE_CANNOT_CREATE_CACHED_DATA(); | | 373 | + } | | 374 | + return this[kWrap].createCachedData(); | | 375 | + } | 351 | 376 | } | 352 | 377 |
| 353 | 378 | class SyntheticModule extends Module { | |
Collapse file Copy file name to clipboardExpand all lines: src/module_wrap.cc +65-8Lines changed: 65 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 |
| 3 | 3 | #include "env.h" | 4 | 4 | #include "memory_tracker-inl.h" | | 5 | +#include "node_contextify.h" | 5 | 6 | #include "node_errors.h" | | 7 | +#include "node_internals.h" | | 8 | +#include "node_process.h" | 6 | 9 | #include "node_url.h" | 7 | | -#include "util-inl.h" | 8 | | -#include "node_contextify.h" | 9 | 10 | #include "node_watchdog.h" | 10 | | -#include "node_process.h" | | 11 | +#include "util-inl.h" | 11 | 12 |
| 12 | 13 | #include <sys/stat.h> // S_IFDIR | 13 | 14 |
| @@ -22,6 +23,7 @@ using node::contextify::ContextifyContext; | 22 | 23 | using node::url::URL; | 23 | 24 | using node::url::URL_FLAGS_FAILED; | 24 | 25 | using v8::Array; | | 26 | +using v8::ArrayBufferView; | 25 | 27 | using v8::Context; | 26 | 28 | using v8::Function; | 27 | 29 | using v8::FunctionCallbackInfo; | @@ -44,6 +46,7 @@ using v8::Promise; | 44 | 46 | using v8::ScriptCompiler; | 45 | 47 | using v8::ScriptOrigin; | 46 | 48 | using v8::String; | | 49 | +using v8::UnboundModuleScript; | 47 | 50 | using v8::Undefined; | 48 | 51 | using v8::Value; | 49 | 52 |
| @@ -131,7 +134,7 @@ void ModuleWrap::New(const FunctionCallbackInfo<Value>& args) { | 131 | 134 | // new ModuleWrap(url, context, exportNames, syntheticExecutionFunction) | 132 | 135 | CHECK(args[3]->IsFunction()); | 133 | 136 | } else { | 134 | | - // new ModuleWrap(url, context, source, lineOffset, columOffset) | | 137 | + // new ModuleWrap(url, context, source, lineOffset, columOffset, cachedData) | 135 | 138 | CHECK(args[2]->IsString()); | 136 | 139 | CHECK(args[3]->IsNumber()); | 137 | 140 | line_offset = args[3].As<Integer>(); | @@ -167,6 +170,17 @@ void ModuleWrap::New(const FunctionCallbackInfo<Value>& args) { | 167 | 170 | module = Module::CreateSyntheticModule(isolate, url, export_names, | 168 | 171 | SyntheticModuleEvaluationStepsCallback); | 169 | 172 | } else { | | 173 | + ScriptCompiler::CachedData* cached_data = nullptr; | | 174 | + if (!args[5]->IsUndefined()) { | | 175 | + CHECK(args[5]->IsArrayBufferView()); | | 176 | + Local<ArrayBufferView> cached_data_buf = args[5].As<ArrayBufferView>(); | | 177 | + uint8_t* data = static_cast<uint8_t*>( | | 178 | + cached_data_buf->Buffer()->GetBackingStore()->Data()); | | 179 | + cached_data = | | 180 | + new ScriptCompiler::CachedData(data + cached_data_buf->ByteOffset(), | | 181 | + cached_data_buf->ByteLength()); | | 182 | + } | | 183 | + | 170 | 184 | Local<String> source_text = args[2].As<String>(); | 171 | 185 | ScriptOrigin origin(url, | 172 | 186 | line_offset, // line offset | @@ -178,8 +192,15 @@ void ModuleWrap::New(const FunctionCallbackInfo<Value>& args) { | 178 | 192 | False(isolate), // is WASM | 179 | 193 | True(isolate), // is ES Module | 180 | 194 | host_defined_options); | 181 | | - ScriptCompiler::Source source(source_text, origin); | 182 | | - if (!ScriptCompiler::CompileModule(isolate, &source).ToLocal(&module)) { | | 195 | + ScriptCompiler::Source source(source_text, origin, cached_data); | | 196 | + ScriptCompiler::CompileOptions options; | | 197 | + if (source.GetCachedData() == nullptr) { | | 198 | + options = ScriptCompiler::kNoCompileOptions; | | 199 | + } else { | | 200 | + options = ScriptCompiler::kConsumeCodeCache; | | 201 | + } | | 202 | + if (!ScriptCompiler::CompileModule(isolate, &source, options) | | 203 | + .ToLocal(&module)) { | 183 | 204 | if (try_catch.HasCaught() && !try_catch.HasTerminated()) { | 184 | 205 | CHECK(!try_catch.Message().IsEmpty()); | 185 | 206 | CHECK(!try_catch.Exception().IsEmpty()); | @@ -189,6 +210,13 @@ void ModuleWrap::New(const FunctionCallbackInfo<Value>& args) { | 189 | 210 | } | 190 | 211 | return; | 191 | 212 | } | | 213 | + if (options == ScriptCompiler::kConsumeCodeCache && | | 214 | + source.GetCachedData()->rejected) { | | 215 | + THROW_ERR_VM_MODULE_CACHED_DATA_REJECTED( | | 216 | + env, "cachedData buffer was rejected"); | | 217 | + try_catch.ReThrow(); | | 218 | + return; | | 219 | + } | 192 | 220 | } | 193 | 221 | } | 194 | 222 |
| @@ -1507,8 +1535,7 @@ MaybeLocal<Value> ModuleWrap::SyntheticModuleEvaluationStepsCallback( | 1507 | 1535 | return ret; | 1508 | 1536 | } | 1509 | 1537 |
| 1510 | | -void ModuleWrap::SetSyntheticExport( | 1511 | | - const v8::FunctionCallbackInfo<v8::Value>& args) { | | 1538 | +void ModuleWrap::SetSyntheticExport(const FunctionCallbackInfo<Value>& args) { | 1512 | 1539 | Isolate* isolate = args.GetIsolate(); | 1513 | 1540 | Local<Object> that = args.This(); | 1514 | 1541 |
| @@ -1528,6 +1555,35 @@ void ModuleWrap::SetSyntheticExport( | 1528 | 1555 | USE(module->SetSyntheticModuleExport(isolate, export_name, export_value)); | 1529 | 1556 | } | 1530 | 1557 |
| | 1558 | +void ModuleWrap::CreateCachedData(const FunctionCallbackInfo<Value>& args) { | | 1559 | + Isolate* isolate = args.GetIsolate(); | | 1560 | + Local<Object> that = args.This(); | | 1561 | + | | 1562 | + ModuleWrap* obj; | | 1563 | + ASSIGN_OR_RETURN_UNWRAP(&obj, that); | | 1564 | + | | 1565 | + CHECK(!obj->synthetic_); | | 1566 | + | | 1567 | + Local<Module> module = obj->module_.Get(isolate); | | 1568 | + | | 1569 | + CHECK_LT(module->GetStatus(), v8::Module::Status::kEvaluating); | | 1570 | + | | 1571 | + Local<UnboundModuleScript> unbound_module_script = | | 1572 | + module->GetUnboundModuleScript(); | | 1573 | + std::unique_ptr<ScriptCompiler::CachedData> cached_data( | | 1574 | + ScriptCompiler::CreateCodeCache(unbound_module_script)); | | 1575 | + Environment* env = Environment::GetCurrent(args); | | 1576 | + if (!cached_data) { | | 1577 | + args.GetReturnValue().Set(Buffer::New(env, 0).ToLocalChecked()); | | 1578 | + } else { | | 1579 | + MaybeLocal<Object> buf = | | 1580 | + Buffer::Copy(env, | | 1581 | + reinterpret_cast<const char*>(cached_data->data), | | 1582 | + cached_data->length); | | 1583 | + args.GetReturnValue().Set(buf.ToLocalChecked()); | | 1584 | + } | | 1585 | +} | | 1586 | + | 1531 | 1587 | void ModuleWrap::Initialize(Local<Object> target, | 1532 | 1588 | Local<Value> unused, | 1533 | 1589 | Local<Context> context, | @@ -1543,6 +1599,7 @@ void ModuleWrap::Initialize(Local<Object> target, | 1543 | 1599 | env->SetProtoMethod(tpl, "instantiate", Instantiate); | 1544 | 1600 | env->SetProtoMethod(tpl, "evaluate", Evaluate); | 1545 | 1601 | env->SetProtoMethod(tpl, "setExport", SetSyntheticExport); | | 1602 | + env->SetProtoMethodNoSideEffect(tpl, "createCachedData", CreateCachedData); | 1546 | 1603 | env->SetProtoMethodNoSideEffect(tpl, "getNamespace", GetNamespace); | 1547 | 1604 | env->SetProtoMethodNoSideEffect(tpl, "getStatus", GetStatus); | 1548 | 1605 | env->SetProtoMethodNoSideEffect(tpl, "getError", GetError); | |
Collapse file Copy file name to clipboardExpand all lines: src/module_wrap.h +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -75,6 +75,7 @@ class ModuleWrap : public BaseObject { | 75 | 75 | v8::Local<v8::Context> context, v8::Local<v8::Module> module); | 76 | 76 | static void SetSyntheticExport( | 77 | 77 | const v8::FunctionCallbackInfo<v8::Value>& args); | | 78 | + static void CreateCachedData(const v8::FunctionCallbackInfo<v8::Value>& args); | 78 | 79 |
| 79 | 80 | static v8::MaybeLocal<v8::Module> ResolveCallback( | 80 | 81 | v8::Local<v8::Context> context, | |
Collapse file Copy file name to clipboardExpand all lines: src/node_errors.h +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -59,6 +59,7 @@ void PrintErrorString(const char* format, ...); | 59 | 59 | V(ERR_TLS_INVALID_PROTOCOL_METHOD, TypeError) \ | 60 | 60 | V(ERR_TRANSFERRING_EXTERNALIZED_SHAREDARRAYBUFFER, TypeError) \ | 61 | 61 | V(ERR_TLS_PSK_SET_IDENTIY_HINT_FAILED, Error) \ | | 62 | + V(ERR_VM_MODULE_CACHED_DATA_REJECTED, Error) \ | 62 | 63 |
| 63 | 64 | #define V(code, type) \ | 64 | 65 | inline v8::Local<v8::Value> code(v8::Isolate* isolate, \ | |
Collapse file Copy file name to clipboard +30Lines changed: 30 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +// Flags: --experimental-vm-modules | | 4 | + | | 5 | +require('../common'); | | 6 | + | | 7 | +const assert = require('assert'); | | 8 | +const { SourceTextModule } = require('vm'); | | 9 | + | | 10 | +{ | | 11 | + const m = new SourceTextModule('const a = 1'); | | 12 | + const cachedData = m.createCachedData(); | | 13 | + | | 14 | + new SourceTextModule('const a = 1', { cachedData }); | | 15 | + | | 16 | + assert.throws(() => { | | 17 | + new SourceTextModule('differentSource', { cachedData }); | | 18 | + }, { | | 19 | + code: 'ERR_VM_MODULE_CACHED_DATA_REJECTED', | | 20 | + }); | | 21 | +} | | 22 | + | | 23 | +assert.rejects(async () => { | | 24 | + const m = new SourceTextModule('const a = 1'); | | 25 | + await m.link(() => {}); | | 26 | + m.evaluate(); | | 27 | + m.createCachedData(); | | 28 | +}, { | | 29 | + code: 'ERR_VM_MODULE_CANNOT_CREATE_CACHED_DATA', | | 30 | +}); |
|