blob: b44456932d90b5c833145ec3f342fc5046f79e0b [file] [log] [blame]
import * as assert from '../assert.js';
import Builder from '../Builder.js';
(function StartNamedFunction() {
const b = (new Builder())
.Type().End()
.Import()
.Function("imp", "func", { params: ["i32"] })
.End()
.Function().End()
.Start("foo").End()
.Code()
.Function("foo", { params: [] })
.I32Const(42)
.Call(0) // Calls func(42).
.End()
.End();
const bin = b.WebAssembly().get();
const module = new WebAssembly.Module(bin);
let value = 0;
const setter = v => value = v;
const instance = new WebAssembly.Instance(module, { imp: { func: setter } });
assert.eq(value, 42);
})();
(function InvalidStartFunctionIndex() {
const b = (new Builder())
.setChecked(false)
.Type().End()
.Function().End()
.Start(0).End() // Invalid index.
.Code().End();
const bin = b.WebAssembly().get();
assert.throws(() => new WebAssembly.Module(bin), Error, `WebAssembly.Module doesn't parse at byte 17 / 20: Start index 0 exceeds function index space 0 (evaluating 'new WebAssembly.Module(bin)')`);
})();