Skip to content

Commit

Permalink
refactor(wasm-api): minor updates C & Zig codegens
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Oct 26, 2022
1 parent 825add3 commit a94e1cc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
3 changes: 2 additions & 1 deletion packages/wasm-api/src/codegen/c11.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ const __generateFields = (
) => {
const res: string[] = [];
const ftypes: Record<string, string> = {};
const isUnion = parent.type === "union";
const name = typePrefix + parent.name;
let padID = 0;
for (let f of parent.fields) {
Expand Down Expand Up @@ -203,7 +204,7 @@ const __generateFields = (
for (let f of parent.fields) {
if (isPadding(f)) continue;
fn(f.name + "_align", `alignof(${ftypes[f.name]})`);
fn(f.name + "_offset", `offsetof(${name}, ${f.name})`);
!isUnion && fn(f.name + "_offset", `offsetof(${name}, ${f.name})`);
fn(f.name + "_size", `sizeof(${ftypes[f.name]})`);
}
}
Expand Down
12 changes: 5 additions & 7 deletions packages/wasm-api/src/codegen/zig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ const __generateFields = (
const res: string[] = [];
const ftypes: Record<string, string> = {};
const isUnion = parent.type === "union";
const name = parent.name;
let padID = 0;
for (let f of parent.fields) {
// autolabel explicit padding fields
Expand Down Expand Up @@ -175,22 +176,19 @@ const __generateFields = (
const fn = (fname: string, body: string) =>
res.push(
"",
`export fn ${parent.name}_${fname}() usize {`,
`export fn ${name}_${fname}() usize {`,
`return ${body};`,
`}`
);

fn("align", `@alignOf(${parent.name})`);
fn("size", `@sizeOf(${parent.name})`);
fn("align", `@alignOf(${name})`);
fn("size", `@sizeOf(${name})`);

for (let f of parent.fields) {
if (isPadding(f)) continue;
fn(f.name + "_align", `@alignOf(${ftypes[f.name]})`);
!isUnion &&
fn(
f.name + "_offset",
`@offsetOf(${parent.name}, "${f.name}")`
);
fn(f.name + "_offset", `@offsetOf(${name}, "${f.name}")`);
fn(f.name + "_size", `@sizeOf(${ftypes[f.name]})`);
}
}
Expand Down

0 comments on commit a94e1cc

Please sign in to comment.