Skip to content

Commit 21096f9

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 9c5db93 commit 21096f9

13 files changed

+47
-32
lines changed

lib/internal/bootstrap/node.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ function startup() {
319319
} = perf.constants;
320320
perf.markMilestone(NODE_PERFORMANCE_MILESTONE_BOOTSTRAP_COMPLETE);
321321

322-
startExecution();
322+
return startExecution;
323323
}
324324

325325
// There are various modes that Node can run in. The most common two
@@ -737,4 +737,4 @@ function checkScriptSyntax(source, filename) {
737737
new vm.Script(source, { displayErrors: true, filename });
738738
}
739739

740-
startup();
740+
return startup();

src/env-inl.h

+8
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,14 @@ inline void Environment::set_can_call_into_js(bool can_call_into_js) {
633633
can_call_into_js_ = can_call_into_js;
634634
}
635635

636+
inline bool Environment::has_run_bootstrapping_code() const {
637+
return has_run_bootstrapping_code_;
638+
}
639+
640+
inline void Environment::set_has_run_bootstrapping_code(bool value) {
641+
has_run_bootstrapping_code_ = value;
642+
}
643+
636644
inline bool Environment::is_main_thread() const {
637645
return thread_id_ == 0;
638646
}

src/env.h

+5
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ constexpr size_t kFsStatsBufferLength = kFsStatsFieldsNumber * 2;
369369
V(script_data_constructor_function, v8::Function) \
370370
V(secure_context_constructor_template, v8::FunctionTemplate) \
371371
V(shutdown_wrap_template, v8::ObjectTemplate) \
372+
V(start_execution_function, v8::Function) \
372373
V(tcp_constructor_template, v8::FunctionTemplate) \
373374
V(tick_callback_function, v8::Function) \
374375
V(timers_callback_function, v8::Function) \
@@ -751,6 +752,9 @@ class Environment {
751752
inline bool can_call_into_js() const;
752753
inline void set_can_call_into_js(bool can_call_into_js);
753754

755+
inline bool has_run_bootstrapping_code() const;
756+
inline void set_has_run_bootstrapping_code(bool has_run_bootstrapping_code);
757+
754758
inline bool is_main_thread() const;
755759
inline uint64_t thread_id() const;
756760
inline void set_thread_id(uint64_t id);
@@ -976,6 +980,7 @@ class Environment {
976980
std::unique_ptr<performance::performance_state> performance_state_;
977981
std::unordered_map<std::string, uint64_t> performance_marks_;
978982

983+
bool has_run_bootstrapping_code_ = false;
979984
bool can_call_into_js_ = true;
980985
uint64_t thread_id_ = 0;
981986
std::unordered_set<worker::Worker*> sub_worker_contexts_;

src/node.cc

+28-2
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,14 @@ static MaybeLocal<Value> ExecuteBootstrapper(
10791079
}
10801080

10811081
void LoadEnvironment(Environment* env) {
1082+
RunBootstrapping(env);
1083+
StartExecution(env);
1084+
}
1085+
1086+
void RunBootstrapping(Environment* env) {
1087+
CHECK(!env->has_run_bootstrapping_code());
1088+
env->set_has_run_bootstrapping_code(true);
1089+
10821090
HandleScope handle_scope(env->isolate());
10831091
Isolate* isolate = env->isolate();
10841092
Local<Context> context = env->context();
@@ -1140,11 +1148,29 @@ void LoadEnvironment(Environment* env) {
11401148
loader_exports.ToLocalChecked(),
11411149
Boolean::New(isolate, env->is_main_thread())};
11421150

1143-
if (ExecuteBootstrapper(
1151+
Local<Value> start_execution;
1152+
if (!ExecuteBootstrapper(
11441153
env, "internal/bootstrap/node", &node_params, &node_args)
1145-
.IsEmpty()) {
1154+
.ToLocal(&start_execution)) {
11461155
return;
11471156
}
1157+
1158+
if (start_execution->IsFunction())
1159+
env->set_start_execution_function(start_execution.As<Function>());
1160+
}
1161+
1162+
void StartExecution(Environment* env) {
1163+
HandleScope handle_scope(env->isolate());
1164+
// We have to use Local<>::New because of the optimized way in which we access
1165+
// the object in the env->...() getters, which does not play well with
1166+
// resetting the handle while we're accessing the object through the Local<>.
1167+
Local<Function> start_execution =
1168+
Local<Function>::New(env->isolate(), env->start_execution_function());
1169+
env->set_start_execution_function(Local<Function>());
1170+
1171+
if (start_execution.IsEmpty()) return;
1172+
USE(start_execution->Call(
1173+
env->context(), Undefined(env->isolate()), 0, nullptr));
11481174
}
11491175

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

src/node_internals.h

+3
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,9 @@ bool SafeGetenv(const char* key, std::string* text);
397397

398398
void DefineZlibConstants(v8::Local<v8::Object> target);
399399

400+
void RunBootstrapping(Environment* env);
401+
void StartExecution(Environment* env);
402+
400403
} // namespace node
401404

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

test/message/assert_throws_stack.out

-1
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

-1
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

-8
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

-3
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 processTicksAndRejections (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

+1-2
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:*:*)

test/message/nexttick_throw.out

-2
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,3 @@ ReferenceError: undefined_reference_error_maker is not defined
99
at Function.Module.runMain (internal/modules/cjs/loader.js:*:*)
1010
at executeUserCode (internal/bootstrap/node.js:*:*)
1111
at startExecution (internal/bootstrap/node.js:*:*)
12-
at startup (internal/bootstrap/node.js:*:*)
13-
at internal/bootstrap/node.js:*:*

test/message/unhandled_promise_trace_warnings.out

-6
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
at *
1616
at *
1717
at *
18-
at *
19-
at *
20-
at *
2118
(node:*) Error: This was rejected
2219
at * (*test*message*unhandled_promise_trace_warnings.js:*)
2320
at *
@@ -28,7 +25,6 @@
2825
at *
2926
at *
3027
at *
31-
at *
3228
(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.
3329
at *
3430
at *
@@ -38,8 +34,6 @@
3834
at *
3935
at *
4036
at *
41-
at *
42-
at *
4337
(node:*) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously (rejection id: 1)
4438
at handledRejection (internal/process/promises.js:*)
4539
at promiseRejectHandler (internal/process/promises.js:*)

test/message/util_inspect_error.out

-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
at *
1111
at *
1212
at *
13-
at *
1413
nested:
1514
{ err:
1615
Error: foo
@@ -23,7 +22,6 @@
2322
at *
2423
at *
2524
at *
26-
at *
2725
at * } }
2826
{
2927
err: Error: foo
@@ -36,7 +34,6 @@
3634
at *
3735
at *
3836
at *
39-
at *
4037
at *,
4138
nested: {
4239
err: Error: foo
@@ -50,7 +47,6 @@
5047
at *
5148
at *
5249
at *
53-
at *
5450
}
5551
}
5652
{ Error: foo
@@ -64,5 +60,4 @@ bar
6460
at *
6561
at *
6662
at *
67-
at *
6863
foo: 'bar' }

0 commit comments

Comments
 (0)