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