@thi.ng/wasm-api 0.5.0 → 0.8.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.
- package/CHANGELOG.md +60 -1
- package/README.md +425 -16
- package/api.d.ts +217 -3
- package/api.js +9 -1
- package/bin/wasm-api +12 -0
- package/bridge.d.ts +41 -7
- package/bridge.js +45 -9
- package/cli.d.ts +5 -0
- package/cli.js +142 -0
- package/codegen/typescript.d.ts +35 -0
- package/codegen/typescript.js +189 -0
- package/codegen/utils.d.ts +30 -0
- package/codegen/utils.js +30 -0
- package/codegen/zig.d.ts +25 -0
- package/codegen/zig.js +75 -0
- package/codegen.d.ts +53 -0
- package/codegen.js +148 -0
- package/doc/assets/main.js +52 -0
- package/doc/assets/search.js +1 -0
- package/include/wasmapi.h +60 -0
- package/{zig/core.zig → include/wasmapi.zig} +45 -32
- package/index.d.ts +4 -0
- package/index.js +4 -0
- package/package.json +31 -7
- package/dev/custom.zig +0 -12
- package/dev/fieldinfo.zig +0 -135
- package/dev/hello.zig +0 -14
- package/dev/zig-cache/o/0fd683610fe16c12563bf410950c8193/builtin.zig +0 -39
- package/test/custom.zig +0 -16
- package/test/zig-cache/o/117d44467ded6ce3864cde3c180ef73c/builtin.zig +0 -39
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/wasm-api",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.8.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",
|
|
8
|
+
"bin": "bin/wasm-api",
|
|
8
9
|
"sideEffects": false,
|
|
9
10
|
"repository": {
|
|
10
11
|
"type": "git",
|
|
@@ -32,13 +33,19 @@
|
|
|
32
33
|
"doc:stats": "tools:module-stats",
|
|
33
34
|
"pub": "yarn npm publish --access public",
|
|
34
35
|
"test": "testament test",
|
|
35
|
-
"test:build-zig": "zig build-lib -O ReleaseSmall -target wasm32-freestanding -dynamic --strip --pkg-begin wasmapi
|
|
36
|
+
"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
37
|
},
|
|
37
38
|
"dependencies": {
|
|
38
|
-
"@thi.ng/api": "^8.
|
|
39
|
+
"@thi.ng/api": "^8.4.0",
|
|
40
|
+
"@thi.ng/args": "^2.2.0",
|
|
41
|
+
"@thi.ng/binary": "^3.3.3",
|
|
42
|
+
"@thi.ng/checks": "^3.2.4",
|
|
43
|
+
"@thi.ng/compare": "^2.1.10",
|
|
44
|
+
"@thi.ng/defmulti": "^2.1.12",
|
|
39
45
|
"@thi.ng/errors": "^2.1.10",
|
|
46
|
+
"@thi.ng/file-io": "^0.3.9",
|
|
40
47
|
"@thi.ng/hex": "^2.1.9",
|
|
41
|
-
"@thi.ng/idgen": "^2.1.
|
|
48
|
+
"@thi.ng/idgen": "^2.1.11",
|
|
42
49
|
"@thi.ng/logger": "^1.2.0"
|
|
43
50
|
},
|
|
44
51
|
"devDependencies": {
|
|
@@ -51,6 +58,9 @@
|
|
|
51
58
|
},
|
|
52
59
|
"keywords": [
|
|
53
60
|
"api",
|
|
61
|
+
"bindings",
|
|
62
|
+
"c",
|
|
63
|
+
"codegen",
|
|
54
64
|
"id",
|
|
55
65
|
"logger",
|
|
56
66
|
"memory",
|
|
@@ -73,7 +83,9 @@
|
|
|
73
83
|
"files": [
|
|
74
84
|
"*.js",
|
|
75
85
|
"*.d.ts",
|
|
76
|
-
"
|
|
86
|
+
"bin",
|
|
87
|
+
"include",
|
|
88
|
+
"codegen"
|
|
77
89
|
],
|
|
78
90
|
"exports": {
|
|
79
91
|
".": {
|
|
@@ -85,6 +97,18 @@
|
|
|
85
97
|
"./bridge": {
|
|
86
98
|
"default": "./bridge.js"
|
|
87
99
|
},
|
|
100
|
+
"./codegen": {
|
|
101
|
+
"default": "./codegen.js"
|
|
102
|
+
},
|
|
103
|
+
"./codegen/typescript": {
|
|
104
|
+
"default": "./codegen/typescript.js"
|
|
105
|
+
},
|
|
106
|
+
"./codegen/utils": {
|
|
107
|
+
"default": "./codegen/utils.js"
|
|
108
|
+
},
|
|
109
|
+
"./codegen/zig": {
|
|
110
|
+
"default": "./codegen/zig.js"
|
|
111
|
+
},
|
|
88
112
|
"./object-index": {
|
|
89
113
|
"default": "./object-index.js"
|
|
90
114
|
}
|
|
@@ -93,5 +117,5 @@
|
|
|
93
117
|
"status": "alpha",
|
|
94
118
|
"year": 2022
|
|
95
119
|
},
|
|
96
|
-
"gitHead": "
|
|
120
|
+
"gitHead": "4383dd462ecbafabbb7ae7ba567e3b3738abb9ab\n"
|
|
97
121
|
}
|
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;
|