@thi.ng/wasm-api 1.4.36 → 1.4.37
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 +1 -1
- package/README.md +1 -1
- package/api.js +6 -2
- package/bridge.js +436 -409
- package/object-index.js +74 -72
- package/package.json +13 -10
- package/pointer.js +34 -58
- package/string.js +201 -218
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
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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
|
-
|
|
14
|
-
|
|
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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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
|
-
|
|
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.
|
|
3
|
+
"version": "1.4.37",
|
|
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
|
|
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.
|
|
38
|
-
"@thi.ng/arrays": "^2.7.
|
|
39
|
-
"@thi.ng/checks": "^3.4.
|
|
40
|
-
"@thi.ng/errors": "^2.4.
|
|
41
|
-
"@thi.ng/hex": "^2.3.
|
|
42
|
-
"@thi.ng/idgen": "^2.2.
|
|
43
|
-
"@thi.ng/logger": "^2.0.
|
|
39
|
+
"@thi.ng/api": "^8.9.12",
|
|
40
|
+
"@thi.ng/arrays": "^2.7.8",
|
|
41
|
+
"@thi.ng/checks": "^3.4.12",
|
|
42
|
+
"@thi.ng/errors": "^2.4.6",
|
|
43
|
+
"@thi.ng/hex": "^2.3.24",
|
|
44
|
+
"@thi.ng/idgen": "^2.2.15",
|
|
45
|
+
"@thi.ng/logger": "^2.0.2"
|
|
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",
|
|
@@ -113,5 +116,5 @@
|
|
|
113
116
|
"status": "alpha",
|
|
114
117
|
"year": 2022
|
|
115
118
|
},
|
|
116
|
-
"gitHead": "
|
|
119
|
+
"gitHead": "5e7bafedfc3d53bc131469a28de31dd8e5b4a3ff\n"
|
|
117
120
|
}
|
package/pointer.js
CHANGED
|
@@ -1,60 +1,36 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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
|
+
};
|