Skip to content

Commit

Permalink
fix(wasm-api): fix TS code gen array mem mapping
Browse files Browse the repository at this point in the history
- use correct type size for array mem mapping
- update test fixtures
  • Loading branch information
postspectacular committed Nov 1, 2022
1 parent dae2279 commit a0e7132
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
17 changes: 12 additions & 5 deletions packages/wasm-api/src/codegen/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
ICodeGen,
PKG_NAME,
Struct,
TypeColl,
Union,
WasmPrim,
WasmTarget,
Expand Down Expand Up @@ -201,7 +202,11 @@ import { MemorySlice, Pointer, ${__stringImpl(
);
} else {
const fn = f.len
? [`(addr) => {`, ...__mapArray(f, f.len), `}`]
? [
`(addr) => {`,
...__mapArray(f, types, f.len),
`}`,
]
: [`(addr) => new $${f.type}.instance(addr)`];
lines.push(
`return $${f.name} || ($${
Expand Down Expand Up @@ -238,7 +243,7 @@ import { MemorySlice, Pointer, ${__stringImpl(
} else {
lines.push(
`const addr = ${__ptr(opts.target, offset)};`,
...__mapArray(f)
...__mapArray(f, types)
);
}
} else if (f.tag === "array" || f.tag === "vec") {
Expand All @@ -262,7 +267,7 @@ import { MemorySlice, Pointer, ${__stringImpl(
} else {
lines.push(
`const addr = ${__addr(offset)};`,
...__mapArray(f, f.len)
...__mapArray(f, types, f.len)
);
}
} else {
Expand Down Expand Up @@ -375,10 +380,12 @@ const __mem = (type: string, offset: number) =>
`mem.${type}[${__addrShift(offset!, type)}]`;

/** @internal */
const __mapArray = (f: Field, len: NumOrString = "len") => [
const __mapArray = (f: Field, types: TypeColl, len: NumOrString = "len") => [
`const inst = $${f.type}(mem);`,
`const slice: ${f.type}[] = [];`,
`for(let i = 0; i < ${len}; i++) slice.push(inst.instance(addr + i * ${f.__size}));`,
`for(let i = 0; i < ${len}; i++) slice.push(inst.instance(addr + i * ${
types[f.type].__size
}));`,
`return slice;`,
];

Expand Down
6 changes: 3 additions & 3 deletions packages/wasm-api/test/fixtures/union.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ export const $A: WasmTypeConstructor<A> = (mem) => ({
return $c || ($c = new Pointer<Uint16Array>(mem, (base + 8), (base) => mem.u16.subarray(base >>> 1, (base >>> 1) + 3)));
},
get d(): number {
return mem.f64[(base + 16) >>> 2];
return mem.f64[(base + 16) >>> 3];
},
set d(x: number) {
mem.f64[(base + 16) >>> 2] = x;
mem.f64[(base + 16) >>> 3] = x;
},
};
}
Expand Down Expand Up @@ -88,7 +88,7 @@ export const $B: WasmTypeConstructor<B> = (mem) => ({
const addr = base;
const inst = $A(mem);
const slice: A[] = [];
for(let i = 0; i < 3; i++) slice.push(inst.instance(addr + i * 72));
for(let i = 0; i < 3; i++) slice.push(inst.instance(addr + i * 24));
return slice;
},
get b(): bigint {
Expand Down

0 comments on commit a0e7132

Please sign in to comment.