@@ -182,11 +182,34 @@ if (config.hasInspector) {
182
182
183
183
const browserGlobals = ! process . _noBrowserGlobals ;
184
184
if ( browserGlobals ) {
185
- setupGlobalTimeouts ( ) ;
186
- setupGlobalConsole ( ) ;
187
- setupGlobalURL ( ) ;
188
- setupGlobalEncoding ( ) ;
185
+ // Override global console from the one provided by the VM
186
+ // to the one implemented by Node.js
187
+ // https://console.spec.whatwg.org/#console-namespace
188
+ exposeNamespace ( global , 'console' , createGlobalConsole ( global . console ) ) ;
189
+
190
+ const { URL , URLSearchParams } = NativeModule . require ( 'internal/url' ) ;
191
+ // https://url.spec.whatwg.org/#url
192
+ exposeInterface ( global , 'URL' , URL ) ;
193
+ // https://url.spec.whatwg.org/#urlsearchparams
194
+ exposeInterface ( global , 'URLSearchParams' , URLSearchParams ) ;
195
+
196
+ const { TextEncoder, TextDecoder } = NativeModule . require ( 'util' ) ;
197
+ // https://encoding.spec.whatwg.org/#textencoder
198
+ exposeInterface ( global , 'TextEncoder' , TextEncoder ) ;
199
+ // https://encoding.spec.whatwg.org/#textdecoder
200
+ exposeInterface ( global , 'TextDecoder' , TextDecoder ) ;
201
+
202
+ // https://html.spec.whatwg.org/multipage/webappapis.html#windoworworkerglobalscope
203
+ const timers = NativeModule . require ( 'timers' ) ;
204
+ defineOperation ( global , 'clearInterval' , timers . clearInterval ) ;
205
+ defineOperation ( global , 'clearTimeout' , timers . clearTimeout ) ;
206
+ defineOperation ( global , 'setInterval' , timers . setInterval ) ;
207
+ defineOperation ( global , 'setTimeout' , timers . setTimeout ) ;
189
208
setupQueueMicrotask ( ) ;
209
+
210
+ // Non-standard extensions:
211
+ defineOperation ( global , 'clearImmediate' , timers . clearImmediate ) ;
212
+ defineOperation ( global , 'setImmediate' , timers . setImmediate ) ;
190
213
}
191
214
192
215
setupDOMException ( ) ;
@@ -404,29 +427,9 @@ function setupBuffer() {
404
427
global . Buffer = Buffer ;
405
428
}
406
429
407
- function setupGlobalTimeouts ( ) {
408
- const timers = NativeModule . require ( 'timers' ) ;
409
- global . clearImmediate = timers . clearImmediate ;
410
- global . clearInterval = timers . clearInterval ;
411
- global . clearTimeout = timers . clearTimeout ;
412
- global . setImmediate = timers . setImmediate ;
413
- global . setInterval = timers . setInterval ;
414
- global . setTimeout = timers . setTimeout ;
415
- }
416
-
417
- function setupGlobalConsole ( ) {
418
- const consoleFromVM = global . console ;
430
+ function createGlobalConsole ( consoleFromVM ) {
419
431
const consoleFromNode =
420
432
NativeModule . require ( 'internal/console/global' ) ;
421
- // Override global console from the one provided by the VM
422
- // to the one implemented by Node.js
423
- Object . defineProperty ( global , 'console' , {
424
- configurable : true ,
425
- enumerable : false ,
426
- value : consoleFromNode ,
427
- writable : true
428
- } ) ;
429
-
430
433
if ( config . hasInspector ) {
431
434
const inspector = NativeModule . require ( 'internal/util/inspector' ) ;
432
435
// This will be exposed by `require('inspector').console` later.
@@ -438,42 +441,7 @@ function setupGlobalConsole() {
438
441
// Setup inspector command line API.
439
442
setConsoleExtensionInstaller ( inspector . installConsoleExtensions ) ;
440
443
}
441
- }
442
-
443
- function setupGlobalURL ( ) {
444
- const { URL , URLSearchParams } = NativeModule . require ( 'internal/url' ) ;
445
- Object . defineProperties ( global , {
446
- URL : {
447
- value : URL ,
448
- writable : true ,
449
- configurable : true ,
450
- enumerable : false
451
- } ,
452
- URLSearchParams : {
453
- value : URLSearchParams ,
454
- writable : true ,
455
- configurable : true ,
456
- enumerable : false
457
- }
458
- } ) ;
459
- }
460
-
461
- function setupGlobalEncoding ( ) {
462
- const { TextEncoder, TextDecoder } = NativeModule . require ( 'util' ) ;
463
- Object . defineProperties ( global , {
464
- TextEncoder : {
465
- value : TextEncoder ,
466
- writable : true ,
467
- configurable : true ,
468
- enumerable : false
469
- } ,
470
- TextDecoder : {
471
- value : TextDecoder ,
472
- writable : true ,
473
- configurable : true ,
474
- enumerable : false
475
- }
476
- } ) ;
444
+ return consoleFromNode ;
477
445
}
478
446
479
447
function setupQueueMicrotask ( ) {
@@ -513,3 +481,33 @@ function setupDOMException() {
513
481
}
514
482
515
483
function noop ( ) { }
484
+
485
+ // https://heycam.github.io/webidl/#es-namespaces
486
+ function exposeNamespace ( target , name , namespaceObject ) {
487
+ Object . defineProperty ( target , name , {
488
+ writable : true ,
489
+ enumerable : false ,
490
+ configurable : true ,
491
+ value : namespaceObject
492
+ } ) ;
493
+ }
494
+
495
+ // https://heycam.github.io/webidl/#es-interfaces
496
+ function exposeInterface ( target , name , interfaceObject ) {
497
+ Object . defineProperty ( target , name , {
498
+ writable : true ,
499
+ enumerable : false ,
500
+ configurable : true ,
501
+ value : interfaceObject
502
+ } ) ;
503
+ }
504
+
505
+ // https://heycam.github.io/webidl/#define-the-operations
506
+ function defineOperation ( target , name , method ) {
507
+ Object . defineProperty ( target , name , {
508
+ writable : true ,
509
+ enumerable : true ,
510
+ configurable : true ,
511
+ value : method
512
+ } ) ;
513
+ }
0 commit comments