De-duplicate consecutive locals of same type in wasm_section_code()

This commit is contained in:
2026-03-15 13:43:42 +00:00
parent acf5b6e284
commit e9beacba3a

37
asm.js
View File

@@ -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;
}, []);
}
}