@thi.ng/wasm-api 0.7.0 → 0.10.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.
@@ -3,28 +3,26 @@
3
3
  const std = @import("std");
4
4
  const root = @import("root");
5
5
 
6
- /// Initialize the allocator to be exposed to the WASM host env
6
+ /// Obtains the allocator to be exposed to the WASM host env
7
7
  /// (via `_wasm_allocate()` and `_wasm_free()`).
8
8
  /// If the user defines a public `WASM_ALLOCATOR` in their root file
9
- /// then this allocator will be used, otherwise the implementation
10
- /// falls back to using GPA.
9
+ /// then this allocator will be used, otherwise the implementations
10
+ /// of the two mentioned functions are no-ops.
11
+ /// The `WASM_ALLOCATOR` can be changed and/or enabled/disabled dynamically
12
+ /// This helper function here is used to always lookup the current value/impl.
13
+ ///
11
14
  /// Note: The type for this var is purposefully chosen as an optional,
12
- /// effectively disabling allocations from the WASM host side if
13
- /// `WASM_ALLOCATOR` is set to null.
14
- pub const allocator: ?std.mem.Allocator = alloc: {
15
- if (@hasDecl(root, "WASM_ALLOCATOR")) {
16
- break :alloc root.WASM_ALLOCATOR;
17
- } else {
18
- var gpa = std.heap.GeneralPurposeAllocator(.{}){};
19
- break :alloc gpa.allocator();
20
- }
21
- };
15
+ /// effectively disabling allocations from the WASM host side if no
16
+ /// `WASM_ALLOCATOR` is set (or set to null).
17
+ pub fn allocator() ?std.mem.Allocator {
18
+ return if (@hasDecl(root, "WASM_ALLOCATOR")) root.WASM_ALLOCATOR else null;
19
+ }
22
20
 
23
- /// Attempts to allocate memory using configured `allocator` and if
21
+ /// Attempts to allocate memory using configured `allocator()` and if
24
22
  /// successful returns address of new chunk or zero if failed
25
23
  /// Note: For SIMD compatibility all allocations are aligned to 16 bytes
26
24
  pub export fn _wasm_allocate(numBytes: usize) usize {
27
- if (allocator) |alloc| {
25
+ if (allocator()) |alloc| {
28
26
  var mem = alloc.alignedAlloc(u8, 16, numBytes) catch return 0;
29
27
  return @ptrToInt(mem.ptr);
30
28
  }
@@ -33,11 +31,10 @@ pub export fn _wasm_allocate(numBytes: usize) usize {
33
31
 
34
32
  /// Frees chunk of heap memory (previously allocated using `_wasm_allocate()`)
35
33
  /// starting at given address and of given byte length.
36
- /// Note: This is a no-op if the allocator is explicitly disabled (see `setAllocator()`),
34
+ /// Note: This is a no-op if no allocator is configured (see `allocator()`)
37
35
  pub export fn _wasm_free(addr: usize, numBytes: usize) void {
38
- if (allocator) |alloc| {
36
+ if (allocator()) |alloc| {
39
37
  var mem = [2]usize{ addr, numBytes };
40
- printFmt("{d}", .{@ptrCast(*[]u8, &mem).*});
41
38
  alloc.free(@ptrCast(*[]u8, &mem).*);
42
39
  }
43
40
  }
@@ -165,10 +162,16 @@ pub fn printStr(msg: []const u8) void {
165
162
  _printStr(@ptrToInt(msg.ptr), msg.len);
166
163
  }
167
164
 
165
+ /// Calls std.fmt.allocPrint to format given string, then calls `printStr()`
166
+ /// to output it via JS, then frees string's memory again
167
+ /// (Only available if the allocator used by `_wasm_allocate()` hasn't been disabled.)
168
168
  pub fn printFmt(comptime fmt: []const u8, args: anytype) void {
169
- if (allocator) |alloc| {
169
+ if (allocator()) |alloc| {
170
170
  const res = std.fmt.allocPrint(alloc, fmt, args) catch return;
171
171
  defer alloc.free(res);
172
172
  printStr(res);
173
173
  }
174
174
  }
175
+
176
+ /// Triggers the JS/browser debugger
177
+ pub extern "wasmapi" fn debug() void;
package/index.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  export * from "./api.js";
2
2
  export * from "./bridge.js";
3
3
  export * from "./codegen.js";
4
- export * from "./object-index.js";
5
4
  export * from "./codegen/typescript.js";
6
- export * from "./codegen/zig.js";
7
5
  export * from "./codegen/utils.js";
6
+ export * from "./codegen/zig.js";
7
+ export * from "./object-index.js";
8
8
  //# sourceMappingURL=index.d.ts.map
package/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  export * from "./api.js";
2
2
  export * from "./bridge.js";
3
3
  export * from "./codegen.js";
4
- export * from "./object-index.js";
5
4
  export * from "./codegen/typescript.js";
6
- export * from "./codegen/zig.js";
7
5
  export * from "./codegen/utils.js";
6
+ export * from "./codegen/zig.js";
7
+ export * from "./object-index.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/wasm-api",
3
- "version": "0.7.0",
3
+ "version": "0.10.0",
4
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",
@@ -36,35 +36,41 @@
36
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"
37
37
  },
38
38
  "dependencies": {
39
- "@thi.ng/api": "^8.4.0",
40
- "@thi.ng/args": "^2.2.0",
41
- "@thi.ng/binary": "^3.3.3",
39
+ "@thi.ng/api": "^8.4.1",
40
+ "@thi.ng/args": "^2.2.1",
41
+ "@thi.ng/binary": "^3.3.4",
42
42
  "@thi.ng/checks": "^3.2.4",
43
- "@thi.ng/compare": "^2.1.10",
44
- "@thi.ng/defmulti": "^2.1.12",
43
+ "@thi.ng/compare": "^2.1.11",
44
+ "@thi.ng/defmulti": "^2.1.13",
45
45
  "@thi.ng/errors": "^2.1.10",
46
- "@thi.ng/file-io": "^0.3.9",
46
+ "@thi.ng/file-io": "^0.3.10",
47
47
  "@thi.ng/hex": "^2.1.9",
48
- "@thi.ng/idgen": "^2.1.11",
49
- "@thi.ng/logger": "^1.2.0"
48
+ "@thi.ng/idgen": "^2.1.12",
49
+ "@thi.ng/logger": "^1.2.1"
50
50
  },
51
51
  "devDependencies": {
52
52
  "@microsoft/api-extractor": "^7.25.0",
53
- "@thi.ng/testament": "^0.2.11",
53
+ "@thi.ng/testament": "^0.2.12",
54
54
  "rimraf": "^3.0.2",
55
55
  "tools": "^0.0.1",
56
56
  "typedoc": "^0.22.17",
57
57
  "typescript": "^4.7.4"
58
58
  },
59
59
  "keywords": [
60
+ "allocator",
60
61
  "api",
61
62
  "bindings",
62
63
  "c",
63
64
  "codegen",
65
+ "event",
64
66
  "id",
65
67
  "logger",
66
68
  "memory",
69
+ "string",
70
+ "struct",
71
+ "typedarray",
67
72
  "typescript",
73
+ "utf8",
68
74
  "wasm",
69
75
  "webassembly",
70
76
  "wrapper",
@@ -117,5 +123,5 @@
117
123
  "status": "alpha",
118
124
  "year": 2022
119
125
  },
120
- "gitHead": "78193cdd838d9d09114352b117b6c5e92631e3cd\n"
126
+ "gitHead": "4264b91052b8c1f74db9fd51dc48c830dc0d5829\n"
121
127
  }