@mica-js/simulator 2.3.0 → 2.4.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.
- package/README.md +14 -8
- package/dist/device.d.ts +11 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +244 -280
- package/dist/index.js.map +1 -1
- package/dist/rpc-BB5wM0QN.js +289 -0
- package/dist/rpc-BB5wM0QN.js.map +1 -0
- package/dist/sandbox.js +2 -2
- package/package.json +3 -2
- package/dist/rpc-D-9O3TZf.js +0 -108
- package/dist/rpc-D-9O3TZf.js.map +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mica-js/simulator",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.0",
|
|
4
4
|
"description": "Synchronous compiled device simulation runtime",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -66,6 +66,7 @@
|
|
|
66
66
|
"bench:mcu:typical": "node bench/typical-mcu.mjs",
|
|
67
67
|
"bench:mcu:camera": "node bench/camera-mcu.mjs",
|
|
68
68
|
"bench:mcu:stm32": "node bench/stm32g431.mjs",
|
|
69
|
-
"bench:mcu:stm32:v8": "node bench/stm32g431/v8-trace.mjs"
|
|
69
|
+
"bench:mcu:stm32:v8": "node bench/stm32g431/v8-trace.mjs",
|
|
70
|
+
"bench:v8-optimize": "node bench/v8-optimize.mjs"
|
|
70
71
|
}
|
|
71
72
|
}
|
package/dist/rpc-D-9O3TZf.js
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
import { defineRpcService } from "@mica-js/core/rpc";
|
|
2
|
-
//#region src/simulation-runtime/payload.ts
|
|
3
|
-
/** Stable key of one device endpoint. */
|
|
4
|
-
function targetKey(symbol, port) {
|
|
5
|
-
return `${symbol}\u0000${port}`;
|
|
6
|
-
}
|
|
7
|
-
/**
|
|
8
|
-
* Value equality of two payloads.
|
|
9
|
-
*
|
|
10
|
-
* A canvas topic is a `level` unless it carries text or bytes (RFC 0017), so re-sending the value a
|
|
11
|
-
* device already published only costs a widget repaint. Byte payloads compare by content because a
|
|
12
|
-
* device may hand out a fresh buffer per step.
|
|
13
|
-
*/
|
|
14
|
-
function samePayload(previous, next) {
|
|
15
|
-
if (Object.is(previous, next)) return true;
|
|
16
|
-
if (previous instanceof Uint8Array && next instanceof Uint8Array) {
|
|
17
|
-
if (previous.length !== next.length) return false;
|
|
18
|
-
for (let index = 0; index < previous.length; index += 1) if (previous[index] !== next[index]) return false;
|
|
19
|
-
return true;
|
|
20
|
-
}
|
|
21
|
-
if (typeof previous !== "object" || previous === null) return false;
|
|
22
|
-
if (typeof next !== "object" || next === null) return false;
|
|
23
|
-
const a = previous;
|
|
24
|
-
const b = next;
|
|
25
|
-
return a.x === b.x && a.y === b.y && a.z === b.z;
|
|
26
|
-
}
|
|
27
|
-
function isVec2d(value) {
|
|
28
|
-
if (typeof value !== "object" || value === null) return false;
|
|
29
|
-
const point = value;
|
|
30
|
-
return typeof point.x === "number" && typeof point.y === "number";
|
|
31
|
-
}
|
|
32
|
-
/** Whether `payload` matches the declared topic payload type. */
|
|
33
|
-
function payloadMatches(type, payload) {
|
|
34
|
-
switch (type) {
|
|
35
|
-
case "boolean": return typeof payload === "boolean";
|
|
36
|
-
case "number": return typeof payload === "number";
|
|
37
|
-
case "string": return typeof payload === "string";
|
|
38
|
-
case "bytes":
|
|
39
|
-
case "bytes<rgb565>":
|
|
40
|
-
case "bytes<segments>": return payload instanceof Uint8Array;
|
|
41
|
-
case "vec2d": return isVec2d(payload);
|
|
42
|
-
case "vec3d": return isVec2d(payload) && typeof payload.z === "number";
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
/** Copies a byte payload so an RPC transfer never detaches the producer's buffer. */
|
|
46
|
-
function copyBytePayload(payload) {
|
|
47
|
-
return payload instanceof Uint8Array ? new Uint8Array(payload) : payload;
|
|
48
|
-
}
|
|
49
|
-
/** Snapshots mutable payloads accepted by the prepared topic contract. */
|
|
50
|
-
function copyPayload(payload) {
|
|
51
|
-
if (payload instanceof Uint8Array) return new Uint8Array(payload);
|
|
52
|
-
return typeof payload === "object" && payload !== null ? { ...payload } : payload;
|
|
53
|
-
}
|
|
54
|
-
//#endregion
|
|
55
|
-
//#region src/simulation-runtime/rpc.ts
|
|
56
|
-
/**
|
|
57
|
-
* Wire name of the prepared simulation runtime.
|
|
58
|
-
*
|
|
59
|
-
* The same id is registered on the primary sandbox RPC channel and on the direct topic port, so a
|
|
60
|
-
* widget guest reaches the running simulation without a second protocol.
|
|
61
|
-
*/
|
|
62
|
-
var simulationRuntimeService = defineRpcService("simulation-runtime");
|
|
63
|
-
/** Topic-port view of the same wire service registered by the runtime guest. */
|
|
64
|
-
var simulationRuntimeTopicService = defineRpcService("simulation-runtime");
|
|
65
|
-
/**
|
|
66
|
-
* Transfer list for one proxy call.
|
|
67
|
-
*
|
|
68
|
-
* Asset bytes and delivered byte payloads are copied by the adapter before the call, so transferring
|
|
69
|
-
* their buffers here never detaches a caller-owned array.
|
|
70
|
-
*/
|
|
71
|
-
function simulationRuntimeCallTransfer(member, args) {
|
|
72
|
-
if (member === "deliver") {
|
|
73
|
-
const message = args[0];
|
|
74
|
-
return transferableBuffer(message?.payload);
|
|
75
|
-
}
|
|
76
|
-
if (member === "load") return assetTransferList(args[0]);
|
|
77
|
-
return [];
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* Transfer list for one event registration callback.
|
|
81
|
-
*
|
|
82
|
-
* `value` is the callback's argument array (see `RpcRegistrationOptions.transfer`). The guest copies
|
|
83
|
-
* a byte payload before invoking the listener, so the published buffer is safe to transfer.
|
|
84
|
-
*/
|
|
85
|
-
function simulationRuntimeEventTransfer(member, value) {
|
|
86
|
-
if (member !== "onDidPublish" || !Array.isArray(value)) return [];
|
|
87
|
-
const message = value[0];
|
|
88
|
-
return transferableBuffer(message?.payload);
|
|
89
|
-
}
|
|
90
|
-
function transferableBuffer(payload) {
|
|
91
|
-
if (!(payload instanceof Uint8Array)) return [];
|
|
92
|
-
return payload.buffer instanceof ArrayBuffer ? [payload.buffer] : [];
|
|
93
|
-
}
|
|
94
|
-
function assetTransferList(input) {
|
|
95
|
-
if (typeof input !== "object" || input === null) return [];
|
|
96
|
-
const assets = input.assets;
|
|
97
|
-
if (typeof assets !== "object" || assets === null) return [];
|
|
98
|
-
const buffers = /* @__PURE__ */ new Set();
|
|
99
|
-
for (const entries of Object.values(assets)) {
|
|
100
|
-
if (typeof entries !== "object" || entries === null) continue;
|
|
101
|
-
for (const bytes of Object.values(entries)) if (bytes instanceof Uint8Array && bytes.buffer instanceof ArrayBuffer) buffers.add(bytes.buffer);
|
|
102
|
-
}
|
|
103
|
-
return [...buffers];
|
|
104
|
-
}
|
|
105
|
-
//#endregion
|
|
106
|
-
export { copyBytePayload as a, samePayload as c, simulationRuntimeTopicService as i, targetKey as l, simulationRuntimeEventTransfer as n, copyPayload as o, simulationRuntimeService as r, payloadMatches as s, simulationRuntimeCallTransfer as t };
|
|
107
|
-
|
|
108
|
-
//# sourceMappingURL=rpc-D-9O3TZf.js.map
|
package/dist/rpc-D-9O3TZf.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"rpc-D-9O3TZf.js","names":[],"sources":["../src/simulation-runtime/payload.ts","../src/simulation-runtime/rpc.ts"],"sourcesContent":["import type { PayloadType } from '@mica-js/document/canvas';\n\n/** Stable key of one device endpoint. */\nexport function targetKey(symbol: string, port: string): string {\n return `${symbol}\\u0000${port}`;\n}\n\n/**\n * Value equality of two payloads.\n *\n * A canvas topic is a `level` unless it carries text or bytes (RFC 0017), so re-sending the value a\n * device already published only costs a widget repaint. Byte payloads compare by content because a\n * device may hand out a fresh buffer per step.\n */\nexport function samePayload(previous: unknown, next: unknown): boolean {\n if (Object.is(previous, next)) return true;\n if (previous instanceof Uint8Array && next instanceof Uint8Array) {\n if (previous.length !== next.length) return false;\n for (let index = 0; index < previous.length; index += 1) {\n if (previous[index] !== next[index]) return false;\n }\n return true;\n }\n if (typeof previous !== 'object' || previous === null) return false;\n if (typeof next !== 'object' || next === null) return false;\n const a = previous as { readonly x?: unknown; readonly y?: unknown; readonly z?: unknown };\n const b = next as { readonly x?: unknown; readonly y?: unknown; readonly z?: unknown };\n return a.x === b.x && a.y === b.y && a.z === b.z;\n}\n\nfunction isVec2d(value: unknown): boolean {\n if (typeof value !== 'object' || value === null) return false;\n const point = value as { readonly x?: unknown; readonly y?: unknown };\n return typeof point.x === 'number' && typeof point.y === 'number';\n}\n\n/** Whether `payload` matches the declared topic payload type. */\nexport function payloadMatches(type: PayloadType, payload: unknown): boolean {\n switch (type) {\n case 'boolean':\n return typeof payload === 'boolean';\n case 'number':\n return typeof payload === 'number';\n case 'string':\n return typeof payload === 'string';\n case 'bytes':\n case 'bytes<rgb565>':\n case 'bytes<segments>':\n return payload instanceof Uint8Array;\n case 'vec2d':\n return isVec2d(payload);\n case 'vec3d':\n return isVec2d(payload) && typeof (payload as { readonly z?: unknown }).z === 'number';\n }\n}\n\n/** Copies a byte payload so an RPC transfer never detaches the producer's buffer. */\nexport function copyBytePayload(payload: unknown): unknown {\n return payload instanceof Uint8Array ? new Uint8Array(payload) : payload;\n}\n\n/** Snapshots mutable payloads accepted by the prepared topic contract. */\nexport function copyPayload(payload: unknown): unknown {\n if (payload instanceof Uint8Array) return new Uint8Array(payload);\n return typeof payload === 'object' && payload !== null ? { ...payload } : payload;\n}\n","import { defineRpcService } from '@mica-js/core/rpc';\nimport type { TimePs } from '@mica-js/core';\nimport type {\n IPreparedSimulation,\n ISimulationDiagnostic,\n ISimulationMessage,\n ISimulationTickReport,\n} from './types';\n\n/** Methods the runtime guest exposes over the primary sandbox RPC channel. */\nexport interface ISimulationRuntimeRpc {\n load(input: IPreparedSimulation): Promise<void>;\n reset(): Promise<void>;\n tick(dt: TimePs): Promise<ISimulationTickReport>;\n deliver(message: ISimulationMessage): Promise<void>;\n onDidPublish(listener: (message: ISimulationMessage) => void): () => void;\n onDidDiagnostic(listener: (diagnostic: ISimulationDiagnostic) => void): () => void;\n dispose(): Promise<void>;\n}\n\n/**\n * Wire name of the prepared simulation runtime.\n *\n * The same id is registered on the primary sandbox RPC channel and on the direct topic port, so a\n * widget guest reaches the running simulation without a second protocol.\n */\nexport const simulationRuntimeService =\n defineRpcService<ISimulationRuntimeRpc>('simulation-runtime');\n\n/** The data plane the direct topic port exposes: deliveries in, publishes out. */\nexport interface ISimulationRuntimeTopicRpc {\n deliver(message: ISimulationMessage): Promise<void>;\n onDidPublish(listener: (message: ISimulationMessage) => void): () => void;\n}\n\n/** Topic-port view of the same wire service registered by the runtime guest. */\nexport const simulationRuntimeTopicService =\n defineRpcService<ISimulationRuntimeTopicRpc>('simulation-runtime');\n\n/**\n * Transfer list for one proxy call.\n *\n * Asset bytes and delivered byte payloads are copied by the adapter before the call, so transferring\n * their buffers here never detaches a caller-owned array.\n */\nexport function simulationRuntimeCallTransfer(\n member: string,\n args: readonly unknown[],\n): readonly ArrayBuffer[] {\n if (member === 'deliver') {\n const message = args[0] as ISimulationMessage | undefined;\n return transferableBuffer(message?.payload);\n }\n if (member === 'load') return assetTransferList(args[0]);\n return [];\n}\n\n/**\n * Transfer list for one event registration callback.\n *\n * `value` is the callback's argument array (see `RpcRegistrationOptions.transfer`). The guest copies\n * a byte payload before invoking the listener, so the published buffer is safe to transfer.\n */\nexport function simulationRuntimeEventTransfer(\n member: string,\n value: unknown,\n): readonly ArrayBuffer[] {\n if (member !== 'onDidPublish' || !Array.isArray(value)) return [];\n const message = value[0] as ISimulationMessage | undefined;\n return transferableBuffer(message?.payload);\n}\n\nfunction transferableBuffer(payload: unknown): readonly ArrayBuffer[] {\n if (!(payload instanceof Uint8Array)) return [];\n return payload.buffer instanceof ArrayBuffer ? [payload.buffer] : [];\n}\n\nfunction assetTransferList(input: unknown): readonly ArrayBuffer[] {\n if (typeof input !== 'object' || input === null) return [];\n const assets = (input as { readonly assets?: unknown }).assets;\n if (typeof assets !== 'object' || assets === null) return [];\n const buffers = new Set<ArrayBuffer>();\n for (const entries of Object.values(assets)) {\n if (typeof entries !== 'object' || entries === null) continue;\n for (const bytes of Object.values(entries)) {\n if (bytes instanceof Uint8Array && bytes.buffer instanceof ArrayBuffer) {\n buffers.add(bytes.buffer);\n }\n }\n }\n return [...buffers];\n}\n"],"mappings":";;;AAGA,SAAgB,UAAU,QAAgB,MAAsB;CAC9D,OAAO,GAAG,OAAO,QAAQ;AAC3B;;;;;;;;AASA,SAAgB,YAAY,UAAmB,MAAwB;CACrE,IAAI,OAAO,GAAG,UAAU,IAAI,GAAG,OAAO;CACtC,IAAI,oBAAoB,cAAc,gBAAgB,YAAY;EAChE,IAAI,SAAS,WAAW,KAAK,QAAQ,OAAO;EAC5C,KAAK,IAAI,QAAQ,GAAG,QAAQ,SAAS,QAAQ,SAAS,GACpD,IAAI,SAAS,WAAW,KAAK,QAAQ,OAAO;EAE9C,OAAO;CACT;CACA,IAAI,OAAO,aAAa,YAAY,aAAa,MAAM,OAAO;CAC9D,IAAI,OAAO,SAAS,YAAY,SAAS,MAAM,OAAO;CACtD,MAAM,IAAI;CACV,MAAM,IAAI;CACV,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE;AACjD;AAEA,SAAS,QAAQ,OAAyB;CACxC,IAAI,OAAO,UAAU,YAAY,UAAU,MAAM,OAAO;CACxD,MAAM,QAAQ;CACd,OAAO,OAAO,MAAM,MAAM,YAAY,OAAO,MAAM,MAAM;AAC3D;;AAGA,SAAgB,eAAe,MAAmB,SAA2B;CAC3E,QAAQ,MAAR;EACE,KAAK,WACH,OAAO,OAAO,YAAY;EAC5B,KAAK,UACH,OAAO,OAAO,YAAY;EAC5B,KAAK,UACH,OAAO,OAAO,YAAY;EAC5B,KAAK;EACL,KAAK;EACL,KAAK,mBACH,OAAO,mBAAmB;EAC5B,KAAK,SACH,OAAO,QAAQ,OAAO;EACxB,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