99 ObjectDefineProperty,
1010 ObjectFreeze,
1111 ObjectGetPrototypeOf,
12+ ObjectPrototypeHasOwnProperty,
1213 ObjectSetPrototypeOf,
1314 ReflectApply,
1415 SafePromiseAllReturnVoid,
@@ -44,6 +45,7 @@ const {
4445 validateObject,
4546 validateUint32,
4647 validateString,
48+ validateInternalField,
4749} = require ( 'internal/validators' ) ;
4850
4951const binding = internalBinding ( 'module_wrap' ) ;
@@ -76,6 +78,13 @@ const kLink = Symbol('kLink');
7678
7779const { isContext } = require ( 'internal/vm' ) ;
7880
81+ function isModule ( object ) {
82+ if ( typeof object !== 'object' || object === null || ! ObjectPrototypeHasOwnProperty ( object , kWrap ) ) {
83+ return false ;
84+ }
85+ return true ;
86+ }
87+
7988class Module {
8089 constructor ( options ) {
8190 emitExperimentalWarning ( 'VM Modules' ) ;
@@ -149,50 +158,38 @@ class Module {
149158 }
150159
151160 get identifier ( ) {
152- if ( this [ kWrap ] === undefined ) {
153- throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
154- }
161+ validateInternalField ( this , kWrap , 'Module' ) ;
155162 return this [ kWrap ] . url ;
156163 }
157164
158165 get context ( ) {
159- if ( this [ kWrap ] === undefined ) {
160- throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
161- }
166+ validateInternalField ( this , kWrap , 'Module' ) ;
162167 return this [ kContext ] ;
163168 }
164169
165170 get namespace ( ) {
166- if ( this [ kWrap ] === undefined ) {
167- throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
168- }
171+ validateInternalField ( this , kWrap , 'Module' ) ;
169172 if ( this [ kWrap ] . getStatus ( ) < kInstantiated ) {
170173 throw new ERR_VM_MODULE_STATUS ( 'must not be unlinked or linking' ) ;
171174 }
172175 return this [ kWrap ] . getNamespace ( ) ;
173176 }
174177
175178 get status ( ) {
176- if ( this [ kWrap ] === undefined ) {
177- throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
178- }
179+ validateInternalField ( this , kWrap , 'Module' ) ;
179180 return STATUS_MAP [ this [ kWrap ] . getStatus ( ) ] ;
180181 }
181182
182183 get error ( ) {
183- if ( this [ kWrap ] === undefined ) {
184- throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
185- }
184+ validateInternalField ( this , kWrap , 'Module' ) ;
186185 if ( this [ kWrap ] . getStatus ( ) !== kErrored ) {
187186 throw new ERR_VM_MODULE_STATUS ( 'must be errored' ) ;
188187 }
189188 return this [ kWrap ] . getError ( ) ;
190189 }
191190
192191 async link ( linker ) {
193- if ( this [ kWrap ] === undefined ) {
194- throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
195- }
192+ validateInternalField ( this , kWrap , 'Module' ) ;
196193 validateFunction ( linker , 'linker' ) ;
197194 if ( this . status === 'linked' ) {
198195 throw new ERR_VM_MODULE_ALREADY_LINKED ( ) ;
@@ -205,10 +202,7 @@ class Module {
205202 }
206203
207204 async evaluate ( options = kEmptyObject ) {
208- if ( this [ kWrap ] === undefined ) {
209- throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
210- }
211-
205+ validateInternalField ( this , kWrap , 'Module' ) ;
212206 validateObject ( options , 'options' ) ;
213207
214208 let timeout = options . timeout ;
@@ -231,9 +225,7 @@ class Module {
231225 }
232226
233227 [ customInspectSymbol ] ( depth , options ) {
234- if ( this [ kWrap ] === undefined ) {
235- throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
236- }
228+ validateInternalField ( this , kWrap , 'Module' ) ;
237229 if ( typeof depth === 'number' && depth < 0 )
238230 return this ;
239231
@@ -308,7 +300,7 @@ class SourceTextModule extends Module {
308300
309301 const promises = this [ kWrap ] . link ( async ( identifier , attributes ) => {
310302 const module = await linker ( identifier , this , { attributes, assert : attributes } ) ;
311- if ( module [ kWrap ] === undefined ) {
303+ if ( ! isModule ( module ) ) {
312304 throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
313305 }
314306 if ( module . context !== this . context ) {
@@ -339,17 +331,13 @@ class SourceTextModule extends Module {
339331 }
340332
341333 get dependencySpecifiers ( ) {
342- if ( this [ kWrap ] === undefined ) {
343- throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
344- }
334+ validateInternalField ( this , kDependencySpecifiers , 'SourceTextModule' ) ;
345335 this [ kDependencySpecifiers ] ??= ObjectFreeze ( this [ kWrap ] . getStaticDependencySpecifiers ( ) ) ;
346336 return this [ kDependencySpecifiers ] ;
347337 }
348338
349339 get status ( ) {
350- if ( this [ kWrap ] === undefined ) {
351- throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
352- }
340+ validateInternalField ( this , kDependencySpecifiers , 'SourceTextModule' ) ;
353341 if ( this . #error !== kNoError ) {
354342 return 'errored' ;
355343 }
@@ -360,9 +348,7 @@ class SourceTextModule extends Module {
360348 }
361349
362350 get error ( ) {
363- if ( this [ kWrap ] === undefined ) {
364- throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
365- }
351+ validateInternalField ( this , kDependencySpecifiers , 'SourceTextModule' ) ;
366352 if ( this . #error !== kNoError ) {
367353 return this . #error;
368354 }
@@ -415,9 +401,7 @@ class SyntheticModule extends Module {
415401 }
416402
417403 setExport ( name , value ) {
418- if ( this [ kWrap ] === undefined ) {
419- throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
420- }
404+ validateInternalField ( this , kWrap , 'SyntheticModule' ) ;
421405 validateString ( name , 'name' ) ;
422406 if ( this [ kWrap ] . getStatus ( ) < kInstantiated ) {
423407 throw new ERR_VM_MODULE_STATUS ( 'must be linked' ) ;
@@ -432,7 +416,7 @@ function importModuleDynamicallyWrap(importModuleDynamically) {
432416 if ( isModuleNamespaceObject ( m ) ) {
433417 return m ;
434418 }
435- if ( ! m || m [ kWrap ] === undefined ) {
419+ if ( ! isModule ( m ) ) {
436420 throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
437421 }
438422 if ( m . status === 'errored' ) {
0 commit comments