| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 0b67673 commit d1d5da2
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,6 +9,7 @@ const { | |||
| 9 | 9 | ObjectDefineProperty, | |
| 10 | 10 | ObjectFreeze, | |
| 11 | 11 | ObjectGetPrototypeOf, | |
| 12 | + ObjectPrototypeHasOwnProperty, | ||
| 12 | 13 | ObjectSetPrototypeOf, | |
| 13 | 14 | ReflectApply, | |
| 14 | 15 | SafePromiseAllReturnVoid, | |
@@ -44,6 +45,7 @@ const { | |||
| 44 | 45 | validateObject, | |
| 45 | 46 | validateUint32, | |
| 46 | 47 | validateString, | |
| 48 | + validateInternalField, | ||
| 47 | 49 | } = require('internal/validators'); | |
| 48 | 50 | ||
| 49 | 51 | const binding = internalBinding('module_wrap'); | |
@@ -76,6 +78,13 @@ const kLink = Symbol('kLink'); | |||
| 76 | 78 | ||
| 77 | 79 | const { isContext } = require('internal/vm'); | |
| 78 | 80 | ||
| 81 | + function isModule(object) { | ||
| 82 | + if (typeof object !== 'object' || object === null || !ObjectPrototypeHasOwnProperty(object, kWrap)) { | ||
| 83 | + return false; | ||
| 84 | + } | ||
| 85 | + return true; | ||
| 86 | + } | ||
| 87 | + | ||
| 79 | 88 | class Module { | |
| 80 | 89 | constructor(options) { | |
| 81 | 90 | emitExperimentalWarning('VM Modules'); | |
@@ -149,50 +158,38 @@ class Module { | |||
| 149 | 158 | } | |
| 150 | 159 | ||
| 151 | 160 | get identifier() { | |
| 152 | - if (this[kWrap] === undefined) { | ||
| 153 | - throw new ERR_VM_MODULE_NOT_MODULE(); | ||
| 154 | - } | ||
| 161 | + validateInternalField(this, kWrap, 'Module'); | ||
| 155 | 162 | return this[kWrap].url; | |
| 156 | 163 | } | |
| 157 | 164 | ||
| 158 | 165 | get context() { | |
| 159 | - if (this[kWrap] === undefined) { | ||
| 160 | - throw new ERR_VM_MODULE_NOT_MODULE(); | ||
| 161 | - } | ||
| 166 | + validateInternalField(this, kWrap, 'Module'); | ||
| 162 | 167 | return this[kContext]; | |
| 163 | 168 | } | |
| 164 | 169 | ||
| 165 | 170 | get namespace() { | |
| 166 | - if (this[kWrap] === undefined) { | ||
| 167 | - throw new ERR_VM_MODULE_NOT_MODULE(); | ||
| 168 | - } | ||
| 171 | + validateInternalField(this, kWrap, 'Module'); | ||
| 169 | 172 | if (this[kWrap].getStatus() < kInstantiated) { | |
| 170 | 173 | throw new ERR_VM_MODULE_STATUS('must not be unlinked or linking'); | |
| 171 | 174 | } | |
| 172 | 175 | return this[kWrap].getNamespace(); | |
| 173 | 176 | } | |
| 174 | 177 | ||
| 175 | 178 | get status() { | |
| 176 | - if (this[kWrap] === undefined) { | ||
| 177 | - throw new ERR_VM_MODULE_NOT_MODULE(); | ||
| 178 | - } | ||
| 179 | + validateInternalField(this, kWrap, 'Module'); | ||
| 179 | 180 | return STATUS_MAP[this[kWrap].getStatus()]; | |
| 180 | 181 | } | |
| 181 | 182 | ||
| 182 | 183 | get error() { | |
| 183 | - if (this[kWrap] === undefined) { | ||
| 184 | - throw new ERR_VM_MODULE_NOT_MODULE(); | ||
| 185 | - } | ||
| 184 | + validateInternalField(this, kWrap, 'Module'); | ||
| 186 | 185 | if (this[kWrap].getStatus() !== kErrored) { | |
| 187 | 186 | throw new ERR_VM_MODULE_STATUS('must be errored'); | |
| 188 | 187 | } | |
| 189 | 188 | return this[kWrap].getError(); | |
| 190 | 189 | } | |
| 191 | 190 | ||
| 192 | 191 | async link(linker) { | |
| 193 | - if (this[kWrap] === undefined) { | ||
| 194 | - throw new ERR_VM_MODULE_NOT_MODULE(); | ||
| 195 | - } | ||
| 192 | + validateInternalField(this, kWrap, 'Module'); | ||
| 196 | 193 | validateFunction(linker, 'linker'); | |
| 197 | 194 | if (this.status === 'linked') { | |
| 198 | 195 | throw new ERR_VM_MODULE_ALREADY_LINKED(); | |
@@ -205,10 +202,7 @@ class Module { | |||
| 205 | 202 | } | |
| 206 | 203 | ||
| 207 | 204 | async evaluate(options = kEmptyObject) { | |
| 208 | - if (this[kWrap] === undefined) { | ||
| 209 | - throw new ERR_VM_MODULE_NOT_MODULE(); | ||
| 210 | - } | ||
| 211 | - | ||
| 205 | + validateInternalField(this, kWrap, 'Module'); | ||
| 212 | 206 | validateObject(options, 'options'); | |
| 213 | 207 | ||
| 214 | 208 | let timeout = options.timeout; | |
@@ -231,9 +225,7 @@ class Module { | |||
| 231 | 225 | } | |
| 232 | 226 | ||
| 233 | 227 | [customInspectSymbol](depth, options) { | |
| 234 | - if (this[kWrap] === undefined) { | ||
| 235 | - throw new ERR_VM_MODULE_NOT_MODULE(); | ||
| 236 | - } | ||
| 228 | + validateInternalField(this, kWrap, 'Module'); | ||
| 237 | 229 | if (typeof depth === 'number' && depth < 0) | |
| 238 | 230 | return this; | |
| 239 | 231 | ||
@@ -308,7 +300,7 @@ class SourceTextModule extends Module { | |||
| 308 | 300 | ||
| 309 | 301 | const promises = this[kWrap].link(async (identifier, attributes) => { | |
| 310 | 302 | const module = await linker(identifier, this, { attributes, assert: attributes }); | |
| 311 | - if (module[kWrap] === undefined) { | ||
| 303 | + if (!isModule(module)) { | ||
| 312 | 304 | throw new ERR_VM_MODULE_NOT_MODULE(); | |
| 313 | 305 | } | |
| 314 | 306 | if (module.context !== this.context) { | |
@@ -339,17 +331,13 @@ class SourceTextModule extends Module { | |||
| 339 | 331 | } | |
| 340 | 332 | ||
| 341 | 333 | get dependencySpecifiers() { | |
| 342 | - if (this[kWrap] === undefined) { | ||
| 343 | - throw new ERR_VM_MODULE_NOT_MODULE(); | ||
| 344 | - } | ||
| 334 | + validateInternalField(this, kDependencySpecifiers, 'SourceTextModule'); | ||
| 345 | 335 | this[kDependencySpecifiers] ??= ObjectFreeze(this[kWrap].getStaticDependencySpecifiers()); | |
| 346 | 336 | return this[kDependencySpecifiers]; | |
| 347 | 337 | } | |
| 348 | 338 | ||
| 349 | 339 | get status() { | |
| 350 | - if (this[kWrap] === undefined) { | ||
| 351 | - throw new ERR_VM_MODULE_NOT_MODULE(); | ||
| 352 | - } | ||
| 340 | + validateInternalField(this, kDependencySpecifiers, 'SourceTextModule'); | ||
| 353 | 341 | if (this.#error !== kNoError) { | |
| 354 | 342 | return 'errored'; | |
| 355 | 343 | } | |
@@ -360,9 +348,7 @@ class SourceTextModule extends Module { | |||
| 360 | 348 | } | |
| 361 | 349 | ||
| 362 | 350 | get error() { | |
| 363 | - if (this[kWrap] === undefined) { | ||
| 364 | - throw new ERR_VM_MODULE_NOT_MODULE(); | ||
| 365 | - } | ||
| 351 | + validateInternalField(this, kDependencySpecifiers, 'SourceTextModule'); | ||
| 366 | 352 | if (this.#error !== kNoError) { | |
| 367 | 353 | return this.#error; | |
| 368 | 354 | } | |
@@ -415,9 +401,7 @@ class SyntheticModule extends Module { | |||
| 415 | 401 | } | |
| 416 | 402 | ||
| 417 | 403 | setExport(name, value) { | |
| 418 | - if (this[kWrap] === undefined) { | ||
| 419 | - throw new ERR_VM_MODULE_NOT_MODULE(); | ||
| 420 | - } | ||
| 404 | + validateInternalField(this, kWrap, 'SyntheticModule'); | ||
| 421 | 405 | validateString(name, 'name'); | |
| 422 | 406 | if (this[kWrap].getStatus() < kInstantiated) { | |
| 423 | 407 | throw new ERR_VM_MODULE_STATUS('must be linked'); | |
@@ -432,7 +416,7 @@ function importModuleDynamicallyWrap(importModuleDynamically) { | |||
| 432 | 416 | if (isModuleNamespaceObject(m)) { | |
| 433 | 417 | return m; | |
| 434 | 418 | } | |
| 435 | - if (!m || m[kWrap] === undefined) { | ||
| 419 | + if (!isModule(m)) { | ||
| 436 | 420 | throw new ERR_VM_MODULE_NOT_MODULE(); | |
| 437 | 421 | } | |
| 438 | 422 | 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