From 574af0ff89e9838a6cadc800cf568950bfac1213 Mon Sep 17 00:00:00 2001 From: Karsten Schmidt Date: Wed, 30 Nov 2022 15:50:23 +0100 Subject: [PATCH] refactor(wasm-api-bindgen): update Zig codegen default imports (#368) - adopt unscoped JS pkg name also as Zig import - update fixtures - update readme --- packages/wasm-api-bindgen/README.md | 14 +++--- packages/wasm-api-bindgen/src/zig.ts | 14 +++--- .../wasm-api-bindgen/test/fixtures/enum.zig | 6 +-- .../test/fixtures/funcptr.zig | 8 ++-- .../wasm-api-bindgen/test/fixtures/opaque.zig | 18 ++++---- .../wasm-api-bindgen/test/fixtures/slices.zig | 10 ++--- .../test/fixtures/string-ptr.zig | 44 +++++++++---------- .../test/fixtures/string-slice.zig | 44 +++++++++---------- .../wasm-api-bindgen/test/fixtures/union.zig | 2 +- packages/wasm-api-bindgen/tpl.readme.md | 12 ++--- 10 files changed, 86 insertions(+), 86 deletions(-) diff --git a/packages/wasm-api-bindgen/README.md b/packages/wasm-api-bindgen/README.md index 02af8bb818..5ad8eb3c96 100644 --- a/packages/wasm-api-bindgen/README.md +++ b/packages/wasm-api-bindgen/README.md @@ -615,7 +615,7 @@ export const $Event: WasmTypeConstructor = (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) { @@ -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, }; @@ -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 @@ -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 diff --git a/packages/wasm-api-bindgen/src/zig.ts b/packages/wasm-api-bindgen/src/zig.ts index 456c2f5ed5..d6f052040c 100644 --- a/packages/wasm-api-bindgen/src/zig.ts +++ b/packages/wasm-api-bindgen/src/zig.ts @@ -52,14 +52,14 @@ export const ZIG = (opts: Partial = {}) => { 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});` ); } } @@ -141,7 +141,7 @@ export const ZIG = (opts: Partial = {}) => { opts ).type; acc.push( - `pub const ${ptr.name} = *const fn(${args}) ${rtype};`, + `pub const ${ptr.name} = *const fn (${args}) ${rtype};`, "" ); }, @@ -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}`; @@ -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}`; diff --git a/packages/wasm-api-bindgen/test/fixtures/enum.zig b/packages/wasm-api-bindgen/test/fixtures/enum.zig index 48bca2aec0..a8a2842741 100644 --- a/packages/wasm-api-bindgen/test/fixtures/enum.zig +++ b/packages/wasm-api-bindgen/test/fixtures/enum.zig @@ -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, diff --git a/packages/wasm-api-bindgen/test/fixtures/funcptr.zig b/packages/wasm-api-bindgen/test/fixtures/funcptr.zig index d7ddfb7e38..ab1497b162 100644 --- a/packages/wasm-api-bindgen/test/fixtures/funcptr.zig +++ b/packages/wasm-api-bindgen/test/fixtures/funcptr.zig @@ -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, diff --git a/packages/wasm-api-bindgen/test/fixtures/opaque.zig b/packages/wasm-api-bindgen/test/fixtures/opaque.zig index 3eb137e28a..93469e3ab8 100644 --- a/packages/wasm-api-bindgen/test/fixtures/opaque.zig +++ b/packages/wasm-api-bindgen/test/fixtures/opaque.zig @@ -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, }; diff --git a/packages/wasm-api-bindgen/test/fixtures/slices.zig b/packages/wasm-api-bindgen/test/fixtures/slices.zig index 00c16f6f5f..7d1ab5b18c 100644 --- a/packages/wasm-api-bindgen/test/fixtures/slices.zig +++ b/packages/wasm-api-bindgen/test/fixtures/slices.zig @@ -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, diff --git a/packages/wasm-api-bindgen/test/fixtures/string-ptr.zig b/packages/wasm-api-bindgen/test/fixtures/string-ptr.zig index 0eda5761e2..a854e622cb 100644 --- a/packages/wasm-api-bindgen/test/fixtures/string-ptr.zig +++ b/packages/wasm-api-bindgen/test/fixtures/string-ptr.zig @@ -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 { @@ -20,7 +20,7 @@ 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 { @@ -28,11 +28,11 @@ export fn Foo_single_offset() usize { } 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 { @@ -40,11 +40,11 @@ export fn Foo_constSingle_offset() usize { } 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 { @@ -52,11 +52,11 @@ export fn Foo_multi_offset() usize { } 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 { @@ -64,11 +64,11 @@ export fn Foo_singlePtr_offset() usize { } 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 { @@ -76,11 +76,11 @@ export fn Foo_multiPtr_offset() usize { } 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 { @@ -88,11 +88,11 @@ export fn Foo_slice_offset() usize { } 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 { @@ -100,5 +100,5 @@ export fn Foo_constSlice_offset() usize { } export fn Foo_constSlice_size() usize { - return @sizeOf(wasmtypes.ConstStringPtrSlice); + return @sizeOf(bindgen.ConstStringPtrSlice); } diff --git a/packages/wasm-api-bindgen/test/fixtures/string-slice.zig b/packages/wasm-api-bindgen/test/fixtures/string-slice.zig index 714454acf8..31c5574941 100644 --- a/packages/wasm-api-bindgen/test/fixtures/string-slice.zig +++ b/packages/wasm-api-bindgen/test/fixtures/string-slice.zig @@ -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.String, - constSingle: wasmtypes.ConstString, - multi: [2]wasmtypes.ConstString, - singlePtr: *wasmtypes.ConstString, - multiPtr: *[2]wasmtypes.ConstString, - slice: wasmtypes.StringSlice, - constSlice: wasmtypes.ConstStringSlice, + single: bindgen.String, + constSingle: bindgen.ConstString, + multi: [2]bindgen.ConstString, + singlePtr: *bindgen.ConstString, + multiPtr: *[2]bindgen.ConstString, + slice: bindgen.StringSlice, + constSlice: bindgen.ConstStringSlice, }; export fn Foo_align() usize { @@ -20,7 +20,7 @@ export fn Foo_size() usize { } export fn Foo_single_align() usize { - return @alignOf(wasmtypes.String); + return @alignOf(bindgen.String); } export fn Foo_single_offset() usize { @@ -28,11 +28,11 @@ export fn Foo_single_offset() usize { } export fn Foo_single_size() usize { - return @sizeOf(wasmtypes.String); + return @sizeOf(bindgen.String); } export fn Foo_constSingle_align() usize { - return @alignOf(wasmtypes.ConstString); + return @alignOf(bindgen.ConstString); } export fn Foo_constSingle_offset() usize { @@ -40,11 +40,11 @@ export fn Foo_constSingle_offset() usize { } export fn Foo_constSingle_size() usize { - return @sizeOf(wasmtypes.ConstString); + return @sizeOf(bindgen.ConstString); } export fn Foo_multi_align() usize { - return @alignOf([2]wasmtypes.ConstString); + return @alignOf([2]bindgen.ConstString); } export fn Foo_multi_offset() usize { @@ -52,11 +52,11 @@ export fn Foo_multi_offset() usize { } export fn Foo_multi_size() usize { - return @sizeOf([2]wasmtypes.ConstString); + return @sizeOf([2]bindgen.ConstString); } export fn Foo_singlePtr_align() usize { - return @alignOf(*wasmtypes.ConstString); + return @alignOf(*bindgen.ConstString); } export fn Foo_singlePtr_offset() usize { @@ -64,11 +64,11 @@ export fn Foo_singlePtr_offset() usize { } export fn Foo_singlePtr_size() usize { - return @sizeOf(*wasmtypes.ConstString); + return @sizeOf(*bindgen.ConstString); } export fn Foo_multiPtr_align() usize { - return @alignOf(*[2]wasmtypes.ConstString); + return @alignOf(*[2]bindgen.ConstString); } export fn Foo_multiPtr_offset() usize { @@ -76,11 +76,11 @@ export fn Foo_multiPtr_offset() usize { } export fn Foo_multiPtr_size() usize { - return @sizeOf(*[2]wasmtypes.ConstString); + return @sizeOf(*[2]bindgen.ConstString); } export fn Foo_slice_align() usize { - return @alignOf(wasmtypes.StringSlice); + return @alignOf(bindgen.StringSlice); } export fn Foo_slice_offset() usize { @@ -88,11 +88,11 @@ export fn Foo_slice_offset() usize { } export fn Foo_slice_size() usize { - return @sizeOf(wasmtypes.StringSlice); + return @sizeOf(bindgen.StringSlice); } export fn Foo_constSlice_align() usize { - return @alignOf(wasmtypes.ConstStringSlice); + return @alignOf(bindgen.ConstStringSlice); } export fn Foo_constSlice_offset() usize { @@ -100,5 +100,5 @@ export fn Foo_constSlice_offset() usize { } export fn Foo_constSlice_size() usize { - return @sizeOf(wasmtypes.ConstStringSlice); + return @sizeOf(bindgen.ConstStringSlice); } diff --git a/packages/wasm-api-bindgen/test/fixtures/union.zig b/packages/wasm-api-bindgen/test/fixtures/union.zig index 0f36d724fc..c3a370c66a 100644 --- a/packages/wasm-api-bindgen/test/fixtures/union.zig +++ b/packages/wasm-api-bindgen/test/fixtures/union.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const wasmtypes = @import("wasmapi-types"); +const bindgen = @import("wasm-api-bindgen"); pub const A = extern struct { a: u8, diff --git a/packages/wasm-api-bindgen/tpl.readme.md b/packages/wasm-api-bindgen/tpl.readme.md index 8de54964b9..23dd530d00 100644 --- a/packages/wasm-api-bindgen/tpl.readme.md +++ b/packages/wasm-api-bindgen/tpl.readme.md @@ -586,7 +586,7 @@ export const $Event: WasmTypeConstructor = (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) { @@ -607,7 +607,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, }; @@ -626,15 +626,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