@thi.ng/wasm-api 1.4.36 → 1.4.38

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/object-index.js CHANGED
@@ -1,79 +1,81 @@
1
1
  import { assert } from "@thi.ng/errors/assert";
2
2
  import { IDGen } from "@thi.ng/idgen";
3
- export class ObjectIndex {
4
- name;
5
- logger;
6
- idgen;
7
- items = [];
8
- constructor(opts) {
9
- this.name = opts.name;
10
- this.logger = opts.logger;
11
- this.idgen = new IDGen(opts.bits || 32, 0);
3
+ class ObjectIndex {
4
+ name;
5
+ logger;
6
+ idgen;
7
+ items = [];
8
+ constructor(opts) {
9
+ this.name = opts.name;
10
+ this.logger = opts.logger;
11
+ this.idgen = new IDGen(opts.bits || 32, 0);
12
+ }
13
+ keys() {
14
+ return this.idgen[Symbol.iterator]();
15
+ }
16
+ *values() {
17
+ for (let id of this.idgen) {
18
+ yield this.items[id];
12
19
  }
13
- keys() {
14
- return this.idgen[Symbol.iterator]();
20
+ }
21
+ /**
22
+ * Indexes given `item` and assigns it to the next available ID (which might
23
+ * be a previously freed ID) and returns it.
24
+ *
25
+ * @param item
26
+ */
27
+ add(item) {
28
+ const id = this.idgen.next();
29
+ this.logger && this.logger.debug(`adding ${this.name} ID: ${id}`);
30
+ this.items[id] = item;
31
+ return id;
32
+ }
33
+ /**
34
+ * Returns true if the given `id` is valid/active.
35
+ *
36
+ * @param id
37
+ */
38
+ has(id) {
39
+ return this.idgen.has(id);
40
+ }
41
+ /**
42
+ * First checks if given `id` is valid and if so frees it (for recycling)
43
+ * and deletes its corresponding item. If `ensure` is true (default), throws
44
+ * an error if the ID is invalid (otherwise returns false for invalid IDs).
45
+ *
46
+ * @param id
47
+ * @param ensure
48
+ */
49
+ delete(id, ensure = true) {
50
+ if (this.idgen.has(id)) {
51
+ this.logger && this.logger.debug(`deleting ${this.name} ID: ${id}`);
52
+ this.idgen.free(id);
53
+ delete this.items[id];
54
+ return true;
15
55
  }
16
- *values() {
17
- for (let id of this.idgen) {
18
- yield this.items[id];
19
- }
20
- }
21
- /**
22
- * Indexes given `item` and assigns it to the next available ID (which might
23
- * be a previously freed ID) and returns it.
24
- *
25
- * @param item
26
- */
27
- add(item) {
28
- const id = this.idgen.next();
29
- this.logger && this.logger.debug(`adding ${this.name} ID: ${id}`);
30
- this.items[id] = item;
56
+ assert(!ensure, `can't delete missing ${this.name} ID: ${id}`);
57
+ return false;
58
+ }
59
+ get(id, ensure = true) {
60
+ ensure && assert(this.idgen.has(id), `missing ${this.name} for ID: ${id}`);
61
+ return this.items[id];
62
+ }
63
+ /**
64
+ * Applies given predicate to all active items and returns ID of first
65
+ * matching. If `ensure` is true (default), throws an error if the `pred`
66
+ * didn't match anything (otherwise returns undefined).
67
+ *
68
+ * @param pred
69
+ * @param ensure
70
+ */
71
+ find(pred, ensure = true) {
72
+ for (let id of this.idgen) {
73
+ if (pred(this.items[id]))
31
74
  return id;
32
75
  }
33
- /**
34
- * Returns true if the given `id` is valid/active.
35
- *
36
- * @param id
37
- */
38
- has(id) {
39
- return this.idgen.has(id);
40
- }
41
- /**
42
- * First checks if given `id` is valid and if so frees it (for recycling)
43
- * and deletes its corresponding item. If `ensure` is true (default), throws
44
- * an error if the ID is invalid (otherwise returns false for invalid IDs).
45
- *
46
- * @param id
47
- * @param ensure
48
- */
49
- delete(id, ensure = true) {
50
- if (this.idgen.has(id)) {
51
- this.logger && this.logger.debug(`deleting ${this.name} ID: ${id}`);
52
- this.idgen.free(id);
53
- delete this.items[id];
54
- return true;
55
- }
56
- assert(!ensure, `can't delete missing ${this.name} ID: ${id}`);
57
- return false;
58
- }
59
- get(id, ensure = true) {
60
- ensure &&
61
- assert(this.idgen.has(id), `missing ${this.name} for ID: ${id}`);
62
- return this.items[id];
63
- }
64
- /**
65
- * Applies given predicate to all active items and returns ID of first
66
- * matching. If `ensure` is true (default), throws an error if the `pred`
67
- * didn't match anything (otherwise returns undefined).
68
- *
69
- * @param pred
70
- * @param ensure
71
- */
72
- find(pred, ensure = true) {
73
- for (let id of this.idgen) {
74
- if (pred(this.items[id]))
75
- return id;
76
- }
77
- assert(!ensure, `given predicate matched no ${this.name}`);
78
- }
76
+ assert(!ensure, `given predicate matched no ${this.name}`);
77
+ }
79
78
  }
79
+ export {
80
+ ObjectIndex
81
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/wasm-api",
3
- "version": "1.4.36",
3
+ "version": "1.4.38",
4
4
  "description": "Generic, modular, extensible API bridge and infrastructure for hybrid JS & WebAssembly projects",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -24,7 +24,9 @@
24
24
  "author": "Karsten Schmidt (https://thi.ng)",
25
25
  "license": "Apache-2.0",
26
26
  "scripts": {
27
- "build": "yarn clean && tsc --declaration",
27
+ "build": "yarn build:esbuild && yarn build:decl",
28
+ "build:decl": "tsc --declaration --emitDeclarationOnly",
29
+ "build:esbuild": "esbuild --format=esm --platform=neutral --target=es2022 --tsconfig=tsconfig.json --outdir=. src/**/*.ts",
28
30
  "clean": "rimraf --glob '*.js' '*.d.ts' '*.map' doc",
29
31
  "doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts",
30
32
  "doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
@@ -34,16 +36,17 @@
34
36
  "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"
35
37
  },
36
38
  "dependencies": {
37
- "@thi.ng/api": "^8.9.11",
38
- "@thi.ng/arrays": "^2.7.7",
39
- "@thi.ng/checks": "^3.4.11",
40
- "@thi.ng/errors": "^2.4.5",
41
- "@thi.ng/hex": "^2.3.23",
42
- "@thi.ng/idgen": "^2.2.14",
43
- "@thi.ng/logger": "^2.0.1"
39
+ "@thi.ng/api": "^8.9.13",
40
+ "@thi.ng/arrays": "^2.7.9",
41
+ "@thi.ng/checks": "^3.4.13",
42
+ "@thi.ng/errors": "^2.4.7",
43
+ "@thi.ng/hex": "^2.3.25",
44
+ "@thi.ng/idgen": "^2.2.16",
45
+ "@thi.ng/logger": "^2.1.0"
44
46
  },
45
47
  "devDependencies": {
46
48
  "@microsoft/api-extractor": "^7.38.3",
49
+ "esbuild": "^0.19.8",
47
50
  "rimraf": "^5.0.5",
48
51
  "tools": "^0.0.1",
49
52
  "typedoc": "^0.25.4",
@@ -81,7 +84,7 @@
81
84
  "setTimeout": false
82
85
  },
83
86
  "engines": {
84
- "node": ">=14"
87
+ "node": ">=18"
85
88
  },
86
89
  "files": [
87
90
  "./*.js",
@@ -113,5 +116,5 @@
113
116
  "status": "alpha",
114
117
  "year": 2022
115
118
  },
116
- "gitHead": "25f2ac8ff795a432a930119661b364d4d93b59a0\n"
119
+ "gitHead": "25a42a81fac8603a1e440a7aa8bc343276211ff4\n"
117
120
  }
package/pointer.js CHANGED
@@ -1,60 +1,36 @@
1
- /**
2
- * Generic pointer facility which on {@link Pointer.deref()} calls wrapper
3
- * function (provided as ctor arg) to realise the pointer's target value. The
4
- * pointer's target address can be accessed via {@link Pointer.addr}
5
- * (read/write).
6
- *
7
- * @remarks
8
- * The pointer always behaves like `volatile`, i.e. memoization of target values
9
- * is purposfully avoided and the wrapper function is executed anew _each_ time
10
- * the pointer is deref'd.
11
- */
12
- export class Pointer {
13
- mem;
14
- base;
15
- fn;
16
- constructor(mem, base, fn) {
17
- this.mem = mem;
18
- this.base = base;
19
- this.fn = fn;
20
- }
21
- get addr() {
22
- return this.mem.u32[this.base >>> 2];
23
- }
24
- set addr(addr) {
25
- this.mem.u32[this.base >>> 2] = addr;
26
- }
27
- deref() {
28
- return this.fn(this.addr);
29
- }
1
+ class Pointer {
2
+ constructor(mem, base, fn) {
3
+ this.mem = mem;
4
+ this.base = base;
5
+ this.fn = fn;
6
+ }
7
+ get addr() {
8
+ return this.mem.u32[this.base >>> 2];
9
+ }
10
+ set addr(addr) {
11
+ this.mem.u32[this.base >>> 2] = addr;
12
+ }
13
+ deref() {
14
+ return this.fn(this.addr);
15
+ }
30
16
  }
31
- /**
32
- * Generic pointer facility for {@link WASM64} target, which on
33
- * {@link Pointer64.deref()} calls wrapper function (provided as ctor arg) to
34
- * realise the pointer's target value. The pointer's target address can be
35
- * accessed via {@link Pointer.addr} (read/write).
36
- *
37
- * @remarks
38
- * The pointer always behaves like `volatile`, i.e. memoization of target values
39
- * is purposfully avoided and the wrapper function is executed anew _each_ time
40
- * the pointer is deref'd.
41
- */
42
- export class Pointer64 {
43
- mem;
44
- base;
45
- fn;
46
- constructor(mem, base, fn) {
47
- this.mem = mem;
48
- this.base = base;
49
- this.fn = fn;
50
- }
51
- get addr() {
52
- return this.mem.u64[Number(this.base >> BigInt(3))];
53
- }
54
- set addr(addr) {
55
- this.mem.u64[Number(this.base >> BigInt(3))] = addr;
56
- }
57
- deref() {
58
- return this.fn(this.addr);
59
- }
17
+ class Pointer64 {
18
+ constructor(mem, base, fn) {
19
+ this.mem = mem;
20
+ this.base = base;
21
+ this.fn = fn;
22
+ }
23
+ get addr() {
24
+ return this.mem.u64[Number(this.base >> BigInt(3))];
25
+ }
26
+ set addr(addr) {
27
+ this.mem.u64[Number(this.base >> BigInt(3))] = addr;
28
+ }
29
+ deref() {
30
+ return this.fn(this.addr);
31
+ }
60
32
  }
33
+ export {
34
+ Pointer,
35
+ Pointer64
36
+ };