virtual-machine 0.0.36 → 0.0.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.
@@ -0,0 +1,120 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") {
10
+ for (let key of __getOwnPropNames(from))
11
+ if (!__hasOwnProp.call(to, key) && key !== except)
12
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
13
+ }
14
+ return to;
15
+ };
16
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
17
+ // If the importer is in node compatibility mode or this is not an ESM
18
+ // file that has been converted to a CommonJS file using a Babel-
19
+ // compatible transform (i.e. "__esModule" has not been set), then set
20
+ // "default" to the CommonJS "module.exports" for node compatibility.
21
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
+ mod
23
+ ));
24
+
25
+ // node-worker.ts
26
+ var import_node_worker_threads = require("worker_threads");
27
+ var import_node_fs = __toESM(require("fs"));
28
+ var WorkerStepResult = {
29
+ Continue: 0,
30
+ Halted: 1,
31
+ Shutdown: 2,
32
+ Error: 3
33
+ };
34
+ var CTRL_HALT_REQUESTED = 0;
35
+ var controlView = null;
36
+ var workerState = null;
37
+ var BATCH_SIZE = 1e5;
38
+ var BATCHES_PER_YIELD = 10;
39
+ function postMessage(msg) {
40
+ import_node_worker_threads.parentPort?.postMessage(msg);
41
+ }
42
+ function runLoop(hartId) {
43
+ if (!workerState || !controlView) {
44
+ console.error("[Worker] runLoop called without workerState or controlView");
45
+ return;
46
+ }
47
+ let shouldContinue = true;
48
+ let batchCount = 0;
49
+ while (shouldContinue) {
50
+ const result = workerState.step_batch(BATCH_SIZE);
51
+ switch (result) {
52
+ case WorkerStepResult.Continue:
53
+ batchCount++;
54
+ if (batchCount >= BATCHES_PER_YIELD) {
55
+ batchCount = 0;
56
+ try {
57
+ Atomics.wait(controlView, CTRL_HALT_REQUESTED, 0, 0);
58
+ } catch {
59
+ }
60
+ }
61
+ break;
62
+ case WorkerStepResult.Halted:
63
+ console.log(`[Worker ${hartId}] Halted after ${workerState.step_count()} steps`);
64
+ postMessage({ type: "halted", hartId, stepCount: Number(workerState.step_count()) });
65
+ cleanup();
66
+ shouldContinue = false;
67
+ break;
68
+ case WorkerStepResult.Shutdown:
69
+ console.log(`[Worker ${hartId}] Shutdown after ${workerState.step_count()} steps`);
70
+ postMessage({ type: "halted", hartId, stepCount: Number(workerState.step_count()) });
71
+ cleanup();
72
+ shouldContinue = false;
73
+ break;
74
+ case WorkerStepResult.Error:
75
+ console.error(`[Worker ${hartId}] Error after ${workerState.step_count()} steps`);
76
+ postMessage({ type: "error", hartId, error: "Execution error" });
77
+ cleanup();
78
+ shouldContinue = false;
79
+ break;
80
+ }
81
+ }
82
+ }
83
+ function cleanup() {
84
+ workerState = null;
85
+ controlView = null;
86
+ }
87
+ async function main() {
88
+ const { hartId, sharedMem, entryPc, wasmPath } = import_node_worker_threads.workerData;
89
+ console.log(`[Worker ${hartId}] Starting with WASM from ${wasmPath}`);
90
+ try {
91
+ const wasmBytes = import_node_fs.default.readFileSync(wasmPath);
92
+ const jsPath = wasmPath.replace("_bg.wasm", ".js");
93
+ const wasmModule = await import(jsPath);
94
+ wasmModule.initSync(wasmBytes);
95
+ console.log(`[Worker ${hartId}] WASM initialized`);
96
+ if (!(sharedMem instanceof SharedArrayBuffer)) {
97
+ throw new Error("sharedMem must be a SharedArrayBuffer");
98
+ }
99
+ postMessage({ type: "ready", hartId });
100
+ const pc = BigInt(Math.floor(entryPc));
101
+ console.log(`[Worker ${hartId}] Starting execution at PC=0x${pc.toString(16)}`);
102
+ controlView = new Int32Array(sharedMem);
103
+ workerState = new wasmModule.WorkerState(hartId, sharedMem, pc);
104
+ runLoop(hartId);
105
+ } catch (e) {
106
+ console.error(`[Worker ${hartId}] Error:`, e);
107
+ postMessage({
108
+ type: "error",
109
+ hartId,
110
+ error: String(e)
111
+ });
112
+ }
113
+ }
114
+ main().catch((e) => {
115
+ console.error("[Worker] Fatal error:", e);
116
+ postMessage({
117
+ type: "error",
118
+ error: String(e)
119
+ });
120
+ });
@@ -7,7 +7,7 @@ import {
7
7
  riscv_vm_default,
8
8
  worker_check_interrupts,
9
9
  worker_entry
10
- } from "./chunk-YJ5P3VGQ.mjs";
10
+ } from "./chunk-ASAZFOYQ.mjs";
11
11
  export {
12
12
  NetworkStatus,
13
13
  WasmVm,