77 ArrayPrototypeIndexOf,
88 ArrayPrototypeSome,
99 ObjectDefineProperty,
10+ ObjectFreeze,
1011 ObjectGetPrototypeOf,
12+ ObjectPrototypeHasOwnProperty,
1113 ObjectSetPrototypeOf,
1214 ReflectApply,
1315 SafePromiseAllReturnVoid,
@@ -43,6 +45,7 @@ const {
4345 validateObject,
4446 validateUint32,
4547 validateString,
48+ validateInternalField,
4649} = require ( 'internal/validators' ) ;
4750
4851const binding = internalBinding ( 'module_wrap' ) ;
@@ -75,6 +78,13 @@ const kLink = Symbol('kLink');
7578
7679const { isContext } = require ( 'internal/vm' ) ;
7780
81+ function isModule ( object ) {
82+ if ( typeof object !== 'object' || object === null || ! ObjectPrototypeHasOwnProperty ( object , kWrap ) ) {
83+ return false ;
84+ }
85+ return true ;
86+ }
87+
7888class Module {
7989 constructor ( options ) {
8090 emitExperimentalWarning ( 'VM Modules' ) ;
@@ -148,50 +158,38 @@ class Module {
148158 }
149159
150160 get identifier ( ) {
151- if ( this [ kWrap ] === undefined ) {
152- throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
153- }
161+ validateInternalField ( this , kWrap , 'Module' ) ;
154162 return this [ kWrap ] . url ;
155163 }
156164
157165 get context ( ) {
158- if ( this [ kWrap ] === undefined ) {
159- throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
160- }
166+ validateInternalField ( this , kWrap , 'Module' ) ;
161167 return this [ kContext ] ;
162168 }
163169
164170 get namespace ( ) {
165- if ( this [ kWrap ] === undefined ) {
166- throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
167- }
171+ validateInternalField ( this , kWrap , 'Module' ) ;
168172 if ( this [ kWrap ] . getStatus ( ) < kInstantiated ) {
169173 throw new ERR_VM_MODULE_STATUS ( 'must not be unlinked or linking' ) ;
170174 }
171175 return this [ kWrap ] . getNamespace ( ) ;
172176 }
173177
174178 get status ( ) {
175- if ( this [ kWrap ] === undefined ) {
176- throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
177- }
179+ validateInternalField ( this , kWrap , 'Module' ) ;
178180 return STATUS_MAP [ this [ kWrap ] . getStatus ( ) ] ;
179181 }
180182
181183 get error ( ) {
182- if ( this [ kWrap ] === undefined ) {
183- throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
184- }
184+ validateInternalField ( this , kWrap , 'Module' ) ;
185185 if ( this [ kWrap ] . getStatus ( ) !== kErrored ) {
186186 throw new ERR_VM_MODULE_STATUS ( 'must be errored' ) ;
187187 }
188188 return this [ kWrap ] . getError ( ) ;
189189 }
190190
191191 async link ( linker ) {
192- if ( this [ kWrap ] === undefined ) {
193- throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
194- }
192+ validateInternalField ( this , kWrap , 'Module' ) ;
195193 validateFunction ( linker , 'linker' ) ;
196194 if ( this . status === 'linked' ) {
197195 throw new ERR_VM_MODULE_ALREADY_LINKED ( ) ;
@@ -204,10 +202,7 @@ class Module {
204202 }
205203
206204 async evaluate ( options = kEmptyObject ) {
207- if ( this [ kWrap ] === undefined ) {
208- throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
209- }
210-
205+ validateInternalField ( this , kWrap , 'Module' ) ;
211206 validateObject ( options , 'options' ) ;
212207
213208 let timeout = options . timeout ;
@@ -230,9 +225,7 @@ class Module {
230225 }
231226
232227 [ customInspectSymbol ] ( depth , options ) {
233- if ( this [ kWrap ] === undefined ) {
234- throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
235- }
228+ validateInternalField ( this , kWrap , 'Module' ) ;
236229 if ( typeof depth === 'number' && depth < 0 )
237230 return this ;
238231
@@ -307,7 +300,7 @@ class SourceTextModule extends Module {
307300
308301 const promises = this [ kWrap ] . link ( async ( identifier , attributes ) => {
309302 const module = await linker ( identifier , this , { attributes, assert : attributes } ) ;
310- if ( module [ kWrap ] === undefined ) {
303+ if ( ! isModule ( module ) ) {
311304 throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
312305 }
313306 if ( module . context !== this . context ) {
@@ -338,19 +331,13 @@ class SourceTextModule extends Module {
338331 }
339332
340333 get dependencySpecifiers ( ) {
341- if ( this [ kWrap ] === undefined ) {
342- throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
343- }
344- if ( this [ kDependencySpecifiers ] === undefined ) {
345- this [ kDependencySpecifiers ] = this [ kWrap ] . getStaticDependencySpecifiers ( ) ;
346- }
334+ validateInternalField ( this , kDependencySpecifiers , 'SourceTextModule' ) ;
335+ this [ kDependencySpecifiers ] ??= ObjectFreeze ( this [ kWrap ] . getStaticDependencySpecifiers ( ) ) ;
347336 return this [ kDependencySpecifiers ] ;
348337 }
349338
350339 get status ( ) {
351- if ( this [ kWrap ] === undefined ) {
352- throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
353- }
340+ validateInternalField ( this , kDependencySpecifiers , 'SourceTextModule' ) ;
354341 if ( this . #error !== kNoError ) {
355342 return 'errored' ;
356343 }
@@ -361,9 +348,7 @@ class SourceTextModule extends Module {
361348 }
362349
363350 get error ( ) {
364- if ( this [ kWrap ] === undefined ) {
365- throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
366- }
351+ validateInternalField ( this , kDependencySpecifiers , 'SourceTextModule' ) ;
367352 if ( this . #error !== kNoError ) {
368353 return this . #error;
369354 }
@@ -416,9 +401,7 @@ class SyntheticModule extends Module {
416401 }
417402
418403 setExport ( name , value ) {
419- if ( this [ kWrap ] === undefined ) {
420- throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
421- }
404+ validateInternalField ( this , kWrap , 'SyntheticModule' ) ;
422405 validateString ( name , 'name' ) ;
423406 if ( this [ kWrap ] . getStatus ( ) < kInstantiated ) {
424407 throw new ERR_VM_MODULE_STATUS ( 'must be linked' ) ;
@@ -433,7 +416,7 @@ function importModuleDynamicallyWrap(importModuleDynamically) {
433416 if ( isModuleNamespaceObject ( m ) ) {
434417 return m ;
435418 }
436- if ( ! m || m [ kWrap ] === undefined ) {
419+ if ( ! isModule ( m ) ) {
437420 throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
438421 }
439422 if ( m . status === 'errored' ) {
0 commit comments