Skip to content

Instantly share code, notes, and snippets.

@jeremyroman
Last active March 15, 2017 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremyroman/44ca356ef74b6538968cbfb1a410c8a8 to your computer and use it in GitHub Desktop.
Save jeremyroman/44ca356ef74b6538968cbfb1a410c8a8 to your computer and use it in GitHub Desktop.
class ValueSerializer {
class Delegate {
/*
* Delegate serializes the module content and returns an ID representing it.
* Using v8::WasmCompiledModule::GetTransferrableModule is recommended for efficiency,
* but a V8 client could do something else (like store the wire bytes, or store
* a URL to remote server that hosts wasm modules, or something else).
*/
virtual Maybe<uint32_t> GetWasmCompiledModuleId(
Isolate* isolate, Local<WasmCompiledModule> wasm_module);
};
};
class ValueDeserializer {
/*
* Maps an ID assigned by the deserializer delegate to a compiled module in
* the receiving context.
*/
void TransferWasmCompiledModule(uint32_t id, Local<WasmCompiledCompile> module);
};
// Usage
class ClientSerializerImpl : public v8::ValueSerializer::Delegate {
public:
Maybe<uint32_t> GetWasmCompiledModuleId(
Isolate* isolate, Local<WasmCompiledModule> wasm_module) override {
m_modules.push_back(wasm_module->GetTransferrableModule());
return Just(m_modules.size() - 1);
}
private:
Vector<v8::WasmCompiledModule::TransferrableModule> m_modules;
};
void Serialize() {
// uses ValueSerializer+ClientSerializerImpl, takes ownership of m_modules afterwards
}
void Deserialize(Vector<v8::WasmCompiledModule::TransferrableModule> modules) {
ValueDeserializer deserializer(...);
for (uint32_t i = 0; i < modules.size(); i++) {
v8::Local<v8::WasmCompiledModule> module = v8::WasmCompiledModule::FromTransferrableModule(isolate, modules[i]);
deserializer.TransferWasmCompiledModule(i, module);
}
// ReadHeader, ReadValue, etc. No support from delegate needed from this point.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment