diff --git a/include/v8.h b/include/v8.h index 037611b302..e193e78bfa 100644 --- a/include/v8.h +++ b/include/v8.h @@ -4453,6 +4453,19 @@ class V8_EXPORT WasmStreaming final { public: class WasmStreamingImpl; + /** + * Client to receive streaming event notifications. + */ + class Client { + public: + virtual ~Client() = default; + /** + * Passes the fully compiled module to the client. This can be used to + * implement code caching. + */ + virtual void OnModuleCompiled(CompiledWasmModule compiled_module) = 0; + }; + explicit WasmStreaming(std::unique_ptr impl); ~WasmStreaming(); @@ -4478,28 +4491,20 @@ class V8_EXPORT WasmStreaming final { void Abort(MaybeLocal exception); /** - * Callback for module compiled notifications. |data| is the identifier - * passed to {SetModuleCompiledCallback}, |compiled_module| is the result. - */ - typedef void (*ModuleCompiledCallback)(intptr_t data, - CompiledWasmModule compiled_module); - - /** - * Sets a callback for when compilation of the Wasm module has been completed - * to the highest tier. |data| will be passed as the first callback parameter. - */ - void SetModuleCompiledCallback(ModuleCompiledCallback callback, - intptr_t data); - - /** - * Passes previously compiled module bytes. This must be called before calling - * any non-static methods of this class. Returns true if the module bytes can - * be used, false otherwise. The buffer passed into {SetCompiledModuleBytes} + * Passes previously compiled module bytes. This must be called before + * {OnBytesReceived}, {Finish}, or {Abort}. Returns true if the module bytes + * can be used, false otherwise. The buffer passed via {bytes} and {size} * is owned by the caller. If {SetCompiledModuleBytes} returns true, the * buffer must remain valid until either {Finish} or {Abort} completes. */ bool SetCompiledModuleBytes(const uint8_t* bytes, size_t size); + /** + * Sets the client object that will receive streaming event notifications. + * This must be called before {OnBytesReceived}, {Finish}, or {Abort}. + */ + void SetClient(std::shared_ptr client); + /** * Unpacks a {WasmStreaming} object wrapped in a {Managed} for the embedder. * Since the embedder is on the other side of the API, it cannot unpack the diff --git a/src/wasm/streaming-decoder.cc b/src/wasm/streaming-decoder.cc index 7b336778e6..7910cb31cf 100644 --- a/src/wasm/streaming-decoder.cc +++ b/src/wasm/streaming-decoder.cc @@ -157,8 +157,7 @@ void StreamingDecoder::NotifyRuntimeObjectsCreated( auto* comp_state = module_object->native_module()->compilation_state(); comp_state->AddCallback(TopTierCompiledCallback{ std::move(native_module), std::move(module_compiled_callback_)}); - // The callback took ownership of the callback: - DCHECK_NULL(module_compiled_callback_); + module_compiled_callback_ = {}; } // An abstract class to share code among the states which decode VarInts. This diff --git a/src/wasm/wasm-js.cc b/src/wasm/wasm-js.cc index 518c4306dd..e2936a613f 100644 --- a/src/wasm/wasm-js.cc +++ b/src/wasm/wasm-js.cc @@ -59,22 +59,20 @@ class WasmStreaming::WasmStreamingImpl { Utils::OpenHandle(*exception.ToLocalChecked())); } - void SetModuleCompiledCallback(ModuleCompiledCallback callback, - intptr_t data) { - // Wrap the embedder callback here so we can also wrap the result as a - // Local here. - streaming_decoder_->SetModuleCompiledCallback( - [callback, - data](const std::shared_ptr& native_module) { - callback(data, Utils::Convert(native_module)); - }); - } - bool SetCompiledModuleBytes(const uint8_t* bytes, size_t size) { if (!i::wasm::IsSupportedVersion({bytes, size})) return false; return streaming_decoder_->SetCompiledModuleBytes({bytes, size}); } + void SetClient(std::shared_ptr client) { + // There are no other event notifications so just pass client to decoder. + // Wrap the client with a callback here so we can also wrap the result. + streaming_decoder_->SetModuleCompiledCallback( + [client](const std::shared_ptr& native_module) { + client->OnModuleCompiled(Utils::Convert(native_module)); + }); + } + private: Isolate* isolate_ = nullptr; std::shared_ptr streaming_decoder_; @@ -98,15 +96,14 @@ void WasmStreaming::Abort(MaybeLocal exception) { impl_->Abort(exception); } -void WasmStreaming::SetModuleCompiledCallback(ModuleCompiledCallback callback, - intptr_t data) { - impl_->SetModuleCompiledCallback(callback, data); -} - bool WasmStreaming::SetCompiledModuleBytes(const uint8_t* bytes, size_t size) { return impl_->SetCompiledModuleBytes(bytes, size); } +void WasmStreaming::SetClient(std::shared_ptr client) { + impl_->SetClient(client); +} + // static std::shared_ptr WasmStreaming::Unpack(Isolate* isolate, Local value) {