Skip to content

Commit f6a1d88

Browse files
committed
src: split LoadEnvironment() at startExecution()
This makes it easier to cater to embedders which wish to skip the `startExecution()` part. PR-URL: #25320 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Minwoo Jung <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Franziska Hinkelmann <[email protected]>
1 parent 728505a commit f6a1d88

13 files changed

+47
-32
lines changed

lib/internal/bootstrap/node.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ function startup() {
303303
} = perf.constants;
304304
perf.markMilestone(NODE_PERFORMANCE_MILESTONE_BOOTSTRAP_COMPLETE);
305305

306-
startExecution();
306+
return startExecution;
307307
}
308308

309309
// There are various modes that Node can run in. The most common two
@@ -729,4 +729,4 @@ function checkScriptSyntax(source, filename) {
729729
new vm.Script(source, { displayErrors: true, filename });
730730
}
731731

732-
startup();
732+
return startup();

src/env-inl.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,14 @@ inline void Environment::set_can_call_into_js(bool can_call_into_js) {
637637
can_call_into_js_ = can_call_into_js;
638638
}
639639

640+
inline bool Environment::has_run_bootstrapping_code() const {
641+
return has_run_bootstrapping_code_;
642+
}
643+
644+
inline void Environment::set_has_run_bootstrapping_code(bool value) {
645+
has_run_bootstrapping_code_ = value;
646+
}
647+
640648
inline bool Environment::is_main_thread() const {
641649
return thread_id_ == 0;
642650
}

src/env.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ constexpr size_t kFsStatsBufferLength = kFsStatsFieldsNumber * 2;
371371
V(script_data_constructor_function, v8::Function) \
372372
V(secure_context_constructor_template, v8::FunctionTemplate) \
373373
V(shutdown_wrap_template, v8::ObjectTemplate) \
374+
V(start_execution_function, v8::Function) \
374375
V(tcp_constructor_template, v8::FunctionTemplate) \
375376
V(tick_callback_function, v8::Function) \
376377
V(timers_callback_function, v8::Function) \
@@ -755,6 +756,9 @@ class Environment {
755756
inline bool can_call_into_js() const;
756757
inline void set_can_call_into_js(bool can_call_into_js);
757758

759+
inline bool has_run_bootstrapping_code() const;
760+
inline void set_has_run_bootstrapping_code(bool has_run_bootstrapping_code);
761+
758762
inline bool is_main_thread() const;
759763
inline uint64_t thread_id() const;
760764
inline void set_thread_id(uint64_t id);
@@ -980,6 +984,7 @@ class Environment {
980984
std::unique_ptr<performance::performance_state> performance_state_;
981985
std::unordered_map<std::string, uint64_t> performance_marks_;
982986

987+
bool has_run_bootstrapping_code_ = false;
983988
bool can_call_into_js_ = true;
984989
uint64_t thread_id_ = 0;
985990
std::unordered_set<worker::Worker*> sub_worker_contexts_;

src/node.cc

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,14 @@ static MaybeLocal<Value> ExecuteBootstrapper(
10851085
}
10861086

10871087
void LoadEnvironment(Environment* env) {
1088+
RunBootstrapping(env);
1089+
StartExecution(env);
1090+
}
1091+
1092+
void RunBootstrapping(Environment* env) {
1093+
CHECK(!env->has_run_bootstrapping_code());
1094+
env->set_has_run_bootstrapping_code(true);
1095+
10881096
HandleScope handle_scope(env->isolate());
10891097
Isolate* isolate = env->isolate();
10901098
Local<Context> context = env->context();
@@ -1146,11 +1154,29 @@ void LoadEnvironment(Environment* env) {
11461154
loader_exports.ToLocalChecked(),
11471155
Boolean::New(isolate, env->is_main_thread())};
11481156

1149-
if (ExecuteBootstrapper(
1157+
Local<Value> start_execution;
1158+
if (!ExecuteBootstrapper(
11501159
env, "internal/bootstrap/node", &node_params, &node_args)
1151-
.IsEmpty()) {
1160+
.ToLocal(&start_execution)) {
11521161
return;
11531162
}
1163+
1164+
if (start_execution->IsFunction())
1165+
env->set_start_execution_function(start_execution.As<Function>());
1166+
}
1167+
1168+
void StartExecution(Environment* env) {
1169+
HandleScope handle_scope(env->isolate());
1170+
// We have to use Local<>::New because of the optimized way in which we access
1171+
// the object in the env->...() getters, which does not play well with
1172+
// resetting the handle while we're accessing the object through the Local<>.
1173+
Local<Function> start_execution =
1174+
Local<Function>::New(env->isolate(), env->start_execution_function());
1175+
env->set_start_execution_function(Local<Function>());
1176+
1177+
if (start_execution.IsEmpty()) return;
1178+
USE(start_execution->Call(
1179+
env->context(), Undefined(env->isolate()), 0, nullptr));
11541180
}
11551181

11561182
static void StartInspector(Environment* env, const char* path) {

src/node_internals.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,9 @@ bool SafeGetenv(const char* key, std::string* text);
724724

725725
void DefineZlibConstants(v8::Local<v8::Object> target);
726726

727+
void RunBootstrapping(Environment* env);
728+
void StartExecution(Environment* env);
729+
727730
} // namespace node
728731

729732
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

test/message/assert_throws_stack.out

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,3 @@ AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal:
1818
at *
1919
at *
2020
at *
21-
at *

test/message/error_exit.out

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,3 @@ AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
1616
at Function.Module.runMain (internal/modules/cjs/loader.js:*:*)
1717
at executeUserCode (internal/bootstrap/node.js:*:*)
1818
at startExecution (internal/bootstrap/node.js:*:*)
19-
at startup (internal/bootstrap/node.js:*:*)

test/message/eval_messages.out

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ SyntaxError: Strict mode code may not include a with statement
1111
at evalScript (internal/process/execution.js:*:*)
1212
at executeUserCode (internal/bootstrap/node.js:*:*)
1313
at startExecution (internal/bootstrap/node.js:*:*)
14-
at startup (internal/bootstrap/node.js:*:*)
15-
at internal/bootstrap/node.js:*:*
1614
42
1715
42
1816
[eval]:1
@@ -28,8 +26,6 @@ Error: hello
2826
at evalScript (internal/process/execution.js:*:*)
2927
at executeUserCode (internal/bootstrap/node.js:*:*)
3028
at startExecution (internal/bootstrap/node.js:*:*)
31-
at startup (internal/bootstrap/node.js:*:*)
32-
at internal/bootstrap/node.js:*:*
3329

3430
[eval]:1
3531
throw new Error("hello")
@@ -44,8 +40,6 @@ Error: hello
4440
at evalScript (internal/process/execution.js:*:*)
4541
at executeUserCode (internal/bootstrap/node.js:*:*)
4642
at startExecution (internal/bootstrap/node.js:*:*)
47-
at startup (internal/bootstrap/node.js:*:*)
48-
at internal/bootstrap/node.js:*:*
4943
100
5044
[eval]:1
5145
var x = 100; y = x;
@@ -60,8 +54,6 @@ ReferenceError: y is not defined
6054
at evalScript (internal/process/execution.js:*:*)
6155
at executeUserCode (internal/bootstrap/node.js:*:*)
6256
at startExecution (internal/bootstrap/node.js:*:*)
63-
at startup (internal/bootstrap/node.js:*:*)
64-
at internal/bootstrap/node.js:*:*
6557

6658
[eval]:1
6759
var ______________________________________________; throw 10

test/message/events_unhandled_error_nexttick.out

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,10 @@ Error
1212
at Function.Module.runMain (internal/modules/cjs/loader.js:*:*)
1313
at executeUserCode (internal/bootstrap/node.js:*:*)
1414
at startExecution (internal/bootstrap/node.js:*:*)
15-
at startup (internal/bootstrap/node.js:*:*)
1615
Emitted 'error' event at:
1716
at process.nextTick (*events_unhandled_error_nexttick.js:*:*)
1817
at internalTickCallback (internal/process/next_tick.js:*:*)
1918
at process.runNextTicks [as _tickCallback] (internal/process/next_tick.js:*:*)
2019
at Function.Module.runMain (internal/modules/cjs/loader.js:*:*)
2120
at executeUserCode (internal/bootstrap/node.js:*:*)
2221
at startExecution (internal/bootstrap/node.js:*:*)
23-
at startup (internal/bootstrap/node.js:*:*)
24-
at internal/bootstrap/node.js:*:*

test/message/events_unhandled_error_sameline.out

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ Error
1212
at Function.Module.runMain (internal/modules/cjs/loader.js:*:*)
1313
at executeUserCode (internal/bootstrap/node.js:*:*)
1414
at startExecution (internal/bootstrap/node.js:*:*)
15-
at startup (internal/bootstrap/node.js:*:*)
1615
Emitted 'error' event at:
1716
at Object.<anonymous> (*events_unhandled_error_sameline.js:*:*)
1817
at Module._compile (internal/modules/cjs/loader.js:*:*)
1918
[... lines matching original stack trace ...]
20-
at startup (internal/bootstrap/node.js:*:*)
19+
at startExecution (internal/bootstrap/node.js:*:*)

0 commit comments

Comments
 (0)