| 1 |
assertErrorMessage(() => wasmEvalText('(module (func) (start 0) (start 0))'), SyntaxError, /wasm text error/); |
| 2 |
assertErrorMessage(() => wasmEvalText('(module (func) (start $unknown))'), SyntaxError, /failed to find/); |
| 3 |
|
| 4 |
wasmFailValidateText('(module (func) (start 1))', /unknown start function/); |
| 5 |
wasmFailValidateText('(module (func (param i32)) (start 0))', /must be nullary/); |
| 6 |
wasmFailValidateText('(module (func (param i32) (param f32)) (start 0))', /must be nullary/); |
| 7 |
wasmFailValidateText('(module (func (param i32) (param f32) (param f64)) (start 0))', /must be nullary/); |
| 8 |
wasmFailValidateText('(module (func (result f32)) (start 0))', /must not return anything/); |
| 9 |
|
| 10 |
// Basic use case. |
| 11 |
var count = 0; |
| 12 |
function inc() { count++; } |
| 13 |
var exports = wasmEvalText(`(module (import "" "inc" (func $imp)) (func $f (call $imp)) (start $f))`, { "":{inc} }).exports; |
| 14 |
assertEq(count, 1); |
| 15 |
assertEq(Object.keys(exports).length, 0); |
| 16 |
|
| 17 |
count = 0; |
| 18 |
exports = wasmEvalText(`(module (import "" "inc" (func)) (func $start (call 0)) (start $start) (export "" (func 0)))`, { "":{inc} }).exports; |
| 19 |
assertEq(count, 1); |
| 20 |
assertEq(typeof exports[""], 'function'); |
| 21 |
assertEq(exports[""](), undefined); |
| 22 |
assertEq(count, 2); |
| 23 |
|
| 24 |
// New API. |
| 25 |
const Module = WebAssembly.Module; |
| 26 |
const Instance = WebAssembly.Instance; |
| 27 |
|
| 28 |
count = 0; |
| 29 |
const m = new Module(wasmTextToBinary('(module (import "" "inc" (func $imp)) (func) (func $start (call $imp)) (start $start) (export "" (func $start)))')); |
| 30 |
assertEq(count, 0); |
| 31 |
|
| 32 |
assertErrorMessage(() => new Instance(m), TypeError, /second argument must be an object/); |
| 33 |
assertEq(count, 0); |
| 34 |
|
| 35 |
const i1 = new Instance(m, { "":{inc} }); |
| 36 |
assertEq(count, 1); |
| 37 |
i1.exports[""](); |
| 38 |
assertEq(count, 2); |
| 39 |
|
| 40 |
const i2 = new Instance(m, { "":{inc} }); |
| 41 |
assertEq(count, 3); |
| 42 |
|
| 43 |
function fail() { assertEq(true, false); } |
| 44 |
|
| 45 |
count = 0; |
| 46 |
const m2 = new Module(wasmTextToBinary('(module (import "" "fail" (func)) (import "" "inc" (func $imp)) (func) (start $imp))')); |
| 47 |
assertEq(count, 0); |
| 48 |
new Instance(m2, {"":{ inc, fail }}); |
| 49 |
assertEq(count, 1); |