@mikrojs/native 0.6.0-pr-70.gc88e298 → 0.6.0-pr-70.g7bdc6dd

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikrojs/native",
3
- "version": "0.6.0-pr-70.gc88e298",
3
+ "version": "0.6.0-pr-70.g7bdc6dd",
4
4
  "description": "Mikro.js C++ runtime library and Node.js native addon",
5
5
  "keywords": [
6
6
  "esp32",
@@ -78,7 +78,7 @@
78
78
  "cmake-js": "^8.0.0",
79
79
  "node-addon-api": "^8.7.0",
80
80
  "node-gyp-build": "^4.8.4",
81
- "@mikrojs/quickjs": "0.6.0-pr-70.gc88e298"
81
+ "@mikrojs/quickjs": "0.6.0-pr-70.g7bdc6dd"
82
82
  },
83
83
  "devDependencies": {
84
84
  "@swc/core": "^1.15.30",
@@ -1,18 +1,26 @@
1
- /* eslint-disable no-console */
2
- /* console.error inside dispatch matches the mik_call_handler precedent in
3
- * the C runtime (log + continue, isolated to that subscriber). */
4
-
5
1
  // Host-side shim for `native:observable`, used only in vitest (Node) where
6
2
  // the mikrojs C runtime isn't available. Keep in sync with mik_observable.cpp.
7
3
  //
8
4
  // Mirrors the locked design in .claude/plans/observable.md:
9
5
  // - subscribe() returns a Subscription with unsubscribe() (no AbortSignal)
10
- // - no error channel — throws caught + logged at the dispatch boundary,
11
- // isolated to that subscriber
6
+ // - no error channel — throws inside dispatch or teardown are caught at the
7
+ // boundary, isolated to the offending subscriber, and re-thrown
8
+ // asynchronously via setTimeout(0) so the synchronous producer keeps
9
+ // running but the bug eventually surfaces (and on device, halts the
10
+ // runtime via the existing unhandled-rejection path)
12
11
  // - sync emission allowed
13
12
  // - pipe-only composition (operators live in operators.ts)
14
13
  // - withEmitters() factory: {observable, next, complete}
15
14
 
15
+ /* Catch a thrown error and re-throw it on the next tick. The synchronous
16
+ * caller keeps going (other subscribers receive the value, remaining
17
+ * teardowns run); the error eventually surfaces as an uncaught exception. */
18
+ function panicAsync(err: unknown): void {
19
+ setTimeout(() => {
20
+ throw err
21
+ }, 0)
22
+ }
23
+
16
24
  class Subscriber<T> {
17
25
  closed = false
18
26
  private next_fn: ((v: T) => void) | undefined
@@ -38,7 +46,7 @@ class Subscriber<T> {
38
46
  try {
39
47
  this.next_fn(value)
40
48
  } catch (err) {
41
- console.error(err)
49
+ panicAsync(err)
42
50
  }
43
51
  }
44
52
  }
@@ -50,7 +58,7 @@ class Subscriber<T> {
50
58
  try {
51
59
  this.complete_fn()
52
60
  } catch (err) {
53
- console.error(err)
61
+ panicAsync(err)
54
62
  }
55
63
  }
56
64
  this.runTeardowns()
@@ -64,7 +72,7 @@ class Subscriber<T> {
64
72
  try {
65
73
  fn()
66
74
  } catch (err) {
67
- console.error(err)
75
+ panicAsync(err)
68
76
  }
69
77
  return
70
78
  }
@@ -85,7 +93,7 @@ class Subscriber<T> {
85
93
  try {
86
94
  list[i]!()
87
95
  } catch (err) {
88
- console.error(err)
96
+ panicAsync(err)
89
97
  }
90
98
  }
91
99
  }
@@ -1,8 +1,3 @@
1
- /* eslint-disable no-console */
2
- /* console.error inside operator dispatch is intentional: it matches the
3
- * existing mik_call_handler precedent in the C layer (log + continue rather
4
- * than panic). See .claude/plans/observable.md → Errors section. */
5
-
6
1
  /* Operators for `Observable.pipe(...)`. Each is a factory returning a
7
2
  * function `(source) => Observable`. Composition is pure pipe — no method
8
3
  * chaining on Observable itself.
@@ -12,6 +7,13 @@
12
7
  * Result-aware operators (`mapOk`, `filterOk`, ...) ship when a concrete
13
8
  * consumer asks. Today no module produces fallible event streams.
14
9
  *
10
+ * Errors: throws inside operator transforms or finalize callbacks are caught
11
+ * at the dispatch boundary, isolated to that subscriber, and re-thrown
12
+ * asynchronously via setTimeout(0). The synchronous producer keeps going
13
+ * (the bad value is dropped, sibling subscribers untouched), and on device
14
+ * the eventual uncaught throw halts the runtime via the existing
15
+ * unhandled-rejection path. Stream errors are panics.
16
+ *
15
17
  * See `.claude/plans/observable.md` for the full design.
16
18
  */
17
19
 
@@ -22,9 +24,18 @@
22
24
  * runtime/observable/types.ts. */
23
25
  import {Observable} from 'mikrojs/observable'
24
26
 
25
- /* Map values through a transform. Throws inside `fn` are caught at the
26
- * dispatch boundary; the bad value is dropped for that subscription, others
27
- * are unaffected. */
27
+ /* Catch a thrown error and re-throw it on the next tick. The synchronous
28
+ * caller keeps going; the error eventually surfaces as an uncaught
29
+ * exception. */
30
+ function panicAsync(err: unknown): void {
31
+ setTimeout(() => {
32
+ throw err
33
+ }, 0)
34
+ }
35
+
36
+ /* Map values through a transform. Throws inside `fn` are caught and
37
+ * scheduled to re-throw on the next tick (panic). The bad value is dropped
38
+ * for that subscription; sibling subscriptions are unaffected. */
28
39
  export const map =
29
40
  <A, B>(fn: (value: A) => B) =>
30
41
  (source: Observable<A>): Observable<B> =>
@@ -35,7 +46,7 @@ export const map =
35
46
  try {
36
47
  next = fn(value)
37
48
  } catch (err) {
38
- console.error(err)
49
+ panicAsync(err)
39
50
  return
40
51
  }
41
52
  sub.next(next)
@@ -45,7 +56,7 @@ export const map =
45
56
  sub.addTeardown(() => upstream.unsubscribe())
46
57
  })
47
58
 
48
- /* Pass through values matching `pred`. `pred` errors are caught + logged. */
59
+ /* Pass through values matching `pred`. `pred` errors panic asynchronously. */
49
60
  export const filter =
50
61
  <A>(pred: (value: A) => boolean) =>
51
62
  (source: Observable<A>): Observable<A> =>
@@ -56,7 +67,7 @@ export const filter =
56
67
  try {
57
68
  keep = pred(value)
58
69
  } catch (err) {
59
- console.error(err)
70
+ panicAsync(err)
60
71
  return
61
72
  }
62
73
  if (keep) sub.next(value)
@@ -108,8 +119,8 @@ export const takeUntil =
108
119
  })
109
120
 
110
121
  /* Run `fn` when the subscription ends for any reason (unsubscribe or
111
- * natural completion). Throws inside `fn` are caught + logged so other
112
- * teardowns still run. RxJS naming. */
122
+ * natural completion). Throws inside `fn` are caught and panic
123
+ * asynchronously, so subsequent teardowns still run. RxJS naming. */
113
124
  export const finalize =
114
125
  (fn: () => void) =>
115
126
  <A>(source: Observable<A>): Observable<A> =>
@@ -123,7 +134,7 @@ export const finalize =
123
134
  try {
124
135
  fn()
125
136
  } catch (err) {
126
- console.error(err)
137
+ panicAsync(err)
127
138
  }
128
139
  })
129
140
  })
@@ -9,11 +9,17 @@
9
9
  * addTeardown, closed
10
10
  * - Subscription: unsubscribe
11
11
  *
12
- * Error semantics: throws inside observer/operator callbacks are caught at
13
- * the dispatch boundary and logged via mik_dump_error (matching the existing
14
- * mik_call_handler precedent). Producer-setup throws inside the subscribe
15
- * callback bubble up to the .subscribe() caller — that's a bug in the
16
- * producer, not a runtime event.
12
+ * Error semantics: throws inside observer or operator callbacks (and inside
13
+ * teardown callbacks) are caught at the dispatch boundary, isolated to the
14
+ * offending subscriber, and re-thrown asynchronously via setTimeout(0). The
15
+ * synchronous producer keeps running (sibling subscribers receive the value,
16
+ * remaining teardowns run); the bug eventually surfaces as an uncaught
17
+ * exception, which the runtime treats as fatal via the existing
18
+ * unhandled-rejection halt mechanism. Stream errors are panics.
19
+ *
20
+ * Producer-setup throws inside the subscribe callback bubble synchronously
21
+ * to the .subscribe() caller — that's a bug in the producer factory itself,
22
+ * not a runtime dispatch event.
17
23
  */
18
24
 
19
25
  #include <cstddef>
@@ -55,17 +61,87 @@ struct SubscriptionData {
55
61
 
56
62
  /* ── Helpers ─────────────────────────────────────────────────────── */
57
63
 
64
+ /* Re-throws the captured exception from func_data[0]. Used as the
65
+ * `setTimeout(callback, 0)` payload in panic_async. */
66
+ static JSValue throw_captured(JSContext* ctx, JSValueConst this_val, int argc,
67
+ JSValueConst* argv, int magic, JSValue* func_data) {
68
+ (void)this_val;
69
+ (void)argc;
70
+ (void)argv;
71
+ (void)magic;
72
+ return JS_Throw(ctx, JS_DupValue(ctx, func_data[0]));
73
+ }
74
+
75
+ /* Catch a thrown error and re-throw it on the next event-loop tick via the
76
+ * runtime's setTimeout. The synchronous caller keeps going (sibling
77
+ * subscribers receive the value, remaining teardowns run); the eventual
78
+ * uncaught throw halts the runtime via the existing unhandled-rejection
79
+ * path. Stream errors are panics.
80
+ *
81
+ * Takes ownership of `exception` — caller must not free after this call. */
82
+ static void panic_async(JSContext* ctx, JSValue exception) {
83
+ JSValue global = JS_GetGlobalObject(ctx);
84
+ JSValue setTimeout = JS_GetPropertyStr(ctx, global, "setTimeout");
85
+ JS_FreeValue(ctx, global);
86
+ if (!JS_IsFunction(ctx, setTimeout)) {
87
+ /* setTimeout missing or not a function — should not happen in the
88
+ * mikrojs runtime since it's globally defined. Drop on the floor
89
+ * rather than disrupting the dispatch path. */
90
+ JS_FreeValue(ctx, setTimeout);
91
+ JS_FreeValue(ctx, exception);
92
+ return;
93
+ }
94
+ JSValueConst data[1] = {exception};
95
+ JSValue thrower = JS_NewCFunctionData(ctx, throw_captured, 0, 0, 1, data);
96
+ JS_FreeValue(ctx, exception);
97
+ if (JS_IsException(thrower)) {
98
+ JS_FreeValue(ctx, setTimeout);
99
+ return;
100
+ }
101
+ JSValue zero = JS_NewInt32(ctx, 0);
102
+ JSValueConst call_args[2] = {thrower, zero};
103
+ JSValue ret = JS_Call(ctx, setTimeout, JS_UNDEFINED, 2, call_args);
104
+ JS_FreeValue(ctx, setTimeout);
105
+ JS_FreeValue(ctx, thrower);
106
+ JS_FreeValue(ctx, zero);
107
+ if (JS_IsException(ret)) {
108
+ /* setTimeout itself threw — best effort, drop the secondary error. */
109
+ JSValue suppressed = JS_GetException(ctx);
110
+ JS_FreeValue(ctx, suppressed);
111
+ } else {
112
+ JS_FreeValue(ctx, ret);
113
+ }
114
+ }
115
+
116
+ /* Call `fn(argv...)` synchronously; if it throws, schedule the exception
117
+ * to re-throw on the next tick. Caller is not informed of the throw. */
58
118
  static void run_safely(JSContext* ctx, JSValue fn, int argc, JSValue* argv) {
59
119
  JSValue ret = JS_Call(ctx, fn, JS_UNDEFINED, argc, argv);
60
120
  if (JS_IsException(ret)) {
61
- mik_dump_error(ctx);
121
+ JSValue exc = JS_GetException(ctx);
122
+ panic_async(ctx, exc);
62
123
  } else {
63
124
  JS_FreeValue(ctx, ret);
64
125
  }
65
126
  }
66
127
 
67
- /* Run all registered teardowns in reverse insertion order. Throws inside
68
- * teardowns are caught and logged so subsequent teardowns still run. */
128
+ /* Same panic-on-throw semantics as run_safely, for invoke-by-method calls
129
+ * (used when we don't have direct access to the C-level subscriber struct
130
+ * — multicast dispatch, from-iterable, from-promise). */
131
+ static void invoke_safely(JSContext* ctx, JSValueConst this_val, JSAtom method, int argc,
132
+ JSValueConst* argv) {
133
+ JSValue ret = JS_Invoke(ctx, this_val, method, argc, argv);
134
+ if (JS_IsException(ret)) {
135
+ JSValue exc = JS_GetException(ctx);
136
+ panic_async(ctx, exc);
137
+ } else {
138
+ JS_FreeValue(ctx, ret);
139
+ }
140
+ }
141
+
142
+ /* Run all registered teardowns in reverse insertion order. Throws are
143
+ * scheduled to re-throw async via panic_async, so subsequent teardowns
144
+ * still run synchronously. */
69
145
  static void run_teardowns(JSContext* ctx, SubscriberData* d) {
70
146
  /* Swap into a local list. If a teardown calls addTeardown synchronously,
71
147
  * the SubscriberData.closed flag is already true so addTeardown fires the
@@ -477,9 +553,7 @@ static JSValue from_iterable_subscribe(JSContext* ctx, JSValueConst this_val, in
477
553
  JS_FreeValue(ctx, value);
478
554
  break;
479
555
  }
480
- JSValue ret = JS_Invoke(ctx, subscriber, next_atom, 1, &value);
481
- if (JS_IsException(ret)) mik_dump_error(ctx);
482
- else JS_FreeValue(ctx, ret);
556
+ invoke_safely(ctx, subscriber, next_atom, 1, &value);
483
557
  JS_FreeValue(ctx, value);
484
558
  }
485
559
  JS_FreeAtom(ctx, next_atom);
@@ -488,10 +562,8 @@ static JSValue from_iterable_subscribe(JSContext* ctx, JSValueConst this_val, in
488
562
  auto* sd = static_cast<SubscriberData*>(JS_GetOpaque(subscriber, subscriber_class_id));
489
563
  if (sd && !sd->closed) {
490
564
  JSAtom complete_atom = JS_NewAtom(ctx, "complete");
491
- JSValue ret = JS_Invoke(ctx, subscriber, complete_atom, 0, nullptr);
565
+ invoke_safely(ctx, subscriber, complete_atom, 0, nullptr);
492
566
  JS_FreeAtom(ctx, complete_atom);
493
- if (JS_IsException(ret)) mik_dump_error(ctx);
494
- else JS_FreeValue(ctx, ret);
495
567
  }
496
568
  return JS_UNDEFINED;
497
569
  }
@@ -530,15 +602,15 @@ static JSValue from_promise_on_resolve(JSContext* ctx, JSValueConst this_val, in
530
602
  if (!sd || sd->closed) return JS_UNDEFINED;
531
603
 
532
604
  JSValue value = argc > 0 ? argv[0] : JS_UNDEFINED;
533
- JSValue ret = JS_Invoke(ctx, subscriber, JS_NewAtom(ctx, "next"), 1, &value);
534
- if (JS_IsException(ret)) mik_dump_error(ctx);
535
- else JS_FreeValue(ctx, ret);
605
+ JSAtom next_atom = JS_NewAtom(ctx, "next");
606
+ invoke_safely(ctx, subscriber, next_atom, 1, &value);
607
+ JS_FreeAtom(ctx, next_atom);
536
608
 
537
609
  /* Re-check closed: an observer's next handler may have unsubscribed. */
538
610
  if (sd->closed) return JS_UNDEFINED;
539
- JSValue cret = JS_Invoke(ctx, subscriber, JS_NewAtom(ctx, "complete"), 0, nullptr);
540
- if (JS_IsException(cret)) mik_dump_error(ctx);
541
- else JS_FreeValue(ctx, cret);
611
+ JSAtom complete_atom = JS_NewAtom(ctx, "complete");
612
+ invoke_safely(ctx, subscriber, complete_atom, 0, nullptr);
613
+ JS_FreeAtom(ctx, complete_atom);
542
614
  return JS_UNDEFINED;
543
615
  }
544
616
 
@@ -674,9 +746,9 @@ static JSValue multicast_subscribe(JSContext* ctx, JSValueConst this_val, int ar
674
746
 
675
747
  if (m->completed) {
676
748
  /* Late subscriber: complete immediately. */
677
- JSValue ret = JS_Invoke(ctx, subscriber, JS_NewAtom(ctx, "complete"), 0, nullptr);
678
- if (JS_IsException(ret)) mik_dump_error(ctx);
679
- else JS_FreeValue(ctx, ret);
749
+ JSAtom complete_atom = JS_NewAtom(ctx, "complete");
750
+ invoke_safely(ctx, subscriber, complete_atom, 0, nullptr);
751
+ JS_FreeAtom(ctx, complete_atom);
680
752
  return JS_UNDEFINED;
681
753
  }
682
754
 
@@ -743,9 +815,7 @@ static JSValue multicast_emit_next(JSContext* ctx, JSValueConst this_val, int ar
743
815
  JS_FreeValue(ctx, s);
744
816
  continue;
745
817
  }
746
- JSValue ret = JS_Invoke(ctx, s, next_atom, 1, &value);
747
- if (JS_IsException(ret)) mik_dump_error(ctx);
748
- else JS_FreeValue(ctx, ret);
818
+ invoke_safely(ctx, s, next_atom, 1, &value);
749
819
  JS_FreeValue(ctx, s);
750
820
  }
751
821
  JS_FreeAtom(ctx, next_atom);
@@ -772,9 +842,7 @@ static JSValue multicast_emit_complete(JSContext* ctx, JSValueConst this_val, in
772
842
  for (auto& s : snapshot) {
773
843
  auto* sd = static_cast<SubscriberData*>(JS_GetOpaque(s, subscriber_class_id));
774
844
  if (sd && !sd->closed) {
775
- JSValue ret = JS_Invoke(ctx, s, complete_atom, 0, nullptr);
776
- if (JS_IsException(ret)) mik_dump_error(ctx);
777
- else JS_FreeValue(ctx, ret);
845
+ invoke_safely(ctx, s, complete_atom, 0, nullptr);
778
846
  }
779
847
  JS_FreeValue(ctx, s);
780
848
  }