Add support for extended opcodes

This commit is contained in:
2026-03-14 18:48:11 +00:00
parent 347dd8f534
commit 1105daaad0

38
asm.js
View File

@@ -712,7 +712,7 @@ export class Assembler {
action_append(action) { action_append(action) {
const code = action.opcode != undefined const code = action.opcode != undefined
? [ action.opcode ] ? [ action.opcode ].flat()
: this.leb128(action.literal); : this.leb128(action.literal);
this.funcs[this.current_func].body.push(...code); this.funcs[this.current_func].body.push(...code);
} }
@@ -799,7 +799,7 @@ export class Assembler {
const data = this.data.at(-1).data; const data = this.data.at(-1).data;
const value = action.value != null const value = action.value != null
? action.value ? action.value
: this.le_bytes(this.lookup_def(action.symbol), action.size); : this.le(this.lookup_def(action.symbol), action.size);
data.push(...value); data.push(...value);
this.pos.addr += action.size; this.pos.addr += action.size;
} }
@@ -871,17 +871,33 @@ export class Assembler {
return this.defs[symbol]; return this.defs[symbol];
} }
le_bytes(value, count) { le(value, count) {
let bytes = [] let bytes = []
while (value != 0 && bytes.length < count) { while (value != 0) {
bytes.push(value & 0xff); bytes.push(value & 0xff);
value >>= 8; value >>= 8;
} }
if (count != undefined) {
while (bytes.length < count) while (bytes.length < count)
bytes.push(0); bytes.push(0);
}
return bytes; 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() { wasm_section_type() {
const funcs = Object.values(this.funcs); const funcs = Object.values(this.funcs);
if (funcs.length == 0) return null; if (funcs.length == 0) return null;
@@ -1022,18 +1038,4 @@ export class Assembler {
else else
return [ flags, init ]; 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);
}
}
} }