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/encoder.h" | 8 #include "src/wasm/encoder.h" |
9 #include "src/wasm/module-decoder.h" | 9 #include "src/wasm/module-decoder.h" |
10 #include "src/wasm/wasm-js.h" | 10 #include "src/wasm/wasm-js.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 using namespace v8::internal::wasm; | 22 using namespace v8::internal::wasm; |
23 | 23 |
24 namespace { | 24 namespace { |
25 void TestModule(Zone* zone, WasmModuleBuilder* builder, | 25 void TestModule(Zone* zone, WasmModuleBuilder* builder, |
26 int32_t expected_result) { | 26 int32_t expected_result) { |
27 ZoneBuffer buffer(zone); | 27 ZoneBuffer buffer(zone); |
28 builder->WriteTo(buffer); | 28 builder->WriteTo(buffer); |
29 | 29 |
30 Isolate* isolate = CcTest::InitIsolateOnce(); | 30 Isolate* isolate = CcTest::InitIsolateOnce(); |
31 HandleScope scope(isolate); | 31 HandleScope scope(isolate); |
32 WasmJs::InstallWasmFunctionMap(isolate, isolate->native_context()); | 32 WasmJs::SetupIsolateForWasm(isolate); |
33 int32_t result = | 33 int32_t result = testing::CompileAndRunWasmModule( |
34 testing::CompileAndRunWasmModule(isolate, buffer.begin(), buffer.end()); | 34 isolate, buffer.begin(), buffer.end(), ModuleOrigin::kWasmOrigin); |
35 CHECK_EQ(expected_result, result); | 35 CHECK_EQ(expected_result, result); |
36 } | 36 } |
37 | 37 |
38 void ExportAs(WasmFunctionBuilder* f, const char* name) { | 38 void ExportAs(WasmFunctionBuilder* f, const char* name) { |
39 f->SetExported(); | 39 f->SetExported(); |
40 f->SetName(name, static_cast<int>(strlen(name))); | 40 f->SetName(name, static_cast<int>(strlen(name))); |
41 } | 41 } |
42 | 42 |
43 void ExportAsMain(WasmFunctionBuilder* f) { | 43 void ExportAsMain(WasmFunctionBuilder* f) { |
44 static const char kMainName[] = "main"; | 44 static const char kMainName[] = "main"; |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 f->SetSignature(sigs.i_v()); | 176 f->SetSignature(sigs.i_v()); |
177 ExportAsMain(f); | 177 ExportAsMain(f); |
178 byte code2[] = {WASM_SET_GLOBAL(global1, WASM_I32V_1(56)), | 178 byte code2[] = {WASM_SET_GLOBAL(global1, WASM_I32V_1(56)), |
179 WASM_SET_GLOBAL(global2, WASM_I32V_1(41)), | 179 WASM_SET_GLOBAL(global2, WASM_I32V_1(41)), |
180 WASM_RETURN1(WASM_CALL_FUNCTION0(f1_index))}; | 180 WASM_RETURN1(WASM_CALL_FUNCTION0(f1_index))}; |
181 f->EmitCode(code2, sizeof(code2)); | 181 f->EmitCode(code2, sizeof(code2)); |
182 TestModule(&zone, builder, 97); | 182 TestModule(&zone, builder, 97); |
183 } | 183 } |
184 | 184 |
185 TEST(Run_WasmModule_Serialization) { | 185 TEST(Run_WasmModule_Serialization) { |
186 FLAG_expose_wasm = true; | |
187 static const char* kFunctionName = "increment"; | 186 static const char* kFunctionName = "increment"; |
188 v8::base::AccountingAllocator allocator; | 187 v8::base::AccountingAllocator allocator; |
189 Zone zone(&allocator); | 188 Zone zone(&allocator); |
190 | 189 |
191 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 190 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
192 uint16_t f_index = builder->AddFunction(); | 191 uint16_t f_index = builder->AddFunction(); |
193 TestSignatures sigs; | 192 TestSignatures sigs; |
194 | 193 |
195 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | 194 WasmFunctionBuilder* f = builder->FunctionAt(f_index); |
196 f->SetSignature(sigs.i_i()); | 195 f->SetSignature(sigs.i_i()); |
197 byte code[] = {WASM_GET_LOCAL(0), kExprI32Const, 1, kExprI32Add}; | 196 byte code[] = {WASM_GET_LOCAL(0), kExprI32Const, 1, kExprI32Add}; |
198 f->EmitCode(code, sizeof(code)); | 197 f->EmitCode(code, sizeof(code)); |
199 ExportAs(f, kFunctionName); | 198 ExportAs(f, kFunctionName); |
200 | 199 |
201 ZoneBuffer buffer(&zone); | 200 ZoneBuffer buffer(&zone); |
202 builder->WriteTo(buffer); | 201 builder->WriteTo(buffer); |
203 | 202 |
204 v8::Isolate::CreateParams create_params; | 203 Isolate* isolate = CcTest::InitIsolateOnce(); |
205 create_params.array_buffer_allocator = | |
206 CcTest::InitIsolateOnce()->array_buffer_allocator(); | |
207 | |
208 v8::Isolate* v8_isolate = v8::Isolate::New(create_params); | |
209 Isolate* isolate = reinterpret_cast<Isolate*>(v8_isolate); | |
210 v8::HandleScope new_scope(v8_isolate); | |
211 v8::Local<v8::Context> new_ctx = v8::Context::New(v8_isolate); | |
212 new_ctx->Enter(); | |
213 | |
214 ErrorThrower thrower(isolate, ""); | 204 ErrorThrower thrower(isolate, ""); |
215 v8::WasmCompiledModule::SerializedModule data; | 205 v8::WasmCompiledModule::SerializedModule data; |
216 { | 206 { |
217 HandleScope scope(isolate); | 207 HandleScope scope(isolate); |
| 208 WasmJs::SetupIsolateForWasm(isolate); |
218 | 209 |
219 ModuleResult decoding_result = DecodeWasmModule( | 210 ModuleResult decoding_result = DecodeWasmModule( |
220 isolate, &zone, buffer.begin(), buffer.end(), false, kWasmOrigin); | 211 isolate, &zone, buffer.begin(), buffer.end(), false, kWasmOrigin); |
221 std::unique_ptr<const WasmModule> module(decoding_result.val); | 212 std::unique_ptr<const WasmModule> module(decoding_result.val); |
222 CHECK(!decoding_result.failed()); | 213 CHECK(!decoding_result.failed()); |
223 | 214 |
224 MaybeHandle<FixedArray> compiled_module = | 215 MaybeHandle<FixedArray> compiled_module = |
225 module->CompileFunctions(isolate, &thrower); | 216 module->CompileFunctions(isolate, &thrower); |
226 CHECK(!compiled_module.is_null()); | 217 CHECK(!compiled_module.is_null()); |
227 Handle<JSObject> module_obj = CreateCompiledModuleObject( | 218 Handle<JSObject> module_obj = CreateCompiledModuleObject( |
228 isolate, compiled_module.ToHandleChecked(), ModuleOrigin::kWasmOrigin); | 219 isolate, compiled_module.ToHandleChecked(), ModuleOrigin::kWasmOrigin); |
229 v8::Local<v8::Object> v8_module_obj = v8::Utils::ToLocal(module_obj); | 220 v8::Local<v8::Object> v8_module_obj = v8::Utils::ToLocal(module_obj); |
230 CHECK(v8_module_obj->IsWebAssemblyCompiledModule()); | 221 CHECK(v8_module_obj->IsWebAssemblyCompiledModule()); |
231 | 222 |
232 v8::Local<v8::WasmCompiledModule> v8_compiled_module = | 223 v8::Local<v8::WasmCompiledModule> v8_compiled_module = |
233 v8_module_obj.As<v8::WasmCompiledModule>(); | 224 v8_module_obj.As<v8::WasmCompiledModule>(); |
234 data = v8_compiled_module->Serialize(); | 225 data = v8_compiled_module->Serialize(); |
235 } | 226 } |
236 | 227 |
237 create_params.array_buffer_allocator = isolate->array_buffer_allocator(); | 228 v8::Isolate::CreateParams create_params; |
| 229 create_params.array_buffer_allocator = |
| 230 CcTest::InitIsolateOnce()->array_buffer_allocator(); |
238 | 231 |
239 isolate = reinterpret_cast<Isolate*>(v8_isolate); | 232 v8::Isolate* v8_isolate = v8::Isolate::New(create_params); |
240 { | 233 { |
241 v8::Isolate::Scope isolate_scope(v8_isolate); | 234 v8::Isolate::Scope isolate_scope(v8_isolate); |
242 v8::HandleScope new_scope(v8_isolate); | 235 v8::HandleScope new_scope(v8_isolate); |
243 v8::Local<v8::Context> new_ctx = v8::Context::New(v8_isolate); | 236 v8::Local<v8::Context> new_ctx = v8::Context::New(v8_isolate); |
244 new_ctx->Enter(); | 237 new_ctx->Enter(); |
| 238 isolate = reinterpret_cast<Isolate*>(v8_isolate); |
| 239 WasmJs::SetupIsolateForWasm(isolate); |
245 | 240 |
246 v8::MaybeLocal<v8::WasmCompiledModule> deserialized = | 241 v8::MaybeLocal<v8::WasmCompiledModule> deserialized = |
247 v8::WasmCompiledModule::Deserialize(v8_isolate, data); | 242 v8::WasmCompiledModule::Deserialize(v8_isolate, data); |
248 v8::Local<v8::WasmCompiledModule> compiled_module; | 243 v8::Local<v8::WasmCompiledModule> compiled_module; |
249 CHECK(deserialized.ToLocal(&compiled_module)); | 244 CHECK(deserialized.ToLocal(&compiled_module)); |
250 Handle<JSObject> module_object = | 245 Handle<JSObject> module_object = |
251 Handle<JSObject>::cast(v8::Utils::OpenHandle(*compiled_module)); | 246 Handle<JSObject>::cast(v8::Utils::OpenHandle(*compiled_module)); |
252 Handle<FixedArray> compiled_part = | |
253 handle(FixedArray::cast(module_object->GetInternalField(0))); | |
254 Handle<JSObject> instance = | 247 Handle<JSObject> instance = |
255 WasmModule::Instantiate(isolate, compiled_part, | 248 WasmModule::Instantiate(isolate, module_object, |
256 Handle<JSReceiver>::null(), | 249 Handle<JSReceiver>::null(), |
257 Handle<JSArrayBuffer>::null()) | 250 Handle<JSArrayBuffer>::null()) |
258 .ToHandleChecked(); | 251 .ToHandleChecked(); |
259 Handle<Object> params[1] = {Handle<Object>(Smi::FromInt(41), isolate)}; | 252 Handle<Object> params[1] = {Handle<Object>(Smi::FromInt(41), isolate)}; |
260 int32_t result = testing::CallWasmFunctionForTesting( | 253 int32_t result = testing::CallWasmFunctionForTesting( |
261 isolate, instance, thrower, kFunctionName, 1, params); | 254 isolate, instance, thrower, kFunctionName, 1, params, |
| 255 ModuleOrigin::kWasmOrigin); |
262 CHECK(result == 42); | 256 CHECK(result == 42); |
263 new_ctx->Exit(); | 257 new_ctx->Exit(); |
264 } | 258 } |
265 } | 259 } |
266 | 260 |
267 TEST(Run_WasmModule_MemSize_GrowMem) { | 261 TEST(Run_WasmModule_MemSize_GrowMem) { |
268 static const int kPageSize = 0x10000; | 262 static const int kPageSize = 0x10000; |
269 // Initial memory size = 16 + GrowMemory(10) | 263 // Initial memory size = 16 + GrowMemory(10) |
270 static const int kExpectedValue = kPageSize * 26; | 264 static const int kExpectedValue = kPageSize * 26; |
271 TestSignatures sigs; | 265 TestSignatures sigs; |
(...skipping 17 matching lines...) Expand all Loading... |
289 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 283 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
290 uint16_t f_index = builder->AddFunction(); | 284 uint16_t f_index = builder->AddFunction(); |
291 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | 285 WasmFunctionBuilder* f = builder->FunctionAt(f_index); |
292 f->SetSignature(sigs.i_v()); | 286 f->SetSignature(sigs.i_v()); |
293 ExportAsMain(f); | 287 ExportAsMain(f); |
294 byte code[] = {WASM_IF_ELSE(WASM_I32V(0), WASM_GROW_MEMORY(WASM_I32V(1)), | 288 byte code[] = {WASM_IF_ELSE(WASM_I32V(0), WASM_GROW_MEMORY(WASM_I32V(1)), |
295 WASM_I32V(12))}; | 289 WASM_I32V(12))}; |
296 f->EmitCode(code, sizeof(code)); | 290 f->EmitCode(code, sizeof(code)); |
297 TestModule(&zone, builder, 12); | 291 TestModule(&zone, builder, 12); |
298 } | 292 } |
OLD | NEW |