@thi.ng/wasm-api-bindgen
Version:
Polyglot bindings code generators (TS/JS, Zig, C11) for hybrid WebAssembly projects
27 lines (26 loc) • 727 B
JavaScript
import { SIZEOF } from "@thi.ng/api/typedarray";
import { align as $align } from "@thi.ng/binary/align";
import { ceilPow2 } from "@thi.ng/binary/pow";
import { isStruct, isUnion } from "./utils.js";
const ALIGN_C = {
align: (field) => {
let align = SIZEOF[field.type];
if (field.tag === "vec") {
align *= ceilPow2(field.len);
}
return align;
},
size: (size, align) => $align(size, align),
offset: (offset, align) => $align(offset, align)
};
const ALIGN_PACKED = {
align: () => 1,
size: (size) => size,
offset: (offset) => offset
};
const selectAlignment = (type) => isStruct(type) || isUnion(type) ? type.align || ALIGN_C : ALIGN_C;
export {
ALIGN_C,
ALIGN_PACKED,
selectAlignment
};