diff options
author | Anna Henningsen <anna@addaleax.net> | 2019-01-02 18:33:58 +0300 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2019-01-06 02:54:13 +0300 |
commit | 0a549aaeaca8e3be7d19691d40f6a1c0798c82bf (patch) | |
tree | f2c981a1118e0690e4cf768829d05bd1b890e936 /src/node_messaging.cc | |
parent | 47a9eea8c8843414aa09dbdea9ca5fb74279f5ab (diff) |
worker: enable transferring WASM modules
Enable in-memory transfer of WASM modules without recompilation.
Previously, the serialization step worked, but deserialization failed
because we did not explicitly enable decoding inline WASM modules,
and so the message was not successfully received.
PR-URL: https://github.com/nodejs/node/pull/25314
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Diffstat (limited to 'src/node_messaging.cc')
-rw-r--r-- | src/node_messaging.cc | 38 |
1 files changed, 30 insertions, 8 deletions
diff --git a/src/node_messaging.cc b/src/node_messaging.cc index ae60187b6f3..6036648dd4c 100644 --- a/src/node_messaging.cc +++ b/src/node_messaging.cc @@ -30,6 +30,7 @@ using v8::String; using v8::Value; using v8::ValueDeserializer; using v8::ValueSerializer; +using v8::WasmCompiledModule; namespace node { namespace worker { @@ -43,13 +44,15 @@ namespace { // `MessagePort`s and `SharedArrayBuffer`s, and make new JS objects out of them. class DeserializerDelegate : public ValueDeserializer::Delegate { public: - DeserializerDelegate(Message* m, - Environment* env, - const std::vector<MessagePort*>& message_ports, - const std::vector<Local<SharedArrayBuffer>>& - shared_array_buffers) - : message_ports_(message_ports), - shared_array_buffers_(shared_array_buffers) {} + DeserializerDelegate( + Message* m, + Environment* env, + const std::vector<MessagePort*>& message_ports, + const std::vector<Local<SharedArrayBuffer>>& shared_array_buffers, + const std::vector<WasmCompiledModule::TransferrableModule>& wasm_modules) + : message_ports_(message_ports), + shared_array_buffers_(shared_array_buffers), + wasm_modules_(wasm_modules) {} MaybeLocal<Object> ReadHostObject(Isolate* isolate) override { // Currently, only MessagePort hosts objects are supported, so identifying @@ -67,11 +70,19 @@ class DeserializerDelegate : public ValueDeserializer::Delegate { return shared_array_buffers_[clone_id]; } + MaybeLocal<WasmCompiledModule> GetWasmModuleFromId( + Isolate* isolate, uint32_t transfer_id) override { + CHECK_LE(transfer_id, wasm_modules_.size()); + return WasmCompiledModule::FromTransferrableModule( + isolate, wasm_modules_[transfer_id]); + } + ValueDeserializer* deserializer = nullptr; private: const std::vector<MessagePort*>& message_ports_; const std::vector<Local<SharedArrayBuffer>>& shared_array_buffers_; + const std::vector<WasmCompiledModule::TransferrableModule>& wasm_modules_; }; } // anonymous namespace @@ -109,7 +120,8 @@ MaybeLocal<Value> Message::Deserialize(Environment* env, } shared_array_buffers_.clear(); - DeserializerDelegate delegate(this, env, ports, shared_array_buffers); + DeserializerDelegate delegate( + this, env, ports, shared_array_buffers, wasm_modules_); ValueDeserializer deserializer( env->isolate(), reinterpret_cast<const uint8_t*>(main_message_buf_.data), @@ -143,6 +155,11 @@ void Message::AddMessagePort(std::unique_ptr<MessagePortData>&& data) { message_ports_.emplace_back(std::move(data)); } +uint32_t Message::AddWASMModule(WasmCompiledModule::TransferrableModule&& mod) { + wasm_modules_.emplace_back(std::move(mod)); + return wasm_modules_.size() - 1; +} + namespace { void ThrowDataCloneException(Environment* env, Local<String> message) { @@ -202,6 +219,11 @@ class SerializerDelegate : public ValueSerializer::Delegate { return Just(i); } + Maybe<uint32_t> GetWasmModuleTransferId( + Isolate* isolate, Local<WasmCompiledModule> module) override { + return Just(msg_->AddWASMModule(module->GetTransferrableModule())); + } + void Finish() { // Only close the MessagePort handles and actually transfer them // once we know that serialization succeeded. |