Skip to content

Commit

Permalink
fix(wasm-api): fix i64/u64 handling in sizeof()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Oct 26, 2022
1 parent 6bbc882 commit 825add3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
34 changes: 17 additions & 17 deletions packages/wasm-api/src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,13 @@ const sizeOf = defmulti<
} else if (isSlice(field)) {
size = opts.target.usizeBytes * 2;
} else {
size = isNumeric(field.type)
? SIZEOF[<Type>field.type]
: isWasmString(field.type)
? opts.target.usizeBytes *
(opts.stringType === "slice" ? 2 : 1)
: sizeOf(types[field.type], types, align, opts);
size =
isNumeric(field.type) || isBigNumeric(field.type)
? SIZEOF[<Type>field.type]
: isWasmString(field.type)
? opts.target.usizeBytes *
(opts.stringType === "slice" ? 2 : 1)
: sizeOf(types[field.type], types, align, opts);
if (field.tag == "array" || field.tag === "vec") {
size *= field.len!;
if (field.sentinel !== undefined && field.tag === "array") {
Expand Down Expand Up @@ -128,17 +129,16 @@ const alignOf = defmulti<
field.type = opts.target.usize;
}
if (field.pad) return (field.__align = 1);
return (field.__align =
isPointerLike(field) || isWasmString(field.type)
? align.align(<StructField>{ type: opts.target.usize })
: isNumeric(field.type) || isBigNumeric(field.type)
? align.align(field)
: alignOf(
types[field.type],
types,
selectAlignment(types[field.type]),
opts
));
return (field.__align = isPointerLike(field)
? align.align(<StructField>{ type: opts.target.usize })
: isNumeric(field.type) || isBigNumeric(field.type)
? align.align(field)
: alignOf(
types[field.type],
types,
selectAlignment(types[field.type]),
opts
));
},

enum: (type, _, align) => {
Expand Down
8 changes: 7 additions & 1 deletion packages/wasm-api/src/codegen/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ export const isPointer = (f: StructField) => f.tag === "ptr";

export const isSlice = (f: StructField) => f.tag === "slice";

export const isPointerLike = (f: StructField) => isPointer(f) || isSlice(f);
/**
* Returns true iff the struct field is a pointer, slice or "string" type
*
* @param f
*/
export const isPointerLike = (f: StructField) =>
isPointer(f) || isSlice(f) || isWasmString(f.type);

/**
* Takes an array of strings or splits given string into lines, word wraps and
Expand Down

0 comments on commit 825add3

Please sign in to comment.