18
18
19
19
const { internalBinding, NativeModule } = loaderExports ;
20
20
21
- const exceptionHandlerState = { captureFn : null } ;
22
21
let getOptionValue ;
23
22
24
23
function startup ( ) {
25
24
setupTraceCategoryState ( ) ;
26
25
27
26
setupProcessObject ( ) ;
28
27
29
- // Do this good and early, since it handles errors.
30
- setupProcessFatal ( ) ;
28
+ // TODO(joyeecheung): this does not have to done so early, any fatal errors
29
+ // thrown before user code execution should simply crash the process
30
+ // and we do not care about any clean up at that point. We don't care
31
+ // about emitting any events if the process crash upon bootstrap either.
32
+ {
33
+ const {
34
+ fatalException,
35
+ setUncaughtExceptionCaptureCallback,
36
+ hasUncaughtExceptionCaptureCallback
37
+ } = NativeModule . require ( 'internal/process/execution' ) ;
38
+
39
+ process . _fatalException = fatalException ;
40
+ process . setUncaughtExceptionCaptureCallback =
41
+ setUncaughtExceptionCaptureCallback ;
42
+ process . hasUncaughtExceptionCaptureCallback =
43
+ hasUncaughtExceptionCaptureCallback ;
44
+ }
31
45
32
46
setupGlobalVariables ( ) ;
33
47
@@ -83,20 +97,14 @@ function startup() {
83
97
process . reallyExit = rawMethods . reallyExit ;
84
98
process . _kill = rawMethods . _kill ;
85
99
86
- const wrapped = perThreadSetup . wrapProcessMethods (
87
- rawMethods , exceptionHandlerState
88
- ) ;
100
+ const wrapped = perThreadSetup . wrapProcessMethods ( rawMethods ) ;
89
101
process . _rawDebug = wrapped . _rawDebug ;
90
102
process . hrtime = wrapped . hrtime ;
91
103
process . hrtime . bigint = wrapped . hrtimeBigInt ;
92
104
process . cpuUsage = wrapped . cpuUsage ;
93
105
process . memoryUsage = wrapped . memoryUsage ;
94
106
process . kill = wrapped . kill ;
95
107
process . exit = wrapped . exit ;
96
- process . setUncaughtExceptionCaptureCallback =
97
- wrapped . setUncaughtExceptionCaptureCallback ;
98
- process . hasUncaughtExceptionCaptureCallback =
99
- wrapped . hasUncaughtExceptionCaptureCallback ;
100
108
}
101
109
102
110
NativeModule . require ( 'internal/process/warning' ) . setup ( ) ;
@@ -305,7 +313,7 @@ function startExecution() {
305
313
// This means we are in a Worker context, and any script execution
306
314
// will be directed by the worker module.
307
315
if ( internalBinding ( 'worker' ) . getEnvMessagePort ( ) !== undefined ) {
308
- NativeModule . require ( 'internal/worker' ) . setupChild ( evalScript ) ;
316
+ NativeModule . require ( 'internal/worker' ) . setupChild ( ) ;
309
317
return ;
310
318
}
311
319
@@ -376,7 +384,9 @@ function executeUserCode() {
376
384
addBuiltinLibsToObject
377
385
} = NativeModule . require ( 'internal/modules/cjs/helpers' ) ;
378
386
addBuiltinLibsToObject ( global ) ;
379
- evalScript ( '[eval]' , wrapForBreakOnFirstLine ( getOptionValue ( '--eval' ) ) ) ;
387
+ const source = getOptionValue ( '--eval' ) ;
388
+ const { evalScript } = NativeModule . require ( 'internal/process/execution' ) ;
389
+ evalScript ( '[eval]' , source , process . _breakFirstLine ) ;
380
390
return ;
381
391
}
382
392
@@ -430,7 +440,8 @@ function executeUserCode() {
430
440
431
441
// User passed '-e' or '--eval' along with `-i` or `--interactive`
432
442
if ( process . _eval != null ) {
433
- evalScript ( '[eval]' , wrapForBreakOnFirstLine ( process . _eval ) ) ;
443
+ const { evalScript } = NativeModule . require ( 'internal/process/execution' ) ;
444
+ evalScript ( '[eval]' , process . _eval , process . _breakFirstLine ) ;
434
445
}
435
446
return ;
436
447
}
@@ -452,7 +463,8 @@ function readAndExecuteStdin() {
452
463
checkScriptSyntax ( code , '[stdin]' ) ;
453
464
} else {
454
465
process . _eval = code ;
455
- evalScript ( '[stdin]' , wrapForBreakOnFirstLine ( process . _eval ) ) ;
466
+ const { evalScript } = NativeModule . require ( 'internal/process/execution' ) ;
467
+ evalScript ( '[stdin]' , process . _eval , process . _breakFirstLine ) ;
456
468
}
457
469
} ) ;
458
470
}
@@ -656,95 +668,6 @@ function setupDOMException() {
656
668
registerDOMException ( DOMException ) ;
657
669
}
658
670
659
- function noop ( ) { }
660
-
661
- function setupProcessFatal ( ) {
662
- const {
663
- executionAsyncId,
664
- clearDefaultTriggerAsyncId,
665
- clearAsyncIdStack,
666
- hasAsyncIdStack,
667
- afterHooksExist,
668
- emitAfter
669
- } = NativeModule . require ( 'internal/async_hooks' ) ;
670
-
671
- process . _fatalException = ( er ) => {
672
- // It's possible that defaultTriggerAsyncId was set for a constructor
673
- // call that threw and was never cleared. So clear it now.
674
- clearDefaultTriggerAsyncId ( ) ;
675
-
676
- if ( exceptionHandlerState . captureFn !== null ) {
677
- exceptionHandlerState . captureFn ( er ) ;
678
- } else if ( ! process . emit ( 'uncaughtException' , er ) ) {
679
- // If someone handled it, then great. otherwise, die in C++ land
680
- // since that means that we'll exit the process, emit the 'exit' event.
681
- try {
682
- if ( ! process . _exiting ) {
683
- process . _exiting = true ;
684
- process . exitCode = 1 ;
685
- process . emit ( 'exit' , 1 ) ;
686
- }
687
- } catch {
688
- // Nothing to be done about it at this point.
689
- }
690
- try {
691
- const { kExpandStackSymbol } = NativeModule . require ( 'internal/util' ) ;
692
- if ( typeof er [ kExpandStackSymbol ] === 'function' )
693
- er [ kExpandStackSymbol ] ( ) ;
694
- } catch {
695
- // Nothing to be done about it at this point.
696
- }
697
- return false ;
698
- }
699
-
700
- // If we handled an error, then make sure any ticks get processed
701
- // by ensuring that the next Immediate cycle isn't empty.
702
- NativeModule . require ( 'timers' ) . setImmediate ( noop ) ;
703
-
704
- // Emit the after() hooks now that the exception has been handled.
705
- if ( afterHooksExist ( ) ) {
706
- do {
707
- emitAfter ( executionAsyncId ( ) ) ;
708
- } while ( hasAsyncIdStack ( ) ) ;
709
- // Or completely empty the id stack.
710
- } else {
711
- clearAsyncIdStack ( ) ;
712
- }
713
-
714
- return true ;
715
- } ;
716
- }
717
-
718
- function wrapForBreakOnFirstLine ( source ) {
719
- if ( ! process . _breakFirstLine )
720
- return source ;
721
- const fn = `function() {\n\n${ source } ;\n\n}` ;
722
- return `process.binding('inspector').callAndPauseOnStart(${ fn } , {})` ;
723
- }
724
-
725
- function evalScript ( name , body ) {
726
- const CJSModule = NativeModule . require ( 'internal/modules/cjs/loader' ) ;
727
- const path = NativeModule . require ( 'path' ) ;
728
- const { tryGetCwd } = NativeModule . require ( 'internal/util' ) ;
729
- const cwd = tryGetCwd ( path ) ;
730
-
731
- const module = new CJSModule ( name ) ;
732
- module . filename = path . join ( cwd , name ) ;
733
- module . paths = CJSModule . _nodeModulePaths ( cwd ) ;
734
- const script = `global.__filename = ${ JSON . stringify ( name ) } ;\n` +
735
- 'global.exports = exports;\n' +
736
- 'global.module = module;\n' +
737
- 'global.__dirname = __dirname;\n' +
738
- 'global.require = require;\n' +
739
- 'return require("vm").runInThisContext(' +
740
- `${ JSON . stringify ( body ) } , { filename: ` +
741
- `${ JSON . stringify ( name ) } , displayErrors: true });\n` ;
742
- const result = module . _compile ( script , `${ name } -wrapper` ) ;
743
- if ( getOptionValue ( '--print' ) ) console . log ( result ) ;
744
- // Handle any nextTicks added in the first tick of the program.
745
- process . _tickCallback ( ) ;
746
- }
747
-
748
671
function checkScriptSyntax ( source , filename ) {
749
672
const CJSModule = NativeModule . require ( 'internal/modules/cjs/loader' ) ;
750
673
const vm = NativeModule . require ( 'vm' ) ;
0 commit comments