@thi.ng/wasm-api 1.3.2 → 1.4.1
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 +14 -1
- package/README.md +41 -1
- package/package.json +6 -6
- package/zig/build-v0.11.zig +128 -0
- package/zig/build.zig +6 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
-
- **Last updated**: 2023-
|
|
3
|
+
- **Last updated**: 2023-03-02T18:09:03Z
|
|
4
4
|
- **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
|
|
5
5
|
|
|
6
6
|
All notable changes to this project will be documented in this file.
|
|
@@ -9,6 +9,19 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
|
|
|
9
9
|
**Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
|
|
10
10
|
and/or version bumps of transitive dependencies.
|
|
11
11
|
|
|
12
|
+
## [1.4.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/wasm-api@1.4.0) (2023-02-17)
|
|
13
|
+
|
|
14
|
+
#### 🚀 Features
|
|
15
|
+
|
|
16
|
+
- add build script for Zig v0.11.0-dev ([4c8abcb](https://github.com/thi-ng/umbrella/commit/4c8abcb))
|
|
17
|
+
|
|
18
|
+
#### ♻️ Refactoring
|
|
19
|
+
|
|
20
|
+
- update build-v0.11.zig build script ([fe7c24c](https://github.com/thi-ng/umbrella/commit/fe7c24c))
|
|
21
|
+
- single source of truth for core module names
|
|
22
|
+
- rename .mode => .optimize (as per new zig nomenclature)
|
|
23
|
+
- add autodoc generation option
|
|
24
|
+
|
|
12
25
|
## [1.3.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/wasm-api@1.3.0) (2023-02-05)
|
|
13
26
|
|
|
14
27
|
#### 🚀 Features
|
package/README.md
CHANGED
|
@@ -16,6 +16,9 @@ This project is part of the
|
|
|
16
16
|
- [API module auto-initialization](#api-module-auto-initialization)
|
|
17
17
|
- [Object indices & handles](#object-indices--handles)
|
|
18
18
|
- [Using the Zig build system](#using-the-zig-build-system)
|
|
19
|
+
- [Zig v0.10.1 or older](#zig-v0101-or-older)
|
|
20
|
+
- [Zig v0.11.0-dev or newer](#zig-v0110-dev-or-newer)
|
|
21
|
+
- [Example projects](#example-projects)
|
|
19
22
|
- [Naming & structural conventions](#naming--structural-conventions)
|
|
20
23
|
- [Status](#status)
|
|
21
24
|
- [Support packages](#support-packages)
|
|
@@ -309,6 +312,8 @@ This package provides utilities to simplify using hybrid TS/Zig WASM API modules
|
|
|
309
312
|
which are distributed as NPM packages. Using these utils, a build file for Zig's
|
|
310
313
|
built-in build system is as simple as:
|
|
311
314
|
|
|
315
|
+
### Zig v0.10.1 or older
|
|
316
|
+
|
|
312
317
|
```zig
|
|
313
318
|
const std = @import("std");
|
|
314
319
|
|
|
@@ -335,10 +340,45 @@ pub fn build(b: *std.build.Builder) void {
|
|
|
335
340
|
}
|
|
336
341
|
```
|
|
337
342
|
|
|
343
|
+
### Zig v0.11.0-dev or newer
|
|
344
|
+
|
|
345
|
+
(Note: Several new build options have been added, e.g. initial/max memory
|
|
346
|
+
config, autodoc generation)
|
|
347
|
+
|
|
348
|
+
```zig
|
|
349
|
+
const std = @import("std");
|
|
350
|
+
|
|
351
|
+
pub fn build(b: *std.Build) void {
|
|
352
|
+
// obtain a standard std.Build.CompileStep, pre-configured w/ given options
|
|
353
|
+
// see source comments in imported build-v0.11.zig for further details...
|
|
354
|
+
var lib = @import("node_modules/@thi.ng/wasm-api/zig/build-v0.11.zig").wasmLib(b, .{
|
|
355
|
+
// Declare extra WASM API modules to use
|
|
356
|
+
// Each can also declare dependencies to other modules
|
|
357
|
+
// (`wasm-api` and `wasm-api-bindgen` are made available everywhere)
|
|
358
|
+
.modules = &.{
|
|
359
|
+
.{ .name = "wasm-api-dom", .path = "@thi.ng/wasm-api-dom/zig/lib.zig" },
|
|
360
|
+
.{ .name = "wasm-api-schedule", .path = "@thi.ng/wasm-api-schedule/zig/lib.zig" },
|
|
361
|
+
},
|
|
362
|
+
// (optional) optimization mode override
|
|
363
|
+
// if commented out, we can pass CLI args to choose mode (default: .Debug)
|
|
364
|
+
.optimize = .ReleaseSmall,
|
|
365
|
+
});
|
|
366
|
+
// optionally, add further custom configuration
|
|
367
|
+
// ...
|
|
368
|
+
|
|
369
|
+
// finally trigger build
|
|
370
|
+
lib.install();
|
|
371
|
+
}
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
### Example projects
|
|
375
|
+
|
|
338
376
|
All bundled example projects (see [list below](#usage-examples)) are being built
|
|
339
377
|
via this script. **Please find more details/options in the commented source
|
|
340
378
|
code:**
|
|
341
|
-
|
|
379
|
+
|
|
380
|
+
- [`/zig/build.zig`](https://github.com/thi-ng/umbrella/blob/develop/packages/wasm-api/zig/build.zig)
|
|
381
|
+
- [`/zig/build-v0.11.zig`](https://github.com/thi-ng/umbrella/blob/develop/packages/wasm-api/zig/build-v0.11.zig)
|
|
342
382
|
|
|
343
383
|
## Naming & structural conventions
|
|
344
384
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/wasm-api",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.1",
|
|
4
4
|
"description": "Generic, modular, extensible API bridge and infrastructure for hybrid JS & WebAssembly projects",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -35,12 +35,12 @@
|
|
|
35
35
|
"test:build-zig": "zig build-lib -O ReleaseSmall -target wasm32-freestanding -dynamic --strip --pkg-begin wasmapi zig/lib.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.7.
|
|
39
|
-
"@thi.ng/arrays": "^2.5.
|
|
38
|
+
"@thi.ng/api": "^8.7.3",
|
|
39
|
+
"@thi.ng/arrays": "^2.5.6",
|
|
40
40
|
"@thi.ng/checks": "^3.3.9",
|
|
41
|
-
"@thi.ng/errors": "^2.2.
|
|
41
|
+
"@thi.ng/errors": "^2.2.12",
|
|
42
42
|
"@thi.ng/hex": "^2.3.6",
|
|
43
|
-
"@thi.ng/idgen": "^2.1.
|
|
43
|
+
"@thi.ng/idgen": "^2.1.28",
|
|
44
44
|
"@thi.ng/logger": "^1.4.9"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
@@ -115,5 +115,5 @@
|
|
|
115
115
|
"status": "alpha",
|
|
116
116
|
"year": 2022
|
|
117
117
|
},
|
|
118
|
-
"gitHead": "
|
|
118
|
+
"gitHead": "a2915dee637c1b8cd2e11a78a23bd035e4f750e7\n"
|
|
119
119
|
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
//! Build helpers for using modules/packages distributed via NPM
|
|
2
|
+
//! Intended for use with https://thi.ng/wasm-api and support packages
|
|
3
|
+
//!
|
|
4
|
+
//! This version of the script is only compatible with:
|
|
5
|
+
//! Zig v0.11.0-dev.1622+fc48467a9 or newer
|
|
6
|
+
//!
|
|
7
|
+
//! Use build.zig (in this same directory) for earlier Zig versions
|
|
8
|
+
|
|
9
|
+
const std = @import("std");
|
|
10
|
+
const Build = std.Build;
|
|
11
|
+
|
|
12
|
+
/// Config options
|
|
13
|
+
pub const WasmLibOpts = struct {
|
|
14
|
+
/// Base path to common node_modules directory under which
|
|
15
|
+
/// all to be imported modules are located
|
|
16
|
+
base: []const u8 = "node_modules",
|
|
17
|
+
/// Relative path to root source file
|
|
18
|
+
root: []const u8 = "zig/main.zig",
|
|
19
|
+
/// Relative path to output directory
|
|
20
|
+
out: []const u8 = "src",
|
|
21
|
+
/// Additional WASM API support modules.
|
|
22
|
+
/// Only need to specify custom/extra modules
|
|
23
|
+
/// `wasm-api` and `wasm-api-bindgen` are auto-added as dependency for all...
|
|
24
|
+
modules: ?[]const ModuleSpec = null,
|
|
25
|
+
/// Build mode override (else allows config via CLI args)
|
|
26
|
+
optimize: ?std.builtin.Mode = null,
|
|
27
|
+
/// Additional WASM target features, e.g. `.simd128` to enable SIMD
|
|
28
|
+
features: ?[]const std.Target.wasm.Feature = null,
|
|
29
|
+
/// Initial memory (in bytes, MUST be multiple of 0x10000)
|
|
30
|
+
initialMemory: ?u64 = null,
|
|
31
|
+
/// Max memory (in bytes, MUST be multiple of 0x10000)
|
|
32
|
+
maxMemory: ?u64 = null,
|
|
33
|
+
/// If true, build will also generate docs
|
|
34
|
+
docs: bool = false,
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/// WASM API sub-module declaration
|
|
38
|
+
pub const ModuleSpec = struct {
|
|
39
|
+
/// module import name/ID
|
|
40
|
+
name: []const u8,
|
|
41
|
+
/// Relative path to configured base path
|
|
42
|
+
path: []const u8,
|
|
43
|
+
/// Module IDs of other modules this module depends on
|
|
44
|
+
/// Only need to specify custom/extra modules
|
|
45
|
+
/// `wasm-api` and `wasm-api-bindgen` are auto-added as dependency for all...
|
|
46
|
+
dependencies: ?[]const []const u8 = null,
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const wasmapi = "wasm-api";
|
|
50
|
+
const wasmbind = "wasm-api-bindgen";
|
|
51
|
+
|
|
52
|
+
/// Creates and returns a build step to build a dynamic WASM library, configured
|
|
53
|
+
/// with given options and package declarations. The given package specs will be inserted
|
|
54
|
+
pub fn wasmLib(b: *Build, opts: WasmLibOpts) *Build.CompileStep {
|
|
55
|
+
const lib = b.addSharedLibrary(.{
|
|
56
|
+
.name = "main",
|
|
57
|
+
.root_source_file = .{ .path = opts.root },
|
|
58
|
+
.target = .{
|
|
59
|
+
.cpu_arch = .wasm32,
|
|
60
|
+
.os_tag = .freestanding,
|
|
61
|
+
.cpu_features_add = if (opts.features) |features| std.Target.wasm.featureSet(features) else std.Target.Cpu.Feature.Set.empty,
|
|
62
|
+
},
|
|
63
|
+
.optimize = if (opts.optimize) |m| m else b.standardOptimizeOption(.{}),
|
|
64
|
+
});
|
|
65
|
+
if (lib.optimize == .ReleaseSmall or lib.optimize == .ReleaseFast) lib.strip = true;
|
|
66
|
+
lib.setOutputDir(opts.out);
|
|
67
|
+
// build flags
|
|
68
|
+
lib.rdynamic = true;
|
|
69
|
+
lib.import_symbols = true;
|
|
70
|
+
lib.initial_memory = opts.initialMemory;
|
|
71
|
+
lib.max_memory = opts.maxMemory;
|
|
72
|
+
if (opts.docs) lib.emit_docs = .emit;
|
|
73
|
+
// add default dependencies
|
|
74
|
+
lib.addModule(
|
|
75
|
+
wasmapi,
|
|
76
|
+
b.createModule(.{
|
|
77
|
+
.source_file = modulePath(b.allocator, opts.base, "@thi.ng/wasm-api/zig/lib.zig"),
|
|
78
|
+
}),
|
|
79
|
+
);
|
|
80
|
+
lib.addModule(
|
|
81
|
+
wasmbind,
|
|
82
|
+
b.createModule(.{
|
|
83
|
+
.source_file = modulePath(b.allocator, opts.base, "@thi.ng/wasm-api-bindgen/zig/lib.zig"),
|
|
84
|
+
}),
|
|
85
|
+
);
|
|
86
|
+
if (opts.modules) |modules| {
|
|
87
|
+
for (modules) |mod| {
|
|
88
|
+
register(lib, mod, opts);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return lib;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/// Registers a single package and its deps (also injects core wasm-api deps)
|
|
95
|
+
pub fn register(step: *Build.CompileStep, mod: ModuleSpec, opts: WasmLibOpts) void {
|
|
96
|
+
const num = if (mod.dependencies) |ids| ids.len else 0;
|
|
97
|
+
var dpkgs = step.builder.allocator.alloc(Build.ModuleDependency, num + 2) catch unreachable;
|
|
98
|
+
dpkgs[0] = if (step.modules.get(wasmapi)) |m| .{ .name = wasmapi, .module = m } else unreachable;
|
|
99
|
+
dpkgs[1] = if (step.modules.get(wasmbind)) |m| .{ .name = wasmbind, .module = m } else unreachable;
|
|
100
|
+
var i: usize = 2;
|
|
101
|
+
if (mod.dependencies) |ids| {
|
|
102
|
+
for (ids) |id| {
|
|
103
|
+
if (step.modules.get(id)) |m| {
|
|
104
|
+
if (isDuplicateId(dpkgs[0..i], id)) {
|
|
105
|
+
std.log.info("ignoring duplicate dependency: {s}", .{id});
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
dpkgs[i] = .{ .name = id, .module = m };
|
|
109
|
+
i += 1;
|
|
110
|
+
} else @panic("unknown dependency");
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
step.addModule(mod.name, step.builder.createModule(.{
|
|
114
|
+
.source_file = modulePath(step.builder.allocator, opts.base, mod.path),
|
|
115
|
+
.dependencies = dpkgs[0..i],
|
|
116
|
+
}));
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
fn isDuplicateId(deps: []Build.ModuleDependency, name: []const u8) bool {
|
|
120
|
+
for (deps) |d| {
|
|
121
|
+
if (std.mem.eql(u8, d.name, name)) return true;
|
|
122
|
+
}
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
fn modulePath(allocator: std.mem.Allocator, base: []const u8, path: []const u8) Build.FileSource {
|
|
127
|
+
return .{ .path = std.fs.path.join(allocator, &.{ base, path }) catch unreachable };
|
|
128
|
+
}
|
package/zig/build.zig
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
//! Build helpers for using modules/packages distributed via NPM
|
|
2
|
+
//! Intended for use with https://thi.ng/wasm-api and support packages
|
|
3
|
+
//!
|
|
4
|
+
//! This version of the script is only compatible with Zig v0.10.1 or older
|
|
5
|
+
//! Use build-v0.11.zig (in this same directory) for newer Zig versions
|
|
6
|
+
|
|
1
7
|
const std = @import("std");
|
|
2
8
|
|
|
3
9
|
/// Definition for a (usually hybrid) Zig package which will be distributed via NPM
|