| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 92f3447 commit 9e1c229
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,6 +8,7 @@ const { | |||
| 8 | 8 | ArrayPrototypeSome, | |
| 9 | 9 | ObjectDefineProperty, | |
| 10 | 10 | ObjectGetPrototypeOf, | |
| 11 | + ObjectPrototypeHasOwnProperty, | ||
| 11 | 12 | ObjectSetPrototypeOf, | |
| 12 | 13 | ReflectApply, | |
| 13 | 14 | SafePromiseAllReturnVoid, | |
@@ -43,6 +44,7 @@ const { | |||
| 43 | 44 | validateObject, | |
| 44 | 45 | validateUint32, | |
| 45 | 46 | validateString, | |
| 47 | + validateInternalField, | ||
| 46 | 48 | } = require('internal/validators'); | |
| 47 | 49 | ||
| 48 | 50 | const binding = internalBinding('module_wrap'); | |
@@ -75,6 +77,13 @@ const kLink = Symbol('kLink'); | |||
| 75 | 77 | ||
| 76 | 78 | const { isContext } = require('internal/vm'); | |
| 77 | 79 | ||
| 80 | + function isModule(object) { | ||
| 81 | + if (typeof object !== 'object' || object === null || !ObjectPrototypeHasOwnProperty(object, kWrap)) { | ||
| 82 | + return false; | ||
| 83 | + } | ||
| 84 | + return true; | ||
| 85 | + } | ||
| 86 | + | ||
| 78 | 87 | class Module { | |
| 79 | 88 | constructor(options) { | |
| 80 | 89 | emitExperimentalWarning('VM Modules'); | |
@@ -147,50 +156,38 @@ class Module { | |||
| 147 | 156 | } | |
| 148 | 157 | ||
| 149 | 158 | get identifier() { | |
| 150 | - if (this[kWrap] === undefined) { | ||
| 151 | - throw new ERR_VM_MODULE_NOT_MODULE(); | ||
| 152 | - } | ||
| 159 | + validateInternalField(this, kWrap, 'Module'); | ||
| 153 | 160 | return this[kWrap].url; | |
| 154 | 161 | } | |
| 155 | 162 | ||
| 156 | 163 | get context() { | |
| 157 | - if (this[kWrap] === undefined) { | ||
| 158 | - throw new ERR_VM_MODULE_NOT_MODULE(); | ||
| 159 | - } | ||
| 164 | + validateInternalField(this, kWrap, 'Module'); | ||
| 160 | 165 | return this[kContext]; | |
| 161 | 166 | } | |
| 162 | 167 | ||
| 163 | 168 | get namespace() { | |
| 164 | - if (this[kWrap] === undefined) { | ||
| 165 | - throw new ERR_VM_MODULE_NOT_MODULE(); | ||
| 166 | - } | ||
| 169 | + validateInternalField(this, kWrap, 'Module'); | ||
| 167 | 170 | if (this[kWrap].getStatus() < kInstantiated) { | |
| 168 | 171 | throw new ERR_VM_MODULE_STATUS('must not be unlinked or linking'); | |
| 169 | 172 | } | |
| 170 | 173 | return this[kWrap].getNamespace(); | |
| 171 | 174 | } | |
| 172 | 175 | ||
| 173 | 176 | get status() { | |
| 174 | - if (this[kWrap] === undefined) { | ||
| 175 | - throw new ERR_VM_MODULE_NOT_MODULE(); | ||
| 176 | - } | ||
| 177 | + validateInternalField(this, kWrap, 'Module'); | ||
| 177 | 178 | return STATUS_MAP[this[kWrap].getStatus()]; | |
| 178 | 179 | } | |
| 179 | 180 | ||
| 180 | 181 | get error() { | |
| 181 | - if (this[kWrap] === undefined) { | ||
| 182 | - throw new ERR_VM_MODULE_NOT_MODULE(); | ||
| 183 | - } | ||
| 182 | + validateInternalField(this, kWrap, 'Module'); | ||
| 184 | 183 | if (this[kWrap].getStatus() !== kErrored) { | |
| 185 | 184 | throw new ERR_VM_MODULE_STATUS('must be errored'); | |
| 186 | 185 | } | |
| 187 | 186 | return this[kWrap].getError(); | |
| 188 | 187 | } | |
| 189 | 188 | ||
| 190 | 189 | async link(linker) { | |
| 191 | - if (this[kWrap] === undefined) { | ||
| 192 | - throw new ERR_VM_MODULE_NOT_MODULE(); | ||
| 193 | - } | ||
| 190 | + validateInternalField(this, kWrap, 'Module'); | ||
| 194 | 191 | validateFunction(linker, 'linker'); | |
| 195 | 192 | if (this.status === 'linked') { | |
| 196 | 193 | throw new ERR_VM_MODULE_ALREADY_LINKED(); | |
@@ -203,10 +200,7 @@ class Module { | |||
| 203 | 200 | } | |
| 204 | 201 | ||
| 205 | 202 | async evaluate(options = kEmptyObject) { | |
| 206 | - if (this[kWrap] === undefined) { | ||
| 207 | - throw new ERR_VM_MODULE_NOT_MODULE(); | ||
| 208 | - } | ||
| 209 | - | ||
| 203 | + validateInternalField(this, kWrap, 'Module'); | ||
| 210 | 204 | validateObject(options, 'options'); | |
| 211 | 205 | ||
| 212 | 206 | let timeout = options.timeout; | |
@@ -229,9 +223,7 @@ class Module { | |||
| 229 | 223 | } | |
| 230 | 224 | ||
| 231 | 225 | [customInspectSymbol](depth, options) { | |
| 232 | - if (this[kWrap] === undefined) { | ||
| 233 | - throw new ERR_VM_MODULE_NOT_MODULE(); | ||
| 234 | - } | ||
| 226 | + validateInternalField(this, kWrap, 'Module'); | ||
| 235 | 227 | if (typeof depth === 'number' && depth < 0) | |
| 236 | 228 | return this; | |
| 237 | 229 | ||
@@ -306,7 +298,7 @@ class SourceTextModule extends Module { | |||
| 306 | 298 | ||
| 307 | 299 | const promises = this[kWrap].link(async (identifier, attributes) => { | |
| 308 | 300 | const module = await linker(identifier, this, { attributes, assert: attributes }); | |
| 309 | - if (module[kWrap] === undefined) { | ||
| 301 | + if (!isModule(module)) { | ||
| 310 | 302 | throw new ERR_VM_MODULE_NOT_MODULE(); | |
| 311 | 303 | } | |
| 312 | 304 | if (module.context !== this.context) { | |
@@ -337,19 +329,13 @@ class SourceTextModule extends Module { | |||
| 337 | 329 | } | |
| 338 | 330 | ||
| 339 | 331 | get dependencySpecifiers() { | |
| 340 | - if (this[kWrap] === undefined) { | ||
| 341 | - throw new ERR_VM_MODULE_NOT_MODULE(); | ||
| 342 | - } | ||
| 343 | - if (this[kDependencySpecifiers] === undefined) { | ||
| 344 | - this[kDependencySpecifiers] = this[kWrap].getStaticDependencySpecifiers(); | ||
| 345 | - } | ||
| 332 | + validateInternalField(this, kDependencySpecifiers, 'SourceTextModule'); | ||
| 333 | + this[kDependencySpecifiers] ??= this[kWrap].getStaticDependencySpecifiers(); | ||
| 346 | 334 | return this[kDependencySpecifiers]; | |
| 347 | 335 | } | |
| 348 | 336 | ||
| 349 | 337 | get status() { | |
| 350 | - if (this[kWrap] === undefined) { | ||
| 351 | - throw new ERR_VM_MODULE_NOT_MODULE(); | ||
| 352 | - } | ||
| 338 | + validateInternalField(this, kDependencySpecifiers, 'SourceTextModule'); | ||
| 353 | 339 | if (this.#error !== kNoError) { | |
| 354 | 340 | return 'errored'; | |
| 355 | 341 | } | |
@@ -360,9 +346,7 @@ class SourceTextModule extends Module { | |||
| 360 | 346 | } | |
| 361 | 347 | ||
| 362 | 348 | get error() { | |
| 363 | - if (this[kWrap] === undefined) { | ||
| 364 | - throw new ERR_VM_MODULE_NOT_MODULE(); | ||
| 365 | - } | ||
| 349 | + validateInternalField(this, kDependencySpecifiers, 'SourceTextModule'); | ||
| 366 | 350 | if (this.#error !== kNoError) { | |
| 367 | 351 | return this.#error; | |
| 368 | 352 | } | |
@@ -415,9 +399,7 @@ class SyntheticModule extends Module { | |||
| 415 | 399 | } | |
| 416 | 400 | ||
| 417 | 401 | setExport(name, value) { | |
| 418 | - if (this[kWrap] === undefined) { | ||
| 419 | - throw new ERR_VM_MODULE_NOT_MODULE(); | ||
| 420 | - } | ||
| 402 | + validateInternalField(this, kWrap, 'SyntheticModule'); | ||
| 421 | 403 | validateString(name, 'name'); | |
| 422 | 404 | if (this[kWrap].getStatus() < kInstantiated) { | |
| 423 | 405 | throw new ERR_VM_MODULE_STATUS('must be linked'); | |
@@ -432,7 +414,7 @@ function importModuleDynamicallyWrap(importModuleDynamically) { | |||
| 432 | 414 | if (isModuleNamespaceObject(m)) { | |
| 433 | 415 | return m; | |
| 434 | 416 | } | |
| 435 | - if (!m || m[kWrap] === undefined) { | ||
| 417 | + if (!isModule(m)) { | ||
| 436 | 418 | throw new ERR_VM_MODULE_NOT_MODULE(); | |
| 437 | 419 | } | |
| 438 | 420 | if (m.status === 'errored') { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -84,13 +84,15 @@ const util = require('util'); | |||
| 84 | 84 | ||
| 85 | 85 | assert.strictEqual(util.inspect(m, { depth: -1 }), '[SourceTextModule]'); | |
| 86 | 86 | ||
| 87 | - assert.throws( | ||
| 88 | - () => m[util.inspect.custom].call({ __proto__: null }), | ||
| 89 | - { | ||
| 90 | - code: 'ERR_VM_MODULE_NOT_MODULE', | ||
| 91 | - message: 'Provided module is not an instance of Module' | ||
| 92 | - }, | ||
| 93 | - ); | ||
| 87 | + for (const value of [null, { __proto__: null }, SourceTextModule.prototype]) { | ||
| 88 | + assert.throws( | ||
| 89 | + () => m[util.inspect.custom].call(value), | ||
| 90 | + { | ||
| 91 | + code: 'ERR_INVALID_ARG_TYPE', | ||
| 92 | + message: /The "this" argument must be an instance of Module/, | ||
| 93 | + }, | ||
| 94 | + ); | ||
| 95 | + } | ||
| 94 | 96 | } | |
| 95 | 97 | ||
| 96 | 98 | { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -216,8 +216,8 @@ async function checkInvalidOptionForEvaluate() { | |||
| 216 | 216 | await assert.rejects(async () => { | |
| 217 | 217 | await Module.prototype[method](); | |
| 218 | 218 | }, { | |
| 219 | - code: 'ERR_VM_MODULE_NOT_MODULE', | ||
| 220 | - message: /Provided module is not an instance of Module/ | ||
| 219 | + code: 'ERR_INVALID_ARG_TYPE', | ||
| 220 | + message: /The "this" argument must be an instance of Module/ | ||
| 221 | 221 | }); | |
| 222 | 222 | }); | |
| 223 | 223 | } | |
@@ -241,8 +241,8 @@ function checkInvalidCachedData() { | |||
| 241 | 241 | ||
| 242 | 242 | function checkGettersErrors() { | |
| 243 | 243 | const expectedError = { | |
| 244 | - code: 'ERR_VM_MODULE_NOT_MODULE', | ||
| 245 | - message: /Provided module is not an instance of Module/ | ||
| 244 | + code: 'ERR_INVALID_ARG_TYPE', | ||
| 245 | + message: /The "this" argument must be an instance of (?:Module|SourceTextModule)/, | ||
| 246 | 246 | }; | |
| 247 | 247 | const getters = ['identifier', 'context', 'namespace', 'status', 'error']; | |
| 248 | 248 | getters.forEach((getter) => { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -28,6 +28,22 @@ async function simple() { | |||
| 28 | 28 | delete globalThis.fiveResult; | |
| 29 | 29 | } | |
| 30 | 30 | ||
| 31 | + async function invalidLinkValue() { | ||
| 32 | + const invalidValues = [ | ||
| 33 | + undefined, | ||
| 34 | + null, | ||
| 35 | + {}, | ||
| 36 | + SourceTextModule.prototype, | ||
| 37 | + ]; | ||
| 38 | + | ||
| 39 | + for (const value of invalidValues) { | ||
| 40 | + const module = new SourceTextModule('import "foo"'); | ||
| 41 | + await assert.rejects(module.link(() => value), { | ||
| 42 | + code: 'ERR_VM_MODULE_NOT_MODULE', | ||
| 43 | + }); | ||
| 44 | + } | ||
| 45 | + } | ||
| 46 | + | ||
| 31 | 47 | async function depth() { | |
| 32 | 48 | const foo = new SourceTextModule('export default 5'); | |
| 33 | 49 | await foo.link(common.mustNotCall()); | |
@@ -143,6 +159,7 @@ const finished = common.mustCall(); | |||
| 143 | 159 | ||
| 144 | 160 | (async function main() { | |
| 145 | 161 | await simple(); | |
| 162 | + await invalidLinkValue(); | ||
| 146 | 163 | await depth(); | |
| 147 | 164 | await circular(); | |
| 148 | 165 | await circular2(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -66,12 +66,12 @@ const assert = require('assert'); | |||
| 66 | 66 | }); | |
| 67 | 67 | } | |
| 68 | 68 | ||
| 69 | - { | ||
| 69 | + for (const value of [null, {}, SyntheticModule.prototype]) { | ||
| 70 | 70 | assert.throws(() => { | |
| 71 | - SyntheticModule.prototype.setExport.call({}, 'foo'); | ||
| 71 | + SyntheticModule.prototype.setExport.call(value, 'foo'); | ||
| 72 | 72 | }, { | |
| 73 | - code: 'ERR_VM_MODULE_NOT_MODULE', | ||
| 74 | - message: /Provided module is not an instance of Module/ | ||
| 73 | + code: 'ERR_INVALID_ARG_TYPE', | ||
| 74 | + message: /The "this" argument must be an instance of SyntheticModule/ | ||
| 75 | 75 | }); | |
| 76 | 76 | } | |
| 77 | 77 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments