|
121 | 121 |
|
122 | 122 | const browserGlobals = !process._noBrowserGlobals; |
123 | 123 | if (browserGlobals) { |
124 | | - // we are setting this here to forward it to the inspector later |
125 | | - perThreadSetup.originalConsole = global.console; |
126 | 124 | setupGlobalTimeouts(); |
127 | 125 | setupGlobalConsole(); |
128 | 126 | setupGlobalURL(); |
|
487 | 485 | } |
488 | 486 |
|
489 | 487 | function setupGlobalConsole() { |
490 | | - const originalConsole = global.console; |
491 | | - // Setup Node.js global.console. |
492 | | - const wrappedConsole = NativeModule.require('console'); |
| 488 | + const consoleFromVM = global.console; |
| 489 | + const consoleFromNode = |
| 490 | + NativeModule.require('internal/console/global'); |
| 491 | + // Override global console from the one provided by the VM |
| 492 | + // to the one implemented by Node.js |
493 | 493 | Object.defineProperty(global, 'console', { |
494 | 494 | configurable: true, |
495 | 495 | enumerable: false, |
496 | | - value: wrappedConsole, |
| 496 | + value: consoleFromNode, |
497 | 497 | writable: true |
498 | 498 | }); |
499 | | - setupInspector(originalConsole, wrappedConsole); |
| 499 | + // TODO(joyeecheung): can we skip this if inspector is not active? |
| 500 | + if (process.config.variables.v8_enable_inspector) { |
| 501 | + const inspector = |
| 502 | + NativeModule.require('internal/console/inspector'); |
| 503 | + inspector.addInspectorApis(consoleFromNode, consoleFromVM); |
| 504 | + // This will be exposed by `require('inspector').console` later. |
| 505 | + inspector.consoleFromVM = consoleFromVM; |
| 506 | + } |
500 | 507 | } |
501 | 508 |
|
502 | 509 | function setupGlobalURL() { |
|
571 | 578 | registerDOMException(DOMException); |
572 | 579 | } |
573 | 580 |
|
574 | | - function setupInspector(originalConsole, wrappedConsole) { |
575 | | - if (!process.config.variables.v8_enable_inspector) { |
576 | | - return; |
577 | | - } |
578 | | - const CJSModule = NativeModule.require('internal/modules/cjs/loader'); |
579 | | - const { addCommandLineAPI, consoleCall } = process.binding('inspector'); |
580 | | - // Setup inspector command line API. |
581 | | - const { makeRequireFunction } = |
582 | | - NativeModule.require('internal/modules/cjs/helpers'); |
583 | | - const path = NativeModule.require('path'); |
584 | | - const cwd = tryGetCwd(path); |
585 | | - |
586 | | - const consoleAPIModule = new CJSModule('<inspector console>'); |
587 | | - consoleAPIModule.paths = |
588 | | - CJSModule._nodeModulePaths(cwd).concat(CJSModule.globalPaths); |
589 | | - addCommandLineAPI('require', makeRequireFunction(consoleAPIModule)); |
590 | | - const config = {}; |
591 | | - for (const key of Object.keys(wrappedConsole)) { |
592 | | - if (!originalConsole.hasOwnProperty(key)) |
593 | | - continue; |
594 | | - // If global console has the same method as inspector console, |
595 | | - // then wrap these two methods into one. Native wrapper will preserve |
596 | | - // the original stack. |
597 | | - wrappedConsole[key] = consoleCall.bind(wrappedConsole, |
598 | | - originalConsole[key], |
599 | | - wrappedConsole[key], |
600 | | - config); |
601 | | - } |
602 | | - for (const key of Object.keys(originalConsole)) { |
603 | | - if (wrappedConsole.hasOwnProperty(key)) |
604 | | - continue; |
605 | | - wrappedConsole[key] = originalConsole[key]; |
606 | | - } |
607 | | - } |
608 | | - |
609 | 581 | function noop() {} |
610 | 582 |
|
611 | 583 | function setupProcessFatal() { |
|
684 | 656 | } |
685 | 657 | } |
686 | 658 |
|
687 | | - function tryGetCwd(path) { |
688 | | - try { |
689 | | - return process.cwd(); |
690 | | - } catch { |
691 | | - // getcwd(3) can fail if the current working directory has been deleted. |
692 | | - // Fall back to the directory name of the (absolute) executable path. |
693 | | - // It's not really correct but what are the alternatives? |
694 | | - return path.dirname(process.execPath); |
695 | | - } |
696 | | - } |
697 | | - |
698 | 659 | function wrapForBreakOnFirstLine(source) { |
699 | 660 | if (!process._breakFirstLine) |
700 | 661 | return source; |
|
705 | 666 | function evalScript(name, body) { |
706 | 667 | const CJSModule = NativeModule.require('internal/modules/cjs/loader'); |
707 | 668 | const path = NativeModule.require('path'); |
| 669 | + const { tryGetCwd } = NativeModule.require('internal/util'); |
708 | 670 | const cwd = tryGetCwd(path); |
709 | 671 |
|
710 | 672 | const module = new CJSModule(name); |
|
0 commit comments