Commit 38ac7fe6 authored by mtrofin's avatar mtrofin Committed by Commit bot

[wasm] API renames for wasm serialization/deserialization

This incorporates recent feedback:
- simpler deserialization API by dropping the std::unique_ptr.
The only purpose there was communicating to the caller that they
own the buffer, and that the deserializer won't delete it. The new
design communicates that through a naming choice.
- renamed *UncompiledBytes to *WasmWireBytes

BUG=

Review-Url: https://codereview.chromium.org/2411263004
Cr-Commit-Position: refs/heads/master@{#40238}
parent f116876e
...@@ -3902,9 +3902,10 @@ class V8_EXPORT Proxy : public Object { ...@@ -3902,9 +3902,10 @@ class V8_EXPORT Proxy : public Object {
class V8_EXPORT WasmCompiledModule : public Object { class V8_EXPORT WasmCompiledModule : public Object {
public: public:
typedef std::pair<std::unique_ptr<const uint8_t[]>, size_t> SerializedModule; typedef std::pair<std::unique_ptr<const uint8_t[]>, size_t> SerializedModule;
typedef std::pair<std::unique_ptr<const uint8_t[]>, size_t> UncompiledBytes; // A buffer that is owned by the caller.
// Get the uncompiled bytes that were used to compile this module. typedef std::pair<const uint8_t*, size_t> CallerOwnedBuffer;
Local<String> GetUncompiledBytes(); // Get the wasm-encoded bytes that were used to compile this module.
Local<String> GetWasmWireBytes();
// Serialize the compiled module. The serialized data does not include the // Serialize the compiled module. The serialized data does not include the
// uncompiled bytes. // uncompiled bytes.
...@@ -3913,15 +3914,17 @@ class V8_EXPORT WasmCompiledModule : public Object { ...@@ -3913,15 +3914,17 @@ class V8_EXPORT WasmCompiledModule : public Object {
// TODO(mtrofin): Back-compat. Move to private once change lands in Chrome. // TODO(mtrofin): Back-compat. Move to private once change lands in Chrome.
// The resulting wasm setup won't have its uncompiled bytes available. // The resulting wasm setup won't have its uncompiled bytes available.
static MaybeLocal<WasmCompiledModule> Deserialize( static MaybeLocal<WasmCompiledModule> Deserialize(
Isolate* isolate, const SerializedModule& serialized_data); Isolate* isolate, const SerializedModule& serialized_module);
// If possible, deserialize the module, otherwise compile it from the provided // If possible, deserialize the module, otherwise compile it from the provided
// uncompiled bytes. // uncompiled bytes.
static MaybeLocal<WasmCompiledModule> DeserializeOrCompile( static MaybeLocal<WasmCompiledModule> DeserializeOrCompile(
Isolate* isolate, const SerializedModule& serialized_data, Isolate* isolate, const CallerOwnedBuffer& serialized_module,
const UncompiledBytes& uncompiled_bytes); const CallerOwnedBuffer& wire_bytes);
V8_INLINE static WasmCompiledModule* Cast(Value* obj); V8_INLINE static WasmCompiledModule* Cast(Value* obj);
private: private:
static MaybeLocal<WasmCompiledModule> Deserialize(
Isolate* isolate, const CallerOwnedBuffer& serialized_module);
static MaybeLocal<WasmCompiledModule> Compile(Isolate* isolate, static MaybeLocal<WasmCompiledModule> Compile(Isolate* isolate,
const uint8_t* start, const uint8_t* start,
size_t length); size_t length);
......
...@@ -7176,13 +7176,13 @@ MaybeLocal<Proxy> Proxy::New(Local<Context> context, Local<Object> local_target, ...@@ -7176,13 +7176,13 @@ MaybeLocal<Proxy> Proxy::New(Local<Context> context, Local<Object> local_target,
RETURN_ESCAPED(result); RETURN_ESCAPED(result);
} }
Local<String> WasmCompiledModule::GetUncompiledBytes() { Local<String> WasmCompiledModule::GetWasmWireBytes() {
i::Handle<i::JSObject> obj = i::Handle<i::JSObject> obj =
i::Handle<i::JSObject>::cast(Utils::OpenHandle(this)); i::Handle<i::JSObject>::cast(Utils::OpenHandle(this));
i::Handle<i::wasm::WasmCompiledModule> compiled_part = i::Handle<i::wasm::WasmCompiledModule> compiled_part =
i::handle(i::wasm::WasmCompiledModule::cast(obj->GetInternalField(0))); i::handle(i::wasm::WasmCompiledModule::cast(obj->GetInternalField(0)));
i::Handle<i::String> module_bytes = compiled_part->module_bytes(); i::Handle<i::String> wire_bytes = compiled_part->module_bytes();
return Local<String>::Cast(Utils::ToLocal(module_bytes)); return Local<String>::Cast(Utils::ToLocal(wire_bytes));
} }
WasmCompiledModule::SerializedModule WasmCompiledModule::Serialize() { WasmCompiledModule::SerializedModule WasmCompiledModule::Serialize() {
...@@ -7191,13 +7191,12 @@ WasmCompiledModule::SerializedModule WasmCompiledModule::Serialize() { ...@@ -7191,13 +7191,12 @@ WasmCompiledModule::SerializedModule WasmCompiledModule::Serialize() {
i::Handle<i::wasm::WasmCompiledModule> compiled_part = i::Handle<i::wasm::WasmCompiledModule> compiled_part =
i::handle(i::wasm::WasmCompiledModule::cast(obj->GetInternalField(0))); i::handle(i::wasm::WasmCompiledModule::cast(obj->GetInternalField(0)));
i::Handle<i::SeqOneByteString> uncompiled_bytes = i::Handle<i::SeqOneByteString> wire_bytes = compiled_part->module_bytes();
compiled_part->module_bytes();
compiled_part->reset_module_bytes(); compiled_part->reset_module_bytes();
std::unique_ptr<i::ScriptData> script_data = std::unique_ptr<i::ScriptData> script_data =
i::WasmCompiledModuleSerializer::SerializeWasmModule(obj->GetIsolate(), i::WasmCompiledModuleSerializer::SerializeWasmModule(obj->GetIsolate(),
compiled_part); compiled_part);
compiled_part->set_module_bytes(uncompiled_bytes); compiled_part->set_module_bytes(wire_bytes);
script_data->ReleaseDataOwnership(); script_data->ReleaseDataOwnership();
size_t size = static_cast<size_t>(script_data->length()); size_t size = static_cast<size_t>(script_data->length());
...@@ -7206,9 +7205,16 @@ WasmCompiledModule::SerializedModule WasmCompiledModule::Serialize() { ...@@ -7206,9 +7205,16 @@ WasmCompiledModule::SerializedModule WasmCompiledModule::Serialize() {
MaybeLocal<WasmCompiledModule> WasmCompiledModule::Deserialize( MaybeLocal<WasmCompiledModule> WasmCompiledModule::Deserialize(
Isolate* isolate, Isolate* isolate,
const WasmCompiledModule::SerializedModule& serialized_data) { const WasmCompiledModule::SerializedModule& serialized_module) {
int size = static_cast<int>(serialized_data.second); return Deserialize(isolate,
i::ScriptData sc(serialized_data.first.get(), size); {serialized_module.first.get(), serialized_module.second});
}
MaybeLocal<WasmCompiledModule> WasmCompiledModule::Deserialize(
Isolate* isolate,
const WasmCompiledModule::CallerOwnedBuffer& serialized_module) {
int size = static_cast<int>(serialized_module.second);
i::ScriptData sc(serialized_module.first, size);
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
i::MaybeHandle<i::FixedArray> maybe_compiled_part = i::MaybeHandle<i::FixedArray> maybe_compiled_part =
i::WasmCompiledModuleSerializer::DeserializeWasmModule(i_isolate, &sc); i::WasmCompiledModuleSerializer::DeserializeWasmModule(i_isolate, &sc);
...@@ -7225,12 +7231,11 @@ MaybeLocal<WasmCompiledModule> WasmCompiledModule::Deserialize( ...@@ -7225,12 +7231,11 @@ MaybeLocal<WasmCompiledModule> WasmCompiledModule::Deserialize(
MaybeLocal<WasmCompiledModule> WasmCompiledModule::DeserializeOrCompile( MaybeLocal<WasmCompiledModule> WasmCompiledModule::DeserializeOrCompile(
Isolate* isolate, Isolate* isolate,
const WasmCompiledModule::SerializedModule& serialized_data, const WasmCompiledModule::CallerOwnedBuffer& serialized_module,
const WasmCompiledModule::UncompiledBytes& uncompiled_bytes) { const WasmCompiledModule::CallerOwnedBuffer& wire_bytes) {
MaybeLocal<WasmCompiledModule> ret = Deserialize(isolate, serialized_data); MaybeLocal<WasmCompiledModule> ret = Deserialize(isolate, serialized_module);
if (!ret.IsEmpty()) return ret; if (!ret.IsEmpty()) return ret;
return Compile(isolate, uncompiled_bytes.first.get(), return Compile(isolate, wire_bytes.first, wire_bytes.second);
uncompiled_bytes.second);
} }
MaybeLocal<WasmCompiledModule> WasmCompiledModule::Compile(Isolate* isolate, MaybeLocal<WasmCompiledModule> WasmCompiledModule::Compile(Isolate* isolate,
......
...@@ -212,17 +212,18 @@ TEST(Run_WasmModule_Serialization) { ...@@ -212,17 +212,18 @@ TEST(Run_WasmModule_Serialization) {
v8::Local<v8::WasmCompiledModule> v8_compiled_module = v8::Local<v8::WasmCompiledModule> v8_compiled_module =
v8_module_obj.As<v8::WasmCompiledModule>(); v8_module_obj.As<v8::WasmCompiledModule>();
v8::Local<v8::String> uncompiled_bytes = v8::Local<v8::String> uncompiled_bytes =
v8_compiled_module->GetUncompiledBytes(); v8_compiled_module->GetWasmWireBytes();
bytes_size = static_cast<size_t>(uncompiled_bytes->Length()); bytes_size = static_cast<size_t>(uncompiled_bytes->Length());
bytes = zone.NewArray<uint8_t>(uncompiled_bytes->Length()); bytes = zone.NewArray<uint8_t>(uncompiled_bytes->Length());
uncompiled_bytes->WriteOneByte(bytes); uncompiled_bytes->WriteOneByte(bytes);
data = v8_compiled_module->Serialize(); data = v8_compiled_module->Serialize();
} }
v8::WasmCompiledModule::UncompiledBytes uncompressed_bytes = { v8::WasmCompiledModule::CallerOwnedBuffer wire_bytes = {
std::unique_ptr<const uint8_t[]>(const_cast<const uint8_t*>(bytes)), const_cast<const uint8_t*>(bytes), bytes_size};
bytes_size};
v8::WasmCompiledModule::CallerOwnedBuffer serialized_bytes = {
data.first.get(), data.second};
v8::Isolate::CreateParams create_params; v8::Isolate::CreateParams create_params;
create_params.array_buffer_allocator = create_params.array_buffer_allocator =
CcTest::InitIsolateOnce()->array_buffer_allocator(); CcTest::InitIsolateOnce()->array_buffer_allocator();
...@@ -230,9 +231,9 @@ TEST(Run_WasmModule_Serialization) { ...@@ -230,9 +231,9 @@ TEST(Run_WasmModule_Serialization) {
for (int i = 0; i < 2; ++i) { for (int i = 0; i < 2; ++i) {
v8::Isolate* v8_isolate = v8::Isolate::New(create_params); v8::Isolate* v8_isolate = v8::Isolate::New(create_params);
if (i == 1) { if (i == 1) {
// Mess with the serialized data to force recompilation. // Provide no serialized data to force recompilation.
data.first.reset(); serialized_bytes.first = nullptr;
data.second = 0; serialized_bytes.second = 0;
} }
{ {
v8::Isolate::Scope isolate_scope(v8_isolate); v8::Isolate::Scope isolate_scope(v8_isolate);
...@@ -242,8 +243,8 @@ TEST(Run_WasmModule_Serialization) { ...@@ -242,8 +243,8 @@ TEST(Run_WasmModule_Serialization) {
isolate = reinterpret_cast<Isolate*>(v8_isolate); isolate = reinterpret_cast<Isolate*>(v8_isolate);
testing::SetupIsolateForWasmModule(isolate); testing::SetupIsolateForWasmModule(isolate);
v8::MaybeLocal<v8::WasmCompiledModule> deserialized = v8::MaybeLocal<v8::WasmCompiledModule> deserialized =
v8::WasmCompiledModule::DeserializeOrCompile(v8_isolate, data, v8::WasmCompiledModule::DeserializeOrCompile(
uncompressed_bytes); v8_isolate, serialized_bytes, wire_bytes);
v8::Local<v8::WasmCompiledModule> compiled_module; v8::Local<v8::WasmCompiledModule> compiled_module;
CHECK(deserialized.ToLocal(&compiled_module)); CHECK(deserialized.ToLocal(&compiled_module));
Handle<JSObject> module_object = Handle<JSObject> module_object =
...@@ -262,9 +263,6 @@ TEST(Run_WasmModule_Serialization) { ...@@ -262,9 +263,6 @@ TEST(Run_WasmModule_Serialization) {
} }
v8_isolate->Dispose(); v8_isolate->Dispose();
} }
// Release, because we allocated the bytes with the zone allocator, and
// that doesn't have a delete.
uncompressed_bytes.first.release();
} }
TEST(MemorySize) { TEST(MemorySize) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment