Skip to content

Commit 305cfcd

Browse files
authored
src: add a flag to keep the embedder's wasm streaming callback
`SetIsolateMiscHandlers()` always installs Node.js's `WebAssembly.compileStreaming()` implementation, which is backed by the Environment's fetch-based handler. An embedder that provides its own streaming callback (for example one wired to its own network stack) has to re-install it after every `SetIsolateUpForNode()` or `NewIsolate()` call. Add `SHOULD_NOT_SET_WASM_STREAMING_CALLBACK` next to the existing `SHOULD_NOT_SET_PROMISE_REJECTION_CALLBACK` and `SHOULD_NOT_SET_PREPARE_STACK_TRACE_CALLBACK` flags so it can opt out the same way. Refs: #36447 Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com> PR-URL: #65690 Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent 18c2b33 commit 305cfcd

3 files changed

Lines changed: 37 additions & 1 deletion

File tree

‎src/api/environment.cc‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,9 @@ void SetIsolateMiscHandlers(v8::Isolate* isolate, const IsolateSettings& s) {
269269
isolate->SetModifyCodeGenerationFromStringsCallback(
270270
modify_code_generation_from_strings_callback);
271271

272-
isolate->SetWasmStreamingCallback(wasm_web_api::StartStreamingCompilation);
272+
if ((s.flags & SHOULD_NOT_SET_WASM_STREAMING_CALLBACK) == 0) {
273+
isolate->SetWasmStreamingCallback(wasm_web_api::StartStreamingCompilation);
274+
}
273275

274276
Mutex::ScopedLock lock(node::per_process::cli_options_mutex);
275277
if (per_process::cli_options->get_per_isolate_options()

‎src/node.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ enum IsolateSettingsFlags {
427427
DETAILED_SOURCE_POSITIONS_FOR_PROFILING = 1 << 1,
428428
SHOULD_NOT_SET_PROMISE_REJECTION_CALLBACK = 1 << 2,
429429
SHOULD_NOT_SET_PREPARE_STACK_TRACE_CALLBACK = 1 << 3,
430+
SHOULD_NOT_SET_WASM_STREAMING_CALLBACK = 1 << 4,
430431
ALLOW_MODIFY_CODE_GENERATION_FROM_STRINGS_CALLBACK = 0, /* legacy no-op */
431432
};
432433

‎test/cctest/test_environment.cc‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,39 @@ TEST_F(EnvironmentTest, ManagedBufferCache) {
6969
(*env)->release_managed_buffer(buffer);
7070
}
7171

72+
static int embedder_wasm_streaming_calls = 0;
73+
static void EmbedderWasmStreaming(const v8::FunctionCallbackInfo<v8::Value>&) {
74+
embedder_wasm_streaming_calls++;
75+
}
76+
77+
TEST_F(EnvironmentTest, KeepsEmbedderWasmStreamingCallbackWhenAsked) {
78+
const v8::HandleScope handle_scope(isolate_);
79+
const Argv argv;
80+
Env env{handle_scope, argv};
81+
82+
auto compile_streaming = [&]() {
83+
v8::Local<v8::Context> context = isolate_->GetCurrentContext();
84+
v8::Script::Compile(context,
85+
v8::String::NewFromUtf8Literal(
86+
isolate_, "WebAssembly.compileStreaming(0); 0"))
87+
.ToLocalChecked()
88+
->Run(context)
89+
.ToLocalChecked();
90+
isolate_->PerformMicrotaskCheckpoint();
91+
};
92+
93+
node::IsolateSettings settings;
94+
settings.flags |= node::SHOULD_NOT_SET_WASM_STREAMING_CALLBACK;
95+
isolate_->SetWasmStreamingCallback(EmbedderWasmStreaming);
96+
node::SetIsolateUpForNode(isolate_, settings);
97+
compile_streaming();
98+
EXPECT_EQ(embedder_wasm_streaming_calls, 1);
99+
100+
node::SetIsolateUpForNode(isolate_);
101+
compile_streaming();
102+
EXPECT_EQ(embedder_wasm_streaming_calls, 1);
103+
}
104+
72105
TEST_F(EnvironmentTest, EnvironmentWithoutBrowserGlobals) {
73106
const v8::HandleScope handle_scope(isolate_);
74107
Argv argv;

0 commit comments

Comments
 (0)