@mica-js/simulator 2.4.0 → 2.5.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/dist/device.d.ts CHANGED
@@ -110,18 +110,7 @@ declare const snapshotTag: unique symbol;
110
110
 
111
111
  export declare interface TopicBridge {
112
112
  readonly onDidReceive: Event_2<TopicMessage>;
113
- /**
114
- * 未携带 owner 的监听者数量,即宿主对板级根 bridge 的直连订阅数;实例作用域包装注册的
115
- * 监听者不计入。计数覆盖整块板共享的话题分发集合,因此器件 `ctx.topics` 读到的是同一个
116
- * 宿主订阅数。只读视图,随订阅创建与释放实时变化。
117
- */
118
- readonly hostListenerCount: number;
119
113
  publish(topic: string, payload: unknown): void;
120
- /**
121
- * 按 `publish` 相同的失效、非法阶段与故障规则校验,但不复制载荷、不组装消息、不派发。
122
- * 发布者可在确认无人接收时先校验再跳过复制与扇出。
123
- */
124
- assertCanPublish(): void;
125
114
  }
126
115
 
127
116
  export declare interface TopicMessage {
package/dist/index.d.ts CHANGED
@@ -359,18 +359,7 @@ export declare function startSimulationRuntimeGuest(port: MessagePort, options?:
359
359
 
360
360
  export declare interface TopicBridge {
361
361
  readonly onDidReceive: Event_2<TopicMessage>;
362
- /**
363
- * 未携带 owner 的监听者数量,即宿主对板级根 bridge 的直连订阅数;实例作用域包装注册的
364
- * 监听者不计入。计数覆盖整块板共享的话题分发集合,因此器件 `ctx.topics` 读到的是同一个
365
- * 宿主订阅数。只读视图,随订阅创建与释放实时变化。
366
- */
367
- readonly hostListenerCount: number;
368
362
  publish(topic: string, payload: unknown): void;
369
- /**
370
- * 按 `publish` 相同的失效、非法阶段与故障规则校验,但不复制载荷、不组装消息、不派发。
371
- * 发布者可在确认无人接收时先校验再跳过复制与扇出。
372
- */
373
- assertCanPublish(): void;
374
363
  }
375
364
 
376
365
  export declare interface TopicMessage {
package/dist/index.js CHANGED
@@ -1920,15 +1920,15 @@ function hasEvery(previous, next) {
1920
1920
  function createContext(owner, board) {
1921
1921
  return Object.freeze({
1922
1922
  get props() {
1923
- board.guard(owner, true);
1923
+ board.assertReadAllowed(owner);
1924
1924
  return Object.freeze(Object.fromEntries([...owner.props].map(([key, value]) => [key, externalValue(value)])));
1925
1925
  },
1926
1926
  get inputs() {
1927
- owner.guard();
1927
+ owner.assertCallAllowed();
1928
1928
  return owner.inputs();
1929
1929
  },
1930
1930
  get memory() {
1931
- board.guard(owner, true);
1931
+ board.assertReadAllowed(owner);
1932
1932
  return owner.publicMemory();
1933
1933
  },
1934
1934
  topics: board.topicBridge(owner),
@@ -2548,18 +2548,18 @@ var Instance = class {
2548
2548
  }
2549
2549
  this.memorySizes.clear();
2550
2550
  }
2551
- guard() {
2552
- this.board.guard(this);
2551
+ assertCallAllowed() {
2552
+ this.board.assertCallAllowed(this);
2553
2553
  }
2554
2554
  inputs() {
2555
2555
  const result = Object.create(null);
2556
2556
  for (const declaration of this.entry.model.declarations) {
2557
2557
  if (declaration.kind === "channel" && declaration.direction === "input") result[declaration.name] = Object.freeze({ send: (value) => {
2558
- this.guard();
2558
+ this.assertCallAllowed();
2559
2559
  this.send(declaration.name, value, true);
2560
2560
  } });
2561
2561
  if (declaration.kind === "notification" && declaration.direction === "input") result[declaration.name] = Object.freeze({ notify: () => {
2562
- this.guard();
2562
+ this.assertCallAllowed();
2563
2563
  this.notificationInputs.add(declaration.name);
2564
2564
  } });
2565
2565
  }
@@ -2568,7 +2568,7 @@ var Instance = class {
2568
2568
  publicMemory() {
2569
2569
  const result = Object.create(null);
2570
2570
  for (const declaration of this.entry.model.declarations) if (declaration.kind === "memory" && declaration.public) result[declaration.name] = bytesView(this.memory(declaration.name), () => {
2571
- this.board.validity(this);
2571
+ this.board.assertAlive(this);
2572
2572
  if (![
2573
2573
  "idle",
2574
2574
  "initialize",
@@ -2599,7 +2599,7 @@ var Instance = class {
2599
2599
  });
2600
2600
  }
2601
2601
  attach(options) {
2602
- this.guard();
2602
+ this.assertCallAllowed();
2603
2603
  if (this.board.phase !== "initialize" || this.board.current !== this) throw this.board.error("INVALID_CALL_STATE", "attachJit is initialization-only", this);
2604
2604
  dataObject(options, [
2605
2605
  "entry",
@@ -2680,7 +2680,7 @@ var Instance = class {
2680
2680
  return disposable;
2681
2681
  }
2682
2682
  loadBytes(name, bytes, offset) {
2683
- this.guard();
2683
+ this.assertCallAllowed();
2684
2684
  if (this.board.phase !== "idle" && !(this.board.phase === "initialize" && this.board.current === this)) throw this.board.error("INVALID_CALL_STATE", "Code loading is not allowed in this phase", this);
2685
2685
  if (typeof name !== "string" || !this.jits.some((jit) => jit.active && jit.options.code === name)) throw this.board.error("INVALID_BINDING", "Memory is not bound as code", this);
2686
2686
  if (!(bytes instanceof Uint8Array)) throw new TypeError("Code must be Uint8Array");
@@ -2706,6 +2706,18 @@ var Instance = class {
2706
2706
  }
2707
2707
  };
2708
2708
  //#endregion
2709
+ //#region src/runtime/publish-gate.ts
2710
+ var gates = /* @__PURE__ */ new WeakMap();
2711
+ /** Attach the gate to the facade the board hands out; never exposed through the package entry. */
2712
+ function registerPublishGate(bridge, gate) {
2713
+ gates.set(bridge, gate);
2714
+ }
2715
+ function publishGate(bridge) {
2716
+ const gate = gates.get(bridge);
2717
+ if (gate === void 0) throw new TypeError("Topic bridge was not created by a board");
2718
+ return gate;
2719
+ }
2720
+ //#endregion
2709
2721
  //#region src/polling.ts
2710
2722
  function isPollingInstruction(body, pc, pcName) {
2711
2723
  const definitions = /* @__PURE__ */ new Map();
@@ -3418,18 +3430,26 @@ var BoardRuntime = class {
3418
3430
  bindings: Object.freeze(this.instances.map((instance) => instance.bindingReport()))
3419
3431
  });
3420
3432
  }
3421
- validity(instance) {
3433
+ assertAlive(instance) {
3422
3434
  if (this.status === "disposed" || instance && !instance.valid) throw this.error("OBJECT_DISPOSED", "Object is disposed", instance);
3423
3435
  }
3424
- guard(instance, readOnly = false) {
3425
- this.validity(instance);
3436
+ /** Admission for a call that may change board state or is visible to a device. */
3437
+ assertCallAllowed(instance) {
3438
+ this.admit(instance, false);
3439
+ }
3440
+ /** Admission for a read-only entry point: a faulted board stays inspectable. */
3441
+ assertReadAllowed(instance) {
3442
+ this.admit(instance, true);
3443
+ }
3444
+ admit(instance, allowFaulted) {
3445
+ this.assertAlive(instance);
3426
3446
  if (instance && [
3427
3447
  "execute",
3428
3448
  "translate",
3429
3449
  "dispose",
3430
3450
  "assembly"
3431
3451
  ].includes(this.phase)) throw this.error("INVALID_CALL_STATE", "Operation is not allowed in this phase", instance);
3432
- if (!readOnly && this.status === "faulted") throw this.error("BOARD_FAULTED", "Board is faulted", instance);
3452
+ if (!allowFaulted && this.status === "faulted") throw this.error("BOARD_FAULTED", "Board is faulted", instance);
3433
3453
  }
3434
3454
  error(code, message, instance, cause, phase) {
3435
3455
  const electrical = cause instanceof ElectricalError ? cause : void 0;
@@ -3495,13 +3515,9 @@ var BoardRuntime = class {
3495
3515
  this.fault(this.error("REPORTED_ERROR", cause instanceof Error ? cause.message : String(cause), instance, cause, "report"));
3496
3516
  }
3497
3517
  topicBridge(owner) {
3498
- const hostListenerCount = () => this.hostListeners;
3499
- return Object.freeze({
3500
- get hostListenerCount() {
3501
- return hostListenerCount();
3502
- },
3518
+ const facade = Object.freeze({
3503
3519
  onDidReceive: ((listener, thisArgs, disposables) => {
3504
- this.guard(owner);
3520
+ this.assertCallAllowed(owner);
3505
3521
  const entry = {
3506
3522
  ...owner ? { owner } : {},
3507
3523
  call: (message) => listener.call(thisArgs, message)
@@ -3519,9 +3535,8 @@ var BoardRuntime = class {
3519
3535
  owner?.resources.push(disposable);
3520
3536
  return disposable;
3521
3537
  }),
3522
- assertCanPublish: () => this.guard(owner),
3523
3538
  publish: (topic, payload) => {
3524
- this.guard(owner);
3539
+ this.assertCallAllowed(owner);
3525
3540
  if (typeof topic !== "string") throw new TypeError("Topic must be a string");
3526
3541
  this.dispatchDepth++;
3527
3542
  try {
@@ -3544,6 +3559,11 @@ var BoardRuntime = class {
3544
3559
  }
3545
3560
  }
3546
3561
  });
3562
+ registerPublishGate(facade, {
3563
+ assertPublishAllowed: () => this.assertCallAllowed(owner),
3564
+ hostListeners: () => this.hostListeners
3565
+ });
3566
+ return facade;
3547
3567
  }
3548
3568
  enqueue(process) {
3549
3569
  if (process.state === "idle") {
@@ -3861,7 +3881,7 @@ var BoardRuntime = class {
3861
3881
  }
3862
3882
  }
3863
3883
  runUntil(target) {
3864
- this.validity();
3884
+ this.assertAlive();
3865
3885
  if (this.phase !== "idle" || this.dispatchDepth) throw this.error("INVALID_CALL_STATE", "Board is busy");
3866
3886
  if (this.status === "faulted") throw this.error("BOARD_FAULTED", "Board is faulted");
3867
3887
  const end = time(target);
@@ -4385,16 +4405,15 @@ function buildLoadedRuntime(prepared, publishObserved) {
4385
4405
  *
4386
4406
  * `publishObserved` reports whether anything outside the board consumes published messages; with no
4387
4407
  * scoped subscriber, no owner-less bridge listener and no consumer, an accepted output is dropped
4388
- * before it is copied, fanned out or queued, while `assertCanPublish` keeps the publish guards.
4408
+ * before it is copied, fanned out or queued, while the bridge's internal publish gate keeps the
4409
+ * board's call admission.
4389
4410
  */
4390
4411
  function scopeTopics(bridge, instance, outputs, diagnostics, publishObserved) {
4391
4412
  const prefix = `${encodeURIComponent(instance.symbol)}/`;
4413
+ const gate = publishGate(bridge);
4392
4414
  let scopedListeners = 0;
4393
- const unobserved = () => scopedListeners === 0 && bridge.hostListenerCount === 0 && !publishObserved();
4415
+ const unobserved = () => scopedListeners === 0 && gate.hostListeners() === 0 && !publishObserved();
4394
4416
  return {
4395
- get hostListenerCount() {
4396
- return bridge.hostListenerCount;
4397
- },
4398
4417
  onDidReceive: ((listener, thisArgs, disposables) => {
4399
4418
  const subscription = bridge.onDidReceive((message) => {
4400
4419
  if (!message.topic.startsWith(prefix)) return;
@@ -4417,7 +4436,6 @@ function scopeTopics(bridge, instance, outputs, diagnostics, publishObserved) {
4417
4436
  disposables?.push(disposable);
4418
4437
  return disposable;
4419
4438
  }),
4420
- assertCanPublish: () => bridge.assertCanPublish(),
4421
4439
  publish(port, payload) {
4422
4440
  const topic = instance.device.topics.get(port);
4423
4441
  if (topic?.direction !== "output" || !payloadMatches(topic.type, payload)) {
@@ -4431,7 +4449,7 @@ function scopeTopics(bridge, instance, outputs, diagnostics, publishObserved) {
4431
4449
  return;
4432
4450
  }
4433
4451
  if (unobserved()) {
4434
- bridge.assertCanPublish();
4452
+ gate.assertPublishAllowed();
4435
4453
  return;
4436
4454
  }
4437
4455
  const snapshot = copyPayload(payload);