| 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 411 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 422 } | 422 } |
| 423 | 423 |
| 424 v8::TryCatch try_catch(reinterpret_cast<v8::Isolate*>(isolate)); | 424 v8::TryCatch try_catch(reinterpret_cast<v8::Isolate*>(isolate)); |
| 425 Handle<Object> params[1] = { | 425 Handle<Object> params[1] = { |
| 426 Handle<Object>(Smi::FromInt(25 * kPageSize), isolate)}; | 426 Handle<Object>(Smi::FromInt(25 * kPageSize), isolate)}; |
| 427 testing::RunWasmModuleForTesting(isolate, instance, 1, params, | 427 testing::RunWasmModuleForTesting(isolate, instance, 1, params, |
| 428 ModuleOrigin::kWasmOrigin); | 428 ModuleOrigin::kWasmOrigin); |
| 429 CHECK(try_catch.HasCaught()); | 429 CHECK(try_catch.HasCaught()); |
| 430 isolate->clear_pending_exception(); | 430 isolate->clear_pending_exception(); |
| 431 } | 431 } |
| 432 | |
| 433 TEST(Run_WasmModule_Global_init) { | |
| 434 v8::internal::AccountingAllocator allocator; | |
| 435 Zone zone(&allocator); | |
| 436 TestSignatures sigs; | |
| 437 | |
| 438 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | |
| 439 uint32_t global1 = | |
| 440 builder->AddGlobal(kAstI32, false, false, WasmInitExpr(777777)); | |
| 441 uint32_t global2 = | |
| 442 builder->AddGlobal(kAstI32, false, false, WasmInitExpr(222222)); | |
| 443 WasmFunctionBuilder* f1 = builder->AddFunction(sigs.i_v()); | |
| 444 byte code[] = { | |
| 445 WASM_I32_ADD(WASM_GET_GLOBAL(global1), WASM_GET_GLOBAL(global2))}; | |
| 446 f1->EmitCode(code, sizeof(code)); | |
| 447 ExportAsMain(f1); | |
| 448 TestModule(&zone, builder, 999999); | |
| 449 } | |
| 450 | |
| 451 template <typename CType> | |
| 452 static void RunWasmModuleGlobalInitTest(LocalType type, CType expected) { | |
| 453 v8::internal::AccountingAllocator allocator; | |
| 454 Zone zone(&allocator); | |
| 455 TestSignatures sigs; | |
| 456 | |
| 457 LocalType types[] = {type}; | |
| 458 FunctionSig sig(1, 0, types); | |
| 459 | |
| 460 for (int padding = 0; padding < 5; padding++) { | |
| 461 // Test with a simple initializer | |
| 462 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | |
| 463 | |
| 464 for (int i = 0; i < padding; i++) { // pad global before | |
| 465 builder->AddGlobal(kAstI32, false, false, WasmInitExpr(i + 20000)); | |
| 466 } | |
| 467 uint32_t global = | |
| 468 builder->AddGlobal(type, false, false, WasmInitExpr(expected)); | |
| 469 for (int i = 0; i < padding; i++) { // pad global after | |
| 470 builder->AddGlobal(kAstI32, false, false, WasmInitExpr(i + 30000)); | |
| 471 } | |
| 472 | |
| 473 WasmFunctionBuilder* f1 = builder->AddFunction(&sig); | |
| 474 byte code[] = {WASM_GET_GLOBAL(global)}; | |
| 475 f1->EmitCode(code, sizeof(code)); | |
| 476 ExportAsMain(f1); | |
| 477 TestModule(&zone, builder, expected); | |
| 478 } | |
| 479 | |
| 480 for (int padding = 0; padding < 5; padding++) { | |
| 481 // Test with a global index | |
| 482 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | |
| 483 for (int i = 0; i < padding; i++) { // pad global before | |
| 484 builder->AddGlobal(kAstI32, false, false, WasmInitExpr(i + 40000)); | |
| 485 } | |
| 486 | |
| 487 uint32_t global1 = | |
| 488 builder->AddGlobal(type, false, false, WasmInitExpr(expected)); | |
| 489 | |
| 490 for (int i = 0; i < padding; i++) { // pad global middle | |
| 491 builder->AddGlobal(kAstI32, false, false, WasmInitExpr(i + 50000)); | |
| 492 } | |
| 493 | |
| 494 uint32_t global2 = builder->AddGlobal( | |
| 495 type, false, false, WasmInitExpr(WasmInitExpr::kGlobalIndex, global1)); | |
| 496 | |
| 497 for (int i = 0; i < padding; i++) { // pad global after | |
| 498 builder->AddGlobal(kAstI32, false, false, WasmInitExpr(i + 60000)); | |
| 499 } | |
| 500 | |
| 501 WasmFunctionBuilder* f1 = builder->AddFunction(&sig); | |
| 502 byte code[] = {WASM_GET_GLOBAL(global2)}; | |
| 503 f1->EmitCode(code, sizeof(code)); | |
| 504 ExportAsMain(f1); | |
| 505 TestModule(&zone, builder, expected); | |
| 506 } | |
| 507 } | |
| 508 | |
| 509 TEST(Run_WasmModule_Global_i32) { | |
| 510 RunWasmModuleGlobalInitTest<int32_t>(kAstI32, -983489); | |
| 511 RunWasmModuleGlobalInitTest<int32_t>(kAstI32, 11223344); | |
| 512 } | |
| 513 | |
| 514 TEST(Run_WasmModule_Global_f32) { | |
| 515 RunWasmModuleGlobalInitTest<float>(kAstF32, -983.9f); | |
| 516 RunWasmModuleGlobalInitTest<float>(kAstF32, 1122.99f); | |
| 517 } | |
| 518 | |
| 519 TEST(Run_WasmModule_Global_f64) { | |
| 520 RunWasmModuleGlobalInitTest<double>(kAstF64, -833.9); | |
| 521 RunWasmModuleGlobalInitTest<double>(kAstF64, 86374.25); | |
| 522 } | |
| OLD | NEW |