88 ArrayPrototypeSome,
99 ObjectDefineProperty,
1010 ObjectGetPrototypeOf,
11+ ObjectPrototypeHasOwnProperty,
1112 ObjectSetPrototypeOf,
1213 ReflectApply,
1314 SafePromiseAllReturnVoid,
@@ -43,6 +44,7 @@ const {
4344 validateObject,
4445 validateUint32,
4546 validateString,
47+ validateInternalField,
4648} = require ( 'internal/validators' ) ;
4749
4850const binding = internalBinding ( 'module_wrap' ) ;
@@ -75,6 +77,13 @@ const kLink = Symbol('kLink');
7577
7678const { isContext } = require ( 'internal/vm' ) ;
7779
80+ function isModule ( object ) {
81+ if ( typeof object !== 'object' || object === null || ! ObjectPrototypeHasOwnProperty ( object , kWrap ) ) {
82+ return false ;
83+ }
84+ return true ;
85+ }
86+
7887class Module {
7988constructor ( options ) {
8089emitExperimentalWarning ( 'VM Modules' ) ;
@@ -147,50 +156,38 @@ class Module {
147156}
148157
149158get identifier ( ) {
150- if ( this [ kWrap ] === undefined ) {
151- throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
152- }
159+ validateInternalField ( this , kWrap , 'Module' ) ;
153160return this [ kWrap ] . url ;
154161}
155162
156163get context ( ) {
157- if ( this [ kWrap ] === undefined ) {
158- throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
159- }
164+ validateInternalField ( this , kWrap , 'Module' ) ;
160165return this [ kContext ] ;
161166}
162167
163168get namespace ( ) {
164- if ( this [ kWrap ] === undefined ) {
165- throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
166- }
169+ validateInternalField ( this , kWrap , 'Module' ) ;
167170if ( this [ kWrap ] . getStatus ( ) < kInstantiated ) {
168171throw new ERR_VM_MODULE_STATUS ( 'must not be unlinked or linking' ) ;
169172}
170173return this [ kWrap ] . getNamespace ( ) ;
171174}
172175
173176get status ( ) {
174- if ( this [ kWrap ] === undefined ) {
175- throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
176- }
177+ validateInternalField ( this , kWrap , 'Module' ) ;
177178return STATUS_MAP [ this [ kWrap ] . getStatus ( ) ] ;
178179}
179180
180181get error ( ) {
181- if ( this [ kWrap ] === undefined ) {
182- throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
183- }
182+ validateInternalField ( this , kWrap , 'Module' ) ;
184183if ( this [ kWrap ] . getStatus ( ) !== kErrored ) {
185184throw new ERR_VM_MODULE_STATUS ( 'must be errored' ) ;
186185}
187186return this [ kWrap ] . getError ( ) ;
188187}
189188
190189async link ( linker ) {
191- if ( this [ kWrap ] === undefined ) {
192- throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
193- }
190+ validateInternalField ( this , kWrap , 'Module' ) ;
194191validateFunction ( linker , 'linker' ) ;
195192if ( this . status === 'linked' ) {
196193throw new ERR_VM_MODULE_ALREADY_LINKED ( ) ;
@@ -203,10 +200,7 @@ class Module {
203200}
204201
205202async evaluate ( options = kEmptyObject ) {
206- if ( this [ kWrap ] === undefined ) {
207- throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
208- }
209-
203+ validateInternalField ( this , kWrap , 'Module' ) ;
210204validateObject ( options , 'options' ) ;
211205
212206let timeout = options . timeout ;
@@ -229,9 +223,7 @@ class Module {
229223}
230224
231225[ customInspectSymbol ] ( depth , options ) {
232- if ( this [ kWrap ] === undefined ) {
233- throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
234- }
226+ validateInternalField ( this , kWrap , 'Module' ) ;
235227if ( typeof depth === 'number' && depth < 0 )
236228return this ;
237229
@@ -306,7 +298,7 @@ class SourceTextModule extends Module {
306298
307299const promises = this [ kWrap ] . link ( async ( identifier , attributes ) => {
308300const module = await linker ( identifier , this , { attributes, assert : attributes } ) ;
309- if ( module [ kWrap ] === undefined ) {
301+ if ( ! isModule ( module ) ) {
310302throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
311303}
312304if ( module . context !== this . context ) {
@@ -337,19 +329,13 @@ class SourceTextModule extends Module {
337329}
338330
339331get 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 ( ) ;
346334return this [ kDependencySpecifiers ] ;
347335}
348336
349337get status ( ) {
350- if ( this [ kWrap ] === undefined ) {
351- throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
352- }
338+ validateInternalField ( this , kDependencySpecifiers , 'SourceTextModule' ) ;
353339if ( this . #error !== kNoError ) {
354340return 'errored' ;
355341}
@@ -360,9 +346,7 @@ class SourceTextModule extends Module {
360346}
361347
362348get error ( ) {
363- if ( this [ kWrap ] === undefined ) {
364- throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
365- }
349+ validateInternalField ( this , kDependencySpecifiers , 'SourceTextModule' ) ;
366350if ( this . #error !== kNoError ) {
367351return this . #error;
368352}
@@ -415,9 +399,7 @@ class SyntheticModule extends Module {
415399}
416400
417401setExport ( name , value ) {
418- if ( this [ kWrap ] === undefined ) {
419- throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
420- }
402+ validateInternalField ( this , kWrap , 'SyntheticModule' ) ;
421403validateString ( name , 'name' ) ;
422404if ( this [ kWrap ] . getStatus ( ) < kInstantiated ) {
423405throw new ERR_VM_MODULE_STATUS ( 'must be linked' ) ;
@@ -432,7 +414,7 @@ function importModuleDynamicallyWrap(importModuleDynamically) {
432414if ( isModuleNamespaceObject ( m ) ) {
433415return m ;
434416}
435- if ( ! m || m [ kWrap ] === undefined ) {
417+ if ( ! isModule ( m ) ) {
436418throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
437419}
438420if ( m . status === 'errored' ) {
0 commit comments