@mica-js/simulator 2.0.0 → 2.1.1
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/README.md +343 -7
- package/dist/ddl.d.ts +4 -0
- package/dist/ddl.js +1 -0
- package/dist/device.d.ts +111 -0
- package/dist/device.js +0 -0
- package/dist/index.d.ts +257 -783
- package/dist/index.js +4339 -3178
- package/dist/index.js.map +1 -1
- package/dist/model.d.ts +4 -0
- package/dist/model.js +1 -0
- package/dist/rpc-D-9O3TZf.js +108 -0
- package/dist/rpc-D-9O3TZf.js.map +1 -0
- package/dist/sandbox.d.ts +75 -109
- package/dist/sandbox.js +71 -80
- package/dist/sandbox.js.map +1 -1
- package/package.json +33 -11
- package/dist/rpc-BIxQr_wd.js +0 -12
- package/dist/rpc-BIxQr_wd.js.map +0 -1
package/dist/sandbox.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sandbox.js","names":["
|
|
1
|
+
{"version":3,"file":"sandbox.js","names":["simulationRuntimeGuestScript"],"sources":["../src/simulation-runtime/adapter.ts","../src/sandbox.ts"],"sourcesContent":["import { toDisposable } from '@mica-js/core/lifecycle';\nimport { createRpc } from '@mica-js/core/rpc';\nimport type { RpcEndpoint } from '@mica-js/core/rpc';\nimport type { SandboxWorker } from '@mica-js/sandbox';\nimport { copyBytePayload } from './payload';\nimport { simulationRuntimeCallTransfer, simulationRuntimeService } from './rpc';\nimport type { IPreparedSimulation, ISimulationMessage, ISimulationRuntime } from './types';\n\n/** Startup options of the worker-backed runtime adapter. */\nexport interface ISimulationRuntimeWorkerOptions {\n /**\n * Extra port transferred after the RPC port.\n *\n * The guest registers the `simulation-runtime` data plane on it; the caller keeps the other end\n * so a widget guest can deliver and subscribe without the host relaying.\n */\n readonly topicPort?: MessagePort;\n}\n\n/**\n * Builds an `ISimulationRuntime` that runs inside one sandbox worker.\n *\n * The adapter owns the worker: it starts the RPC channel, forwards the optional topic port, and\n * terminates the worker on `dispose`. The worker receives only `MessagePort`s, so device modules\n * are imported and executed inside the guest, never on the editor thread.\n */\nexport async function createSimulationRuntimeFromWorker(\n worker: SandboxWorker,\n options: ISimulationRuntimeWorkerOptions = {},\n): Promise<ISimulationRuntime> {\n let channel: MessageChannel | undefined;\n let endpoint: RpcEndpoint | undefined;\n let posted = false;\n const topicPort = options.topicPort;\n try {\n await worker.ready;\n channel = new MessageChannel();\n endpoint = createRpc(channel.port1);\n const ports: MessagePort[] = [channel.port2];\n if (topicPort !== undefined) ports.push(topicPort);\n worker.postMessage(null, ports);\n posted = true;\n } catch (error) {\n endpoint?.dispose();\n if (!posted) {\n closePort(channel?.port1);\n closePort(channel?.port2);\n closePort(topicPort);\n }\n await worker.terminate('simulation runtime failed to start').catch(() => undefined);\n throw error;\n }\n if (endpoint === undefined || channel === undefined) {\n await worker.terminate('simulation runtime failed to start').catch(() => undefined);\n throw new Error('Simulation runtime RPC endpoint was not created.');\n }\n\n const rpc = endpoint;\n const guest = rpc.getProxy(simulationRuntimeService, {\n transfer: simulationRuntimeCallTransfer,\n });\n let disposed = false;\n\n return {\n load: (input: IPreparedSimulation) => guest.load(copyPreparedSimulation(input)),\n reset: () => guest.reset(),\n tick: (dt) => guest.tick(dt),\n deliver: (message: ISimulationMessage) => guest.deliver(copyMessage(message)),\n onDidPublish: (listener) => toDisposable(guest.onDidPublish(listener)),\n onDidDiagnostic: (listener) => toDisposable(guest.onDidDiagnostic(listener)),\n async dispose(): Promise<void> {\n if (disposed) return;\n disposed = true;\n try {\n await guest.dispose();\n } catch {\n // The worker may already be gone; teardown below still has to run.\n } finally {\n rpc.dispose();\n closePort(channel.port1);\n if (!posted) {\n closePort(channel.port2);\n closePort(topicPort);\n }\n await worker.terminate('disposed').catch(() => undefined);\n }\n },\n };\n}\n\nfunction copyPreparedSimulation(input: IPreparedSimulation): IPreparedSimulation {\n if (input.assets === undefined) return input;\n const assets: Record<string, Record<string, Uint8Array>> = {};\n for (const [symbolId, entries] of Object.entries(input.assets)) {\n const copies: Record<string, Uint8Array> = {};\n for (const [src, bytes] of Object.entries(entries)) copies[src] = new Uint8Array(bytes);\n assets[symbolId] = copies;\n }\n return { ...input, assets };\n}\n\nfunction copyMessage(message: ISimulationMessage): ISimulationMessage {\n const payload = copyBytePayload(message.payload);\n return payload === message.payload ? message : { ...message, payload };\n}\n\nfunction closePort(port: MessagePort | undefined): void {\n try {\n port?.close();\n } catch {\n // Already detached by a transfer; the receiving side owns it now.\n }\n}\n","import { SandboxWorker } from '@mica-js/sandbox';\nimport type { SandboxWorkerOptions } from '@mica-js/sandbox';\nimport simulationRuntimeGuestScript from './guest/simulation-runtime.ts?sandbox-worker-code';\n\nexport { simulationRuntimeGuestScript };\nexport { createSimulationRuntimeFromWorker } from './simulation-runtime/adapter';\nexport type { ISimulationRuntimeWorkerOptions } from './simulation-runtime/adapter';\n\n/** Creates the optional browser worker; the runtime adapter takes ownership of it. */\nexport function createSimulationRuntimeGuestSandbox(\n options: SandboxWorkerOptions = {},\n): SandboxWorker {\n return new SandboxWorker(simulationRuntimeGuestScript, options);\n}\n"],"mappings":";;;;;;;;;;;;;;;AA0BA,eAAsB,kCACpB,QACA,UAA2C,CAAC,GACf;CAC7B,IAAI;CACJ,IAAI;CACJ,IAAI,SAAS;CACb,MAAM,YAAY,QAAQ;CAC1B,IAAI;EACF,MAAM,OAAO;EACb,UAAU,IAAI,eAAe;EAC7B,WAAW,UAAU,QAAQ,KAAK;EAClC,MAAM,QAAuB,CAAC,QAAQ,KAAK;EAC3C,IAAI,cAAc,KAAA,GAAW,MAAM,KAAK,SAAS;EACjD,OAAO,YAAY,MAAM,KAAK;EAC9B,SAAS;CACX,SAAS,OAAO;EACd,UAAU,QAAQ;EAClB,IAAI,CAAC,QAAQ;GACX,UAAU,SAAS,KAAK;GACxB,UAAU,SAAS,KAAK;GACxB,UAAU,SAAS;EACrB;EACA,MAAM,OAAO,UAAU,oCAAoC,CAAC,CAAC,YAAY,KAAA,CAAS;EACl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AM,UAAU,gBAAgB,QAAQ,OAAO;CAC/C,OAAO,YAAY,QAAQ,UAAU,UAAU;EAAE,GAAG;EAAS;CAAQ;AACvE;AAEA,SAAS,UAAU,MAAqC;CACtD,IAAI;EACF,MAAM,MAAM;CACd,QAAQ,CAER;AACF;;;;ACvGA,SAAgB,oCACd,UAAgC,CAAC,GAClB;CACf,OAAO,IAAI,cAAcA,4BAA8B,OAAO;AAChE"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mica-js/simulator",
|
|
3
|
-
"version": "2.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "2.1.1",
|
|
4
|
+
"description": "Synchronous compiled device simulation runtime",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -15,6 +15,18 @@
|
|
|
15
15
|
"types": "./dist/index.d.ts",
|
|
16
16
|
"import": "./dist/index.js"
|
|
17
17
|
},
|
|
18
|
+
"./device": {
|
|
19
|
+
"types": "./dist/device.d.ts",
|
|
20
|
+
"import": "./dist/device.js"
|
|
21
|
+
},
|
|
22
|
+
"./model": {
|
|
23
|
+
"types": "./dist/model.d.ts",
|
|
24
|
+
"import": "./dist/model.js"
|
|
25
|
+
},
|
|
26
|
+
"./ddl": {
|
|
27
|
+
"types": "./dist/ddl.d.ts",
|
|
28
|
+
"import": "./dist/ddl.js"
|
|
29
|
+
},
|
|
18
30
|
"./sandbox": {
|
|
19
31
|
"types": "./dist/sandbox.d.ts",
|
|
20
32
|
"import": "./dist/sandbox.js"
|
|
@@ -27,9 +39,12 @@
|
|
|
27
39
|
"access": "public"
|
|
28
40
|
},
|
|
29
41
|
"dependencies": {
|
|
30
|
-
"@mica-js/
|
|
31
|
-
"@mica-js/
|
|
32
|
-
"@mica-js/
|
|
42
|
+
"@mica-js/compiler": "0.3.0",
|
|
43
|
+
"@mica-js/device-model": "0.5.0",
|
|
44
|
+
"@mica-js/ddl": "0.2.0",
|
|
45
|
+
"@mica-js/core": "0.6.0",
|
|
46
|
+
"@mica-js/document": "0.1.0",
|
|
47
|
+
"@mica-js/sandbox": "0.4.0"
|
|
33
48
|
},
|
|
34
49
|
"devDependencies": {
|
|
35
50
|
"typescript": "^6.0.3",
|
|
@@ -38,12 +53,19 @@
|
|
|
38
53
|
"vitest": "^5.0.5"
|
|
39
54
|
},
|
|
40
55
|
"scripts": {
|
|
41
|
-
"bench": "vitest bench --run",
|
|
42
|
-
"bench:unified": "vitest bench --run bench/unified-simple.bench.ts",
|
|
43
|
-
"bench:resistive": "vitest run --config bench/resistive-number.config.ts",
|
|
44
56
|
"build": "vite build",
|
|
45
|
-
"
|
|
46
|
-
"test
|
|
47
|
-
"
|
|
57
|
+
"typecheck": "tsc --noEmit",
|
|
58
|
+
"test": "node --test bench/*.test.mjs bench/stm32g431/*.test.mjs bench/stm32g431/firmware/*.test.mjs",
|
|
59
|
+
"test:mcu:stm32": "node --test bench/stm32g431.test.mjs bench/stm32g431/*.test.mjs bench/stm32g431/firmware/*.test.mjs",
|
|
60
|
+
"test:runtime": "vitest run",
|
|
61
|
+
"bench": "node bench/runtime.mjs",
|
|
62
|
+
"bench:electrical": "node bench/electrical.mjs",
|
|
63
|
+
"bench:workloads": "node bench/workloads.mjs",
|
|
64
|
+
"bench:propagation": "node bench/propagation.mjs",
|
|
65
|
+
"bench:mcu": "node bench/mcu.mjs",
|
|
66
|
+
"bench:mcu:typical": "node bench/typical-mcu.mjs",
|
|
67
|
+
"bench:mcu:camera": "node bench/camera-mcu.mjs",
|
|
68
|
+
"bench:mcu:stm32": "node bench/stm32g431.mjs",
|
|
69
|
+
"bench:mcu:stm32:v8": "node bench/stm32g431/v8-trace.mjs"
|
|
48
70
|
}
|
|
49
71
|
}
|
package/dist/rpc-BIxQr_wd.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { defineRpcService } from "@mica-js/core/rpc";
|
|
2
|
-
//#region src/host/rpc.ts
|
|
3
|
-
/**
|
|
4
|
-
* Wire names both sides of the sandbox agree on: the worker script registers the device simulation
|
|
5
|
-
* contract as `guest`, and calls the host back through `host-capabilities` to read schematic bytes.
|
|
6
|
-
*/
|
|
7
|
-
var deviceSimulationHostGuestService = defineRpcService("guest");
|
|
8
|
-
var deviceSimulationHostFileSystemService = defineRpcService("host-capabilities");
|
|
9
|
-
//#endregion
|
|
10
|
-
export { deviceSimulationHostGuestService as n, deviceSimulationHostFileSystemService as t };
|
|
11
|
-
|
|
12
|
-
//# sourceMappingURL=rpc-BIxQr_wd.js.map
|
package/dist/rpc-BIxQr_wd.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"rpc-BIxQr_wd.js","names":[],"sources":["../src/host/rpc.ts"],"sourcesContent":["import { defineRpcService } from '@mica-js/core/rpc';\nimport type { CanvasBindingTarget } from '@mica-js/core/canvas';\nimport type { SchematicDocument } from '@mica-js/core/schematic';\nimport type { DeviceSimTickReport, DeviceTopic, SimulationPublish } from './types';\n\n/** Host file system the sandbox guest reads schematics through. */\nexport interface DeviceSimulationHostFileSystemRpc {\n read(uri: string): Promise<Uint8Array>;\n}\n\n/** Methods the device simulation guest exposes over the sandbox RPC channel. */\nexport interface DeviceSimulationHostRpc {\n loadSchematic(uri: string | undefined): Promise<void>;\n reset(): Promise<void>;\n tick(dt: number): Promise<DeviceSimTickReport>;\n /**\n * `payload` is the one intentionally arbitrary value: widget/device payloads are plugin-defined\n * and validated against the target topic. Every other argument and result is a precise DTO.\n */\n deliver(target: CanvasBindingTarget, payload: unknown, simTime?: number): Promise<void>;\n onDidPublish(listener: (publish: SimulationPublish) => void): () => void;\n getTopics(): Promise<readonly DeviceTopic[]>;\n getSimTime(): Promise<number>;\n getSchematic(): Promise<SchematicDocument | undefined>;\n dispose(): Promise<void>;\n}\n\n/**\n * Wire names both sides of the sandbox agree on: the worker script registers the device simulation\n * contract as `guest`, and calls the host back through `host-capabilities` to read schematic bytes.\n */\nexport const deviceSimulationHostGuestService = defineRpcService<DeviceSimulationHostRpc>('guest');\nexport const deviceSimulationHostFileSystemService =\n defineRpcService<DeviceSimulationHostFileSystemRpc>('host-capabilities');\n"],"mappings":";;;;;;AA+BA,IAAa,mCAAmC,iBAA0C,OAAO;AACjG,IAAa,wCACX,iBAAoD,mBAAmB"}
|