From f6a1d88c5d1c813c8d55933436b4b834a29f449f Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Wed, 2 Jan 2019 22:14:23 +0100 Subject: [PATCH] src: split `LoadEnvironment()` at `startExecution()` This makes it easier to cater to embedders which wish to skip the `startExecution()` part. PR-URL: https://github.com/nodejs/node/pull/25320 Reviewed-By: Joyee Cheung Reviewed-By: Ben Noordhuis Reviewed-By: Minwoo Jung Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Franziska Hinkelmann --- lib/internal/bootstrap/node.js | 4 +-- src/env-inl.h | 8 +++++ src/env.h | 5 ++++ src/node.cc | 30 +++++++++++++++++-- src/node_internals.h | 3 ++ test/message/assert_throws_stack.out | 1 - test/message/error_exit.out | 1 - test/message/eval_messages.out | 8 ----- .../events_unhandled_error_nexttick.out | 3 -- .../events_unhandled_error_sameline.out | 3 +- test/message/nexttick_throw.out | 2 -- .../unhandled_promise_trace_warnings.out | 6 ---- test/message/util_inspect_error.out | 5 ---- 13 files changed, 47 insertions(+), 32 deletions(-) diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js index 089143e5d01114..e229d2a2c6b246 100644 --- a/lib/internal/bootstrap/node.js +++ b/lib/internal/bootstrap/node.js @@ -303,7 +303,7 @@ function startup() { } = perf.constants; perf.markMilestone(NODE_PERFORMANCE_MILESTONE_BOOTSTRAP_COMPLETE); - startExecution(); + return startExecution; } // There are various modes that Node can run in. The most common two @@ -729,4 +729,4 @@ function checkScriptSyntax(source, filename) { new vm.Script(source, { displayErrors: true, filename }); } -startup(); +return startup(); diff --git a/src/env-inl.h b/src/env-inl.h index e323d8707e676f..0555e79c812051 100644 --- a/src/env-inl.h +++ b/src/env-inl.h @@ -637,6 +637,14 @@ inline void Environment::set_can_call_into_js(bool can_call_into_js) { can_call_into_js_ = can_call_into_js; } +inline bool Environment::has_run_bootstrapping_code() const { + return has_run_bootstrapping_code_; +} + +inline void Environment::set_has_run_bootstrapping_code(bool value) { + has_run_bootstrapping_code_ = value; +} + inline bool Environment::is_main_thread() const { return thread_id_ == 0; } diff --git a/src/env.h b/src/env.h index fbb64f48b537fa..636ff5c8692d2c 100644 --- a/src/env.h +++ b/src/env.h @@ -371,6 +371,7 @@ constexpr size_t kFsStatsBufferLength = kFsStatsFieldsNumber * 2; V(script_data_constructor_function, v8::Function) \ V(secure_context_constructor_template, v8::FunctionTemplate) \ V(shutdown_wrap_template, v8::ObjectTemplate) \ + V(start_execution_function, v8::Function) \ V(tcp_constructor_template, v8::FunctionTemplate) \ V(tick_callback_function, v8::Function) \ V(timers_callback_function, v8::Function) \ @@ -755,6 +756,9 @@ class Environment { inline bool can_call_into_js() const; inline void set_can_call_into_js(bool can_call_into_js); + inline bool has_run_bootstrapping_code() const; + inline void set_has_run_bootstrapping_code(bool has_run_bootstrapping_code); + inline bool is_main_thread() const; inline uint64_t thread_id() const; inline void set_thread_id(uint64_t id); @@ -980,6 +984,7 @@ class Environment { std::unique_ptr performance_state_; std::unordered_map performance_marks_; + bool has_run_bootstrapping_code_ = false; bool can_call_into_js_ = true; uint64_t thread_id_ = 0; std::unordered_set sub_worker_contexts_; diff --git a/src/node.cc b/src/node.cc index 3fb31134b9ba85..cec333bd82c01c 100644 --- a/src/node.cc +++ b/src/node.cc @@ -1085,6 +1085,14 @@ static MaybeLocal ExecuteBootstrapper( } void LoadEnvironment(Environment* env) { + RunBootstrapping(env); + StartExecution(env); +} + +void RunBootstrapping(Environment* env) { + CHECK(!env->has_run_bootstrapping_code()); + env->set_has_run_bootstrapping_code(true); + HandleScope handle_scope(env->isolate()); Isolate* isolate = env->isolate(); Local context = env->context(); @@ -1146,11 +1154,29 @@ void LoadEnvironment(Environment* env) { loader_exports.ToLocalChecked(), Boolean::New(isolate, env->is_main_thread())}; - if (ExecuteBootstrapper( + Local start_execution; + if (!ExecuteBootstrapper( env, "internal/bootstrap/node", &node_params, &node_args) - .IsEmpty()) { + .ToLocal(&start_execution)) { return; } + + if (start_execution->IsFunction()) + env->set_start_execution_function(start_execution.As()); +} + +void StartExecution(Environment* env) { + HandleScope handle_scope(env->isolate()); + // We have to use Local<>::New because of the optimized way in which we access + // the object in the env->...() getters, which does not play well with + // resetting the handle while we're accessing the object through the Local<>. + Local start_execution = + Local::New(env->isolate(), env->start_execution_function()); + env->set_start_execution_function(Local()); + + if (start_execution.IsEmpty()) return; + USE(start_execution->Call( + env->context(), Undefined(env->isolate()), 0, nullptr)); } static void StartInspector(Environment* env, const char* path) { diff --git a/src/node_internals.h b/src/node_internals.h index 897905b5a76a69..acc97633ce9e78 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -724,6 +724,9 @@ bool SafeGetenv(const char* key, std::string* text); void DefineZlibConstants(v8::Local target); +void RunBootstrapping(Environment* env); +void StartExecution(Environment* env); + } // namespace node #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS diff --git a/test/message/assert_throws_stack.out b/test/message/assert_throws_stack.out index 371bd9ff917b86..cf96ee42c98cb7 100644 --- a/test/message/assert_throws_stack.out +++ b/test/message/assert_throws_stack.out @@ -18,4 +18,3 @@ AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal: at * at * at * - at * diff --git a/test/message/error_exit.out b/test/message/error_exit.out index 39692a7b6e4482..6e9b42167dde18 100644 --- a/test/message/error_exit.out +++ b/test/message/error_exit.out @@ -16,4 +16,3 @@ AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: at Function.Module.runMain (internal/modules/cjs/loader.js:*:*) at executeUserCode (internal/bootstrap/node.js:*:*) at startExecution (internal/bootstrap/node.js:*:*) - at startup (internal/bootstrap/node.js:*:*) diff --git a/test/message/eval_messages.out b/test/message/eval_messages.out index 6bfca9ea32d044..f41af4617d9dc0 100644 --- a/test/message/eval_messages.out +++ b/test/message/eval_messages.out @@ -11,8 +11,6 @@ SyntaxError: Strict mode code may not include a with statement at evalScript (internal/process/execution.js:*:*) at executeUserCode (internal/bootstrap/node.js:*:*) at startExecution (internal/bootstrap/node.js:*:*) - at startup (internal/bootstrap/node.js:*:*) - at internal/bootstrap/node.js:*:* 42 42 [eval]:1 @@ -28,8 +26,6 @@ Error: hello at evalScript (internal/process/execution.js:*:*) at executeUserCode (internal/bootstrap/node.js:*:*) at startExecution (internal/bootstrap/node.js:*:*) - at startup (internal/bootstrap/node.js:*:*) - at internal/bootstrap/node.js:*:* [eval]:1 throw new Error("hello") @@ -44,8 +40,6 @@ Error: hello at evalScript (internal/process/execution.js:*:*) at executeUserCode (internal/bootstrap/node.js:*:*) at startExecution (internal/bootstrap/node.js:*:*) - at startup (internal/bootstrap/node.js:*:*) - at internal/bootstrap/node.js:*:* 100 [eval]:1 var x = 100; y = x; @@ -60,8 +54,6 @@ ReferenceError: y is not defined at evalScript (internal/process/execution.js:*:*) at executeUserCode (internal/bootstrap/node.js:*:*) at startExecution (internal/bootstrap/node.js:*:*) - at startup (internal/bootstrap/node.js:*:*) - at internal/bootstrap/node.js:*:* [eval]:1 var ______________________________________________; throw 10 diff --git a/test/message/events_unhandled_error_nexttick.out b/test/message/events_unhandled_error_nexttick.out index aa52367ba09499..cd098b64bfb5f1 100644 --- a/test/message/events_unhandled_error_nexttick.out +++ b/test/message/events_unhandled_error_nexttick.out @@ -12,7 +12,6 @@ Error at Function.Module.runMain (internal/modules/cjs/loader.js:*:*) at executeUserCode (internal/bootstrap/node.js:*:*) at startExecution (internal/bootstrap/node.js:*:*) - at startup (internal/bootstrap/node.js:*:*) Emitted 'error' event at: at process.nextTick (*events_unhandled_error_nexttick.js:*:*) at internalTickCallback (internal/process/next_tick.js:*:*) @@ -20,5 +19,3 @@ Emitted 'error' event at: at Function.Module.runMain (internal/modules/cjs/loader.js:*:*) at executeUserCode (internal/bootstrap/node.js:*:*) at startExecution (internal/bootstrap/node.js:*:*) - at startup (internal/bootstrap/node.js:*:*) - at internal/bootstrap/node.js:*:* diff --git a/test/message/events_unhandled_error_sameline.out b/test/message/events_unhandled_error_sameline.out index cd2b76120326ca..678a9ae4b11d7d 100644 --- a/test/message/events_unhandled_error_sameline.out +++ b/test/message/events_unhandled_error_sameline.out @@ -12,9 +12,8 @@ Error at Function.Module.runMain (internal/modules/cjs/loader.js:*:*) at executeUserCode (internal/bootstrap/node.js:*:*) at startExecution (internal/bootstrap/node.js:*:*) - at startup (internal/bootstrap/node.js:*:*) Emitted 'error' event at: at Object. (*events_unhandled_error_sameline.js:*:*) at Module._compile (internal/modules/cjs/loader.js:*:*) [... lines matching original stack trace ...] - at startup (internal/bootstrap/node.js:*:*) + at startExecution (internal/bootstrap/node.js:*:*) diff --git a/test/message/nexttick_throw.out b/test/message/nexttick_throw.out index 1fee9075fe5078..6a4d7ed2dabcb0 100644 --- a/test/message/nexttick_throw.out +++ b/test/message/nexttick_throw.out @@ -9,5 +9,3 @@ ReferenceError: undefined_reference_error_maker is not defined at Function.Module.runMain (internal/modules/cjs/loader.js:*:*) at executeUserCode (internal/bootstrap/node.js:*:*) at startExecution (internal/bootstrap/node.js:*:*) - at startup (internal/bootstrap/node.js:*:*) - at internal/bootstrap/node.js:*:* diff --git a/test/message/unhandled_promise_trace_warnings.out b/test/message/unhandled_promise_trace_warnings.out index 0b2ef999414a3f..1f88a136a9aa13 100644 --- a/test/message/unhandled_promise_trace_warnings.out +++ b/test/message/unhandled_promise_trace_warnings.out @@ -15,9 +15,6 @@ at * at * at * - at * - at * - at * (node:*) Error: This was rejected at * (*test*message*unhandled_promise_trace_warnings.js:*) at * @@ -28,7 +25,6 @@ at * at * at * - at * (node:*) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. at * at * @@ -38,8 +34,6 @@ at * at * at * - at * - at * (node:*) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously (rejection id: 1) at handledRejection (internal/process/promises.js:*) at promiseRejectHandler (internal/process/promises.js:*) diff --git a/test/message/util_inspect_error.out b/test/message/util_inspect_error.out index 408a3c8baf6d8f..406d8112ce2599 100644 --- a/test/message/util_inspect_error.out +++ b/test/message/util_inspect_error.out @@ -10,7 +10,6 @@ at * at * at * - at * nested: { err: Error: foo @@ -23,7 +22,6 @@ at * at * at * - at * at * } } { err: Error: foo @@ -36,7 +34,6 @@ at * at * at * - at * at *, nested: { err: Error: foo @@ -50,7 +47,6 @@ at * at * at * - at * } } { Error: foo @@ -64,5 +60,4 @@ bar at * at * at * - at * foo: 'bar' }