From 1105daaad0d20271bd34233ba6768a3f78c285d3 Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Sat, 14 Mar 2026 18:48:11 +0000 Subject: [PATCH] Add support for extended opcodes --- asm.js | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/asm.js b/asm.js index 103f493..5099352 100644 --- a/asm.js +++ b/asm.js @@ -712,7 +712,7 @@ export class Assembler { action_append(action) { const code = action.opcode != undefined - ? [ action.opcode ] + ? [ action.opcode ].flat() : this.leb128(action.literal); this.funcs[this.current_func].body.push(...code); } @@ -799,7 +799,7 @@ export class Assembler { const data = this.data.at(-1).data; const value = action.value != null ? action.value - : this.le_bytes(this.lookup_def(action.symbol), action.size); + : this.le(this.lookup_def(action.symbol), action.size); data.push(...value); this.pos.addr += action.size; } @@ -871,17 +871,33 @@ export class Assembler { return this.defs[symbol]; } - le_bytes(value, count) { + le(value, count) { let bytes = [] - while (value != 0 && bytes.length < count) { + while (value != 0) { bytes.push(value & 0xff); value >>= 8; } - while (bytes.length < count) - bytes.push(0); + if (count != undefined) { + while (bytes.length < count) + bytes.push(0); + } return bytes; } + leb128(x) { + const orig = x; + const bytes = []; + while (true) { + const b = x & 0x7f; + x >>= 7; + if (x == 0 && (b & 0x40) == 0 || x == -1 && (b & 0x40) != 0) { + bytes.push(b); + return bytes; + } + bytes.push(b | 0x80); + } + } + wasm_section_type() { const funcs = Object.values(this.funcs); if (funcs.length == 0) return null; @@ -1022,18 +1038,4 @@ export class Assembler { else return [ flags, init ]; } - - leb128(x) { - const orig = x; - const bytes = []; - while (true) { - const b = x & 0x7f; - x >>= 7; - if (x == 0 && (b & 0x40) == 0 || x == -1 && (b & 0x40) != 0) { - bytes.push(b); - return bytes; - } - bytes.push(b | 0x80); - } - } }