Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/compiler/src/wasm/binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1245,7 +1245,7 @@ class FunctionValidator {
/** A memarg. A memory other than 0 stops typing (multi-memory). */
memarg(naturalAlign: number): void {
const r = this.r;
const { align, memory } = readMemargFlags(r);
const { align, memory } = readMemargFlags(() => r.u32());
if (align < 0) r.malformed("malformed memop flags");
if (memory !== 0) this.otherMemory(memory);
if (this.m.memory64) r.u64();
Expand Down Expand Up @@ -1831,7 +1831,7 @@ class FunctionValidator {

scanMemarg(): void {
const r = this.r;
const { align, memory } = readMemargFlags(r);
const { align, memory } = readMemargFlags(() => r.u32());
if (align < 0) r.malformed("malformed memop flags");
if (memory !== 0) this.m.feature("multi-memory");
if (this.m.memory64) r.u64();
Expand Down
11 changes: 6 additions & 5 deletions packages/compiler/src/wasm/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,12 @@ function decodeUtf8(bytes: Uint8Array, start: number, end: number): string {
* 0; flags of 128 or more are malformed and answer align -1 without
* reading further. The offset follows. Validation (binary.ts), translation
* and skipImmediates all read memargs through this function, so they agree
* on where each instruction ends. */
export function readMemargFlags(r: { u32(): number }): { align: number; memory: number } {
const flags = r.u32();
* on where each instruction ends; `u32` reads the next u32 immediate of
* their own reader. */
export function readMemargFlags(u32: () => number): { align: number; memory: number } {
const flags = u32();
if (flags >= 128) return { align: -1, memory: 0 };
if (flags >= 64) return { align: flags - 64, memory: r.u32() };
if (flags >= 64) return { align: flags - 64, memory: u32() };
return { align: flags, memory: 0 };
}

Expand Down Expand Up @@ -386,7 +387,7 @@ export function skipImmediates(c: WasmCursor, op: number): void {
}
default:
if (op >= 0x28 && op <= 0x3e) {
const flags = readMemargFlags(c);
const flags = readMemargFlags(() => c.u32());
if (flags.align < 0 || flags.memory !== 0)
throw new Error(`wasm: memarg of memory ${flags.memory} is not supported`);
c.u32();
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/src/wasm/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ class FunctionTranslator {
/** The offset of a memarg over memory 0 (the only memory a translatable
* module has). */
memargOffset(c: WasmCursor): number {
const flags = readMemargFlags(c);
const flags = readMemargFlags(() => c.u32());
if (flags.align < 0 || flags.memory !== 0)
throw new WasmTranslateError(`memarg of memory ${flags.memory} is not supported`);
return c.u32();
Expand Down
Loading