Skip to content

Commit

Permalink
fix(wasm-api-bindgen): fix TS codegen address divisor for prim slices
Browse files Browse the repository at this point in the history
- update __primSlice()
  - address needs to be divided based on type size
- update test snapshots
  • Loading branch information
postspectacular committed Aug 14, 2024
1 parent e79275d commit dfd66ba
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
3 changes: 2 additions & 1 deletion packages/wasm-api-bindgen/src/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ const __mapStringArray = (
/** @internal */
const __primSlice = (type: string, _: number, __: CodeGenOpts) => [
// `const addr = ${__ptrShift(opts.target, offset, type)};`,
`addr >>>= ${__shift(type)};`,
`return mem.${type}.subarray(addr, addr + len);`,
];

Expand Down Expand Up @@ -462,7 +463,7 @@ const __generateField = (
case "slice":
case "enumSlice":
getter = [
`const addr = ${__ptr(opts.target, offset)};`,
`let addr = ${__ptr(opts.target, offset)};`,
`const len = ${__ptr(
opts.target,
offset + opts.target.sizeBytes
Expand Down
22 changes: 14 additions & 8 deletions packages/wasm-api-bindgen/test/__snapshots__/main.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -915,13 +915,15 @@ export const $A: WasmTypeConstructor<A> = (mem) => ({
));
},
get slice(): Uint32Array {
const addr = mem.u32[(base + 16) >>> 2];
let addr = mem.u32[(base + 16) >>> 2];
const len = mem.u32[(base + 20) >>> 2];
addr >>>= 2;
return mem.u32.subarray(addr, addr + len);
},
get constSlice(): Uint32Array {
const addr = mem.u32[(base + 24) >>> 2];
let addr = mem.u32[(base + 24) >>> 2];
const len = mem.u32[(base + 28) >>> 2];
addr >>>= 2;
return mem.u32.subarray(addr, addr + len);
},
get array(): Uint32Array {
Expand Down Expand Up @@ -1044,8 +1046,9 @@ export const $B: WasmTypeConstructor<B> = (mem) => ({
return mem.i32.subarray(addr, addr + 2);
},
get slice(): Int32Array {
const addr = mem.u32[(base + 12) >>> 2];
let addr = mem.u32[(base + 12) >>> 2];
const len = mem.u32[(base + 16) >>> 2];
addr >>>= 2;
return mem.i32.subarray(addr, addr + len);
},
get ptr(): Pointer<A> {
Expand Down Expand Up @@ -1188,8 +1191,9 @@ export const $B: WasmTypeConstructor<B> = (mem) => ({
return mem.u32.subarray(addr, addr + 2);
},
get slice(): Uint32Array {
const addr = mem.u32[(base + 20) >>> 2];
let addr = mem.u32[(base + 20) >>> 2];
const len = mem.u32[(base + 24) >>> 2];
addr >>>= 2;
return mem.u32.subarray(addr, addr + len);
},
};
Expand Down Expand Up @@ -1697,13 +1701,15 @@ export const $B: WasmTypeConstructor<B> = (mem) => ({
return mem.u8.subarray(base, base + 108);
},
get slice(): Uint8Array {
const addr = mem.u32[base >>> 2];
let addr = mem.u32[base >>> 2];
const len = mem.u32[(base + 4) >>> 2];
addr >>>= 0;
return mem.u8.subarray(addr, addr + len);
},
get constSlice(): Uint8Array {
const addr = mem.u32[(base + 8) >>> 2];
let addr = mem.u32[(base + 8) >>> 2];
const len = mem.u32[(base + 12) >>> 2];
addr >>>= 0;
return mem.u8.subarray(addr, addr + len);
},
get ptr(): Pointer<number> {
Expand Down Expand Up @@ -1775,15 +1781,15 @@ export const $B: WasmTypeConstructor<B> = (mem) => ({
mem.u8.set(x.__bytes, (base + 76));
},
get aSlice(): A[] {
const addr = mem.u32[(base + 80) >>> 2];
let addr = mem.u32[(base + 80) >>> 2];
const len = mem.u32[(base + 84) >>> 2];
const inst = $A(mem);
const buf: A[] = [];
for(let i = 0; i < len; i++) buf.push(inst.instance(addr + i * 2));
return buf;
},
get constASlice(): A[] {
const addr = mem.u32[(base + 88) >>> 2];
let addr = mem.u32[(base + 88) >>> 2];
const len = mem.u32[(base + 92) >>> 2];
const inst = $A(mem);
const buf: A[] = [];
Expand Down

0 comments on commit dfd66ba

Please sign in to comment.