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