Skip to content

Instantly share code, notes, and snippets.

@bvisness
Last active June 28, 2024 21:59
Show Gist options
  • Save bvisness/3aa14700cd5f9bff0aec9c0dbc460089 to your computer and use it in GitHub Desktop.
Save bvisness/3aa14700cd5f9bff0aec9c0dbc460089 to your computer and use it in GitHub Desktop.
DefaultValue(externref) absurdity
function test(msg, f) {
try {
f();
console.log(`${msg}: ok`);
} catch (e) {
console.log(`${msg}: fail (${e})`);
}
}
function printTable(t) {
let result = "[";
for (let i = 0; i < t.length; i++) {
if (i > 0) {
result += ", ";
}
result += t.get(i);
}
result += "]";
console.log(result);
}
// const bytes = wasmTextToBinary(`(module
// (table $f (export "f") 0 funcref)
// (table $e (export "e") 0 externref)
// (func (export "growF") (result i32)
// (table.grow $f (ref.null func) (i32.const 1))
// )
// (func (export "growE") (result i32)
// (table.grow $e (ref.null extern) (i32.const 1))
// )
// )`);
// console.log(bytes);
const bytes = new Uint8Array([
0,97,115,109,1,0,0,0,1,5,1,96,0,1,127,3,3,2,0,0,4,7,2,112,0,0,111,0,0,7,25,4,1,102,1,0,1,101,1,1,5,103,114,111,119,70,0,0,5,103,114,111,119,69,0,1,10,21,2,9,0,208,112,65,1,252,15,0,11,9,0,208,111,65,1,252,15,1,11,0,14,4,110,97,109,101,5,7,2,0,1,102,1,1,101
]);
const { f, e, growF, growE } = new WebAssembly.Instance(
new WebAssembly.Module(bytes),
).exports;
console.log("===== funcref =====");
test("f.grow(1)", () => f.grow(1));
test("f.grow(1, null)", () => f.grow(1, null));
test("f.grow(1, undefined)", () => f.grow(1, undefined));
test("(table.grow $f)", () => growF());
printTable(f);
console.log("\n===== externref =====");
test("e.grow(1)", () => e.grow(1));
test("e.grow(1, null)", () => e.grow(1, null));
test("e.grow(1, undefined)", () => e.grow(1, undefined));
test("(table.grow $e)", () => growE());
printTable(e);
<!DOCTYPE html>
<head>
<script src="growtable.js"></script>
</head>
<body>
Open yer console
</body>
function test(msg, f) {
try {
f();
console.log(`${msg}: ok`);
} catch (e) {
console.log(`${msg}: fail (${e})`);
}
}
function printTable(t) {
let result = "[";
for (let i = 0; i < t.length; i++) {
if (i > 0) {
result += ", ";
}
result += t.get(i);
}
result += "]";
console.log(result);
}
// const bytes = wasmTextToBinary(`(module
// (table $f (export "f") 0 funcref)
// (table $e (export "e") 0 externref)
// (table $a (export "a") 0 anyref)
// (table $s (export "s") 0 structref)
// (func (export "growF") (result i32)
// (table.grow $f (ref.null func) (i32.const 1))
// )
// (func (export "growE") (result i32)
// (table.grow $e (ref.null extern) (i32.const 1))
// )
// (func (export "growA") (result i32)
// (table.grow $a (ref.null any) (i32.const 1))
// )
// (func (export "growS") (result i32)
// (table.grow $s (ref.null struct) (i32.const 1))
// )
// )`);
// console.log(bytes);
const bytes = new Uint8Array([
0,97,115,109,1,0,0,0,1,5,1,96,0,1,127,3,5,4,0,0,0,0,4,14,4,112,0,0,111,0,0,99,110,0,0,107,0,0,7,49,8,1,102,1,0,1,101,1,1,1,97,1,2,1,115,1,3,5,103,114,111,119,70,0,0,5,103,114,111,119,69,0,1,5,103,114,111,119,65,0,2,5,103,114,111,119,83,0,3,10,41,4,9,0,208,112,65,1,252,15,0,11,9,0,208,111,65,1,252,15,1,11,9,0,208,110,65,1,252,15,2,11,9,0,208,107,65,1,252,15,3,11,0,20,4,110,97,109,101,5,13,4,0,1,102,1,1,101,2,1,97,3,1,115
]);
const { f, e, a, s, growF, growE, growA, growS } = new WebAssembly.Instance(
new WebAssembly.Module(bytes),
).exports;
console.log("===== funcref =====");
test("f.grow(1)", () => f.grow(1));
test("f.grow(1, null)", () => f.grow(1, null));
test("f.grow(1, undefined)", () => f.grow(1, undefined));
test("(table.grow $f)", () => growF());
printTable(f);
console.log("\n===== externref =====");
test("e.grow(1)", () => e.grow(1));
test("e.grow(1, null)", () => e.grow(1, null));
test("e.grow(1, undefined)", () => e.grow(1, undefined));
test("(table.grow $e)", () => growE());
printTable(e);
console.log("\n===== anyref =====");
test("a.grow(1)", () => a.grow(1));
test("a.grow(1, null)", () => a.grow(1, null));
test("a.grow(1, undefined)", () => a.grow(1, undefined));
test("(table.grow $a)", () => growA());
printTable(a);
console.log("\n===== structref =====");
test("s.grow(1)", () => s.grow(1));
test("s.grow(1, null)", () => s.grow(1, null));
test("s.grow(1, undefined)", () => s.grow(1, undefined));
test("(table.grow $s)", () => growS());
printTable(s);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment