diff --git a/asm.js b/asm.js index 38fe606..3cbe273 100644 --- a/asm.js +++ b/asm.js @@ -1278,24 +1278,20 @@ export class Assembler { const local_types = Object.values(locals); const local_count = local_types.length; if (local_count == 0) { - return [ - body.length + 2, - 0, - ...body, - opcodes["end"] - ] + const full_body = [ 0, body, opcodes.end ].flat() + return [ full_body.length, full_body ].flat(); } else { - return [ - body.length + local_count + 3, - local_count, - local_count, - ...local_types, - ...body, - opcodes["end"] - ]; + const groups = this.group(local_types); + const full_body = [ + groups.length, + ...groups.flat(), + body, + opcodes.end, + ].flat(); + return [ full_body.length, full_body ].flat(); } }); - return [ contents.length ].concat(...contents); + return [ contents.length, contents ].flat(Infinity); } wasm_section_data() { @@ -1369,4 +1365,15 @@ export class Assembler { for (const func of Object.values(this.funcs)) func.type = this.ensure_type(this.func_type(func)); } + + group(array) { + return array.reduce((acc, val) => { + const last = acc.at(-1); + if (last != undefined && last[1] == val) + ++last[0] + else + acc.push([1, val]); + return acc; + }, []); + } }