@mica-js/simulator 0.2.0 → 0.3.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.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sandbox.js","names":["deviceGuestScript"],"sources":["../src/host/sandbox-host.ts","../src/sandbox.ts"],"sourcesContent":["import { createRpc } from '@mica-js/core/rpc';\nimport { loggerService } from '@mica-js/core/logging';\nimport { toDisposable } from '@mica-js/core/lifecycle';\nimport type { CanvasBindingTarget } from '@mica-js/core/canvas';\nimport type { SandboxWorker } from '@mica-js/sandbox';\nimport { deviceSimulationHostFileSystemService, deviceSimulationHostGuestService } from './rpc';\nimport type { DeviceSimulationHost, DeviceSimTickReport, DeviceTopic } from './types';\n\n/**\n * Read-only byte access the device host needs for schematics.\n *\n * Structurally equal to RFC 0019's `HostFs`, so one object can serve `WidgetHost` and the device\n * host without a second adapter.\n */\nexport interface DeviceSimulationHostFileSystem {\n read(uri: string): Promise<Uint8Array>;\n}\n\n/**\n * The running sandbox the host adapter drives: the full public surface of `SandboxWorker`, kept\n * structural so tests can inject an in-process RPC peer.\n */\nexport interface SandboxDeviceSimulationHostOptions {\n /**\n * The running sandbox to drive. The caller creates it — for example with the Vite-generated\n * `DeviceSimulationHostGuestSandbox` — and this factory never constructs one.\n */\n readonly sandbox: SandboxWorker;\n /** Host capability: source of schematic bytes; the guest never reads the host file system. */\n readonly fs: DeviceSimulationHostFileSystem;\n /** Extra ports transferred after the RPC port in the guest's first startup message. */\n readonly ports?: readonly MessagePort[];\n}\n\n/**\n * Builds a `DeviceSimulationHost` that runs the unified-IR circuit inside a `@mica-js/sandbox` guest.\n *\n * The caller creates the sandbox and passes it in with the other initialization options, so the\n * same host adapter works with the Vite-generated guest class and with in-process RPC peers. The\n * sandbox receives only a `MessagePort`; every call below travels over the typed `guest` RPC service\n * and schematic bytes arrive through the typed `host-capabilities` service. `Circuit` stays entirely\n * inside the guest.\n */\nexport async function createDeviceSimulationHostFromSandbox({\n sandbox,\n fs,\n ports = [],\n}: SandboxDeviceSimulationHostOptions): Promise<DeviceSimulationHost> {\n let channel: MessageChannel | undefined;\n let rpc: ReturnType<typeof createRpc> | undefined;\n let logRegistration: { dispose(): void } | undefined;\n try {\n await sandbox.ready;\n channel = new MessageChannel();\n const endpoint = createRpc(channel.port1);\n rpc = endpoint;\n logRegistration = endpoint.register(loggerService, {\n write(record) {\n const method = console[record.level] ?? console.log;\n method.call(console, `[${record.scope}]`, record.message, ...record.data);\n },\n });\n sandbox.postMessage(null, [channel.port2, ...ports]);\n } catch (error) {\n logRegistration?.dispose();\n rpc?.dispose();\n channel?.port1.close();\n channel?.port2.close();\n await sandbox.terminate('device simulation host failed to start').catch(() => undefined);\n throw error;\n }\n\n const endpoint = rpc;\n if (endpoint === undefined) {\n throw new Error('Device simulation RPC endpoint was not created.');\n }\n endpoint.register(\n deviceSimulationHostFileSystemService,\n {\n read: (uri: string) => fs.read(uri),\n },\n {\n transfer: (member, value) =>\n member === 'read' && value instanceof Uint8Array ? [value.buffer] : [],\n },\n );\n const guest = endpoint.getProxy(deviceSimulationHostGuestService);\n let disposed = false;\n\n return {\n loadSchematic: (uri: string | undefined) => guest.loadSchematic(uri),\n reset: () => guest.reset(),\n tick: (dt: number): Promise<DeviceSimTickReport> => guest.tick(dt),\n deliver: (target: CanvasBindingTarget, payload: unknown, simTime?: number) =>\n guest.deliver(target, payload, simTime),\n onDidPublish: (listener) => toDisposable(guest.onDidPublish(listener)),\n getTopics: (): Promise<readonly DeviceTopic[]> => guest.getTopics(),\n getSimTime: () => guest.getSimTime(),\n getSchematic: () => guest.getSchematic(),\n async dispose(): Promise<void> {\n if (disposed) return;\n disposed = true;\n try {\n await guest.dispose();\n } finally {\n logRegistration?.dispose();\n endpoint.dispose();\n await sandbox.terminate('disposed');\n }\n },\n };\n}\n","/**\n * Browser entry of `@mica-js/simulator`.\n *\n * The sandbox adapter pulls in `@mica-js/sandbox` and embeds the packaged guest script, so it stays\n * out of the main entry: DOM-free consumers import `@mica-js/simulator`, browser assembly imports\n * `@mica-js/simulator/sandbox`.\n */\n// The ambient module is declared in `virtual-runtime.d.ts`.\n// eslint-disable-next-line @typescript-eslint/triple-slash-reference\n/// <reference path=\"./virtual-runtime.d.ts\" />\nimport { SandboxWorker } from '@mica-js/sandbox';\nimport type { SandboxWorkerOptions } from '@mica-js/sandbox';\nimport deviceGuestScript from './guest/device-simulation-host.ts?sandbox-worker-code';\nimport { createDeviceSimulationHostFromSandbox } from './host/sandbox-host';\n\n/**\n * The default device simulation guest script, bundled and minified into this module while the\n * package is built.\n *\n * It is a classic script: every dependency is inlined, so the sandbox runs it without a module\n * loader. It resolves no devices — a package that owns devices passes its own guest script to the\n * sandbox instead.\n */\nexport const deviceSimulationHostGuestScript: string = deviceGuestScript;\n\n/**\n * Creates the default device simulation guest worker.\n *\n * Pass the worker to `createDeviceSimulationHostFromSandbox` — or let\n * `@mica-js/canvas-editor`'s `SandboxHostProvider` start it, which is what it does by default.\n */\nexport function createDeviceSimulationHostGuestSandbox(\n options: SandboxWorkerOptions = {},\n): SandboxWorker {\n return new SandboxWorker(deviceSimulationHostGuestScript, { name: 'device', ...options });\n}\n\n/**\n * The packaged guest as a worker class, kept for callers that construct it with `new`.\n *\n * @deprecated Prefer {@link createDeviceSimulationHostGuestSandbox}.\n */\nexport const DeviceSimulationHostGuestSandbox: new (\n options?: SandboxWorkerOptions,\n) => SandboxWorker = class GeneratedDeviceSimulationHostSandbox extends SandboxWorker {\n constructor(options: SandboxWorkerOptions = {}) {\n super(deviceSimulationHostGuestScript, { name: 'device', ...options });\n }\n};\n\nexport { createDeviceSimulationHostFromSandbox };\nexport type {\n DeviceSimulationHostFileSystem,\n SandboxDeviceSimulationHostOptions,\n} from './host/sandbox-host';\nexport type {\n DeviceSimulationHost,\n DeviceSimTickReport,\n DeviceTopic,\n SimulationPublish,\n} from './host/types';\n"],"mappings":";;;;;;;;;;;;;;;;;;AA2CA,eAAsB,sCAAsC,E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IAAI,UAAU;GACd,WAAW;GACX,IAAI;IACF,MAAM,MAAM,QAAQ;GACtB,UAAU;IACR,iBAAiB,QAAQ;IACzB,SAAS,QAAQ;IACjB,MAAM,QAAQ,UAAU,UAAU;GACpC;EACF;CACF;AACF;;;;;;;;;;;;;;;;;;ACxFA,IAAa,kCAA0CA;;;;;;;AAQvD,SAAgB,uCACd,UAAgC,CAAC,GAClB;CACf,OAAO,IAAI,cAAc,iCAAiC;EAAE,MAAM;EAAU,GAAG;CAAQ,CAAC;AAC1F;;;;;;AAOA,IAAa,mCAEQ,MAAM,6CAA6C,cAAc;CACpF,YAAY,UAAgC,CAAC,GAAG;EAC9C,MAAM,iCAAiC;GAAE,MAAM;GAAU,GAAG;EAAQ,CAAC;CACvE;AACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mica-js/simulator",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Mica simulator: runs the device instances of a schematic on one virtual clock",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -14,6 +14,10 @@
14
14
  ".": {
15
15
  "types": "./dist/index.d.ts",
16
16
  "import": "./dist/index.js"
17
+ },
18
+ "./sandbox": {
19
+ "types": "./dist/sandbox.d.ts",
20
+ "import": "./dist/sandbox.js"
17
21
  }
18
22
  },
19
23
  "files": [
@@ -23,8 +27,10 @@
23
27
  "access": "public"
24
28
  },
25
29
  "dependencies": {
26
- "@mica-js/core": "0.2.0",
27
- "@mica-js/device": "0.2.0"
30
+ "@mica-js/core": "0.4.0",
31
+ "@mica-js/device": "0.3.0",
32
+ "@mica-js/ir": "0.4.0",
33
+ "@mica-js/sandbox": "0.3.0"
28
34
  },
29
35
  "devDependencies": {
30
36
  "typescript": "^6.0.3",
@@ -33,9 +39,11 @@
33
39
  "vitest": "^5.0.5"
34
40
  },
35
41
  "scripts": {
42
+ "bench": "vitest bench --run",
43
+ "bench:unified": "vitest bench --run bench/unified-simple.bench.ts",
36
44
  "build": "vite build",
37
45
  "test": "vitest run",
38
46
  "test:watch": "vitest",
39
- "typecheck": "tsc --noEmit"
47
+ "typecheck": "tsc --noEmit && tsc --noEmit -p tsconfig.bench.json"
40
48
  }
41
49
  }