| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 6291c10 commit 9f7899e
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,12 +2,16 @@ | |||
| 2 | 2 | ||
| 3 | 3 | const { | |
| 4 | 4 | ArrayPrototypeForEach, | |
| 5 | + Symbol, | ||
| 5 | 6 | } = primordials; | |
| 6 | 7 | ||
| 7 | 8 | const { | |
| 8 | 9 | compileFunction, | |
| 9 | 10 | isContext: _isContext, | |
| 10 | 11 | } = internalBinding('contextify'); | |
| 12 | + const { | ||
| 13 | + default_host_defined_options, | ||
| 14 | + } = internalBinding('symbols'); | ||
| 11 | 15 | const { | |
| 12 | 16 | validateArray, | |
| 13 | 17 | validateBoolean, | |
@@ -28,12 +32,27 @@ function isContext(object) { | |||
| 28 | 32 | return _isContext(object); | |
| 29 | 33 | } | |
| 30 | 34 | ||
| 35 | + function getHostDefinedOptionId(importModuleDynamically, filename) { | ||
| 36 | + if (importModuleDynamically !== undefined) { | ||
| 37 | + // Check that it's either undefined or a function before we pass | ||
| 38 | + // it into the native constructor. | ||
| 39 | + validateFunction(importModuleDynamically, | ||
| 40 | + 'options.importModuleDynamically'); | ||
| 41 | + } | ||
| 42 | + if (importModuleDynamically === undefined) { | ||
| 43 | + // We need a default host defined options that are the same for all | ||
| 44 | + // scripts not needing custom module callbacks so that the isolate | ||
| 45 | + // compilation cache can be hit. | ||
| 46 | + return default_host_defined_options; | ||
| 47 | + } | ||
| 48 | + return Symbol(filename); | ||
| 49 | + } | ||
| 50 | + | ||
| 31 | 51 | function internalCompileFunction(code, params, options) { | |
| 32 | 52 | validateString(code, 'code'); | |
| 33 | 53 | if (params !== undefined) { | |
| 34 | 54 | validateStringArray(params, 'params'); | |
| 35 | 55 | } | |
| 36 | - | ||
| 37 | 56 | const { | |
| 38 | 57 | filename = '', | |
| 39 | 58 | columnOffset = 0, | |
@@ -70,6 +89,8 @@ function internalCompileFunction(code, params, options) { | |||
| 70 | 89 | validateObject(extension, name, { __proto__: null, nullable: true }); | |
| 71 | 90 | }); | |
| 72 | 91 | ||
| 92 | + const hostDefinedOptionId = | ||
| 93 | + getHostDefinedOptionId(importModuleDynamically, filename); | ||
| 73 | 94 | const result = compileFunction( | |
| 74 | 95 | code, | |
| 75 | 96 | filename, | |
@@ -80,6 +101,7 @@ function internalCompileFunction(code, params, options) { | |||
| 80 | 101 | parsingContext, | |
| 81 | 102 | contextExtensions, | |
| 82 | 103 | params, | |
| 104 | + hostDefinedOptionId, | ||
| 83 | 105 | ); | |
| 84 | 106 | ||
| 85 | 107 | if (produceCachedData) { | |
@@ -111,6 +133,7 @@ function internalCompileFunction(code, params, options) { | |||
| 111 | 133 | } | |
| 112 | 134 | ||
| 113 | 135 | module.exports = { | |
| 136 | + getHostDefinedOptionId, | ||
| 114 | 137 | internalCompileFunction, | |
| 115 | 138 | isContext, | |
| 116 | 139 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -42,7 +42,6 @@ const { | |||
| 42 | 42 | const { | |
| 43 | 43 | validateBoolean, | |
| 44 | 44 | validateBuffer, | |
| 45 | - validateFunction, | ||
| 46 | 45 | validateInt32, | |
| 47 | 46 | validateObject, | |
| 48 | 47 | validateOneOf, | |
@@ -55,6 +54,7 @@ const { | |||
| 55 | 54 | kVmBreakFirstLineSymbol, | |
| 56 | 55 | } = require('internal/util'); | |
| 57 | 56 | const { | |
| 57 | + getHostDefinedOptionId, | ||
| 58 | 58 | internalCompileFunction, | |
| 59 | 59 | isContext, | |
| 60 | 60 | } = require('internal/vm'); | |
@@ -87,12 +87,8 @@ class Script extends ContextifyScript { | |||
| 87 | 87 | } | |
| 88 | 88 | validateBoolean(produceCachedData, 'options.produceCachedData'); | |
| 89 | 89 | ||
| 90 | - if (importModuleDynamically !== undefined) { | ||
| 91 | - // Check that it's either undefined or a function before we pass | ||
| 92 | - // it into the native constructor. | ||
| 93 | - validateFunction(importModuleDynamically, | ||
| 94 | - 'options.importModuleDynamically'); | ||
| 95 | - } | ||
| 90 | + const hostDefinedOptionId = | ||
| 91 | + getHostDefinedOptionId(importModuleDynamically, filename); | ||
| 96 | 92 | // Calling `ReThrow()` on a native TryCatch does not generate a new | |
| 97 | 93 | // abort-on-uncaught-exception check. A dummy try/catch in JS land | |
| 98 | 94 | // protects against that. | |
@@ -104,7 +100,7 @@ class Script extends ContextifyScript { | |||
| 104 | 100 | cachedData, | |
| 105 | 101 | produceCachedData, | |
| 106 | 102 | parsingContext, | |
| 107 | - importModuleDynamically !== undefined); | ||
| 103 | + hostDefinedOptionId); | ||
| 108 | 104 | } catch (e) { | |
| 109 | 105 | throw e; /* node-do-not-add-exception-line */ | |
| 110 | 106 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -787,11 +787,11 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) { | |||
| 787 | 787 | bool produce_cached_data = false; | |
| 788 | 788 | Local<Context> parsing_context = context; | |
| 789 | 789 | ||
| 790 | - bool needs_custom_host_defined_options = false; | ||
| 790 | + Local<Symbol> id_symbol; | ||
| 791 | 791 | if (argc > 2) { | |
| 792 | 792 | // new ContextifyScript(code, filename, lineOffset, columnOffset, | |
| 793 | 793 | // cachedData, produceCachedData, parsingContext, | |
| 794 | - // needsCustomHostDefinedOptions) | ||
| 794 | + // hostDefinedOptionId) | ||
| 795 | 795 | CHECK_EQ(argc, 8); | |
| 796 | 796 | CHECK(args[2]->IsNumber()); | |
| 797 | 797 | line_offset = args[2].As<Int32>()->Value(); | |
@@ -811,9 +811,8 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) { | |||
| 811 | 811 | CHECK_NOT_NULL(sandbox); | |
| 812 | 812 | parsing_context = sandbox->context(); | |
| 813 | 813 | } | |
| 814 | - if (args[7]->IsTrue()) { | ||
| 815 | - needs_custom_host_defined_options = true; | ||
| 816 | - } | ||
| 814 | + CHECK(args[7]->IsSymbol()); | ||
| 815 | + id_symbol = args[7].As<Symbol>(); | ||
| 817 | 816 | } | |
| 818 | 817 | ||
| 819 | 818 | ContextifyScript* contextify_script = | |
@@ -837,12 +836,6 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) { | |||
| 837 | 836 | ||
| 838 | 837 | Local<PrimitiveArray> host_defined_options = | |
| 839 | 838 | PrimitiveArray::New(isolate, loader::HostDefinedOptions::kLength); | |
| 840 | - // We need a default host defined options that's the same for all scripts | ||
| 841 | - // not needing custom module callbacks for so that the isolate compilation | ||
| 842 | - // cache can be hit. | ||
| 843 | - Local<Symbol> id_symbol = needs_custom_host_defined_options | ||
| 844 | - ? Symbol::New(isolate, filename) | ||
| 845 | - : env->default_host_defined_options(); | ||
| 846 | 839 | host_defined_options->Set( | |
| 847 | 840 | isolate, loader::HostDefinedOptions::kID, id_symbol); | |
| 848 | 841 | ||
@@ -1201,6 +1194,10 @@ void ContextifyContext::CompileFunction( | |||
| 1201 | 1194 | params_buf = args[8].As<Array>(); | |
| 1202 | 1195 | } | |
| 1203 | 1196 | ||
| 1197 | + // Argument 10: host-defined option symbol | ||
| 1198 | + CHECK(args[9]->IsSymbol()); | ||
| 1199 | + Local<Symbol> id_symbol = args[9].As<Symbol>(); | ||
| 1200 | + | ||
| 1204 | 1201 | // Read cache from cached data buffer | |
| 1205 | 1202 | ScriptCompiler::CachedData* cached_data = nullptr; | |
| 1206 | 1203 | if (!cached_data_buf.IsEmpty()) { | |
@@ -1212,7 +1209,6 @@ void ContextifyContext::CompileFunction( | |||
| 1212 | 1209 | // Set host_defined_options | |
| 1213 | 1210 | Local<PrimitiveArray> host_defined_options = | |
| 1214 | 1211 | PrimitiveArray::New(isolate, loader::HostDefinedOptions::kLength); | |
| 1215 | - Local<Symbol> id_symbol = Symbol::New(isolate, filename); | ||
| 1216 | 1212 | host_defined_options->Set( | |
| 1217 | 1213 | isolate, loader::HostDefinedOptions::kID, id_symbol); | |
| 1218 | 1214 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,7 +1,7 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | ||
| 3 | - require('../common'); | ||
| 4 | - const { Script } = require('vm'); | ||
| 3 | + const common = require('../common'); | ||
| 4 | + const { Script, compileFunction } = require('vm'); | ||
| 5 | 5 | const assert = require('assert'); | |
| 6 | 6 | ||
| 7 | 7 | assert.rejects(async () => { | |
@@ -10,4 +10,11 @@ assert.rejects(async () => { | |||
| 10 | 10 | await imported; | |
| 11 | 11 | }, { | |
| 12 | 12 | code: 'ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING' | |
| 13 | - }); | ||
| 13 | + }).then(common.mustCall()); | ||
| 14 | + | ||
| 15 | + assert.rejects(async () => { | ||
| 16 | + const imported = compileFunction('return import("fs")')(); | ||
| 17 | + await imported; | ||
| 18 | + }, { | ||
| 19 | + code: 'ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING' | ||
| 20 | + }).then(common.mustCall()); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments