OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Flags: --allow-natives-syntax --validate-asm |
| 6 |
| 7 load("test/mjsunit/wasm/wasm-constants.js"); |
| 8 load("test/mjsunit/wasm/wasm-module-builder.js"); |
| 9 |
| 10 let buffer = (() => { |
| 11 var builder = new WasmModuleBuilder(); |
| 12 builder.addFunction("f", kSig_i_v) |
| 13 .addBody([kExprI32Const, 42]) |
| 14 .exportAs("f"); |
| 15 return builder.toBuffer(); |
| 16 })(); |
| 17 |
| 18 // allow up to the buffer size |
| 19 %SetWasmCompileControls(buffer.byteLength, true); |
| 20 %SetWasmInstantiateControls(); |
| 21 var m = new WebAssembly.Module(buffer); |
| 22 var i = new WebAssembly.Instance(m); |
| 23 assertEquals(i.exports.f(), 42); |
| 24 |
| 25 // the buffer can't compile synchronously, but we allow async compile |
| 26 %SetWasmCompileControls(buffer.byteLength - 1, true); |
| 27 // test first that we can't sync-instantiate this module anymore |
| 28 try { |
| 29 i = new WebAssembly.Instance(m); |
| 30 } catch (e) { |
| 31 assertTrue(e instanceof RangeError); |
| 32 } |
| 33 |
| 34 //...but we can async-instantiate it |
| 35 WebAssembly.instantiate(m) |
| 36 .then(instance => i = instance, |
| 37 assertUnreachable); |
| 38 %RunMicrotasks(); |
| 39 assertTrue(i instanceof WebAssembly.Instance); |
| 40 |
| 41 try { |
| 42 m = new WebAssembly.Module(buffer); |
| 43 assertUnreachable(); |
| 44 } catch (e) { |
| 45 assertTrue(e instanceof RangeError); |
| 46 } |
| 47 |
| 48 WebAssembly.compile(buffer) |
| 49 .then(res => m = res, |
| 50 m = null); |
| 51 |
| 52 %RunMicrotasks(); |
| 53 assertTrue(m instanceof WebAssembly.Module) |
| 54 |
| 55 WebAssembly.instantiate(buffer) |
| 56 .then(res => m = res.module, |
| 57 m = null); |
| 58 |
| 59 %RunMicrotasks(); |
| 60 assertTrue(m instanceof WebAssembly.Module); |
| 61 |
| 62 // Async compile works, but using the sync instantiate doesn't |
| 63 i = undefined; |
| 64 m = undefined; |
| 65 var ex = undefined; |
| 66 WebAssembly.compile(buffer) |
| 67 .then(mod => { |
| 68 m = mod; |
| 69 try { |
| 70 i = new WebAssembly.Instance(m); |
| 71 } catch (e) { |
| 72 ex = e; |
| 73 } |
| 74 }, |
| 75 e => ex = e); |
| 76 %RunMicrotasks(); |
| 77 assertTrue(ex instanceof RangeError); |
| 78 assertEquals(i, undefined); |
| 79 assertTrue(m instanceof WebAssembly.Module); |
| 80 |
| 81 // Now block async compile works. |
| 82 %SetWasmCompileControls(buffer.byteLength - 1, false); |
| 83 WebAssembly.compile(buffer) |
| 84 .then(ex = null, |
| 85 e => ex = e); |
| 86 |
| 87 %RunMicrotasks(); |
| 88 assertTrue(ex instanceof RangeError); |
| 89 |
| 90 WebAssembly.instantiate(buffer) |
| 91 .then(ex = null, |
| 92 e => ex = e); |
| 93 %RunMicrotasks(); |
| 94 assertTrue(ex instanceof RangeError); |
| 95 |
| 96 |
| 97 // Verify that, for asm-wasm, these controls are ignored. |
| 98 %SetWasmCompileControls(0, false); |
| 99 function assertValidAsm(func) { |
| 100 assertTrue(%IsAsmWasmCode(func)); |
| 101 } |
| 102 |
| 103 function assertWasm(expected, func) { |
| 104 assertEquals( |
| 105 expected, func(undefined, undefined, new ArrayBuffer(1024)).caller()); |
| 106 assertValidAsm(func); |
| 107 } |
| 108 |
| 109 function TestAsmWasmIsUnaffected() { |
| 110 "use asm"; |
| 111 function caller() { |
| 112 return 43; |
| 113 } |
| 114 |
| 115 return {caller: caller}; |
| 116 } |
| 117 |
| 118 assertWasm(43, TestAsmWasmIsUnaffected); |
OLD | NEW |