@vune-ui/execution 0.1.20

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.
@@ -0,0 +1,108 @@
1
+ let nodeParentPort = null;
2
+ try {
3
+ ({ parentPort: nodeParentPort } = await import("node:worker_threads"));
4
+ } catch {}
5
+
6
+ const post = message => nodeParentPort ? nodeParentPort.postMessage(message) : globalThis.postMessage(message);
7
+ const listen = handler => {
8
+ if (nodeParentPort) nodeParentPort.on("message", handler);
9
+ else globalThis.onmessage = event => handler(event.data);
10
+ };
11
+
12
+ const CONTROL_REQUEST_SEQUENCE = 0;
13
+ const CONTROL_DONE_SEQUENCE = 1;
14
+ const CONTROL_STATUS = 2;
15
+ const CONTROL_STOP = 3;
16
+ const CONTROL_RANGE_COUNT = 4;
17
+ const CONTROL_ERROR_CODE = 5;
18
+ const CONTROL_COMPUTE_MICROS = 6;
19
+ const CONTROL_WASM_CALLS = 7;
20
+
21
+ async function readWasm(url) {
22
+ if (url.protocol === "file:" && typeof process !== "undefined" && process.versions?.node) {
23
+ const { readFile } = await import("node:fs/promises");
24
+ return readFile(url);
25
+ }
26
+ const response = await fetch(url);
27
+ if (!response.ok) throw new Error(`Failed to load resident WASM: ${response.status}`);
28
+ return response.arrayBuffer();
29
+ }
30
+
31
+ function now() {
32
+ return globalThis.performance?.now() ?? Date.now();
33
+ }
34
+
35
+ let initialized = false;
36
+
37
+ listen(async message => {
38
+ if (initialized || message?.type !== "init") return;
39
+ initialized = true;
40
+ try {
41
+ if (!(message.memory instanceof WebAssembly.Memory) || !(message.memory.buffer instanceof SharedArrayBuffer)) {
42
+ throw new TypeError("shared-memory-unavailable");
43
+ }
44
+ const url = new URL(`./resident-kernel-shared-${message.variant}.wasm`, import.meta.url);
45
+ const result = await WebAssembly.instantiate(await readWasm(url), { env: { memory: message.memory } });
46
+ const exports = (result.instance ?? result).exports;
47
+ if ((exports.resident_simd_enabled() === 1) !== (message.variant === "simd")) {
48
+ throw new TypeError("resident Worker variant mismatch");
49
+ }
50
+ let directExecute = null;
51
+ if (message.variant === "simd"
52
+ && message.directSimdModuleBytes instanceof Uint8Array
53
+ && message.directSimdEntrypoint === "resident_execute_direct_simd") {
54
+ try {
55
+ const directResult = await WebAssembly.instantiate(message.directSimdModuleBytes, { env: { memory: message.memory } });
56
+ const directExports = (directResult.instance ?? directResult).exports;
57
+ if (typeof directExports[message.directSimdEntrypoint] === "function") {
58
+ directExecute = directExports[message.directSimdEntrypoint];
59
+ }
60
+ } catch {}
61
+ }
62
+ const control = new Int32Array(message.memory.buffer, message.controlPointer, 16);
63
+ let lastSequence = Atomics.load(control, CONTROL_REQUEST_SEQUENCE);
64
+ post({ type: "ready", variant: message.variant });
65
+ while (Atomics.load(control, CONTROL_STOP) === 0) {
66
+ let sequence = Atomics.load(control, CONTROL_REQUEST_SEQUENCE);
67
+ if (sequence === lastSequence) {
68
+ Atomics.wait(control, CONTROL_REQUEST_SEQUENCE, lastSequence);
69
+ sequence = Atomics.load(control, CONTROL_REQUEST_SEQUENCE);
70
+ if (Atomics.load(control, CONTROL_STOP) !== 0) break;
71
+ if (sequence === lastSequence) continue;
72
+ }
73
+ const started = now();
74
+ const rangeCount = Atomics.load(control, CONTROL_RANGE_COUNT);
75
+ const resultCode = directExecute
76
+ ? directExecute(
77
+ message.columnPointersPointer,
78
+ message.rangesPointer,
79
+ rangeCount,
80
+ message.capturesPointer,
81
+ )
82
+ : exports.resident_execute(
83
+ message.programPointer,
84
+ message.programWords,
85
+ message.columnPointersPointer,
86
+ message.columnCount,
87
+ message.rangesPointer,
88
+ rangeCount,
89
+ message.capturesPointer,
90
+ message.captureCount,
91
+ );
92
+ const computeMicros = Math.max(0, Math.round((now() - started) * 1000));
93
+ Atomics.store(control, CONTROL_ERROR_CODE, resultCode);
94
+ Atomics.store(control, CONTROL_COMPUTE_MICROS, computeMicros);
95
+ Atomics.add(control, CONTROL_WASM_CALLS, 1);
96
+ Atomics.store(control, CONTROL_STATUS, resultCode === 0 ? 2 : -1);
97
+ lastSequence = sequence;
98
+ Atomics.store(control, CONTROL_DONE_SEQUENCE, sequence);
99
+ Atomics.notify(control, CONTROL_DONE_SEQUENCE);
100
+ post(resultCode === 0
101
+ ? { type: "done", sequence }
102
+ : { type: "error", sequence, message: `shared resident WASM execution failed (${resultCode})` });
103
+ }
104
+ post({ type: "stopped" });
105
+ } catch (error) {
106
+ post({ type: "error", message: error?.message ?? String(error) });
107
+ }
108
+ });
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@vune-ui/execution",
3
+ "version": "0.1.20",
4
+ "description": "Shared resident-buffer, execution telemetry, and frame-budget substrate for Vune runtimes.",
5
+ "type": "module",
6
+ "files": [
7
+ "dist"
8
+ ],
9
+ "main": "./dist/index.js",
10
+ "types": "./dist/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "types": "./dist/index.d.ts",
14
+ "import": "./dist/index.js"
15
+ },
16
+ "./resident-wasm": {
17
+ "types": "./dist/resident-wasm.d.ts",
18
+ "import": "./dist/resident-wasm.js"
19
+ },
20
+ "./resident-worker": {
21
+ "types": "./dist/resident-worker.d.ts",
22
+ "import": "./dist/resident-worker.js"
23
+ }
24
+ },
25
+ "sideEffects": false,
26
+ "publishConfig": {
27
+ "access": "public"
28
+ },
29
+ "scripts": {
30
+ "build": "node scripts/build.mjs",
31
+ "build:wasm": "bash wasm/build.sh",
32
+ "check": "tsc -p tsconfig.json --noEmit",
33
+ "watch": "tsc -w -p tsconfig.json --preserveWatchOutput"
34
+ }
35
+ }