diff options
author | Joyee Cheung <joyeec9h3@gmail.com> | 2019-11-10 16:59:16 +0800 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2019-11-19 13:43:59 +0100 |
commit | efce655c0f1671d0e86b5c89092ac93db983ef94 (patch) | |
tree | 26af173e687a6c8ee0f26d2857742ffce01729f2 /test | |
parent | 0f78dcc86d9af8f742f76505c5a104c6dff17ca9 (diff) | |
download | node-new-efce655c0f1671d0e86b5c89092ac93db983ef94.tar.gz |
module: reduce circular dependency of internal/modules/cjs/loader
Previously `internal/bootstrap/pre_execution.js` requires
`internal/modules/cjs/loader.js` which in turn requires
`internal/bootstrap/pre_execution.js`. This patch moves the
entry point execution logic out of `pre_execution.js` and
puts it into `internal/modules/run_main.js`. It also tests
that `Module.runMain` can be monkey-patched before further
deprecation/refactoring can be done.
Also added an internal assertion `hasLoadedAnyUserCJSModule`
for documentation purposes.
PR-URL: https://github.com/nodejs/node/pull/30349
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'test')
19 files changed, 47 insertions, 20 deletions
diff --git a/test/fixtures/monkey-patch-run-main.js b/test/fixtures/monkey-patch-run-main.js new file mode 100644 index 0000000000..949a5eca64 --- /dev/null +++ b/test/fixtures/monkey-patch-run-main.js @@ -0,0 +1,8 @@ +'use strict'; + +const oldRunMain = require('module').runMain; + +require('module').runMain = function(...args) { + console.log('runMain is monkey patched!'); + oldRunMain(...args); +}; diff --git a/test/message/core_line_numbers.out b/test/message/core_line_numbers.out index 53d3894825..26f7458953 100644 --- a/test/message/core_line_numbers.out +++ b/test/message/core_line_numbers.out @@ -10,5 +10,5 @@ RangeError: Invalid input at Object.Module._extensions..js (internal/modules/cjs/loader.js:*:*) at Module.load (internal/modules/cjs/loader.js:*:*) at Function.Module._load (internal/modules/cjs/loader.js:*:*) - at Function.Module.runMain (internal/modules/cjs/loader.js:*:*) + at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:*:*) at internal/main/run_main_module.js:*:* diff --git a/test/message/error_exit.out b/test/message/error_exit.out index a0d848b125..19cd11304e 100644 --- a/test/message/error_exit.out +++ b/test/message/error_exit.out @@ -12,7 +12,7 @@ AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: at Object.Module._extensions..js (internal/modules/cjs/loader.js:*:*) at Module.load (internal/modules/cjs/loader.js:*:*) at Function.Module._load (internal/modules/cjs/loader.js:*:*) - at Function.Module.runMain (internal/modules/cjs/loader.js:*:*) + at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:*:*) at internal/main/run_main_module.js:*:* { generatedMessage: true, code: 'ERR_ASSERTION', diff --git a/test/message/esm_loader_not_found.out b/test/message/esm_loader_not_found.out index 87161cdff5..b03b7641af 100644 --- a/test/message/esm_loader_not_found.out +++ b/test/message/esm_loader_not_found.out @@ -11,8 +11,8 @@ Error: Cannot find package 'i-dont-exist' imported from * at Loader.import (internal/modules/esm/loader.js:*:*) at internal/process/esm_loader.js:*:* at Object.initializeLoader (internal/process/esm_loader.js:*:*) - at runMainESM (internal/bootstrap/pre_execution.js:*:*) - at Function.Module.runMain (internal/modules/cjs/loader.js:*:*) + at runMainESM (internal/modules/run_main.js:*:*) + at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:*:*) at internal/main/run_main_module.js:*:* { code: 'ERR_MODULE_NOT_FOUND' } diff --git a/test/message/events_unhandled_error_common_trace.out b/test/message/events_unhandled_error_common_trace.out index 610ea6064d..72dbe47629 100644 --- a/test/message/events_unhandled_error_common_trace.out +++ b/test/message/events_unhandled_error_common_trace.out @@ -10,7 +10,7 @@ Error: foo:bar at Object.Module._extensions..js (internal/modules/cjs/loader.js:*:*) at Module.load (internal/modules/cjs/loader.js:*:*) at Function.Module._load (internal/modules/cjs/loader.js:*:*) - at Function.Module.runMain (internal/modules/cjs/loader.js:*:*) + at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:*:*) at internal/main/run_main_module.js:*:* Emitted 'error' event at: at quux (*events_unhandled_error_common_trace.js:*:*) diff --git a/test/message/events_unhandled_error_nexttick.out b/test/message/events_unhandled_error_nexttick.out index f592d91b52..75eb666222 100644 --- a/test/message/events_unhandled_error_nexttick.out +++ b/test/message/events_unhandled_error_nexttick.out @@ -8,7 +8,7 @@ Error at Object.Module._extensions..js (internal/modules/cjs/loader.js:*:*) at Module.load (internal/modules/cjs/loader.js:*:*) at Function.Module._load (internal/modules/cjs/loader.js:*:*) - at Function.Module.runMain (internal/modules/cjs/loader.js:*:*) + at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:*:*) at internal/main/run_main_module.js:*:* Emitted 'error' event at: at *events_unhandled_error_nexttick.js:*:* diff --git a/test/message/events_unhandled_error_sameline.out b/test/message/events_unhandled_error_sameline.out index 798061a39b..241412d5a8 100644 --- a/test/message/events_unhandled_error_sameline.out +++ b/test/message/events_unhandled_error_sameline.out @@ -8,7 +8,7 @@ Error at Object.Module._extensions..js (internal/modules/cjs/loader.js:*:*) at Module.load (internal/modules/cjs/loader.js:*:*) at Function.Module._load (internal/modules/cjs/loader.js:*:*) - at Function.Module.runMain (internal/modules/cjs/loader.js:*:*) + at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:*:*) at internal/main/run_main_module.js:*:* Emitted 'error' event at: at Object.<anonymous> (*events_unhandled_error_sameline.js:*:*) diff --git a/test/message/events_unhandled_error_subclass.out b/test/message/events_unhandled_error_subclass.out index 970533299a..859bd4980e 100644 --- a/test/message/events_unhandled_error_subclass.out +++ b/test/message/events_unhandled_error_subclass.out @@ -8,7 +8,7 @@ Error at Object.Module._extensions..js (internal/modules/cjs/loader.js:*:*) at Module.load (internal/modules/cjs/loader.js:*:*) at Function.Module._load (internal/modules/cjs/loader.js:*:*) - at Function.Module.runMain (internal/modules/cjs/loader.js:*:*) + at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:*:*) at internal/main/run_main_module.js:*:* Emitted 'error' event on Foo instance at: at Object.<anonymous> (*events_unhandled_error_subclass.js:*:*) diff --git a/test/message/if-error-has-good-stack.out b/test/message/if-error-has-good-stack.out index 87261c451f..24a8f7beb4 100644 --- a/test/message/if-error-has-good-stack.out +++ b/test/message/if-error-has-good-stack.out @@ -15,7 +15,7 @@ AssertionError [ERR_ASSERTION]: ifError got unwanted exception: test error at Object.Module._extensions..js (internal/modules/cjs/loader.js:*:*) at Module.load (internal/modules/cjs/loader.js:*:*) at Function.Module._load (internal/modules/cjs/loader.js:*:*) - at Function.Module.runMain (internal/modules/cjs/loader.js:*:*) + at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:*:*) at internal/main/run_main_module.js:*:* { generatedMessage: false, code: 'ERR_ASSERTION', @@ -28,7 +28,7 @@ AssertionError [ERR_ASSERTION]: ifError got unwanted exception: test error at Object.Module._extensions..js (internal/modules/cjs/loader.js:*:*) at Module.load (internal/modules/cjs/loader.js:*:*) at Function.Module._load (internal/modules/cjs/loader.js:*:*) - at Function.Module.runMain (internal/modules/cjs/loader.js:*:*) + at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:*:*) at internal/main/run_main_module.js:*:* expected: null, operator: 'ifError' diff --git a/test/message/throw_error_with_getter_throw_traced.out b/test/message/throw_error_with_getter_throw_traced.out index 5ea6aa8e26..8f89c09b24 100644 --- a/test/message/throw_error_with_getter_throw_traced.out +++ b/test/message/throw_error_with_getter_throw_traced.out @@ -9,4 +9,4 @@ Thrown at: at Module._extensions..js (internal/modules/cjs/loader.js:*:*) at Module.load (internal/modules/cjs/loader.js:*:*) at Module._load (internal/modules/cjs/loader.js:*:*) - at Module.runMain (internal/modules/cjs/loader.js:*:*) + at executeUserEntryPoint (internal/modules/run_main.js:*:*) diff --git a/test/message/throw_null_traced.out b/test/message/throw_null_traced.out index 44bc34dcbd..887de70ef8 100644 --- a/test/message/throw_null_traced.out +++ b/test/message/throw_null_traced.out @@ -9,4 +9,4 @@ Thrown at: at Module._extensions..js (internal/modules/cjs/loader.js:*:*) at Module.load (internal/modules/cjs/loader.js:*:*) at Module._load (internal/modules/cjs/loader.js:*:*) - at Module.runMain (internal/modules/cjs/loader.js:*:*) + at executeUserEntryPoint (internal/modules/run_main.js:*:*) diff --git a/test/message/throw_undefined_traced.out b/test/message/throw_undefined_traced.out index 070e9a1ec1..81e2e20c5f 100644 --- a/test/message/throw_undefined_traced.out +++ b/test/message/throw_undefined_traced.out @@ -9,4 +9,4 @@ Thrown at: at Module._extensions..js (internal/modules/cjs/loader.js:*:*) at Module.load (internal/modules/cjs/loader.js:*:*) at Module._load (internal/modules/cjs/loader.js:*:*) - at Module.runMain (internal/modules/cjs/loader.js:*:*) + at executeUserEntryPoint (internal/modules/run_main.js:*:*) diff --git a/test/message/undefined_reference_in_new_context.out b/test/message/undefined_reference_in_new_context.out index 9cc5eced7c..77ff35479b 100644 --- a/test/message/undefined_reference_in_new_context.out +++ b/test/message/undefined_reference_in_new_context.out @@ -13,4 +13,4 @@ ReferenceError: foo is not defined at *..js (internal/modules/cjs/loader.js:*) at Module.load (internal/modules/cjs/loader.js:*) at Function.Module._load (internal/modules/cjs/loader.js:*:*) - at Function.Module.runMain (internal/modules/cjs/loader.js:*:*) + at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:*:*) diff --git a/test/message/vm_display_runtime_error.out b/test/message/vm_display_runtime_error.out index 7927510c41..e944f150ad 100644 --- a/test/message/vm_display_runtime_error.out +++ b/test/message/vm_display_runtime_error.out @@ -12,7 +12,7 @@ Error: boo! at Object.Module._extensions..js (internal/modules/cjs/loader.js:*) at Module.load (internal/modules/cjs/loader.js:*) at Function.Module._load (internal/modules/cjs/loader.js:*) - at Function.Module.runMain (internal/modules/cjs/loader.js:*) + at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:*) at internal/main/run_main_module.js:*:* test.vm:1 throw new Error("spooky!") @@ -27,5 +27,5 @@ Error: spooky! at Object.Module._extensions..js (internal/modules/cjs/loader.js:*) at Module.load (internal/modules/cjs/loader.js:*) at Function.Module._load (internal/modules/cjs/loader.js:*) - at Function.Module.runMain (internal/modules/cjs/loader.js:*) + at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:*) at internal/main/run_main_module.js:*:* diff --git a/test/message/vm_display_syntax_error.out b/test/message/vm_display_syntax_error.out index 71d4a0ea13..efffe9eb4b 100644 --- a/test/message/vm_display_syntax_error.out +++ b/test/message/vm_display_syntax_error.out @@ -11,7 +11,7 @@ SyntaxError: Unexpected number at Object.Module._extensions..js (internal/modules/cjs/loader.js:*) at Module.load (internal/modules/cjs/loader.js:*) at Function.Module._load (internal/modules/cjs/loader.js:*) - at Function.Module.runMain (internal/modules/cjs/loader.js:*) + at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:*) at internal/main/run_main_module.js:*:* test.vm:1 var 5; @@ -25,5 +25,5 @@ SyntaxError: Unexpected number at Object.Module._extensions..js (internal/modules/cjs/loader.js:*) at Module.load (internal/modules/cjs/loader.js:*) at Function.Module._load (internal/modules/cjs/loader.js:*) - at Function.Module.runMain (internal/modules/cjs/loader.js:*) + at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:*) at internal/main/run_main_module.js:*:* diff --git a/test/message/vm_dont_display_runtime_error.out b/test/message/vm_dont_display_runtime_error.out index 831ec8b6be..1561d13c82 100644 --- a/test/message/vm_dont_display_runtime_error.out +++ b/test/message/vm_dont_display_runtime_error.out @@ -13,5 +13,5 @@ Error: boo! at Object.Module._extensions..js (internal/modules/cjs/loader.js:*) at Module.load (internal/modules/cjs/loader.js:*) at Function.Module._load (internal/modules/cjs/loader.js:*) - at Function.Module.runMain (internal/modules/cjs/loader.js:*) + at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:*) at internal/main/run_main_module.js:*:* diff --git a/test/message/vm_dont_display_syntax_error.out b/test/message/vm_dont_display_syntax_error.out index 22924e7a6e..5048ad69f5 100644 --- a/test/message/vm_dont_display_syntax_error.out +++ b/test/message/vm_dont_display_syntax_error.out @@ -13,5 +13,5 @@ SyntaxError: Unexpected number at Object.Module._extensions..js (internal/modules/cjs/loader.js:*) at Module.load (internal/modules/cjs/loader.js:*) at Function.Module._load (internal/modules/cjs/loader.js:*) - at Function.Module.runMain (internal/modules/cjs/loader.js:*) + at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:*) at internal/main/run_main_module.js:*:* diff --git a/test/parallel/test-bootstrap-modules.js b/test/parallel/test-bootstrap-modules.js index 40d37c3b8f..55a989a967 100644 --- a/test/parallel/test-bootstrap-modules.js +++ b/test/parallel/test-bootstrap-modules.js @@ -46,6 +46,7 @@ const expectedModules = new Set([ 'NativeModule internal/fs/utils', 'NativeModule internal/idna', 'NativeModule internal/linkedlist', + 'NativeModule internal/modules/run_main', 'NativeModule internal/modules/cjs/helpers', 'NativeModule internal/modules/cjs/loader', 'NativeModule internal/modules/esm/create_dynamic_module', diff --git a/test/parallel/test-module-run-main-monkey-patch.js b/test/parallel/test-module-run-main-monkey-patch.js new file mode 100644 index 0000000000..c9f189abb6 --- /dev/null +++ b/test/parallel/test-module-run-main-monkey-patch.js @@ -0,0 +1,18 @@ +'use strict'; + +// This tests that module.runMain can be monkey patched using --require. +// TODO(joyeecheung): This probably should be deprecated. + +require('../common'); +const { path } = require('../common/fixtures'); +const assert = require('assert'); +const { spawnSync } = require('child_process'); + +const child = spawnSync(process.execPath, [ + '--require', + path('monkey-patch-run-main.js'), + path('semicolon.js'), +]); + +assert.strictEqual(child.status, 0); +assert(child.stdout.toString().includes('runMain is monkey patched!')); |