Index: test/cctest/wasm/test-run-wasm-module.cc |
diff --git a/test/cctest/wasm/test-run-wasm-module.cc b/test/cctest/wasm/test-run-wasm-module.cc |
index 3c145180b954f7a41fd05588e4aece062d5ee331..8449a52ff3bc0f7be4eea87b12795c7842d6a3f0 100644 |
--- a/test/cctest/wasm/test-run-wasm-module.cc |
+++ b/test/cctest/wasm/test-run-wasm-module.cc |
@@ -6,6 +6,7 @@ |
#include <string.h> |
#include "src/wasm/encoder.h" |
+#include "src/wasm/module-decoder.h" |
#include "src/wasm/wasm-js.h" |
#include "src/wasm/wasm-macro-gen.h" |
#include "src/wasm/wasm-module.h" |
@@ -33,11 +34,16 @@ void TestModule(Zone* zone, WasmModuleBuilder* builder, |
CHECK_EQ(expected_result, result); |
} |
+void ExportAs(WasmFunctionBuilder* f, const char* name) { |
+ f->SetExported(); |
+ f->SetName(name, static_cast<int>(strlen(name))); |
+} |
+ |
void ExportAsMain(WasmFunctionBuilder* f) { |
static const char kMainName[] = "main"; |
- f->SetExported(); |
- f->SetName(kMainName, arraysize(kMainName) - 1); |
+ ExportAs(f, kMainName); |
} |
+ |
} // namespace |
TEST(Run_WasmModule_Return114) { |
@@ -174,3 +180,79 @@ TEST(Run_WasmModule_Global) { |
f->EmitCode(code2, sizeof(code2)); |
TestModule(&zone, builder, 97); |
} |
+ |
+TEST(Run_WasmModule_Serialization) { |
+ FLAG_expose_wasm = true; |
+ static const char* kFunctionName = "increment"; |
+ v8::base::AccountingAllocator allocator; |
+ Zone zone(&allocator); |
+ |
+ WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
+ uint16_t f_index = builder->AddFunction(); |
+ TestSignatures sigs; |
+ |
+ WasmFunctionBuilder* f = builder->FunctionAt(f_index); |
+ f->SetSignature(sigs.i_i()); |
+ byte code[] = {WASM_GET_LOCAL(0), kExprI32Const, 1, kExprI32Add}; |
+ f->EmitCode(code, sizeof(code)); |
+ ExportAs(f, kFunctionName); |
+ |
+ ZoneBuffer buffer(&zone); |
+ builder->WriteTo(buffer); |
+ |
+ Isolate* isolate = CcTest::InitIsolateOnce(); |
+ ErrorThrower thrower(isolate, ""); |
+ |
+ v8::WasmCompiledModule::SerializedModule data; |
+ { |
+ HandleScope scope(isolate); |
+ |
+ ModuleResult decoding_result = DecodeWasmModule( |
+ isolate, &zone, buffer.begin(), buffer.end(), false, kWasmOrigin); |
+ std::unique_ptr<const WasmModule> module(decoding_result.val); |
+ CHECK(!decoding_result.failed()); |
+ |
+ MaybeHandle<FixedArray> compiled_module = |
+ module->CompileFunctions(isolate, &thrower); |
+ CHECK(!compiled_module.is_null()); |
+ Handle<JSObject> module_obj = |
+ CreateCompiledModuleObject(isolate, compiled_module.ToHandleChecked()); |
+ v8::Local<v8::Object> v8_module_obj = v8::Utils::ToLocal(module_obj); |
+ CHECK(v8_module_obj->IsWebAssemblyCompiledModule()); |
+ |
+ v8::Local<v8::WasmCompiledModule> v8_compiled_module = |
+ v8_module_obj.As<v8::WasmCompiledModule>(); |
+ data = v8_compiled_module->Serialize(); |
+ } |
+ |
+ v8::Isolate::CreateParams create_params; |
+ create_params.array_buffer_allocator = isolate->array_buffer_allocator(); |
+ |
+ v8::Isolate* v8_isolate = v8::Isolate::New(create_params); |
+ isolate = reinterpret_cast<Isolate*>(v8_isolate); |
+ { |
+ v8::Isolate::Scope isolate_scope(v8_isolate); |
+ v8::HandleScope new_scope(v8_isolate); |
+ v8::Local<v8::Context> new_ctx = v8::Context::New(v8_isolate); |
+ new_ctx->Enter(); |
+ |
+ v8::MaybeLocal<v8::WasmCompiledModule> deserialized = |
+ v8::WasmCompiledModule::Deserialize(v8_isolate, data); |
+ v8::Local<v8::WasmCompiledModule> compiled_module; |
+ CHECK(deserialized.ToLocal(&compiled_module)); |
+ Handle<JSObject> module_object = |
+ Handle<JSObject>::cast(v8::Utils::OpenHandle(*compiled_module)); |
+ Handle<FixedArray> compiled_part = |
+ handle(FixedArray::cast(module_object->GetInternalField(0))); |
+ Handle<JSObject> instance = |
+ WasmModule::Instantiate(isolate, compiled_part, |
+ Handle<JSReceiver>::null(), |
+ Handle<JSArrayBuffer>::null()) |
+ .ToHandleChecked(); |
+ Handle<Object> params[1] = {Handle<Object>(Smi::FromInt(41), isolate)}; |
+ int32_t result = testing::CallFunction(isolate, instance, &thrower, |
+ kFunctionName, 1, params); |
+ CHECK(result == 42); |
+ new_ctx->Exit(); |
+ } |
+} |