diff options
Diffstat (limited to 'js/src/jit-test/tests/wasm/spec/tail-call')
5 files changed, 1324 insertions, 0 deletions
diff --git a/js/src/jit-test/tests/wasm/spec/tail-call/directives.txt b/js/src/jit-test/tests/wasm/spec/tail-call/directives.txt new file mode 100644 index 0000000000..223b6b843a --- /dev/null +++ b/js/src/jit-test/tests/wasm/spec/tail-call/directives.txt @@ -0,0 +1 @@ +|jit-test| test-also=--wasm-compiler=optimizing; test-also=--wasm-compiler=baseline; test-also=--wasm-test-serialization; test-also=--test-wasm-await-tier2; test-also=--disable-wasm-huge-memory; skip-variant-if: --disable-wasm-huge-memory, !wasmHugeMemorySupported(); local-include:harness/harness.js; --wasm-tail-calls; skip-if: !wasmTailCallsEnabled()
\ No newline at end of file diff --git a/js/src/jit-test/tests/wasm/spec/tail-call/harness/directives.txt b/js/src/jit-test/tests/wasm/spec/tail-call/harness/directives.txt new file mode 100644 index 0000000000..d41243abbb --- /dev/null +++ b/js/src/jit-test/tests/wasm/spec/tail-call/harness/directives.txt @@ -0,0 +1 @@ +|jit-test| skip-if: true
\ No newline at end of file diff --git a/js/src/jit-test/tests/wasm/spec/tail-call/harness/harness.js b/js/src/jit-test/tests/wasm/spec/tail-call/harness/harness.js new file mode 100644 index 0000000000..a96781e8ed --- /dev/null +++ b/js/src/jit-test/tests/wasm/spec/tail-call/harness/harness.js @@ -0,0 +1,448 @@ +"use strict"; + +/* Copyright 2021 Mozilla Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +if (!wasmIsSupported()) { + quit(); +} + +function bytes(type, bytes) { + var typedBuffer = new Uint8Array(bytes); + return wasmGlobalFromArrayBuffer(type, typedBuffer.buffer); +} +function value(type, value) { + return new WebAssembly.Global({ + value: type, + mutable: false, + }, value); +} + +function i8x16(elements) { + let typedBuffer = new Uint8Array(elements); + return wasmGlobalFromArrayBuffer("v128", typedBuffer.buffer); +} +function i16x8(elements) { + let typedBuffer = new Uint16Array(elements); + return wasmGlobalFromArrayBuffer("v128", typedBuffer.buffer); +} +function i32x4(elements) { + let typedBuffer = new Uint32Array(elements); + return wasmGlobalFromArrayBuffer("v128", typedBuffer.buffer); +} +function i64x2(elements) { + let typedBuffer = new BigUint64Array(elements); + return wasmGlobalFromArrayBuffer("v128", typedBuffer.buffer); +} +function f32x4(elements) { + let typedBuffer = new Float32Array(elements); + return wasmGlobalFromArrayBuffer("v128", typedBuffer.buffer); +} +function f64x2(elements) { + let typedBuffer = new Float64Array(elements); + return wasmGlobalFromArrayBuffer("v128", typedBuffer.buffer); +} + +function either(...arr) { + return new EitherVariants(arr); +} + +class F32x4Pattern { + constructor(x, y, z, w) { + this.x = x; + this.y = y; + this.z = z; + this.w = w; + } +} + +class F64x2Pattern { + constructor(x, y) { + this.x = x; + this.y = y; + } +} + +class RefWithType { + constructor(type) { + this.type = type; + } + + formatExpected() { + return `RefWithType(${this.type})`; + } + + test(refGlobal) { + try { + new WebAssembly.Global({value: this.type}, refGlobal.value); + return true; + } catch (err) { + assertEq(err instanceof TypeError, true, `wrong type of error when creating global: ${err}`); + assertEq(!!err.message.match(/can only pass/), true, `wrong type of error when creating global: ${err}`); + return false; + } + } +} + +// ref.extern values created by spec tests will be JS objects of the form +// { [externsym]: <number> }. Other externref values are possible to observe +// if extern.convert_any is used. +let externsym = Symbol("externref"); +function externref(s) { + return { [externsym]: s }; +} +function is_externref(x) { + return (x !== null && externsym in x) ? 1 : 0; +} +function is_funcref(x) { + return typeof x === "function" ? 1 : 0; +} +function eq_externref(x, y) { + return x === y ? 1 : 0; +} +function eq_funcref(x, y) { + return x === y ? 1 : 0; +} + +class ExternRefResult { + constructor(n) { + this.n = n; + } + + formatExpected() { + return `ref.extern ${this.n}`; + } + + test(global) { + // the global's value can either be an externref or just a plain old JS number + let result = global.value; + if (typeof global.value === "object" && externsym in global.value) { + result = global.value[externsym]; + } + return result === this.n; + } +} + +// ref.host values created by spectests will be whatever the JS API does to +// convert the given value to anyref. It should implicitly be like any.convert_extern. +function hostref(v) { + if (!wasmGcEnabled()) { + throw new Error("ref.host only works when wasm GC is enabled"); + } + + const { internalizeNum } = new WebAssembly.Instance( + new WebAssembly.Module(wasmTextToBinary(`(module + (func (import "test" "coerce") (param i32) (result anyref)) + (func (export "internalizeNum") (param i32) (result anyref) + (call 0 (local.get 0)) + ) + )`)), + { "test": { "coerce": x => x } }, + ).exports; + return internalizeNum(v); +} + +class HostRefResult { + constructor(n) { + this.n = n; + } + + formatExpected() { + return `ref.host ${this.n}`; + } + + test(externrefGlobal) { + assertEq(externsym in externrefGlobal.value, true, `HostRefResult only works with externref inputs`); + return externrefGlobal.value[externsym] === this.n; + } +} + +let spectest = { + externref: externref, + is_externref: is_externref, + is_funcref: is_funcref, + eq_externref: eq_externref, + eq_funcref: eq_funcref, + print: console.log.bind(console), + print_i32: console.log.bind(console), + print_i32_f32: console.log.bind(console), + print_f64_f64: console.log.bind(console), + print_f32: console.log.bind(console), + print_f64: console.log.bind(console), + global_i32: 666, + global_i64: 666n, + global_f32: 666, + global_f64: 666, + table: new WebAssembly.Table({ + initial: 10, + maximum: 20, + element: "anyfunc", + }), + memory: new WebAssembly.Memory({ initial: 1, maximum: 2 }), +}; + +let linkage = { + spectest, +}; + +function getInstance(instanceish) { + if (typeof instanceish === "string") { + assertEq( + instanceish in linkage, + true, + `'${instanceish}'' must be registered`, + ); + return linkage[instanceish]; + } + return instanceish; +} + +function instantiate(source) { + let bytecode = wasmTextToBinary(source); + let module = new WebAssembly.Module(bytecode); + let instance = new WebAssembly.Instance(module, linkage); + return instance.exports; +} + +function register(instanceish, name) { + linkage[name] = getInstance(instanceish); +} + +function invoke(instanceish, field, params) { + let func = getInstance(instanceish)[field]; + assertEq(func instanceof Function, true, "expected a function"); + return wasmLosslessInvoke(func, ...params); +} + +function get(instanceish, field) { + let global = getInstance(instanceish)[field]; + assertEq( + global instanceof WebAssembly.Global, + true, + "expected a WebAssembly.Global", + ); + return global; +} + +function assert_trap(thunk, message) { + try { + thunk(); + throw new Error("expected trap"); + } catch (err) { + if (err instanceof WebAssembly.RuntimeError) { + return; + } + throw err; + } +} + +let StackOverflow; +try { + (function f() { + 1 + f(); + })(); +} catch (e) { + StackOverflow = e.constructor; +} +function assert_exhaustion(thunk, message) { + try { + thunk(); + assertEq("normal return", "exhaustion"); + } catch (err) { + assertEq( + err instanceof StackOverflow, + true, + "expected exhaustion", + ); + } +} + +function assert_invalid(thunk, message) { + try { + thunk(); + assertEq("valid module", "invalid module"); + } catch (err) { + assertEq( + err instanceof WebAssembly.LinkError || + err instanceof WebAssembly.CompileError, + true, + "expected an invalid module", + ); + } +} + +function assert_unlinkable(thunk, message) { + try { + thunk(); + assertEq(true, false, "expected an unlinkable module"); + } catch (err) { + assertEq( + err instanceof WebAssembly.LinkError || + err instanceof WebAssembly.CompileError, + true, + "expected an unlinkable module", + ); + } +} + +function assert_malformed(thunk, message) { + try { + thunk(); + assertEq("valid module", "malformed module"); + } catch (err) { + assertEq( + err instanceof TypeError || + err instanceof SyntaxError || + err instanceof WebAssembly.CompileError || + err instanceof WebAssembly.LinkError, + true, + `expected a malformed module`, + ); + } +} + +function assert_exception(thunk) { + let thrown = false; + try { + thunk(); + } catch (err) { + thrown = true; + } + assertEq(thrown, true, "expected an exception to be thrown"); +} + +function assert_return(thunk, expected) { + let results = thunk(); + + if (results === undefined) { + results = []; + } else if (!Array.isArray(results)) { + results = [results]; + } + if (!Array.isArray(expected)) { + expected = [expected]; + } + + if (!compareResults(results, expected)) { + let got = results.map((x) => formatResult(x)).join(", "); + let wanted = expected.map((x) => formatExpected(x)).join(", "); + assertEq( + `[${got}]`, + `[${wanted}]`, + ); + assertEq(true, false, `${got} !== ${wanted}`); + } +} + +function formatResult(result) { + if (typeof (result) === "object") { + return wasmGlobalToString(result); + } else { + return `${result}`; + } +} + +function formatExpected(expected) { + if ( + expected === `f32_canonical_nan` || + expected === `f32_arithmetic_nan` || + expected === `f64_canonical_nan` || + expected === `f64_arithmetic_nan` + ) { + return expected; + } else if (expected instanceof F32x4Pattern) { + return `f32x4(${formatExpected(expected.x)}, ${ + formatExpected(expected.y) + }, ${formatExpected(expected.z)}, ${formatExpected(expected.w)})`; + } else if (expected instanceof F64x2Pattern) { + return `f64x2(${formatExpected(expected.x)}, ${ + formatExpected(expected.y) + })`; + } else if (expected instanceof EitherVariants) { + return expected.formatExpected(); + } else if (expected instanceof RefWithType) { + return expected.formatExpected(); + } else if (expected instanceof ExternRefResult) { + return expected.formatExpected(); + } else if (expected instanceof HostRefResult) { + return expected.formatExpected(); + } else if (typeof (expected) === "object") { + return wasmGlobalToString(expected); + } else { + throw new Error("unknown expected result"); + } +} + +class EitherVariants { + constructor(arr) { + this.arr = arr; + } + matches(v) { + return this.arr.some((e) => compareResult(v, e)); + } + formatExpected() { + return `either(${this.arr.map(formatExpected).join(", ")})`; + } +} + +function compareResults(results, expected) { + if (results.length !== expected.length) { + return false; + } + for (let i in results) { + if (expected[i] instanceof EitherVariants) { + return expected[i].matches(results[i]); + } + if (!compareResult(results[i], expected[i])) { + return false; + } + } + return true; +} + +function compareResult(result, expected) { + if ( + expected === `canonical_nan` || + expected === `arithmetic_nan` + ) { + return wasmGlobalIsNaN(result, expected); + } else if (expected === null) { + return result.value === null; + } else if (expected instanceof F32x4Pattern) { + return compareResult( + wasmGlobalExtractLane(result, "f32x4", 0), + expected.x, + ) && + compareResult(wasmGlobalExtractLane(result, "f32x4", 1), expected.y) && + compareResult(wasmGlobalExtractLane(result, "f32x4", 2), expected.z) && + compareResult(wasmGlobalExtractLane(result, "f32x4", 3), expected.w); + } else if (expected instanceof F64x2Pattern) { + return compareResult( + wasmGlobalExtractLane(result, "f64x2", 0), + expected.x, + ) && + compareResult(wasmGlobalExtractLane(result, "f64x2", 1), expected.y); + } else if (expected instanceof RefWithType) { + return expected.test(result); + } else if (expected instanceof ExternRefResult) { + return expected.test(result); + } else if (expected instanceof HostRefResult) { + return expected.test(result); + } else if (typeof (expected) === "object") { + return wasmGlobalsEqual(result, expected); + } else { + throw new Error("unknown expected result"); + } +} diff --git a/js/src/jit-test/tests/wasm/spec/tail-call/return_call.wast.js b/js/src/jit-test/tests/wasm/spec/tail-call/return_call.wast.js new file mode 100644 index 0000000000..2feda2e0a7 --- /dev/null +++ b/js/src/jit-test/tests/wasm/spec/tail-call/return_call.wast.js @@ -0,0 +1,283 @@ +/* Copyright 2021 Mozilla Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// ./test/core/return_call.wast + +// ./test/core/return_call.wast:3 +let $0 = instantiate(`(module + ;; Auxiliary definitions + (func $$const-i32 (result i32) (i32.const 0x132)) + (func $$const-i64 (result i64) (i64.const 0x164)) + (func $$const-f32 (result f32) (f32.const 0xf32)) + (func $$const-f64 (result f64) (f64.const 0xf64)) + + (func $$id-i32 (param i32) (result i32) (local.get 0)) + (func $$id-i64 (param i64) (result i64) (local.get 0)) + (func $$id-f32 (param f32) (result f32) (local.get 0)) + (func $$id-f64 (param f64) (result f64) (local.get 0)) + + (func $$f32-i32 (param f32 i32) (result i32) (local.get 1)) + (func $$i32-i64 (param i32 i64) (result i64) (local.get 1)) + (func $$f64-f32 (param f64 f32) (result f32) (local.get 1)) + (func $$i64-f64 (param i64 f64) (result f64) (local.get 1)) + + ;; Typing + + (func (export "type-i32") (result i32) (return_call $$const-i32)) + (func (export "type-i64") (result i64) (return_call $$const-i64)) + (func (export "type-f32") (result f32) (return_call $$const-f32)) + (func (export "type-f64") (result f64) (return_call $$const-f64)) + + (func (export "type-first-i32") (result i32) (return_call $$id-i32 (i32.const 32))) + (func (export "type-first-i64") (result i64) (return_call $$id-i64 (i64.const 64))) + (func (export "type-first-f32") (result f32) (return_call $$id-f32 (f32.const 1.32))) + (func (export "type-first-f64") (result f64) (return_call $$id-f64 (f64.const 1.64))) + + (func (export "type-second-i32") (result i32) + (return_call $$f32-i32 (f32.const 32.1) (i32.const 32)) + ) + (func (export "type-second-i64") (result i64) + (return_call $$i32-i64 (i32.const 32) (i64.const 64)) + ) + (func (export "type-second-f32") (result f32) + (return_call $$f64-f32 (f64.const 64) (f32.const 32)) + ) + (func (export "type-second-f64") (result f64) + (return_call $$i64-f64 (i64.const 64) (f64.const 64.1)) + ) + + ;; Recursion + + (func $$fac-acc (export "fac-acc") (param i64 i64) (result i64) + (if (result i64) (i64.eqz (local.get 0)) + (then (local.get 1)) + (else + (return_call $$fac-acc + (i64.sub (local.get 0) (i64.const 1)) + (i64.mul (local.get 0) (local.get 1)) + ) + ) + ) + ) + + (func $$count (export "count") (param i64) (result i64) + (if (result i64) (i64.eqz (local.get 0)) + (then (local.get 0)) + (else (return_call $$count (i64.sub (local.get 0) (i64.const 1)))) + ) + ) + + (func $$even (export "even") (param i64) (result i32) + (if (result i32) (i64.eqz (local.get 0)) + (then (i32.const 44)) + (else (return_call $$odd (i64.sub (local.get 0) (i64.const 1)))) + ) + ) + (func $$odd (export "odd") (param i64) (result i32) + (if (result i32) (i64.eqz (local.get 0)) + (then (i32.const 99)) + (else (return_call $$even (i64.sub (local.get 0) (i64.const 1)))) + ) + ) +)`); + +// ./test/core/return_call.wast:80 +assert_return(() => invoke($0, `type-i32`, []), [value("i32", 306)]); + +// ./test/core/return_call.wast:81 +assert_return(() => invoke($0, `type-i64`, []), [value("i64", 356n)]); + +// ./test/core/return_call.wast:82 +assert_return(() => invoke($0, `type-f32`, []), [value("f32", 3890)]); + +// ./test/core/return_call.wast:83 +assert_return(() => invoke($0, `type-f64`, []), [value("f64", 3940)]); + +// ./test/core/return_call.wast:85 +assert_return(() => invoke($0, `type-first-i32`, []), [value("i32", 32)]); + +// ./test/core/return_call.wast:86 +assert_return(() => invoke($0, `type-first-i64`, []), [value("i64", 64n)]); + +// ./test/core/return_call.wast:87 +assert_return(() => invoke($0, `type-first-f32`, []), [value("f32", 1.32)]); + +// ./test/core/return_call.wast:88 +assert_return(() => invoke($0, `type-first-f64`, []), [value("f64", 1.64)]); + +// ./test/core/return_call.wast:90 +assert_return(() => invoke($0, `type-second-i32`, []), [value("i32", 32)]); + +// ./test/core/return_call.wast:91 +assert_return(() => invoke($0, `type-second-i64`, []), [value("i64", 64n)]); + +// ./test/core/return_call.wast:92 +assert_return(() => invoke($0, `type-second-f32`, []), [value("f32", 32)]); + +// ./test/core/return_call.wast:93 +assert_return(() => invoke($0, `type-second-f64`, []), [value("f64", 64.1)]); + +// ./test/core/return_call.wast:95 +assert_return(() => invoke($0, `fac-acc`, [0n, 1n]), [value("i64", 1n)]); + +// ./test/core/return_call.wast:96 +assert_return(() => invoke($0, `fac-acc`, [1n, 1n]), [value("i64", 1n)]); + +// ./test/core/return_call.wast:97 +assert_return(() => invoke($0, `fac-acc`, [5n, 1n]), [value("i64", 120n)]); + +// ./test/core/return_call.wast:98 +assert_return(() => invoke($0, `fac-acc`, [25n, 1n]), [value("i64", 7034535277573963776n)]); + +// ./test/core/return_call.wast:103 +assert_return(() => invoke($0, `count`, [0n]), [value("i64", 0n)]); + +// ./test/core/return_call.wast:104 +assert_return(() => invoke($0, `count`, [1000n]), [value("i64", 0n)]); + +// ./test/core/return_call.wast:105 +assert_return(() => invoke($0, `count`, [1000000n]), [value("i64", 0n)]); + +// ./test/core/return_call.wast:107 +assert_return(() => invoke($0, `even`, [0n]), [value("i32", 44)]); + +// ./test/core/return_call.wast:108 +assert_return(() => invoke($0, `even`, [1n]), [value("i32", 99)]); + +// ./test/core/return_call.wast:109 +assert_return(() => invoke($0, `even`, [100n]), [value("i32", 44)]); + +// ./test/core/return_call.wast:110 +assert_return(() => invoke($0, `even`, [77n]), [value("i32", 99)]); + +// ./test/core/return_call.wast:111 +assert_return(() => invoke($0, `even`, [1000000n]), [value("i32", 44)]); + +// ./test/core/return_call.wast:112 +assert_return(() => invoke($0, `even`, [1000001n]), [value("i32", 99)]); + +// ./test/core/return_call.wast:113 +assert_return(() => invoke($0, `odd`, [0n]), [value("i32", 99)]); + +// ./test/core/return_call.wast:114 +assert_return(() => invoke($0, `odd`, [1n]), [value("i32", 44)]); + +// ./test/core/return_call.wast:115 +assert_return(() => invoke($0, `odd`, [200n]), [value("i32", 99)]); + +// ./test/core/return_call.wast:116 +assert_return(() => invoke($0, `odd`, [77n]), [value("i32", 44)]); + +// ./test/core/return_call.wast:117 +assert_return(() => invoke($0, `odd`, [1000000n]), [value("i32", 99)]); + +// ./test/core/return_call.wast:118 +assert_return(() => invoke($0, `odd`, [999999n]), [value("i32", 44)]); + +// ./test/core/return_call.wast:123 +assert_invalid( + () => instantiate(`(module + (func $$type-void-vs-num (result i32) (return_call 1) (i32.const 0)) + (func) + )`), + `type mismatch`, +); + +// ./test/core/return_call.wast:130 +assert_invalid( + () => instantiate(`(module + (func $$type-num-vs-num (result i32) (return_call 1) (i32.const 0)) + (func (result i64) (i64.const 1)) + )`), + `type mismatch`, +); + +// ./test/core/return_call.wast:138 +assert_invalid( + () => instantiate(`(module + (func $$arity-0-vs-1 (return_call 1)) + (func (param i32)) + )`), + `type mismatch`, +); + +// ./test/core/return_call.wast:145 +assert_invalid( + () => instantiate(`(module + (func $$arity-0-vs-2 (return_call 1)) + (func (param f64 i32)) + )`), + `type mismatch`, +); + +// ./test/core/return_call.wast:153 +let $1 = instantiate(`(module + (func $$arity-1-vs-0 (i32.const 1) (return_call 1)) + (func) +)`); + +// ./test/core/return_call.wast:158 +let $2 = instantiate(`(module + (func $$arity-2-vs-0 (f64.const 2) (i32.const 1) (return_call 1)) + (func) +)`); + +// ./test/core/return_call.wast:163 +assert_invalid( + () => instantiate(`(module + (func $$type-first-void-vs-num (return_call 1 (nop) (i32.const 1))) + (func (param i32 i32)) + )`), + `type mismatch`, +); + +// ./test/core/return_call.wast:170 +assert_invalid( + () => instantiate(`(module + (func $$type-second-void-vs-num (return_call 1 (i32.const 1) (nop))) + (func (param i32 i32)) + )`), + `type mismatch`, +); + +// ./test/core/return_call.wast:177 +assert_invalid( + () => instantiate(`(module + (func $$type-first-num-vs-num (return_call 1 (f64.const 1) (i32.const 1))) + (func (param i32 f64)) + )`), + `type mismatch`, +); + +// ./test/core/return_call.wast:184 +assert_invalid( + () => instantiate(`(module + (func $$type-second-num-vs-num (return_call 1 (i32.const 1) (f64.const 1))) + (func (param f64 i32)) + )`), + `type mismatch`, +); + +// ./test/core/return_call.wast:195 +assert_invalid( + () => instantiate(`(module (func $$unbound-func (return_call 1)))`), + `unknown function`, +); + +// ./test/core/return_call.wast:199 +assert_invalid( + () => instantiate(`(module (func $$large-func (return_call 1012321300)))`), + `unknown function`, +); diff --git a/js/src/jit-test/tests/wasm/spec/tail-call/return_call_indirect.wast.js b/js/src/jit-test/tests/wasm/spec/tail-call/return_call_indirect.wast.js new file mode 100644 index 0000000000..706aae5c18 --- /dev/null +++ b/js/src/jit-test/tests/wasm/spec/tail-call/return_call_indirect.wast.js @@ -0,0 +1,591 @@ +/* Copyright 2021 Mozilla Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// ./test/core/return_call_indirect.wast + +// ./test/core/return_call_indirect.wast:3 +let $0 = instantiate(`(module + ;; Auxiliary definitions + (type $$proc (func)) + (type $$out-i32 (func (result i32))) + (type $$out-i64 (func (result i64))) + (type $$out-f32 (func (result f32))) + (type $$out-f64 (func (result f64))) + (type $$over-i32 (func (param i32) (result i32))) + (type $$over-i64 (func (param i64) (result i64))) + (type $$over-f32 (func (param f32) (result f32))) + (type $$over-f64 (func (param f64) (result f64))) + (type $$f32-i32 (func (param f32 i32) (result i32))) + (type $$i32-i64 (func (param i32 i64) (result i64))) + (type $$f64-f32 (func (param f64 f32) (result f32))) + (type $$i64-f64 (func (param i64 f64) (result f64))) + (type $$over-i32-duplicate (func (param i32) (result i32))) + (type $$over-i64-duplicate (func (param i64) (result i64))) + (type $$over-f32-duplicate (func (param f32) (result f32))) + (type $$over-f64-duplicate (func (param f64) (result f64))) + + (func $$const-i32 (type $$out-i32) (i32.const 0x132)) + (func $$const-i64 (type $$out-i64) (i64.const 0x164)) + (func $$const-f32 (type $$out-f32) (f32.const 0xf32)) + (func $$const-f64 (type $$out-f64) (f64.const 0xf64)) + + (func $$id-i32 (type $$over-i32) (local.get 0)) + (func $$id-i64 (type $$over-i64) (local.get 0)) + (func $$id-f32 (type $$over-f32) (local.get 0)) + (func $$id-f64 (type $$over-f64) (local.get 0)) + + (func $$i32-i64 (type $$i32-i64) (local.get 1)) + (func $$i64-f64 (type $$i64-f64) (local.get 1)) + (func $$f32-i32 (type $$f32-i32) (local.get 1)) + (func $$f64-f32 (type $$f64-f32) (local.get 1)) + + (func $$over-i32-duplicate (type $$over-i32-duplicate) (local.get 0)) + (func $$over-i64-duplicate (type $$over-i64-duplicate) (local.get 0)) + (func $$over-f32-duplicate (type $$over-f32-duplicate) (local.get 0)) + (func $$over-f64-duplicate (type $$over-f64-duplicate) (local.get 0)) + + (table funcref + (elem + $$const-i32 $$const-i64 $$const-f32 $$const-f64 + $$id-i32 $$id-i64 $$id-f32 $$id-f64 + $$f32-i32 $$i32-i64 $$f64-f32 $$i64-f64 + $$fac $$fac-acc $$even $$odd + $$over-i32-duplicate $$over-i64-duplicate + $$over-f32-duplicate $$over-f64-duplicate + ) + ) + + ;; Syntax + + (func + (return_call_indirect (i32.const 0)) + (return_call_indirect (param i64) (i64.const 0) (i32.const 0)) + (return_call_indirect (param i64) (param) (param f64 i32 i64) + (i64.const 0) (f64.const 0) (i32.const 0) (i64.const 0) (i32.const 0) + ) + (return_call_indirect (result) (i32.const 0)) + ) + + (func (result i32) + (return_call_indirect (result i32) (i32.const 0)) + (return_call_indirect (result i32) (result) (i32.const 0)) + (return_call_indirect (param i64) (result i32) (i64.const 0) (i32.const 0)) + (return_call_indirect + (param) (param i64) (param) (param f64 i32 i64) (param) (param) + (result) (result i32) (result) (result) + (i64.const 0) (f64.const 0) (i32.const 0) (i64.const 0) (i32.const 0) + ) + ) + + (func (result i64) + (return_call_indirect (type $$over-i64) (param i64) (result i64) + (i64.const 0) (i32.const 0) + ) + ) + + ;; Typing + + (func (export "type-i32") (result i32) + (return_call_indirect (type $$out-i32) (i32.const 0)) + ) + (func (export "type-i64") (result i64) + (return_call_indirect (type $$out-i64) (i32.const 1)) + ) + (func (export "type-f32") (result f32) + (return_call_indirect (type $$out-f32) (i32.const 2)) + ) + (func (export "type-f64") (result f64) + (return_call_indirect (type $$out-f64) (i32.const 3)) + ) + + (func (export "type-index") (result i64) + (return_call_indirect (type $$over-i64) (i64.const 100) (i32.const 5)) + ) + + (func (export "type-first-i32") (result i32) + (return_call_indirect (type $$over-i32) (i32.const 32) (i32.const 4)) + ) + (func (export "type-first-i64") (result i64) + (return_call_indirect (type $$over-i64) (i64.const 64) (i32.const 5)) + ) + (func (export "type-first-f32") (result f32) + (return_call_indirect (type $$over-f32) (f32.const 1.32) (i32.const 6)) + ) + (func (export "type-first-f64") (result f64) + (return_call_indirect (type $$over-f64) (f64.const 1.64) (i32.const 7)) + ) + + (func (export "type-second-i32") (result i32) + (return_call_indirect (type $$f32-i32) + (f32.const 32.1) (i32.const 32) (i32.const 8) + ) + ) + (func (export "type-second-i64") (result i64) + (return_call_indirect (type $$i32-i64) + (i32.const 32) (i64.const 64) (i32.const 9) + ) + ) + (func (export "type-second-f32") (result f32) + (return_call_indirect (type $$f64-f32) + (f64.const 64) (f32.const 32) (i32.const 10) + ) + ) + (func (export "type-second-f64") (result f64) + (return_call_indirect (type $$i64-f64) + (i64.const 64) (f64.const 64.1) (i32.const 11) + ) + ) + + ;; Dispatch + + (func (export "dispatch") (param i32 i64) (result i64) + (return_call_indirect (type $$over-i64) (local.get 1) (local.get 0)) + ) + + (func (export "dispatch-structural") (param i32) (result i64) + (return_call_indirect (type $$over-i64-duplicate) + (i64.const 9) (local.get 0) + ) + ) + + ;; Multiple tables + + (table $$tab2 funcref (elem $$tab-f1)) + (table $$tab3 funcref (elem $$tab-f2)) + + (func $$tab-f1 (result i32) (i32.const 0x133)) + (func $$tab-f2 (result i32) (i32.const 0x134)) + + (func (export "call-tab") (param $$i i32) (result i32) + (if (i32.eq (local.get $$i) (i32.const 0)) + (then (return_call_indirect (type $$out-i32) (i32.const 0))) + ) + (if (i32.eq (local.get $$i) (i32.const 1)) + (then (return_call_indirect 1 (type $$out-i32) (i32.const 0))) + ) + (if (i32.eq (local.get $$i) (i32.const 2)) + (then (return_call_indirect $$tab3 (type $$out-i32) (i32.const 0))) + ) + (i32.const 0) + ) + + ;; Recursion + + (func $$fac (export "fac") (type $$over-i64) + (return_call_indirect (param i64 i64) (result i64) + (local.get 0) (i64.const 1) (i32.const 13) + ) + ) + + (func $$fac-acc (param i64 i64) (result i64) + (if (result i64) (i64.eqz (local.get 0)) + (then (local.get 1)) + (else + (return_call_indirect (param i64 i64) (result i64) + (i64.sub (local.get 0) (i64.const 1)) + (i64.mul (local.get 0) (local.get 1)) + (i32.const 13) + ) + ) + ) + ) + + (func $$even (export "even") (param i32) (result i32) + (if (result i32) (i32.eqz (local.get 0)) + (then (i32.const 44)) + (else + (return_call_indirect (type $$over-i32) + (i32.sub (local.get 0) (i32.const 1)) + (i32.const 15) + ) + ) + ) + ) + (func $$odd (export "odd") (param i32) (result i32) + (if (result i32) (i32.eqz (local.get 0)) + (then (i32.const 99)) + (else + (return_call_indirect (type $$over-i32) + (i32.sub (local.get 0) (i32.const 1)) + (i32.const 14) + ) + ) + ) + ) +)`); + +// ./test/core/return_call_indirect.wast:213 +assert_return(() => invoke($0, `type-i32`, []), [value("i32", 306)]); + +// ./test/core/return_call_indirect.wast:214 +assert_return(() => invoke($0, `type-i64`, []), [value("i64", 356n)]); + +// ./test/core/return_call_indirect.wast:215 +assert_return(() => invoke($0, `type-f32`, []), [value("f32", 3890)]); + +// ./test/core/return_call_indirect.wast:216 +assert_return(() => invoke($0, `type-f64`, []), [value("f64", 3940)]); + +// ./test/core/return_call_indirect.wast:218 +assert_return(() => invoke($0, `type-index`, []), [value("i64", 100n)]); + +// ./test/core/return_call_indirect.wast:220 +assert_return(() => invoke($0, `type-first-i32`, []), [value("i32", 32)]); + +// ./test/core/return_call_indirect.wast:221 +assert_return(() => invoke($0, `type-first-i64`, []), [value("i64", 64n)]); + +// ./test/core/return_call_indirect.wast:222 +assert_return(() => invoke($0, `type-first-f32`, []), [value("f32", 1.32)]); + +// ./test/core/return_call_indirect.wast:223 +assert_return(() => invoke($0, `type-first-f64`, []), [value("f64", 1.64)]); + +// ./test/core/return_call_indirect.wast:225 +assert_return(() => invoke($0, `type-second-i32`, []), [value("i32", 32)]); + +// ./test/core/return_call_indirect.wast:226 +assert_return(() => invoke($0, `type-second-i64`, []), [value("i64", 64n)]); + +// ./test/core/return_call_indirect.wast:227 +assert_return(() => invoke($0, `type-second-f32`, []), [value("f32", 32)]); + +// ./test/core/return_call_indirect.wast:228 +assert_return(() => invoke($0, `type-second-f64`, []), [value("f64", 64.1)]); + +// ./test/core/return_call_indirect.wast:230 +assert_return(() => invoke($0, `dispatch`, [5, 2n]), [value("i64", 2n)]); + +// ./test/core/return_call_indirect.wast:231 +assert_return(() => invoke($0, `dispatch`, [5, 5n]), [value("i64", 5n)]); + +// ./test/core/return_call_indirect.wast:232 +assert_return(() => invoke($0, `dispatch`, [12, 5n]), [value("i64", 120n)]); + +// ./test/core/return_call_indirect.wast:233 +assert_return(() => invoke($0, `dispatch`, [17, 2n]), [value("i64", 2n)]); + +// ./test/core/return_call_indirect.wast:234 +assert_trap(() => invoke($0, `dispatch`, [0, 2n]), `indirect call type mismatch`); + +// ./test/core/return_call_indirect.wast:235 +assert_trap(() => invoke($0, `dispatch`, [15, 2n]), `indirect call type mismatch`); + +// ./test/core/return_call_indirect.wast:236 +assert_trap(() => invoke($0, `dispatch`, [20, 2n]), `undefined element`); + +// ./test/core/return_call_indirect.wast:237 +assert_trap(() => invoke($0, `dispatch`, [-1, 2n]), `undefined element`); + +// ./test/core/return_call_indirect.wast:238 +assert_trap(() => invoke($0, `dispatch`, [1213432423, 2n]), `undefined element`); + +// ./test/core/return_call_indirect.wast:240 +assert_return(() => invoke($0, `dispatch-structural`, [5]), [value("i64", 9n)]); + +// ./test/core/return_call_indirect.wast:241 +assert_return(() => invoke($0, `dispatch-structural`, [5]), [value("i64", 9n)]); + +// ./test/core/return_call_indirect.wast:242 +assert_return(() => invoke($0, `dispatch-structural`, [12]), [value("i64", 362880n)]); + +// ./test/core/return_call_indirect.wast:243 +assert_return(() => invoke($0, `dispatch-structural`, [17]), [value("i64", 9n)]); + +// ./test/core/return_call_indirect.wast:244 +assert_trap(() => invoke($0, `dispatch-structural`, [11]), `indirect call type mismatch`); + +// ./test/core/return_call_indirect.wast:245 +assert_trap(() => invoke($0, `dispatch-structural`, [16]), `indirect call type mismatch`); + +// ./test/core/return_call_indirect.wast:247 +assert_return(() => invoke($0, `call-tab`, [0]), [value("i32", 306)]); + +// ./test/core/return_call_indirect.wast:248 +assert_return(() => invoke($0, `call-tab`, [1]), [value("i32", 307)]); + +// ./test/core/return_call_indirect.wast:249 +assert_return(() => invoke($0, `call-tab`, [2]), [value("i32", 308)]); + +// ./test/core/return_call_indirect.wast:251 +assert_return(() => invoke($0, `fac`, [0n]), [value("i64", 1n)]); + +// ./test/core/return_call_indirect.wast:252 +assert_return(() => invoke($0, `fac`, [1n]), [value("i64", 1n)]); + +// ./test/core/return_call_indirect.wast:253 +assert_return(() => invoke($0, `fac`, [5n]), [value("i64", 120n)]); + +// ./test/core/return_call_indirect.wast:254 +assert_return(() => invoke($0, `fac`, [25n]), [value("i64", 7034535277573963776n)]); + +// ./test/core/return_call_indirect.wast:256 +assert_return(() => invoke($0, `even`, [0]), [value("i32", 44)]); + +// ./test/core/return_call_indirect.wast:257 +assert_return(() => invoke($0, `even`, [1]), [value("i32", 99)]); + +// ./test/core/return_call_indirect.wast:258 +assert_return(() => invoke($0, `even`, [100]), [value("i32", 44)]); + +// ./test/core/return_call_indirect.wast:259 +assert_return(() => invoke($0, `even`, [77]), [value("i32", 99)]); + +// ./test/core/return_call_indirect.wast:260 +assert_return(() => invoke($0, `even`, [100000]), [value("i32", 44)]); + +// ./test/core/return_call_indirect.wast:261 +assert_return(() => invoke($0, `even`, [111111]), [value("i32", 99)]); + +// ./test/core/return_call_indirect.wast:262 +assert_return(() => invoke($0, `odd`, [0]), [value("i32", 99)]); + +// ./test/core/return_call_indirect.wast:263 +assert_return(() => invoke($0, `odd`, [1]), [value("i32", 44)]); + +// ./test/core/return_call_indirect.wast:264 +assert_return(() => invoke($0, `odd`, [200]), [value("i32", 99)]); + +// ./test/core/return_call_indirect.wast:265 +assert_return(() => invoke($0, `odd`, [77]), [value("i32", 44)]); + +// ./test/core/return_call_indirect.wast:266 +assert_return(() => invoke($0, `odd`, [200002]), [value("i32", 99)]); + +// ./test/core/return_call_indirect.wast:267 +assert_return(() => invoke($0, `odd`, [300003]), [value("i32", 44)]); + +// ./test/core/return_call_indirect.wast:272 +assert_malformed( + () => instantiate(`(type $$sig (func (param i32) (result i32))) (table 0 funcref) (func (result i32) (return_call_indirect (type $$sig) (result i32) (param i32) (i32.const 0) (i32.const 0) ) ) `), + `unexpected token`, +); + +// ./test/core/return_call_indirect.wast:284 +assert_malformed( + () => instantiate(`(type $$sig (func (param i32) (result i32))) (table 0 funcref) (func (result i32) (return_call_indirect (param i32) (type $$sig) (result i32) (i32.const 0) (i32.const 0) ) ) `), + `unexpected token`, +); + +// ./test/core/return_call_indirect.wast:296 +assert_malformed( + () => instantiate(`(type $$sig (func (param i32) (result i32))) (table 0 funcref) (func (result i32) (return_call_indirect (param i32) (result i32) (type $$sig) (i32.const 0) (i32.const 0) ) ) `), + `unexpected token`, +); + +// ./test/core/return_call_indirect.wast:308 +assert_malformed( + () => instantiate(`(type $$sig (func (param i32) (result i32))) (table 0 funcref) (func (result i32) (return_call_indirect (result i32) (type $$sig) (param i32) (i32.const 0) (i32.const 0) ) ) `), + `unexpected token`, +); + +// ./test/core/return_call_indirect.wast:320 +assert_malformed( + () => instantiate(`(type $$sig (func (param i32) (result i32))) (table 0 funcref) (func (result i32) (return_call_indirect (result i32) (param i32) (type $$sig) (i32.const 0) (i32.const 0) ) ) `), + `unexpected token`, +); + +// ./test/core/return_call_indirect.wast:332 +assert_malformed( + () => instantiate(`(table 0 funcref) (func (result i32) (return_call_indirect (result i32) (param i32) (i32.const 0) (i32.const 0) ) ) `), + `unexpected token`, +); + +// ./test/core/return_call_indirect.wast:344 +assert_malformed( + () => instantiate(`(table 0 funcref) (func (return_call_indirect (param $$x i32) (i32.const 0) (i32.const 0))) `), + `unexpected token`, +); + +// ./test/core/return_call_indirect.wast:351 +assert_malformed( + () => instantiate(`(type $$sig (func)) (table 0 funcref) (func (result i32) (return_call_indirect (type $$sig) (result i32) (i32.const 0)) ) `), + `inline function type`, +); + +// ./test/core/return_call_indirect.wast:361 +assert_malformed( + () => instantiate(`(type $$sig (func (param i32) (result i32))) (table 0 funcref) (func (result i32) (return_call_indirect (type $$sig) (result i32) (i32.const 0)) ) `), + `inline function type`, +); + +// ./test/core/return_call_indirect.wast:371 +assert_malformed( + () => instantiate(`(type $$sig (func (param i32) (result i32))) (table 0 funcref) (func (return_call_indirect (type $$sig) (param i32) (i32.const 0) (i32.const 0) ) ) `), + `inline function type`, +); + +// ./test/core/return_call_indirect.wast:383 +assert_malformed( + () => instantiate(`(type $$sig (func (param i32 i32) (result i32))) (table 0 funcref) (func (result i32) (return_call_indirect (type $$sig) (param i32) (result i32) (i32.const 0) (i32.const 0) ) ) `), + `inline function type`, +); + +// ./test/core/return_call_indirect.wast:398 +assert_invalid( + () => instantiate(`(module + (type (func)) + (func $$no-table (return_call_indirect (type 0) (i32.const 0))) + )`), + `unknown table`, +); + +// ./test/core/return_call_indirect.wast:406 +assert_invalid( + () => instantiate(`(module + (type (func)) + (table 0 funcref) + (func $$type-void-vs-num (i32.eqz (return_call_indirect (type 0) (i32.const 0)))) + )`), + `type mismatch`, +); + +// ./test/core/return_call_indirect.wast:414 +assert_invalid( + () => instantiate(`(module + (type (func (result i64))) + (table 0 funcref) + (func $$type-num-vs-num (i32.eqz (return_call_indirect (type 0) (i32.const 0)))) + )`), + `type mismatch`, +); + +// ./test/core/return_call_indirect.wast:423 +assert_invalid( + () => instantiate(`(module + (type (func (param i32))) + (table 0 funcref) + (func $$arity-0-vs-1 (return_call_indirect (type 0) (i32.const 0))) + )`), + `type mismatch`, +); + +// ./test/core/return_call_indirect.wast:431 +assert_invalid( + () => instantiate(`(module + (type (func (param f64 i32))) + (table 0 funcref) + (func $$arity-0-vs-2 (return_call_indirect (type 0) (i32.const 0))) + )`), + `type mismatch`, +); + +// ./test/core/return_call_indirect.wast:440 +let $1 = instantiate(`(module + (type (func)) + (table 0 funcref) + (func $$arity-1-vs-0 (return_call_indirect (type 0) (i32.const 1) (i32.const 0))) +)`); + +// ./test/core/return_call_indirect.wast:446 +let $2 = instantiate(`(module + (type (func)) + (table 0 funcref) + (func $$arity-2-vs-0 + (return_call_indirect (type 0) (f64.const 2) (i32.const 1) (i32.const 0)) + ) +)`); + +// ./test/core/return_call_indirect.wast:454 +assert_invalid( + () => instantiate(`(module + (type (func (param i32))) + (table 0 funcref) + (func $$type-func-void-vs-i32 (return_call_indirect (type 0) (i32.const 1) (nop))) + )`), + `type mismatch`, +); + +// ./test/core/return_call_indirect.wast:462 +assert_invalid( + () => instantiate(`(module + (type (func (param i32))) + (table 0 funcref) + (func $$type-func-num-vs-i32 (return_call_indirect (type 0) (i32.const 0) (i64.const 1))) + )`), + `type mismatch`, +); + +// ./test/core/return_call_indirect.wast:471 +assert_invalid( + () => instantiate(`(module + (type (func (param i32 i32))) + (table 0 funcref) + (func $$type-first-void-vs-num + (return_call_indirect (type 0) (nop) (i32.const 1) (i32.const 0)) + ) + )`), + `type mismatch`, +); + +// ./test/core/return_call_indirect.wast:481 +assert_invalid( + () => instantiate(`(module + (type (func (param i32 i32))) + (table 0 funcref) + (func $$type-second-void-vs-num + (return_call_indirect (type 0) (i32.const 1) (nop) (i32.const 0)) + ) + )`), + `type mismatch`, +); + +// ./test/core/return_call_indirect.wast:491 +assert_invalid( + () => instantiate(`(module + (type (func (param i32 f64))) + (table 0 funcref) + (func $$type-first-num-vs-num + (return_call_indirect (type 0) (f64.const 1) (i32.const 1) (i32.const 0)) + ) + )`), + `type mismatch`, +); + +// ./test/core/return_call_indirect.wast:501 +assert_invalid( + () => instantiate(`(module + (type (func (param f64 i32))) + (table 0 funcref) + (func $$type-second-num-vs-num + (return_call_indirect (type 0) (i32.const 1) (f64.const 1) (i32.const 0)) + ) + )`), + `type mismatch`, +); + +// ./test/core/return_call_indirect.wast:515 +assert_invalid( + () => instantiate(`(module + (table 0 funcref) + (func $$unbound-type (return_call_indirect (type 1) (i32.const 0))) + )`), + `unknown type`, +); + +// ./test/core/return_call_indirect.wast:522 +assert_invalid( + () => instantiate(`(module + (table 0 funcref) + (func $$large-type (return_call_indirect (type 1012321300) (i32.const 0))) + )`), + `unknown type`, +); + +// ./test/core/return_call_indirect.wast:533 +assert_invalid( + () => instantiate(`(module (table funcref (elem 0 0)))`), + `unknown function 0`, +); |