@thi.ng/wasm-api 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -24,65 +24,70 @@ pub const allocator: ?std.mem.Allocator = alloc: {
24
24
  /// successful returns address of new chunk or zero if failed
25
25
  /// Note: For SIMD compatibility all allocations are aligned to 16 bytes
26
26
  pub export fn _wasm_allocate(numBytes: usize) usize {
27
- if (allocator) |a| {
28
- var buf = a.alignedAlloc(u8, 16, numBytes) catch return 0;
29
- return @ptrToInt(buf.ptr);
27
+ if (allocator) |alloc| {
28
+ var mem = alloc.alignedAlloc(u8, 16, numBytes) catch return 0;
29
+ return @ptrToInt(mem.ptr);
30
30
  }
31
31
  return 0;
32
32
  }
33
33
 
34
34
  /// Frees chunk of heap memory (previously allocated using `_wasm_allocate()`)
35
- /// starting at given address. Note: This is a no-op currently.
36
- pub export fn _wasm_free(addr: usize) void {
37
- _ = addr;
35
+ /// starting at given address and of given byte length.
36
+ /// Note: This is a no-op if the allocator is explicitly disabled (see `setAllocator()`),
37
+ pub export fn _wasm_free(addr: usize, numBytes: usize) void {
38
+ if (allocator) |alloc| {
39
+ var mem = [2]usize{ addr, numBytes };
40
+ printFmt("{d}", .{@ptrCast(*[]u8, &mem).*});
41
+ alloc.free(@ptrCast(*[]u8, &mem).*);
42
+ }
38
43
  }
39
44
 
40
45
  /// Prints number using configured JS logger
41
- pub extern "core" fn printI8(x: i8) void;
46
+ pub extern "wasmapi" fn printI8(x: i8) void;
42
47
  /// Prints number using configured JS logger
43
- pub extern "core" fn printU8(x: u8) void;
48
+ pub extern "wasmapi" fn printU8(x: u8) void;
44
49
  /// Prints hex number using configured JS logger
45
- pub extern "core" fn printU8Hex(x: u8) void;
50
+ pub extern "wasmapi" fn printU8Hex(x: u8) void;
46
51
 
47
52
  /// Prints number using configured JS logger
48
- pub extern "core" fn printI16(x: i16) void;
53
+ pub extern "wasmapi" fn printI16(x: i16) void;
49
54
  /// Prints number using configured JS logger
50
- pub extern "core" fn printU16(x: u16) void;
55
+ pub extern "wasmapi" fn printU16(x: u16) void;
51
56
  /// Prints hex number using configured JS logger
52
- pub extern "core" fn printU16Hex(x: u16) void;
57
+ pub extern "wasmapi" fn printU16Hex(x: u16) void;
53
58
 
54
59
  /// Prints number using configured JS logger
55
- pub extern "core" fn printI32(x: i32) void;
60
+ pub extern "wasmapi" fn printI32(x: i32) void;
56
61
  /// Prints number using configured JS logger
57
- pub extern "core" fn printU32(x: u32) void;
62
+ pub extern "wasmapi" fn printU32(x: u32) void;
58
63
  /// Prints hex number using configured JS logger
59
- pub extern "core" fn printU32Hex(x: u32) void;
64
+ pub extern "wasmapi" fn printU32Hex(x: u32) void;
60
65
 
61
66
  /// Prints decomposed i64 number using configured JS logger
62
- pub extern "core" fn _printI64(hi: i32, lo: i32) void;
67
+ pub extern "wasmapi" fn _printI64(hi: i32, lo: i32) void;
63
68
  /// Convenience wrapper for _printI64(), accepting an i64
64
69
  pub fn printI64(x: i64) void {
65
70
  _printI64(@truncate(i32, x >> 32), @truncate(i32, x));
66
71
  }
67
72
 
68
73
  /// Prints decomposed u64 number using configured JS logger
69
- pub extern "core" fn _printU64(hi: u32, lo: u32) void;
74
+ pub extern "wasmapi" fn _printU64(hi: u32, lo: u32) void;
70
75
  /// Convenience wrapper for _printU64(), accepting an u64
71
76
  pub fn printU64(x: u64) void {
72
77
  _printU64(@truncate(u32, x >> 32), @truncate(u32, x));
73
78
  }
74
79
 
75
80
  /// Prints decomposed u64 hex number using configured JS logger
76
- pub extern "core" fn _printU64Hex(hi: u32, lo: u32) void;
81
+ pub extern "wasmapi" fn _printU64Hex(hi: u32, lo: u32) void;
77
82
  /// Convenience wrapper for _printU64Hex(), accepting an u64
78
83
  pub fn printU64Hex(x: u64) void {
79
84
  _printU64Hex(@truncate(u32, x >> 32), @truncate(u32, x));
80
85
  }
81
86
 
82
87
  /// Prints number using configured JS logger
83
- pub extern "core" fn printF32(x: f32) void;
88
+ pub extern "wasmapi" fn printF32(x: f32) void;
84
89
  /// Prints number using configured JS logger
85
- pub extern "core" fn printF64(x: f64) void;
90
+ pub extern "wasmapi" fn printF64(x: f64) void;
86
91
 
87
92
  /// Prints pointer as hex number using configured JS logger
88
93
  pub fn printPtr(ptr: *const anyopaque) void {
@@ -90,25 +95,25 @@ pub fn printPtr(ptr: *const anyopaque) void {
90
95
  }
91
96
 
92
97
  /// Prints number array using configured JS logger
93
- pub extern "core" fn _printI8Array(addr: usize, len: usize) void;
98
+ pub extern "wasmapi" fn _printI8Array(addr: usize, len: usize) void;
94
99
  /// Prints number array using configured JS logger
95
- pub extern "core" fn _printU8Array(addr: usize, len: usize) void;
100
+ pub extern "wasmapi" fn _printU8Array(addr: usize, len: usize) void;
96
101
  /// Prints number array using configured JS logger
97
- pub extern "core" fn _printI16Array(addr: usize, len: usize) void;
102
+ pub extern "wasmapi" fn _printI16Array(addr: usize, len: usize) void;
98
103
  /// Prints number array using configured JS logger
99
- pub extern "core" fn _printU16Array(addr: usize, len: usize) void;
104
+ pub extern "wasmapi" fn _printU16Array(addr: usize, len: usize) void;
100
105
  /// Prints number array using configured JS logger
101
- pub extern "core" fn _printI32Array(addr: usize, len: usize) void;
106
+ pub extern "wasmapi" fn _printI32Array(addr: usize, len: usize) void;
102
107
  /// Prints number array using configured JS logger
103
- pub extern "core" fn _printU32Array(addr: usize, len: usize) void;
108
+ pub extern "wasmapi" fn _printU32Array(addr: usize, len: usize) void;
104
109
  /// Prints number array using configured JS logger
105
- pub extern "core" fn _printI64Array(addr: usize, len: usize) void;
110
+ pub extern "wasmapi" fn _printI64Array(addr: usize, len: usize) void;
106
111
  /// Prints number array using configured JS logger
107
- pub extern "core" fn _printU64Array(addr: usize, len: usize) void;
112
+ pub extern "wasmapi" fn _printU64Array(addr: usize, len: usize) void;
108
113
  /// Prints number array using configured JS logger
109
- pub extern "core" fn _printF32Array(addr: usize, len: usize) void;
114
+ pub extern "wasmapi" fn _printF32Array(addr: usize, len: usize) void;
110
115
  /// Prints number array using configured JS logger
111
- pub extern "core" fn _printF64Array(addr: usize, len: usize) void;
116
+ pub extern "wasmapi" fn _printF64Array(addr: usize, len: usize) void;
112
117
 
113
118
  /// Prints number array using configured JS logger
114
119
  pub fn printI8Array(buf: []const i8) void {
@@ -152,10 +157,18 @@ pub fn printF64Array(buf: []const f64) void {
152
157
  }
153
158
 
154
159
  /// Prints a zero-terminated string using configured JS logger
155
- pub extern "core" fn _printStr0(addr: usize) void;
160
+ pub extern "wasmapi" fn _printStr0(addr: usize) void;
156
161
  /// Prints a string of given length using configured JS logger
157
- pub extern "core" fn _printStr(addr: usize, len: usize) void;
162
+ pub extern "wasmapi" fn _printStr(addr: usize, len: usize) void;
158
163
  /// Convenience wrapper for _printStr, accepting a slice as arg
159
164
  pub fn printStr(msg: []const u8) void {
160
165
  _printStr(@ptrToInt(msg.ptr), msg.len);
161
166
  }
167
+
168
+ pub fn printFmt(comptime fmt: []const u8, args: anytype) void {
169
+ if (allocator) |alloc| {
170
+ const res = std.fmt.allocPrint(alloc, fmt, args) catch return;
171
+ defer alloc.free(res);
172
+ printStr(res);
173
+ }
174
+ }
package/index.d.ts CHANGED
@@ -1,4 +1,8 @@
1
1
  export * from "./api.js";
2
2
  export * from "./bridge.js";
3
+ export * from "./codegen.js";
3
4
  export * from "./object-index.js";
5
+ export * from "./codegen/typescript.js";
6
+ export * from "./codegen/zig.js";
7
+ export * from "./codegen/utils.js";
4
8
  //# sourceMappingURL=index.d.ts.map
package/index.js CHANGED
@@ -1,3 +1,7 @@
1
1
  export * from "./api.js";
2
2
  export * from "./bridge.js";
3
+ export * from "./codegen.js";
3
4
  export * from "./object-index.js";
5
+ export * from "./codegen/typescript.js";
6
+ export * from "./codegen/zig.js";
7
+ export * from "./codegen/utils.js";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@thi.ng/wasm-api",
3
- "version": "0.5.0",
4
- "description": "Modular, extensible API bridge and generic glue code between JS & WebAssembly",
3
+ "version": "0.6.0",
4
+ "description": "Generic, modular, extensible API bridge, glue code and bindings code generator for hybrid JS & WebAssembly projects",
5
5
  "type": "module",
6
6
  "module": "./index.js",
7
7
  "typings": "./index.d.ts",
@@ -32,13 +32,17 @@
32
32
  "doc:stats": "tools:module-stats",
33
33
  "pub": "yarn npm publish --access public",
34
34
  "test": "testament test",
35
- "test:build-zig": "zig build-lib -O ReleaseSmall -target wasm32-freestanding -dynamic --strip --pkg-begin wasmapi zig/core.zig --pkg-end test/custom.zig && wasm-dis -o custom.wast custom.wasm && cp custom.wasm test"
35
+ "test:build-zig": "zig build-lib -O ReleaseSmall -target wasm32-freestanding -dynamic --strip --pkg-begin wasmapi include/wasmapi.zig --pkg-end test/custom.zig && wasm-dis -o custom.wast custom.wasm && cp custom.wasm test"
36
36
  },
37
37
  "dependencies": {
38
- "@thi.ng/api": "^8.3.9",
38
+ "@thi.ng/api": "^8.4.0",
39
+ "@thi.ng/binary": "^3.3.3",
40
+ "@thi.ng/checks": "^3.2.4",
41
+ "@thi.ng/compare": "^2.1.10",
42
+ "@thi.ng/defmulti": "^2.1.12",
39
43
  "@thi.ng/errors": "^2.1.10",
40
44
  "@thi.ng/hex": "^2.1.9",
41
- "@thi.ng/idgen": "^2.1.10",
45
+ "@thi.ng/idgen": "^2.1.11",
42
46
  "@thi.ng/logger": "^1.2.0"
43
47
  },
44
48
  "devDependencies": {
@@ -51,6 +55,9 @@
51
55
  },
52
56
  "keywords": [
53
57
  "api",
58
+ "bindings",
59
+ "c",
60
+ "codegen",
54
61
  "id",
55
62
  "logger",
56
63
  "memory",
@@ -73,7 +80,8 @@
73
80
  "files": [
74
81
  "*.js",
75
82
  "*.d.ts",
76
- "*.zig"
83
+ "codegen",
84
+ "include"
77
85
  ],
78
86
  "exports": {
79
87
  ".": {
@@ -85,6 +93,18 @@
85
93
  "./bridge": {
86
94
  "default": "./bridge.js"
87
95
  },
96
+ "./codegen": {
97
+ "default": "./codegen.js"
98
+ },
99
+ "./codegen/typescript": {
100
+ "default": "./codegen/typescript.js"
101
+ },
102
+ "./codegen/utils": {
103
+ "default": "./codegen/utils.js"
104
+ },
105
+ "./codegen/zig": {
106
+ "default": "./codegen/zig.js"
107
+ },
88
108
  "./object-index": {
89
109
  "default": "./object-index.js"
90
110
  }
@@ -93,5 +113,5 @@
93
113
  "status": "alpha",
94
114
  "year": 2022
95
115
  },
96
- "gitHead": "e579cb171fc720cbf0b71d3a5f4adfacccdaf214\n"
116
+ "gitHead": "295e76c6f68ef34ba2117ff77612848e09f5c587\n"
97
117
  }
package/dev/custom.zig DELETED
@@ -1,12 +0,0 @@
1
- // Import JS core API
2
- const js = @import("wasmapi");
3
-
4
- /// Fill vec2 with random values
5
- extern fn custom_randomVec2(addr: usize) void;
6
-
7
- export fn test_random_vec2() void {
8
- var foo = [2]f32{ 0, 0 };
9
- js.printF32Array(foo[0..]);
10
- custom_randomVec2(@ptrToInt(&foo));
11
- js.printF32Array(foo[0..]);
12
- }
package/dev/fieldinfo.zig DELETED
@@ -1,135 +0,0 @@
1
- const js = @import("wasmapi");
2
- const std = @import("std");
3
-
4
- const Foo = struct {
5
- pos: [2]f32,
6
- col: [3]f32,
7
- speed: u8,
8
- acc: u16,
9
- };
10
-
11
- fn writeU32(buf: [*]u8, i: u32, x: u32) void {
12
- buf[i] = @intCast(u8, x & 0xff);
13
- buf[i + 1] = @intCast(u8, x >> 8 & 0xff);
14
- buf[i + 2] = @intCast(u8, x >> 16 & 0xff);
15
- buf[i + 3] = @intCast(u8, x >> 24);
16
- }
17
-
18
- const FieldType = enum(u8) {
19
- I8,
20
- U8,
21
- I16,
22
- U16,
23
- I32,
24
- U32,
25
- F32,
26
- F64,
27
-
28
- pub fn fromTypeInfo(comptime info: std.builtin.TypeInfo) FieldType {
29
- if (info == .Int) {
30
- const bits = info.Int.bits;
31
- if (!(bits == 8 or bits == 16 or bits == 32)) {
32
- @compileError("unsupported int type (only 8, 16, 32)");
33
- }
34
- if (info.Int.signedness == .signed) {
35
- return switch (bits) {
36
- 8 => .I8,
37
- 16 => .I16,
38
- 32 => .I32,
39
- else => unreachable,
40
- };
41
- } else {
42
- return switch (bits) {
43
- 8 => .U8,
44
- 16 => .U16,
45
- 32 => .U32,
46
- else => unreachable,
47
- };
48
- }
49
- } else if (info == .Float) {
50
- return switch (info.Float.bits) {
51
- 32 => .F32,
52
- 64 => .F64,
53
- else => @compileError("unsupported float type (only f32, f64)"),
54
- };
55
- }
56
- @compileError("unsupported field type");
57
- }
58
- };
59
-
60
- // int
61
- // float
62
- // ptr
63
- // array
64
- // slice
65
-
66
- // 8
67
- // 16
68
- // 32
69
- // 64
70
-
71
- const Field = packed struct {
72
- name: [15]u8 = [_]u8{0} ** 15,
73
- tag: FieldType = .U8,
74
- offset: u32,
75
- len: u32 = 0,
76
-
77
- pub fn fromTypeInfo(comptime T: type, comptime field: std.builtin.TypeInfo.StructField) Field {
78
- const finfo = @typeInfo(field.field_type);
79
- var ftype: FieldType = .U8;
80
- var flen = 0;
81
- if (!(finfo == .Int or finfo == .Float or finfo == .Array)) {
82
- @compileError("unsupported field type: " ++ @typeName(field.field_type));
83
- }
84
- if (finfo == .Array) {
85
- const cinfo = @typeInfo(finfo.Array.child);
86
- if (!(cinfo == .Int or cinfo == .Float)) {
87
- @compileError("unsupported field array type: " ++ @typeName(field.field_type));
88
- }
89
- ftype = FieldType.fromTypeInfo(cinfo);
90
- flen = finfo.Array.len;
91
- } else {
92
- ftype = FieldType.fromTypeInfo(finfo);
93
- }
94
- var res: Field = .{
95
- .tag = ftype,
96
- .offset = @offsetOf(T, field.name),
97
- .len = flen,
98
- };
99
- const len = @minimum(14, field.name.len);
100
- std.mem.copy(u8, res.name[0..len], field.name[0..len]);
101
- return res;
102
- }
103
- };
104
-
105
- fn writeTypeInfo(comptime T: type) []u8 {
106
- const fields = @typeInfo(T).Struct.fields;
107
- const fsize = @sizeOf(Field);
108
- var buf: [fields.len * fsize + 4]u8 = undefined;
109
- var i = 4;
110
- writeU32(&buf, 0, fields.len);
111
- for (fields) |field| {
112
- var f = Field.fromTypeInfo(T, field);
113
- std.mem.copy(
114
- u8,
115
- buf[i .. i + fsize],
116
- @ptrCast(*[fsize]u8, &f)[0..],
117
- );
118
- i += fsize;
119
- }
120
- return buf[0..];
121
- }
122
-
123
- export var Foo__info = writeTypeInfo(Foo);
124
- export var Bar__info = writeTypeInfo(struct { x: i16, y: i16 });
125
-
126
- export var U64: u64 = 0xdecafbadcafebabe;
127
- export var I64: i64 = -0x8000000000000000;
128
-
129
- export fn foo() void {
130
- js.printI64(I64);
131
- js.printU64(U64);
132
- js.printI64Array(&[_]i64{ I64, I64 });
133
- js.printU32(@truncate(u32, U64 >> 32));
134
- js.printU32Hex(@truncate(u32, U64 >> 32));
135
- }
package/dev/hello.zig DELETED
@@ -1,14 +0,0 @@
1
- //! Example Zig application (hello.zig)
2
-
3
- /// import externals
4
- /// see build command for configuration
5
- const js = @import("wasmapi");
6
- const std = @import("std");
7
-
8
- // var buf: [1024]u8 = undefined;
9
- // var fba = std.heap.FixedBufferAllocator.init(&buf);
10
- pub const WASM_ALLOCATOR: ?std.mem.Allocator = null; //fba.allocator();
11
-
12
- export fn start() void {
13
- js.printStr("hello world!");
14
- }
@@ -1,39 +0,0 @@
1
- const std = @import("std");
2
- /// Zig version. When writing code that supports multiple versions of Zig, prefer
3
- /// feature detection (i.e. with `@hasDecl` or `@hasField`) over version checks.
4
- pub const zig_version = std.SemanticVersion.parse("0.10.0-dev.3034+6fab6c3e4") catch unreachable;
5
- pub const zig_backend = std.builtin.CompilerBackend.stage1;
6
- /// Temporary until self-hosted supports the `cpu.arch` value.
7
- pub const stage2_arch: std.Target.Cpu.Arch = .wasm32;
8
-
9
- pub const output_mode = std.builtin.OutputMode.Lib;
10
- pub const link_mode = std.builtin.LinkMode.Dynamic;
11
- pub const is_test = false;
12
- pub const single_threaded = true;
13
- pub const abi = std.Target.Abi.musl;
14
- pub const cpu: std.Target.Cpu = .{
15
- .arch = .wasm32,
16
- .model = &std.Target.wasm.cpu.generic,
17
- .features = std.Target.wasm.featureSet(&[_]std.Target.wasm.Feature{
18
- }),
19
- };
20
- pub const os = std.Target.Os{
21
- .tag = .freestanding,
22
- .version_range = .{ .none = {} },
23
- };
24
- pub const target = std.Target{
25
- .cpu = cpu,
26
- .os = os,
27
- .abi = abi,
28
- };
29
- pub const object_format = std.Target.ObjectFormat.wasm;
30
- pub const mode = std.builtin.Mode.ReleaseSmall;
31
- pub const link_libc = false;
32
- pub const link_libcpp = false;
33
- pub const have_error_return_tracing = false;
34
- pub const valgrind_support = false;
35
- pub const sanitize_thread = false;
36
- pub const position_independent_code = true;
37
- pub const position_independent_executable = false;
38
- pub const strip_debug_info = true;
39
- pub const code_model = std.builtin.CodeModel.default;
package/test/custom.zig DELETED
@@ -1,16 +0,0 @@
1
- // Import JS core API
2
- const js = @import("wasmapi");
3
- const std = @import("std");
4
-
5
- pub const WASM_ALLOCATOR: ?std.mem.Allocator = null;
6
-
7
- /// Fill vec2 with random values
8
- /// Associate this function with the "custom" import section
9
- extern "custom" fn setVec2(addr: usize) void;
10
-
11
- export fn test_setVec2() void {
12
- var foo = [2]f32{ 0, 0 };
13
- js.printF32Array(foo[0..]);
14
- setVec2(@ptrToInt(&foo));
15
- js.printF32Array(foo[0..]);
16
- }
@@ -1,39 +0,0 @@
1
- const std = @import("std");
2
- /// Zig version. When writing code that supports multiple versions of Zig, prefer
3
- /// feature detection (i.e. with `@hasDecl` or `@hasField`) over version checks.
4
- pub const zig_version = std.SemanticVersion.parse("0.10.0-dev.3034+6fab6c3e4") catch unreachable;
5
- pub const zig_backend = std.builtin.CompilerBackend.stage1;
6
- /// Temporary until self-hosted supports the `cpu.arch` value.
7
- pub const stage2_arch: std.Target.Cpu.Arch = .wasm32;
8
-
9
- pub const output_mode = std.builtin.OutputMode.Lib;
10
- pub const link_mode = std.builtin.LinkMode.Dynamic;
11
- pub const is_test = false;
12
- pub const single_threaded = true;
13
- pub const abi = std.Target.Abi.musl;
14
- pub const cpu: std.Target.Cpu = .{
15
- .arch = .wasm32,
16
- .model = &std.Target.wasm.cpu.generic,
17
- .features = std.Target.wasm.featureSet(&[_]std.Target.wasm.Feature{
18
- }),
19
- };
20
- pub const os = std.Target.Os{
21
- .tag = .freestanding,
22
- .version_range = .{ .none = {} },
23
- };
24
- pub const target = std.Target{
25
- .cpu = cpu,
26
- .os = os,
27
- .abi = abi,
28
- };
29
- pub const object_format = std.Target.ObjectFormat.wasm;
30
- pub const mode = std.builtin.Mode.ReleaseSmall;
31
- pub const link_libc = false;
32
- pub const link_libcpp = false;
33
- pub const have_error_return_tracing = false;
34
- pub const valgrind_support = false;
35
- pub const sanitize_thread = false;
36
- pub const position_independent_code = true;
37
- pub const position_independent_executable = false;
38
- pub const strip_debug_info = true;
39
- pub const code_model = std.builtin.CodeModel.default;