Skip to content

Commit

Permalink
refactor(wasm-api-bindgen): update Zig codegen default imports (#368)
Browse files Browse the repository at this point in the history
- adopt unscoped JS pkg name also as Zig import
- update fixtures
- update readme
  • Loading branch information
postspectacular committed Nov 30, 2022
1 parent 565990f commit 574af0f
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 86 deletions.
14 changes: 7 additions & 7 deletions packages/wasm-api-bindgen/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ export const $Event: WasmTypeConstructor<Event> = (mem) => ({
//! DO NOT EDIT!
const std = @import("std");
const wasmtypes = @import("wasmapi-types");
const bindgen = @import("wasm-api-bindgen");
/// Supported event types
pub const EventType = enum(u8) {
Expand All @@ -636,7 +636,7 @@ pub const MouseEvent = extern struct {
pub const KeyEvent = extern struct {
type: EventType,
/// Name of key which triggered event
key: wasmtypes.ConstStringPtr,
key: bindgen.ConstStringPtr,
/// Bitmask of active modifier keys
modifiers: u8,
};
Expand All @@ -655,15 +655,15 @@ for the supplied type wrappers:

```bash
zig build-lib \
--pkg-begin wasmapi-types node_modules/@thi.ng/wasm-api-bindgen/zig/lib.zig --pkg-end \
--pkg-begin wasm-api-bindgen node_modules/@thi.ng/wasm-api-bindgen/zig/lib.zig --pkg-end \
-target wasm32-freestanding \
-O ReleaseSmall -dynamic \
main.zig
```

Alternatively, use a [the setup used by various example
projects](https://github.com/thi-ng/umbrella/blob/develop/examples/zig-canvas/build.zig)
in this repo to use Zig's build system...
Alternatively, use a [more elaborate setup used by various example
projects](https://github.com/thi-ng/umbrella/blob/develop/examples/zig-todo-list/build.zig)
in this repo, using Zig's native build system...

### Runtime example

Expand Down Expand Up @@ -737,7 +737,7 @@ node --experimental-repl-await
> const wasmApiBindgen = await import("@thi.ng/wasm-api-bindgen");
```

Package sizes (brotli'd, pre-treeshake): ESM: 5.92 KB
Package sizes (brotli'd, pre-treeshake): ESM: 5.93 KB

## Dependencies

Expand Down
14 changes: 7 additions & 7 deletions packages/wasm-api-bindgen/src/zig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ export const ZIG = (opts: Partial<ZigOpts> = {}) => {
pre: (coll) => {
const res = [
`const std = @import("std");`,
`const wasmtypes = @import("wasmapi-types");`,
`const bindgen = @import("wasm-api-bindgen");`,
];
for (let type of sliceTypes(coll)) {
if (type !== "string" && type !== "opaque") {
const name = capitalize(type!);
res.push(
`\npub const ${name}Slice = wasmtypes.Slice([]${type}, [*]${type});`,
`pub const Const${name}Slice = wasmtypes.Slice([]const ${type}, [*]const ${type});`
`\npub const ${name}Slice = bindgen.Slice([]${type}, [*]${type});`,
`pub const Const${name}Slice = bindgen.Slice([]const ${type}, [*]const ${type});`
);
}
}
Expand Down Expand Up @@ -141,7 +141,7 @@ export const ZIG = (opts: Partial<ZigOpts> = {}) => {
opts
).type;
acc.push(
`pub const ${ptr.name} = *const fn(${args}) ${rtype};`,
`pub const ${ptr.name} = *const fn (${args}) ${rtype};`,
""
);
},
Expand Down Expand Up @@ -213,8 +213,8 @@ export const fieldType = (
const $isConst = isConst ? "Const" : "";
if (isWasmString(f.type)) {
type = isStringSlice(opts.stringType)
? `wasmtypes.${$isConst}String`
: `wasmtypes.${$isConst}StringPtr`;
? `bindgen.${$isConst}String`
: `bindgen.${$isConst}StringPtr`;
switch (classifier) {
case "strPtr":
type = `*${type}`;
Expand All @@ -233,7 +233,7 @@ export const fieldType = (
break;
}
} else if (isOpaque(f.type)) {
type = `wasmtypes.${$isConst}OpaquePtr`;
type = `bindgen.${$isConst}OpaquePtr`;
switch (classifier) {
case "opaquePtr":
type = `*${type}`;
Expand Down
6 changes: 3 additions & 3 deletions packages/wasm-api-bindgen/test/fixtures/enum.zig
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const std = @import("std");
const wasmtypes = @import("wasmapi-types");
const bindgen = @import("wasm-api-bindgen");

pub const ASlice = wasmtypes.Slice([]A, [*]A);
pub const ConstASlice = wasmtypes.Slice([]const A, [*]const A);
pub const ASlice = bindgen.Slice([]A, [*]A);
pub const ConstASlice = bindgen.Slice([]const A, [*]const A);

pub const A = enum(i32) {
foo,
Expand Down
8 changes: 4 additions & 4 deletions packages/wasm-api-bindgen/test/fixtures/funcptr.zig
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const std = @import("std");
const wasmtypes = @import("wasmapi-types");
const bindgen = @import("wasm-api-bindgen");

pub const ASlice = wasmtypes.Slice([]A, [*]A);
pub const ConstASlice = wasmtypes.Slice([]const A, [*]const A);
pub const ASlice = bindgen.Slice([]A, [*]A);
pub const ConstASlice = bindgen.Slice([]const A, [*]const A);

pub const A = *const fn(x: *u32, y: wasmtypes.ConstStringPtr) void;
pub const A = *const fn (x: *u32, y: bindgen.ConstStringPtr) void;

pub const B = extern struct {
a: A,
Expand Down
18 changes: 9 additions & 9 deletions packages/wasm-api-bindgen/test/fixtures/opaque.zig
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const std = @import("std");
const wasmtypes = @import("wasmapi-types");
const bindgen = @import("wasm-api-bindgen");

pub const A = extern struct {
a: wasmtypes.OpaquePtr,
ptr: *wasmtypes.OpaquePtr,
ptr2: *[2]wasmtypes.OpaquePtr,
constPtr: *wasmtypes.ConstOpaquePtr,
slice: wasmtypes.OpaquePtrSlice,
constSlice: wasmtypes.ConstOpaquePtrSlice,
array: [3]wasmtypes.OpaquePtr,
constArray: [3]wasmtypes.ConstOpaquePtr,
a: bindgen.OpaquePtr,
ptr: *bindgen.OpaquePtr,
ptr2: *[2]bindgen.OpaquePtr,
constPtr: *bindgen.ConstOpaquePtr,
slice: bindgen.OpaquePtrSlice,
constSlice: bindgen.ConstOpaquePtrSlice,
array: [3]bindgen.OpaquePtr,
constArray: [3]bindgen.ConstOpaquePtr,
};
10 changes: 5 additions & 5 deletions packages/wasm-api-bindgen/test/fixtures/slices.zig
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const std = @import("std");
const wasmtypes = @import("wasmapi-types");
const bindgen = @import("wasm-api-bindgen");

pub const U8Slice = wasmtypes.Slice([]u8, [*]u8);
pub const ConstU8Slice = wasmtypes.Slice([]const u8, [*]const u8);
pub const U8Slice = bindgen.Slice([]u8, [*]u8);
pub const ConstU8Slice = bindgen.Slice([]const u8, [*]const u8);

pub const ASlice = wasmtypes.Slice([]A, [*]A);
pub const ConstASlice = wasmtypes.Slice([]const A, [*]const A);
pub const ASlice = bindgen.Slice([]A, [*]A);
pub const ConstASlice = bindgen.Slice([]const A, [*]const A);

pub const A = extern struct {
a: u16,
Expand Down
44 changes: 22 additions & 22 deletions packages/wasm-api-bindgen/test/fixtures/string-ptr.zig
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const std = @import("std");
const wasmtypes = @import("wasmapi-types");
const bindgen = @import("wasm-api-bindgen");

pub const Foo = extern struct {
single: wasmtypes.StringPtr,
constSingle: wasmtypes.ConstStringPtr,
multi: [2]wasmtypes.ConstStringPtr,
singlePtr: *wasmtypes.ConstStringPtr,
multiPtr: *[2]wasmtypes.ConstStringPtr,
slice: wasmtypes.StringPtrSlice,
constSlice: wasmtypes.ConstStringPtrSlice,
single: bindgen.StringPtr,
constSingle: bindgen.ConstStringPtr,
multi: [2]bindgen.ConstStringPtr,
singlePtr: *bindgen.ConstStringPtr,
multiPtr: *[2]bindgen.ConstStringPtr,
slice: bindgen.StringPtrSlice,
constSlice: bindgen.ConstStringPtrSlice,
};

export fn Foo_align() usize {
Expand All @@ -20,85 +20,85 @@ export fn Foo_size() usize {
}

export fn Foo_single_align() usize {
return @alignOf(wasmtypes.StringPtr);
return @alignOf(bindgen.StringPtr);
}

export fn Foo_single_offset() usize {
return @offsetOf(Foo, "single");
}

export fn Foo_single_size() usize {
return @sizeOf(wasmtypes.StringPtr);
return @sizeOf(bindgen.StringPtr);
}

export fn Foo_constSingle_align() usize {
return @alignOf(wasmtypes.ConstStringPtr);
return @alignOf(bindgen.ConstStringPtr);
}

export fn Foo_constSingle_offset() usize {
return @offsetOf(Foo, "constSingle");
}

export fn Foo_constSingle_size() usize {
return @sizeOf(wasmtypes.ConstStringPtr);
return @sizeOf(bindgen.ConstStringPtr);
}

export fn Foo_multi_align() usize {
return @alignOf([2]wasmtypes.ConstStringPtr);
return @alignOf([2]bindgen.ConstStringPtr);
}

export fn Foo_multi_offset() usize {
return @offsetOf(Foo, "multi");
}

export fn Foo_multi_size() usize {
return @sizeOf([2]wasmtypes.ConstStringPtr);
return @sizeOf([2]bindgen.ConstStringPtr);
}

export fn Foo_singlePtr_align() usize {
return @alignOf(*wasmtypes.ConstStringPtr);
return @alignOf(*bindgen.ConstStringPtr);
}

export fn Foo_singlePtr_offset() usize {
return @offsetOf(Foo, "singlePtr");
}

export fn Foo_singlePtr_size() usize {
return @sizeOf(*wasmtypes.ConstStringPtr);
return @sizeOf(*bindgen.ConstStringPtr);
}

export fn Foo_multiPtr_align() usize {
return @alignOf(*[2]wasmtypes.ConstStringPtr);
return @alignOf(*[2]bindgen.ConstStringPtr);
}

export fn Foo_multiPtr_offset() usize {
return @offsetOf(Foo, "multiPtr");
}

export fn Foo_multiPtr_size() usize {
return @sizeOf(*[2]wasmtypes.ConstStringPtr);
return @sizeOf(*[2]bindgen.ConstStringPtr);
}

export fn Foo_slice_align() usize {
return @alignOf(wasmtypes.StringPtrSlice);
return @alignOf(bindgen.StringPtrSlice);
}

export fn Foo_slice_offset() usize {
return @offsetOf(Foo, "slice");
}

export fn Foo_slice_size() usize {
return @sizeOf(wasmtypes.StringPtrSlice);
return @sizeOf(bindgen.StringPtrSlice);
}

export fn Foo_constSlice_align() usize {
return @alignOf(wasmtypes.ConstStringPtrSlice);
return @alignOf(bindgen.ConstStringPtrSlice);
}

export fn Foo_constSlice_offset() usize {
return @offsetOf(Foo, "constSlice");
}

export fn Foo_constSlice_size() usize {
return @sizeOf(wasmtypes.ConstStringPtrSlice);
return @sizeOf(bindgen.ConstStringPtrSlice);
}
Loading

0 comments on commit 574af0f

Please sign in to comment.