| OLD | NEW |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stdlib.h> | 5 #include <stdlib.h> |
| 6 #include <string.h> | 6 #include <string.h> |
| 7 | 7 |
| 8 #include "src/wasm/module-decoder.h" | 8 #include "src/wasm/module-decoder.h" |
| 9 #include "src/wasm/wasm-macro-gen.h" | 9 #include "src/wasm/wasm-macro-gen.h" |
| 10 #include "src/wasm/wasm-module-builder.h" | 10 #include "src/wasm/wasm-module-builder.h" |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 WasmFunctionBuilder* f = builder->AddFunction(sigs.i_i()); | 190 WasmFunctionBuilder* f = builder->AddFunction(sigs.i_i()); |
| 191 byte code[] = {WASM_GET_LOCAL(0), kExprI32Const, 1, kExprI32Add}; | 191 byte code[] = {WASM_GET_LOCAL(0), kExprI32Const, 1, kExprI32Add}; |
| 192 f->EmitCode(code, sizeof(code)); | 192 f->EmitCode(code, sizeof(code)); |
| 193 ExportAs(f, kFunctionName); | 193 ExportAs(f, kFunctionName); |
| 194 | 194 |
| 195 ZoneBuffer buffer(&zone); | 195 ZoneBuffer buffer(&zone); |
| 196 builder->WriteTo(buffer); | 196 builder->WriteTo(buffer); |
| 197 | 197 |
| 198 Isolate* isolate = CcTest::InitIsolateOnce(); | 198 Isolate* isolate = CcTest::InitIsolateOnce(); |
| 199 ErrorThrower thrower(isolate, ""); | 199 ErrorThrower thrower(isolate, ""); |
| 200 uint8_t* bytes = nullptr; |
| 201 int buffer_size = -1; |
| 200 v8::WasmCompiledModule::SerializedModule data; | 202 v8::WasmCompiledModule::SerializedModule data; |
| 201 { | 203 { |
| 202 HandleScope scope(isolate); | 204 HandleScope scope(isolate); |
| 203 testing::SetupIsolateForWasmModule(isolate); | 205 testing::SetupIsolateForWasmModule(isolate); |
| 204 | 206 |
| 205 ModuleResult decoding_result = DecodeWasmModule( | 207 ModuleResult decoding_result = DecodeWasmModule( |
| 206 isolate, &zone, buffer.begin(), buffer.end(), false, kWasmOrigin); | 208 isolate, &zone, buffer.begin(), buffer.end(), false, kWasmOrigin); |
| 207 std::unique_ptr<const WasmModule> module(decoding_result.val); | 209 std::unique_ptr<const WasmModule> module(decoding_result.val); |
| 208 CHECK(!decoding_result.failed()); | 210 CHECK(!decoding_result.failed()); |
| 209 | 211 |
| 210 MaybeHandle<FixedArray> compiled_module = | 212 MaybeHandle<FixedArray> compiled_module = |
| 211 module->CompileFunctions(isolate, &thrower); | 213 module->CompileFunctions(isolate, &thrower); |
| 212 CHECK(!compiled_module.is_null()); | 214 CHECK(!compiled_module.is_null()); |
| 213 Handle<JSObject> module_obj = CreateCompiledModuleObject( | 215 Handle<JSObject> module_obj = CreateCompiledModuleObject( |
| 214 isolate, compiled_module.ToHandleChecked(), ModuleOrigin::kWasmOrigin); | 216 isolate, compiled_module.ToHandleChecked(), ModuleOrigin::kWasmOrigin); |
| 215 v8::Local<v8::Object> v8_module_obj = v8::Utils::ToLocal(module_obj); | 217 v8::Local<v8::Object> v8_module_obj = v8::Utils::ToLocal(module_obj); |
| 216 CHECK(v8_module_obj->IsWebAssemblyCompiledModule()); | 218 CHECK(v8_module_obj->IsWebAssemblyCompiledModule()); |
| 217 | 219 |
| 218 v8::Local<v8::WasmCompiledModule> v8_compiled_module = | 220 v8::Local<v8::WasmCompiledModule> v8_compiled_module = |
| 219 v8_module_obj.As<v8::WasmCompiledModule>(); | 221 v8_module_obj.As<v8::WasmCompiledModule>(); |
| 222 v8::Local<v8::String> uncompiled_bytes = |
| 223 v8_compiled_module->GetUncompiledBytes(); |
| 224 buffer_size = uncompiled_bytes->Length(); |
| 225 bytes = zone.NewArray<uint8_t>(buffer_size); |
| 226 uncompiled_bytes->WriteOneByte(bytes); |
| 220 data = v8_compiled_module->Serialize(); | 227 data = v8_compiled_module->Serialize(); |
| 221 } | 228 } |
| 222 | 229 |
| 223 v8::Isolate::CreateParams create_params; | 230 v8::Isolate::CreateParams create_params; |
| 224 create_params.array_buffer_allocator = | 231 create_params.array_buffer_allocator = |
| 225 CcTest::InitIsolateOnce()->array_buffer_allocator(); | 232 CcTest::InitIsolateOnce()->array_buffer_allocator(); |
| 226 | 233 |
| 227 v8::Isolate* v8_isolate = v8::Isolate::New(create_params); | 234 for (int i = 0; i < 2; ++i) { |
| 228 { | 235 v8::Isolate* v8_isolate = v8::Isolate::New(create_params); |
| 229 v8::Isolate::Scope isolate_scope(v8_isolate); | 236 if (i == 1) { |
| 230 v8::HandleScope new_scope(v8_isolate); | 237 // Mess with the serialized data to force recompilation. |
| 231 v8::Local<v8::Context> new_ctx = v8::Context::New(v8_isolate); | 238 data.first.reset(); |
| 232 new_ctx->Enter(); | 239 data.second = 0; |
| 233 isolate = reinterpret_cast<Isolate*>(v8_isolate); | 240 } |
| 234 testing::SetupIsolateForWasmModule(isolate); | 241 { |
| 235 | 242 v8::Isolate::Scope isolate_scope(v8_isolate); |
| 236 v8::MaybeLocal<v8::WasmCompiledModule> deserialized = | 243 v8::HandleScope new_scope(v8_isolate); |
| 237 v8::WasmCompiledModule::Deserialize(v8_isolate, data); | 244 v8::Local<v8::Context> new_ctx = v8::Context::New(v8_isolate); |
| 238 v8::Local<v8::WasmCompiledModule> compiled_module; | 245 new_ctx->Enter(); |
| 239 CHECK(deserialized.ToLocal(&compiled_module)); | 246 isolate = reinterpret_cast<Isolate*>(v8_isolate); |
| 240 Handle<JSObject> module_object = | 247 testing::SetupIsolateForWasmModule(isolate); |
| 241 Handle<JSObject>::cast(v8::Utils::OpenHandle(*compiled_module)); | 248 Vector<const uint8_t> raw(bytes, buffer_size); |
| 242 Handle<JSObject> instance = | 249 v8::MaybeLocal<v8::WasmCompiledModule> deserialized = |
| 243 WasmModule::Instantiate(isolate, &thrower, module_object, | 250 v8::WasmCompiledModule::DeserializeOrCompile( |
| 244 Handle<JSReceiver>::null(), | 251 v8_isolate, data, |
| 245 Handle<JSArrayBuffer>::null()) | 252 v8::Utils::ToLocal(isolate->factory() |
| 246 .ToHandleChecked(); | 253 ->NewStringFromOneByte(raw) |
| 247 Handle<Object> params[1] = {Handle<Object>(Smi::FromInt(41), isolate)}; | 254 .ToHandleChecked())); |
| 248 int32_t result = testing::CallWasmFunctionForTesting( | 255 v8::Local<v8::WasmCompiledModule> compiled_module; |
| 249 isolate, instance, &thrower, kFunctionName, 1, params, | 256 CHECK(deserialized.ToLocal(&compiled_module)); |
| 250 ModuleOrigin::kWasmOrigin); | 257 Handle<JSObject> module_object = |
| 251 CHECK(result == 42); | 258 Handle<JSObject>::cast(v8::Utils::OpenHandle(*compiled_module)); |
| 252 new_ctx->Exit(); | 259 Handle<JSObject> instance = |
| 260 WasmModule::Instantiate(isolate, &thrower, module_object, |
| 261 Handle<JSReceiver>::null(), |
| 262 Handle<JSArrayBuffer>::null()) |
| 263 .ToHandleChecked(); |
| 264 Handle<Object> params[1] = {Handle<Object>(Smi::FromInt(41), isolate)}; |
| 265 int32_t result = testing::CallWasmFunctionForTesting( |
| 266 isolate, instance, &thrower, kFunctionName, 1, params, |
| 267 ModuleOrigin::kWasmOrigin); |
| 268 CHECK(result == 42); |
| 269 new_ctx->Exit(); |
| 270 } |
| 271 v8_isolate->Dispose(); |
| 253 } | 272 } |
| 254 } | 273 } |
| 255 | 274 |
| 256 TEST(MemorySize) { | 275 TEST(MemorySize) { |
| 257 // Initial memory size is 16, see wasm-module-builder.cc | 276 // Initial memory size is 16, see wasm-module-builder.cc |
| 258 static const int kExpectedValue = 16; | 277 static const int kExpectedValue = 16; |
| 259 TestSignatures sigs; | 278 TestSignatures sigs; |
| 260 v8::internal::AccountingAllocator allocator; | 279 v8::internal::AccountingAllocator allocator; |
| 261 Zone zone(&allocator); | 280 Zone zone(&allocator); |
| 262 | 281 |
| (...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 513 | 532 |
| 514 TEST(Run_WasmModule_Global_f32) { | 533 TEST(Run_WasmModule_Global_f32) { |
| 515 RunWasmModuleGlobalInitTest<float>(kAstF32, -983.9f); | 534 RunWasmModuleGlobalInitTest<float>(kAstF32, -983.9f); |
| 516 RunWasmModuleGlobalInitTest<float>(kAstF32, 1122.99f); | 535 RunWasmModuleGlobalInitTest<float>(kAstF32, 1122.99f); |
| 517 } | 536 } |
| 518 | 537 |
| 519 TEST(Run_WasmModule_Global_f64) { | 538 TEST(Run_WasmModule_Global_f64) { |
| 520 RunWasmModuleGlobalInitTest<double>(kAstF64, -833.9); | 539 RunWasmModuleGlobalInitTest<double>(kAstF64, -833.9); |
| 521 RunWasmModuleGlobalInitTest<double>(kAstF64, 86374.25); | 540 RunWasmModuleGlobalInitTest<double>(kAstF64, 86374.25); |
| 522 } | 541 } |
| OLD | NEW |