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(); | |
bradnelson
2017/02/18 00:28:43
Some reason this ended up indented by 4 (should be
Mircea Trofin
2017/02/18 00:41:47
huh... no idea. Emacs?
| |
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 synchrnously, but we allow async compile | |
bradnelson
2017/02/18 00:28:42
typo
Mircea Trofin
2017/02/18 00:41:47
Acknowledged.
| |
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 | |
36 WebAssembly.instantiate(m) | |
37 .then(instance => i = instance, | |
38 assertUnreachable); | |
39 %RunMicrotasks(); | |
40 assertTrue(i instanceof WebAssembly.Instance); | |
41 | |
42 try { | |
43 m = new WebAssembly.Module(buffer); | |
44 assertUnreachable(); | |
45 } catch (e) { | |
46 assertTrue(e instanceof RangeError); | |
47 } | |
48 | |
49 WebAssembly.compile(buffer) | |
50 .then(res => m = res, | |
51 m = null); | |
52 | |
53 %RunMicrotasks(); | |
54 assertTrue(m instanceof WebAssembly.Module) | |
55 | |
56 WebAssembly.instantiate(buffer) | |
57 .then(res => m = res.module, | |
58 m = null); | |
59 | |
60 %RunMicrotasks(); | |
61 assertTrue(m instanceof WebAssembly.Module); | |
62 | |
63 // not even async compile works. | |
64 var ex; | |
65 %SetWasmCompileControls(buffer.byteLength - 1, false); | |
66 WebAssembly.compile(buffer) | |
67 .then(ex = null, | |
68 e => ex = e); | |
69 | |
70 %RunMicrotasks(); | |
71 assertTrue(ex instanceof RangeError); | |
72 | |
73 WebAssembly.instantiate(buffer) | |
74 .then(ex = null, | |
75 e => ex = e); | |
76 %RunMicrotasks(); | |
77 assertTrue(ex instanceof RangeError); | |
78 | |
79 | |
80 // For asm-wasm, these controls are ignored. | |
81 %SetWasmCompileControls(0, false); | |
82 function assertValidAsm(func) { | |
83 assertTrue(%IsAsmWasmCode(func)); | |
84 } | |
85 | |
86 function assertWasm(expected, func) { | |
87 assertEquals( | |
88 expected, func(undefined, undefined, new ArrayBuffer(1024)).caller()); | |
89 assertValidAsm(func); | |
90 } | |
91 | |
92 function TestReturnInBlock() { | |
bradnelson
2017/02/18 00:28:42
Leftover?
Mircea Trofin
2017/02/18 00:41:47
No! Intended. See comment above: we're making sure
| |
93 "use asm"; | |
94 function caller() { | |
95 return 43; | |
96 } | |
97 | |
98 return {caller: caller}; | |
99 } | |
100 | |
101 assertWasm(43, TestReturnInBlock); | |
OLD | NEW |