@tinyactors/wasm 0.3.0 → 0.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/lib/direct.mjs CHANGED
@@ -3,10 +3,12 @@
3
3
  // in JavaScript over the module (docs/browser.md). The convenience layer
4
4
  // (lib/convenience.mjs) imports it as `#direct` under the export condition
5
5
  // `tinyactors-wasm`, and the package's entries (index.mjs, init.mjs) export
6
- // both. Nothing here touches the module before a call: an entry loads it.
6
+ // what that layer exports: the direct Host and Send are its. Nothing here touches the module before a call: an entry loads it.
7
7
  export { Address } from './address.mjs';
8
8
  export { compileCharts, compileSystem } from './compile.mjs';
9
- export { CompileError, ResolveError, TinyactorsError } from './errors.mjs';
10
- export { Host, SimulatedClock } from './host.mjs';
11
- export { encodeHTTPPost, parseHTTPRequest } from './http.mjs';
9
+ export {
10
+ CompileError, InvalidError, ResolveError, StaleError, StateError, TinyactorsError, UnsupportedError,
11
+ } from './errors.mjs';
12
+ export { Host, Send, SimulatedClock } from './host.mjs';
13
+ export { decodeTraces, encodeHTTPPost, parseHTTPRequest } from './http.mjs';
12
14
  export { decode, encode } from './values.mjs';
package/lib/errors.mjs CHANGED
@@ -3,9 +3,20 @@
3
3
  // `status` (core's name of a status, ta_status_name), each undefined when
4
4
  // not given; CompileError and ResolveError, its subclasses. A poisoned
5
5
  // module's error carries what poisoned it as its `cause`.
6
+ // One subclass per status an application branches on (StaleError,
7
+ // StateError, InvalidError, UnsupportedError): `new TinyactorsError(message,
8
+ // {status})` makes the subclass of that status itself, and a typed method's
9
+ // error the subclass of its code (-32001 stale, -32002 state, any other
10
+ // invalid), so no call that throws chooses a class.
11
+ let byStatus, byCode;
12
+
6
13
  export class TinyactorsError extends Error {
7
14
  constructor(message, options) {
8
15
  const given = options ?? {};
16
+ if (new.target === TinyactorsError) {
17
+ const subclass = byStatus[given.status] ?? (given.code === undefined ? undefined : byCode(given.code));
18
+ if (subclass !== undefined) return new subclass(message, options);
19
+ }
9
20
  super(message, given.cause !== undefined ? { cause: given.cause } : undefined);
10
21
  Object.defineProperties(this, {
11
22
  code: { value: given.code, enumerable: true },
@@ -18,6 +29,23 @@ export class TinyactorsError extends Error {
18
29
  get name() { return 'TinyactorsError'; }
19
30
  }
20
31
 
32
+ const of = (name, status) => ({ [name]: class extends TinyactorsError {
33
+ constructor(message, options) {
34
+ const given = options ?? {};
35
+ super(message, given.code === undefined ? { ...given, status: given.status ?? status } : given);
36
+ }
37
+
38
+ get name() { return name; }
39
+ } })[name];
40
+
41
+ export const StaleError = of('StaleError', 'TA_ERROR_STALE');
42
+ export const StateError = of('StateError', 'TA_ERROR_STATE');
43
+ export const InvalidError = of('InvalidError', 'TA_ERROR_INVALID');
44
+ export const UnsupportedError = of('UnsupportedError', 'TA_ERROR_UNSUPPORTED');
45
+ byStatus = { __proto__: null, TA_ERROR_STALE: StaleError, TA_ERROR_STATE: StateError,
46
+ TA_ERROR_INVALID: InvalidError, TA_ERROR_UNSUPPORTED: UnsupportedError };
47
+ byCode = (code) => code === -32001 ? StaleError : code === -32002 ? StateError : InvalidError;
48
+
21
49
  // A compilation with an error: every diagnostic, the message the first
22
50
  // error's as a compiler prints it.
23
51
  export class CompileError extends TinyactorsError {
package/lib/host.mjs CHANGED
@@ -32,7 +32,7 @@ const FIELD = {
32
32
  TARGET: 32, EVENT: 33, SENDID: 34, AUXILIARY: 35, TRACEPARENT: 36, ORIGIN: 37, WIRE: 64,
33
33
  };
34
34
  const EFFECT = {
35
- SEND: 1, INVOKE: 3, UNINVOKE: 4, LOG: 5, WAKE: 8,
35
+ SEND: 1, INVOKE: 3, UNINVOKE: 4, LOG: 5, DONE: 6, ERROR: 7, WAKE: 8,
36
36
  MIGRATED: 16, MIGRATION_DONE: 17, REPLY: 23,
37
37
  };
38
38
  const FLAG_RESTORED = 2;
@@ -65,8 +65,9 @@ const POOLS = [
65
65
  ];
66
66
 
67
67
  const KNOWN_OPTIONS = ['ioProcessors', 'transport', 'log', 'clock', 'telemetry', 'wake', 'migrated', 'migrationDone',
68
- 'services', 'datamodel', 'quickjs'];
69
- const HANDLER_FUNCTIONS = ['log', 'transport', 'telemetry', 'wake', 'migrated', 'migrationDone'];
68
+ 'services', 'datamodel', 'quickjs', 'done', 'error', 'handlerError', 'idle'];
69
+ const HANDLER_FUNCTIONS = ['log', 'transport', 'telemetry', 'wake', 'migrated', 'migrationDone', 'done', 'error', 'handlerError',
70
+ 'idle'];
70
71
  const HANDLER_MAPS = ['ioProcessors', 'services'];
71
72
 
72
73
  // The handlers' key on a Host: not enumerable, not configurable; writable,
@@ -74,6 +75,8 @@ const HANDLER_MAPS = ['ioProcessors', 'services'];
74
75
  const HANDLERS = Symbol('tinyactors.handlers');
75
76
  // What only the layer passes the constructors of views and records.
76
77
  const MAKING = Symbol('making');
78
+ // The mark of an ask's row among the pending typed methods' handlers.
79
+ const ASK = Symbol('tinyactors.ask');
77
80
 
78
81
  // ---- the event loop's macrotasks ---------------------------------------------------------
79
82
 
@@ -159,10 +162,33 @@ class HostState {
159
162
 
160
163
  // ---- errors ----
161
164
 
162
- keep(error) {
165
+ // The first error kept for the turn.
166
+ stash(error) {
163
167
  if (this.error === null) this.error = [error];
164
168
  }
165
169
 
170
+ // An error for the turn: the handlerError hook's when there is one.
171
+ keep(error) {
172
+ this.failed(error, undefined);
173
+ }
174
+
175
+ // A handler's error: the handlerError hook's (the application's
176
+ // onHandlerError), called with (error, effect), or else kept for the
177
+ // turn; the hook's own throw is kept. Answers whether a hook took it.
178
+ failed(error, effect) {
179
+ const hook = this.handlers()?.handlerError;
180
+ if (typeof hook !== 'function') {
181
+ this.stash(error);
182
+ return false;
183
+ }
184
+ try {
185
+ hook(error, effect);
186
+ } catch (thrown) {
187
+ this.stash(thrown);
188
+ }
189
+ return true;
190
+ }
191
+
166
192
  takeKept() {
167
193
  const kept = this.error;
168
194
  this.error = null;
@@ -213,6 +239,8 @@ class HostState {
213
239
  case EFFECT.INVOKE: this.dispatchInvoke(index); break;
214
240
  case EFFECT.UNINVOKE: this.dispatchUninvoke(index); break;
215
241
  case EFFECT.REPLY: this.dispatchReply(index, extra); break;
242
+ case EFFECT.DONE:
243
+ case EFFECT.ERROR: this.dispatchEnd(index); break;
216
244
  default: break;
217
245
  }
218
246
  return TA_OK;
@@ -304,7 +332,8 @@ class HostState {
304
332
  if (typeof then !== 'function') return;
305
333
  then.call(result, () => this.settled(), (reason) => {
306
334
  this.settled();
307
- throw reason;
335
+ if (typeof this.handlers()?.handlerError === 'function') this.failed(reason, undefined); // the hook's
336
+ else throw reason;
308
337
  });
309
338
  } catch {
310
339
  return; // not followed: idle() does not wait for it
@@ -371,8 +400,9 @@ class HostState {
371
400
  next = this.turn();
372
401
  } catch (thrown) {
373
402
  next = NEXT.FAULT;
374
- this.keep(thrown); // the module stopped (a trap): its error
403
+ this.stash(thrown); // the module stopped (a trap): its error
375
404
  }
405
+ if (next === NEXT.IDLE && !this.closing && this.open) this.idleHook();
376
406
  error = this.turnError(next);
377
407
  let settle = false;
378
408
  if (next === NEXT.YIELD && !this.closing) {
@@ -384,7 +414,8 @@ class HostState {
384
414
  const host = this.host();
385
415
  if (delay >= 0 && host !== undefined) this.timer = setTimeout(timerOf, Math.min(delay, TIMEOUT_MAX), host);
386
416
  }
387
- settle = next !== NEXT.IDLE || this.promises === 0;
417
+ // idle, no handler's promise pending, and no input the idle hook made
418
+ settle = next !== NEXT.IDLE || (this.promises === 0 && this.immediate === null);
388
419
  }
389
420
  if (error === null) error = this.takeKept();
390
421
  // a close a handler asked for runs at the leave: the waiter is kept
@@ -452,18 +483,42 @@ class HostState {
452
483
 
453
484
  // ---- dispatch ----
454
485
 
455
- // A handler's call: its error kept for the turn, its promise followed.
456
- invoke(handler, args) {
486
+ // A handler's call: its error the hook's or kept for the turn, its promise followed.
487
+ invoke(handler, args, effect) {
457
488
  let result;
458
489
  try {
459
490
  result = handler(...args);
460
491
  } catch (error) {
461
- this.keep(error);
492
+ this.failed(error, effect);
462
493
  return;
463
494
  }
464
495
  this.follow(result);
465
496
  }
466
497
 
498
+ // A turn went idle: the idle hook (the application layer's bound of
499
+ // resident actors), outside any batch; a promise it returns is followed.
500
+ idleHook() {
501
+ const hook = this.handlers()?.idle;
502
+ if (typeof hook === 'function') this.invoke(hook, []);
503
+ }
504
+
505
+ // A DONE (its donedata) or an ERROR (a fault, FLAGS its code): a
506
+ // top-level actor ended; the hook done(session, donedata) or
507
+ // error(session, fault) gets it.
508
+ dispatchEnd(index) {
509
+ const done = this.word(index, FIELD.KIND) === EFFECT.DONE;
510
+ const handler = this.handlers()?.[done ? 'done' : 'error'];
511
+ if (typeof handler !== 'function') return;
512
+ let value;
513
+ try {
514
+ value = done ? this.data(index) : this.word(index, FIELD.FLAGS);
515
+ } catch (error) {
516
+ this.keep(error);
517
+ return;
518
+ }
519
+ this.invoke(handler, [this.number(index, FIELD.SESSION), value]);
520
+ }
521
+
467
522
  dispatchSend(index) {
468
523
  const processor = this.word(index, FIELD.IO_PROCESSOR);
469
524
  if (processor === NIL) {
@@ -475,8 +530,20 @@ class HostState {
475
530
  const handlers = this.handlers();
476
531
  if (host === undefined || handlers === undefined) return;
477
532
  const view = new Send(MAKING, this, index, processor);
533
+ // the transport is a function, an I/O processor an object with receive(send)
478
534
  const handler = handlers.processors?.[processor];
479
- if (typeof handler !== 'function') { // no handler: a failed completion
535
+ let receiver, receive = handler;
536
+ if (handler !== null && typeof handler === 'object') {
537
+ receiver = handler;
538
+ try {
539
+ receive = handler.receive;
540
+ } catch (error) {
541
+ failUnanswered(view);
542
+ this.failed(error, view);
543
+ return;
544
+ }
545
+ }
546
+ if (typeof receive !== 'function') { // no handler: a failed completion
480
547
  try {
481
548
  replyTo(view, FAILURE);
482
549
  } catch (error) {
@@ -485,31 +552,33 @@ class HostState {
485
552
  return;
486
553
  }
487
554
  try {
488
- const result = handler(host, view);
555
+ const result = receive.call(receiver, view);
489
556
  followSend(this, view, result);
490
557
  } catch (error) {
491
558
  failUnanswered(view);
492
- this.keep(error);
559
+ this.failed(error, view);
493
560
  }
494
561
  }
495
562
 
496
563
  dispatchLog(index) {
497
564
  const handler = this.handlers()?.log;
498
565
  if (typeof handler !== 'function') return;
499
- this.invoke(handler, [this.host(), new LogEntry(MAKING, this, index)]);
566
+ const entry = new LogEntry(MAKING, this, index);
567
+ this.invoke(handler, [entry], entry);
500
568
  }
501
569
 
502
570
  dispatchMigrated(index) {
503
571
  const handler = this.handlers()?.migrated;
504
572
  if (typeof handler !== 'function') return;
505
- this.invoke(handler, [this.host(), new Migrated(MAKING, this, index)]);
573
+ const migrated = new Migrated(MAKING, this, index);
574
+ this.invoke(handler, [migrated], migrated);
506
575
  }
507
576
 
508
577
  dispatchWake(index) {
509
578
  const handler = this.handlers()?.wake;
510
579
  if (typeof handler !== 'function') return;
511
580
  const migration = this.number(index, FIELD.MIGRATION);
512
- this.invoke(handler, [this.host(), this.number(index, FIELD.SESSION), migration === 0n ? undefined : Number(migration)]);
581
+ this.invoke(handler, [this.number(index, FIELD.SESSION), migration === 0n ? undefined : Number(migration)]);
513
582
  }
514
583
 
515
584
  // {migration, migrated, failed, pending}, read now (valid until the
@@ -550,7 +619,7 @@ class HostState {
550
619
  this.keep(error);
551
620
  return;
552
621
  }
553
- this.invoke(handler, [this.host(), result]);
622
+ this.invoke(handler, [result]);
554
623
  }
555
624
 
556
625
  // The telemetry handler gets the request as a Uint8Array it keeps; in
@@ -560,9 +629,9 @@ class HostState {
560
629
  if (typeof handler !== 'function') return;
561
630
  let result;
562
631
  try {
563
- result = handler(this.host(), signal, bytes);
632
+ result = handler(signal, bytes);
564
633
  } catch (error) {
565
- this.keep(error);
634
+ this.failed(error, undefined);
566
635
  return;
567
636
  }
568
637
  if (this.endings === null) this.follow(result);
@@ -605,10 +674,10 @@ class HostState {
605
674
  this.keep(error);
606
675
  return;
607
676
  }
608
- this.invoke(onError, [this.host(), error]);
677
+ this.invoke(onError, [error]);
609
678
  return;
610
679
  }
611
- if (typeof onResult === 'function') this.invoke(onResult, [this.host(), reply.result]);
680
+ if (typeof onResult === 'function') this.invoke(onResult, [reply.result]);
612
681
  }
613
682
 
614
683
  // ---- invoked services ----
@@ -631,7 +700,7 @@ class HostState {
631
700
  let started = false, result, thrown = null;
632
701
  if (typeof handler === 'function') {
633
702
  try {
634
- result = handler(this.host(), record);
703
+ result = handler(record);
635
704
  const then = result !== null && (typeof result === 'object' || typeof result === 'function') ? result.then : undefined;
636
705
  if (typeof then === 'function') {
637
706
  startInvocation(state, result);
@@ -644,7 +713,7 @@ class HostState {
644
713
  }
645
714
  if (!started) { // it does not run: kept as failed, so sends to it fail too, until the UNINVOKE
646
715
  failInvocation(state, { sendid: undefined, tell: !this.uninvokedInBatch(index) });
647
- if (thrown !== null) this.keep(thrown[0]);
716
+ if (thrown !== null) this.failed(thrown[0], record);
648
717
  return;
649
718
  }
650
719
  if (state.flags & INVOCATION.ENDED) return; // done() in the handler: nothing to keep
@@ -720,7 +789,7 @@ class HostState {
720
789
  if (failed || starting) return;
721
790
  try {
722
791
  const stop = serviceMethod(service, 'stop');
723
- if (stop !== null) stop.call(service, this.host());
792
+ if (stop !== null) stop.call(service);
724
793
  } catch (error) {
725
794
  this.keep(error);
726
795
  }
@@ -795,6 +864,7 @@ class Send {
795
864
  get type() { return sendField(viewOf(this), 'type'); }
796
865
  get origin() { return sendField(viewOf(this), 'origin'); }
797
866
  get traceparent() { return sendField(viewOf(this), 'traceparent'); }
867
+ get host() { return viewOf(this).state.host(); }
798
868
 
799
869
  // The send's outgoing half, the object host.send(message) takes: a text
800
870
  // field the send lacks is null.
@@ -885,7 +955,9 @@ function followSend(state, send, result) {
885
955
  materialize(send);
886
956
  const chained = then.call(result, undefined, (reason) => {
887
957
  failUnanswered(send);
888
- throw reason;
958
+ // with a handlerError hook the rejection is the hook's, and the chain goes on
959
+ if (typeof state.handlers()?.handlerError === 'function') state.failed(reason, send);
960
+ else throw reason;
889
961
  });
890
962
  state.follow(chained);
891
963
  }
@@ -941,7 +1013,7 @@ function failUnanswered(send) {
941
1013
  }
942
1014
  }
943
1015
 
944
- let entryOf;
1016
+ let entryOf, entryHost;
945
1017
 
946
1018
  // A <log> line: (session, label, text), a thin view until the ack.
947
1019
  class LogEntry {
@@ -953,6 +1025,10 @@ class LogEntry {
953
1025
  }
954
1026
 
955
1027
  static {
1028
+ entryHost = (self) => {
1029
+ if (self === null || typeof self !== 'object' || !(#view in self)) throw new TypeError('not a view of an effect');
1030
+ return self.#view.state.host();
1031
+ };
956
1032
  entryOf = (self, name, read) => {
957
1033
  if (self === null || typeof self !== 'object' || !(#view in self)) throw new TypeError('not a view of an effect');
958
1034
  return cachedField(self.#view, name, read);
@@ -962,6 +1038,7 @@ class LogEntry {
962
1038
  get session() { return entryOf(this, 'session', (s, i) => s.number(i, FIELD.SESSION)); }
963
1039
  get label() { return entryOf(this, 'label', (s, i) => s.text(i, FIELD.AUXILIARY)); }
964
1040
  get text() { return entryOf(this, 'text', (s, i) => s.text(i, FIELD.EVENT)); }
1041
+ get host() { return entryHost(this); }
965
1042
  }
966
1043
 
967
1044
  function cachedField(view, name, read) {
@@ -972,7 +1049,7 @@ function cachedField(view, name, read) {
972
1049
  return value;
973
1050
  }
974
1051
 
975
- let migratedOf;
1052
+ let migratedOf, migratedHost;
976
1053
 
977
1054
  // An actor moved by a migration: a thin view until the ack.
978
1055
  class Migrated {
@@ -984,6 +1061,10 @@ class Migrated {
984
1061
  }
985
1062
 
986
1063
  static {
1064
+ migratedHost = (self) => {
1065
+ if (self === null || typeof self !== 'object' || !(#view in self)) throw new TypeError('not a view of an effect');
1066
+ return self.#view.state.host();
1067
+ };
987
1068
  migratedOf = (self, name, read) => {
988
1069
  if (self === null || typeof self !== 'object' || !(#view in self)) throw new TypeError('not a view of an effect');
989
1070
  return cachedField(self.#view, name, read);
@@ -994,6 +1075,7 @@ class Migrated {
994
1075
  get segment() { return migratedOf(this, 'segment', (s, i) => s.word(i, FIELD.SEGMENT)); }
995
1076
  get chart() { return migratedOf(this, 'chart', (s, i) => s.text(i, FIELD.EVENT)); }
996
1077
  get migration() { return migratedOf(this, 'migration', (s, i) => s.word(i, FIELD.MIGRATION)); }
1078
+ get host() { return migratedHost(this); }
997
1079
  }
998
1080
 
999
1081
  // ---- invoked services --------------------------------------------------------------------------
@@ -1055,6 +1137,7 @@ class Invocation {
1055
1137
  get data() { return invocationState(this).data; }
1056
1138
  get traceparent() { return invocationState(this).traceparent; }
1057
1139
  get restored() { return (invocationState(this).flags & INVOCATION.RESTORED) !== 0; }
1140
+ get host() { return invocationState(this).host.host(); }
1058
1141
 
1059
1142
  get running() {
1060
1143
  const state = invocationState(this);
@@ -1156,7 +1239,7 @@ function serviceMethod(service, name) {
1156
1239
  function serviceReceive(state, service, receive, send) {
1157
1240
  let result;
1158
1241
  try {
1159
- result = receive.call(service, state.host.host(), send);
1242
+ result = receive.call(service, send);
1160
1243
  const then = result !== null && (typeof result === 'object' || typeof result === 'function') ? result.then : undefined;
1161
1244
  if (typeof then === 'function') {
1162
1245
  materialize(send);
@@ -1206,7 +1289,7 @@ function startedInvocation(state, service) {
1206
1289
  if (!host.open) return; // closed: dropped, not started
1207
1290
  if (state.flags & INVOCATION.UNINVOKED) { // cancelled while it started: stopped now
1208
1291
  const stop = serviceMethod(service, 'stop');
1209
- if (stop !== null) stop.call(service, host.host());
1292
+ if (stop !== null) stop.call(service);
1210
1293
  return;
1211
1294
  }
1212
1295
  if (state.flags & INVOCATION.ENDED) return; // done in its start
@@ -1254,6 +1337,8 @@ let clockOf;
1254
1337
  // A clock in milliseconds (nanoseconds inside, exact) that moves only with
1255
1338
  // increment(ms), which schedules a turn on each host made with it; such a
1256
1339
  // host arms no timer. It holds the hosts weakly.
1340
+ export { Send };
1341
+
1257
1342
  export class SimulatedClock {
1258
1343
  #clock = { ns: 0n, hosts: [] };
1259
1344
 
@@ -1303,9 +1388,14 @@ function readHandlers(options) {
1303
1388
  for (const name of HANDLER_MAPS) {
1304
1389
  const value = options?.[name];
1305
1390
  if (value === undefined) continue;
1306
- if (value === null || typeof value !== 'object') throw new TypeError('ioProcessors and services are objects of a type -> its handler');
1391
+ const usage = name === 'ioProcessors'
1392
+ ? 'ioProcessors is an object of a type -> its processor, an object with receive(send)'
1393
+ : 'services is an object of a type -> its handler, a function';
1394
+ if (value === null || typeof value !== 'object') throw new TypeError(usage);
1307
1395
  for (const key of Object.keys(value)) {
1308
- if (typeof value[key] !== 'function') throw new TypeError('ioProcessors and services are objects of a type -> its handler');
1396
+ const item = value[key];
1397
+ const ok = name === 'ioProcessors' ? item !== null && typeof item === 'object' && typeof item.receive === 'function' : typeof item === 'function';
1398
+ if (!ok) throw new TypeError(usage);
1309
1399
  }
1310
1400
  handlers[name] = value;
1311
1401
  }
@@ -1673,6 +1763,73 @@ export class Host {
1673
1763
  });
1674
1764
  }
1675
1765
 
1766
+ // whether a live segment runs an image of these bytes: the application
1767
+ // layer adds a restored host only the images its snapshot lacks
1768
+ holdsImage(image) {
1769
+ const state = checked(this, true);
1770
+ return state.call(() => {
1771
+ const bytes = bytesOf(image, 'holdsImage takes the image\'s bytes: a Uint8Array or an ArrayBuffer');
1772
+ const module = state.module;
1773
+ const [at, length] = module.copyIn(state.scratch, bytes);
1774
+ module.setWord(0, at);
1775
+ module.setWord(1, length);
1776
+ return state.fn.ta_web_host_holds_image(state.pointer) !== 0;
1777
+ });
1778
+ }
1779
+
1780
+ // the actors in memory now: the application layer's bound of resident actors
1781
+ residentCount() {
1782
+ const state = checked(this, true);
1783
+ return state.call(() => {
1784
+ state.fn.ta_web_host_resident(state.pointer);
1785
+ return Number(state.module.word64(0));
1786
+ });
1787
+ }
1788
+
1789
+ // a live actor's {chart, hibernated}, or undefined
1790
+ actorInfo(session) {
1791
+ const id = sessionOf(session);
1792
+ const state = checked(this, true);
1793
+ return state.call(() => {
1794
+ const answer = state.fn.ta_web_host_actor_info(state.pointer, Number(id & 0xFFFFFFFFn), Number(id >> 32n));
1795
+ if (answer < 0) return undefined;
1796
+ const module = state.module;
1797
+ return { chart: module.string(module.word(0), module.word(1)), hibernated: answer === 1 };
1798
+ });
1799
+ }
1800
+
1801
+ // an ask's row in the pending table, its settle function kept at the
1802
+ // row's slot; answers the row's handle (Actor.ask's reply address)
1803
+ askOpen(settle) {
1804
+ const state = checked(this, true);
1805
+ if (typeof settle !== 'function') throw new TypeError('askOpen takes the ask\'s settle function');
1806
+ return state.call(() => {
1807
+ const handlers = state.handlers();
1808
+ const slot = state.fn.ta_web_pending_add(state.pointer);
1809
+ if (slot === 0) throw refusal('TA_ERROR_NO_MEMORY', 'no memory for an ask');
1810
+ const id = state.module.word64(0);
1811
+ handlers.pending ??= [];
1812
+ handlers.pending[2 * (slot - 1)] = settle;
1813
+ handlers.pending[2 * (slot - 1) + 1] = ASK;
1814
+ return id;
1815
+ });
1816
+ }
1817
+
1818
+ // the ask's row removed and its settle function given back; undefined
1819
+ // for a handle that names no ask
1820
+ askClose(handle) {
1821
+ const state = checked(this, false);
1822
+ if (!state.open || typeof handle !== 'bigint' || handle < 1n || handle >= 1n << 64n) return undefined;
1823
+ const handlers = state.handlers();
1824
+ const at = 2 * (Number(handle & 0xFFFFFFFFn) - 1);
1825
+ if (handlers === undefined || !Array.isArray(handlers.pending) || handlers.pending[at + 1] !== ASK) return undefined;
1826
+ if (state.fn.ta_web_pending_take(state.pointer, Number(handle & 0xFFFFFFFFn), Number(handle >> 32n)) === 0) return undefined;
1827
+ const settle = handlers.pending[at];
1828
+ handlers.pending[at] = undefined;
1829
+ handlers.pending[at + 1] = undefined;
1830
+ return settle;
1831
+ }
1832
+
1676
1833
  actorByName(name) {
1677
1834
  const state = checked(this, true);
1678
1835
  return state.call(() => {
@@ -1980,8 +2137,8 @@ function eventOf(state, message) {
1980
2137
  function targetOf(state, target) {
1981
2138
  const value = isAddress(target) ? target.text : target;
1982
2139
  if (typeof value !== 'string') {
1983
- if (typeof value === 'bigint' || typeof value === 'number') return sessionOf(value);
1984
- throw new TypeError('a target is a session (a bigint or a safe integer) or a local address (a string or an Address)');
2140
+ if (typeof value === 'bigint' || typeof value === 'number' || (value !== null && typeof value === 'object')) return sessionOf(value);
2141
+ throw new TypeError('a target is an Actor, a session (a bigint or a safe integer) or a local address (a string or an Address)');
1985
2142
  }
1986
2143
  const module = state.module;
1987
2144
  const [at, length] = module.text(state.scratch, value);
@@ -2019,7 +2176,7 @@ for (const [js, core] of METHODS) {
2019
2176
  const id = module.word64(0);
2020
2177
  const at = 2 * (slot - 1);
2021
2178
  // actor.get's timers[].due: the host's ticks as milliseconds on now()'s clock
2022
- handlers.pending[at] = core === 'actor.get' ? (host, result) => onResult(host, state.timesInMilliseconds(result)) : onResult;
2179
+ handlers.pending[at] = core === 'actor.get' ? (result) => onResult(state.timesInMilliseconds(result)) : onResult;
2023
2180
  handlers.pending[at + 1] = failed;
2024
2181
  let posted = false;
2025
2182
  try {
package/lib/http.mjs CHANGED
@@ -6,6 +6,7 @@
6
6
  // strings).
7
7
  import { bytesOf, optionalText, textOf } from './checks.mjs';
8
8
  import { refusal } from './errors.mjs';
9
+ import * as cbor from './cbor.mjs';
9
10
  import { instance } from './state.mjs';
10
11
 
11
12
  const utf8 = new TextDecoder('utf-8', { fatal: true });
@@ -52,6 +53,24 @@ function strict(module, pointer, length) {
52
53
  }
53
54
  }
54
55
 
56
+ // decodeTraces(bytes) -> spans: an OTLP ExportTraceServiceRequest's spans
57
+ // (the addon's src/traces.c, compiled into the module), read as CBOR.
58
+ export function decodeTraces(traces) {
59
+ const bytes = bytesOf(traces, 'decodeTraces takes a Uint8Array or an ArrayBuffer');
60
+ const module = instance();
61
+ const scratch = module.scratch;
62
+ return module.scoped(scratch, () => {
63
+ const [at, length] = module.copyIn(scratch, bytes);
64
+ module.setWord(0, at);
65
+ module.setWord(1, length);
66
+ const status = module.call('ta_web_traces_decode', module.env);
67
+ if (status !== 0) {
68
+ throw refusal(module.statusName(status), status === -3 ? 'the bytes are not an ExportTraceServiceRequest' : 'no memory for the spans');
69
+ }
70
+ return cbor.read(module, scratch, module.word(0), module.word(1), { addresses: false });
71
+ });
72
+ }
73
+
55
74
  // parseHTTPRequest(message) -> {method, target, body, event, fields}
56
75
  export function parseHTTPRequest(message) {
57
76
  const bytes = bytesOf(message, 'parseHTTPRequest takes the message\'s bytes: a Uint8Array or an ArrayBuffer');