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 {
class V8_EXPORT WasmCompiledModule : public Object {
public:
typedef std::pair<std::unique_ptr<const uint8_t[]>, size_t> SerializedModule;
typedef std::pair<std::unique_ptr<const uint8_t[]>, size_t> UncompiledBytes;
// Get the uncompiled bytes that were used to compile this module.
Local<String> GetUncompiledBytes();
// A buffer that is owned by the caller.
typedef std::pair<const uint8_t*, size_t> CallerOwnedBuffer;
// 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
// uncompiled bytes.
......@@ -3913,15 +3914,17 @@ class V8_EXPORT WasmCompiledModule : public Object {
// TODO(mtrofin): Back-compat. Move to private once change lands in Chrome.
// The resulting wasm setup won't have its uncompiled bytes available.
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
// uncompiled bytes.
static MaybeLocal<WasmCompiledModule> DeserializeOrCompile(
Isolate* isolate, const SerializedModule& serialized_data,
const UncompiledBytes& uncompiled_bytes);
Isolate* isolate, const CallerOwnedBuffer& serialized_module,
const CallerOwnedBuffer& wire_bytes);
V8_INLINE static WasmCompiledModule* Cast(Value* obj);
private:
static MaybeLocal<WasmCompiledModule> Deserialize(
Isolate* isolate, const CallerOwnedBuffer& serialized_module);
static MaybeLocal<WasmCompiledModule> Compile(Isolate* isolate,
const uint8_t* start,
size_t length);
......
......@@ -7176,13 +7176,13 @@ MaybeLocal<Proxy> Proxy::New(Local<Context> context, Local<Object> local_target,
RETURN_ESCAPED(result);
}
Local<String> WasmCompiledModule::GetUncompiledBytes() {
Local<String> WasmCompiledModule::GetWasmWireBytes() {
i::Handle<i::JSObject> obj =
i::Handle<i::JSObject>::cast(Utils::OpenHandle(this));
i::Handle<i::wasm::WasmCompiledModule> compiled_part =
i::handle(i::wasm::WasmCompiledModule::cast(obj->GetInternalField(0)));
i::Handle<i::String> module_bytes = compiled_part->module_bytes();
return Local<String>::Cast(Utils::ToLocal(module_bytes));
i::Handle<i::String> wire_bytes = compiled_part->module_bytes();
return Local<String>::Cast(Utils::ToLocal(wire_bytes));
}
WasmCompiledModule::SerializedModule WasmCompiledModule::Serialize() {
......@@ -7191,13 +7191,12 @@ WasmCompiledModule::SerializedModule WasmCompiledModule::Serialize() {
i::Handle<i::wasm::WasmCompiledModule> compiled_part =
i::handle(i::wasm::WasmCompiledModule::cast(obj->GetInternalField(0)));
i::Handle<i::SeqOneByteString> uncompiled_bytes =
compiled_part->module_bytes();
i::Handle<i::SeqOneByteString> wire_bytes = compiled_part->module_bytes();
compiled_part->reset_module_bytes();
std::unique_ptr<i::ScriptData> script_data =
i::WasmCompiledModuleSerializer::SerializeWasmModule(obj->GetIsolate(),
compiled_part);
compiled_part->set_module_bytes(uncompiled_bytes);
compiled_part->set_module_bytes(wire_bytes);
script_data->ReleaseDataOwnership();
size_t size = static_cast<size_t>(script_data->length());
......@@ -7206,9 +7205,16 @@ WasmCompiledModule::SerializedModule WasmCompiledModule::Serialize() {
MaybeLocal<WasmCompiledModule> WasmCompiledModule::Deserialize(
Isolate* isolate,
const WasmCompiledModule::SerializedModule& serialized_data) {
int size = static_cast<int>(serialized_data.second);
i::ScriptData sc(serialized_data.first.get(), size);
const WasmCompiledModule::SerializedModule& serialized_module) {
return Deserialize(isolate,
{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::MaybeHandle<i::FixedArray> maybe_compiled_part =
i::WasmCompiledModuleSerializer::DeserializeWasmModule(i_isolate, &sc);
......@@ -7225,12 +7231,11 @@ MaybeLocal<WasmCompiledModule> WasmCompiledModule::Deserialize(
MaybeLocal<WasmCompiledModule> WasmCompiledModule::DeserializeOrCompile(
Isolate* isolate,
const WasmCompiledModule::SerializedModule& serialized_data,
const WasmCompiledModule::UncompiledBytes& uncompiled_bytes) {
MaybeLocal<WasmCompiledModule> ret = Deserialize(isolate, serialized_data);
const WasmCompiledModule::CallerOwnedBuffer& serialized_module,
const WasmCompiledModule::CallerOwnedBuffer& wire_bytes) {
MaybeLocal<WasmCompiledModule> ret = Deserialize(isolate, serialized_module);
if (!ret.IsEmpty()) return ret;
return Compile(isolate, uncompiled_bytes.first.get(),
uncompiled_bytes.second);
return Compile(isolate, wire_bytes.first, wire_bytes.second);
}
MaybeLocal<WasmCompiledModule> WasmCompiledModule::Compile(Isolate* isolate,
......
......@@ -212,17 +212,18 @@ TEST(Run_WasmModule_Serialization) {
v8::Local<v8::WasmCompiledModule> v8_compiled_module =
v8_module_obj.As<v8::WasmCompiledModule>();
v8::Local<v8::String> uncompiled_bytes =
v8_compiled_module->GetUncompiledBytes();
v8_compiled_module->GetWasmWireBytes();
bytes_size = static_cast<size_t>(uncompiled_bytes->Length());
bytes = zone.NewArray<uint8_t>(uncompiled_bytes->Length());
uncompiled_bytes->WriteOneByte(bytes);
data = v8_compiled_module->Serialize();
}
v8::WasmCompiledModule::UncompiledBytes uncompressed_bytes = {
std::unique_ptr<const uint8_t[]>(const_cast<const uint8_t*>(bytes)),
bytes_size};
v8::WasmCompiledModule::CallerOwnedBuffer wire_bytes = {
const_cast<const uint8_t*>(bytes), bytes_size};
v8::WasmCompiledModule::CallerOwnedBuffer serialized_bytes = {
data.first.get(), data.second};
v8::Isolate::CreateParams create_params;
create_params.array_buffer_allocator =
CcTest::InitIsolateOnce()->array_buffer_allocator();
......@@ -230,9 +231,9 @@ TEST(Run_WasmModule_Serialization) {
for (int i = 0; i < 2; ++i) {
v8::Isolate* v8_isolate = v8::Isolate::New(create_params);
if (i == 1) {
// Mess with the serialized data to force recompilation.
data.first.reset();
data.second = 0;
// Provide no serialized data to force recompilation.
serialized_bytes.first = nullptr;
serialized_bytes.second = 0;
}
{
v8::Isolate::Scope isolate_scope(v8_isolate);
......@@ -242,8 +243,8 @@ TEST(Run_WasmModule_Serialization) {
isolate = reinterpret_cast<Isolate*>(v8_isolate);
testing::SetupIsolateForWasmModule(isolate);
v8::MaybeLocal<v8::WasmCompiledModule> deserialized =
v8::WasmCompiledModule::DeserializeOrCompile(v8_isolate, data,
uncompressed_bytes);
v8::WasmCompiledModule::DeserializeOrCompile(
v8_isolate, serialized_bytes, wire_bytes);
v8::Local<v8::WasmCompiledModule> compiled_module;
CHECK(deserialized.ToLocal(&compiled_module));
Handle<JSObject> module_object =
......@@ -262,9 +263,6 @@ TEST(Run_WasmModule_Serialization) {
}
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) {
......
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