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

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.
@@ -1,5 +1,6 @@
1
1
  #pragma once
2
2
 
3
+ #include <stdbool.h>
3
4
  #include <stddef.h>
4
5
  #include <stdlib.h>
5
6
 
@@ -9,3 +10,19 @@ void* mik__mallocz(size_t size);
9
10
  void* mik__calloc(size_t count, size_t size);
10
11
  void mik__free(void* ptr);
11
12
  void* mik__realloc(void* ptr, size_t size);
13
+
14
+ /* QuickJS-heap allocators. Backed by libc malloc by default; routed to
15
+ * PSRAM via the platform's malloc_psram/calloc_psram/realloc_psram hooks
16
+ * when mik__set_quickjs_heap_psram(true) has been called and the platform
17
+ * supports PSRAM. Free is unchanged: PSRAM pointers from heap_caps_malloc
18
+ * free cleanly via standard free() on ESP-IDF. */
19
+ void* mik__js_malloc(size_t size);
20
+ void* mik__js_calloc(size_t count, size_t size);
21
+ void* mik__js_realloc(void* ptr, size_t size);
22
+
23
+ /* Toggle QuickJS heap allocations to PSRAM (no-op on platforms without
24
+ * PSRAM). Set once before JS_NewRuntime2; do not change mid-run since
25
+ * mik__js_realloc relies on the flag staying consistent with the heap
26
+ * the original pointer came from. */
27
+ void mik__set_quickjs_heap_psram(bool enable);
28
+ bool mik__is_quickjs_heap_psram(void);
@@ -10,6 +10,17 @@ typedef struct MIKRuntime MIKRuntime;
10
10
  typedef struct MIKRunOptions {
11
11
  int mem_limit;
12
12
  size_t stack_size;
13
+ /* Allocate the QuickJS heap from PSRAM instead of internal SRAM on
14
+ * chips with both. Frees ~150-250 KB of contiguous internal SRAM for
15
+ * mbedTLS, WiFi/BLE, and DMA buffers, the dominant root cause of
16
+ * HTTPS handshake failures (error 0x7002) on apps with significant
17
+ * retained JS state. JS execution is somewhat slower (PSRAM has
18
+ * higher access latency than internal SRAM); for typical embedded
19
+ * IO-bound workloads this is invisible against networking and IO
20
+ * costs. No effect on hosts and chips without PSRAM. Defaults false
21
+ * for backward compatibility; the firmware bootstrap flips it on
22
+ * when CONFIG_MIKROJS_QUICKJS_HEAP_PSRAM is set. */
23
+ bool use_psram_heap;
13
24
  } MIKRunOptions;
14
25
 
15
26
  typedef struct MIKConfig {
@@ -18,11 +18,37 @@ typedef struct MIKPlatform {
18
18
  size_t (*get_free_system_mem)(void);
19
19
  size_t (*get_min_free_system_mem)(void); /* All-time low watermark */
20
20
  size_t (*get_total_system_mem)(void);
21
- /** Largest contiguous free block (for diagnosing heap fragmentation —
21
+ /** Largest contiguous free block (for diagnosing heap fragmentation:
22
22
  * an allocation larger than this fails even if total free is bigger).
23
23
  * On platforms without a native largest-block API, implementations
24
24
  * may return the same value as get_free_system_mem. */
25
25
  size_t (*get_largest_free_system_mem)(void);
26
+ /** Free internal-SRAM bytes. On chips with PSRAM this is the subset of
27
+ * free heap that lives in fast on-chip RAM, which is also what mbedTLS
28
+ * handshake buffers, WiFi/BLE drivers, and DMA-capable buffers compete
29
+ * for. Distinct from get_free_system_mem on PSRAM-equipped chips, where
30
+ * combined heap is dominated by PSRAM and hides internal-SRAM pressure.
31
+ * On hosts and chips without PSRAM, may return the same value as
32
+ * get_free_system_mem (or 0 if unavailable). */
33
+ size_t (*get_free_internal_mem)(void);
34
+ /** Largest contiguous free block in internal SRAM. Allocations that
35
+ * must land in internal RAM (such as mbedTLS record buffers) fail when
36
+ * this drops below their size, even when get_largest_free_system_mem
37
+ * reports plenty of contiguous PSRAM. Same fallback rules as
38
+ * get_free_internal_mem. */
39
+ size_t (*get_largest_free_internal_mem)(void);
40
+ /** Allocate `size` bytes from PSRAM/external RAM. Returns NULL if
41
+ * PSRAM is unavailable (host platforms, chips without PSRAM, or
42
+ * PSRAM exhaustion); the caller falls back to the regular allocator.
43
+ * On ESP32 with CONFIG_SPIRAM=y, implemented via heap_caps_malloc
44
+ * with MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT. The returned pointer is
45
+ * freed via standard free(). */
46
+ void* (*malloc_psram)(size_t size);
47
+ /** PSRAM-targeted calloc. Same fallback rules as malloc_psram. */
48
+ void* (*calloc_psram)(size_t count, size_t size);
49
+ /** PSRAM-targeted realloc. Grows or moves the allocation while keeping
50
+ * it in PSRAM. Same fallback rules as malloc_psram. */
51
+ void* (*realloc_psram)(void* ptr, size_t size);
26
52
  bool (*get_fs_info)(const char* label, size_t* total, size_t* used);
27
53
  void (*log)(int level, const char* tag, const char* fmt, ...);
28
54
  int (*stdout_write)(const void* buf, size_t len);
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.gd2fdc0d",
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.gd2fdc0d"
82
82
  },
83
83
  "devDependencies": {
84
84
  "@swc/core": "^1.15.30",
@@ -2,6 +2,7 @@ import {Observable} from 'mikrojs/observable'
2
2
  import {err, ok} from 'mikrojs/result'
3
3
  import {Ble as NativeBle} from 'native:ble'
4
4
 
5
+ import type {Subscriber} from '../observable/types.js'
5
6
  import type {Result} from '../result/types.js'
6
7
  import type {
7
8
  AdvertiseHandle,
@@ -142,15 +143,31 @@ function normalizeServices(services: Service[]) {
142
143
 
143
144
  const native = new NativeBle()
144
145
 
145
- /* Per-event multicast sources backed by a single native.on registration each.
146
- * See observable.md → Module integration. */
147
- const _onConnect = Observable.withEmitters<ConnectionInfo>()
148
- const _onDisconnect = Observable.withEmitters<ConnectionInfo>()
149
- const _onMtu = Observable.withEmitters<MtuInfo>()
150
-
151
- native.on('connect', (info) => _onConnect.next(info as ConnectionInfo))
152
- native.on('disconnect', (info) => _onDisconnect.next(info as ConnectionInfo))
153
- native.on('mtu', (info) => _onMtu.next(info as MtuInfo))
146
+ /* Lazy-attach: native.on runs only after the first JS subscriber and is
147
+ * removed when the last one leaves. Same pattern as wifi.ts; see the note
148
+ * there for the mbedTLS internal-RAM rationale. */
149
+ function lazyEvent<T>(eventName: string): Observable<T> {
150
+ const subscribers: Subscriber<T>[] = []
151
+ function handler(value: unknown): void {
152
+ const snapshot = subscribers.slice()
153
+ for (const s of snapshot) {
154
+ if (!s.closed) s.next(value as T)
155
+ }
156
+ }
157
+ return new Observable<T>((sub) => {
158
+ subscribers.push(sub)
159
+ if (subscribers.length === 1) {
160
+ native.on(eventName, handler)
161
+ }
162
+ sub.addTeardown(() => {
163
+ const i = subscribers.indexOf(sub)
164
+ if (i >= 0) subscribers.splice(i, 1)
165
+ if (subscribers.length === 0) {
166
+ native.off(eventName, handler)
167
+ }
168
+ })
169
+ })
170
+ }
154
171
 
155
172
  const ble: Ble = {
156
173
  get name(): string {
@@ -231,9 +248,9 @@ const peripheral: Peripheral = {
231
248
  return ok(handle)
232
249
  },
233
250
 
234
- onConnect: _onConnect.observable,
235
- onDisconnect: _onDisconnect.observable,
236
- onMtu: _onMtu.observable,
251
+ onConnect: lazyEvent<ConnectionInfo>('connect'),
252
+ onDisconnect: lazyEvent<ConnectionInfo>('disconnect'),
253
+ onMtu: lazyEvent<MtuInfo>('mtu'),
237
254
  }
238
255
 
239
256
  export {ble, peripheral}
@@ -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
  })
@@ -10,10 +10,23 @@ export interface MemoryUsage {
10
10
  /** Total system heap in bytes (0 on host) */
11
11
  systemTotal: number
12
12
  /** Largest contiguous free block in bytes (0 on host). Allocations
13
- * larger than this fail even when `systemFree` is bigger — the gap
13
+ * larger than this fail even when `systemFree` is bigger; the gap
14
14
  * between `systemLargestFree` and `systemFree` measures heap
15
15
  * fragmentation. */
16
16
  systemLargestFree: number
17
+ /** Free internal-SRAM bytes (0 on host). On chips with PSRAM this is
18
+ * the subset of free heap that lives in fast on-chip RAM, which
19
+ * also holds mbedTLS handshake buffers, WiFi/BLE driver state, and
20
+ * DMA-capable buffers. On PSRAM-equipped chips `systemFree` is
21
+ * dominated by PSRAM and can hide internal-SRAM pressure: watch
22
+ * `internalFree` instead when diagnosing TLS handshake or driver
23
+ * allocation failures. On chips without PSRAM, equals `systemFree`. */
24
+ internalFree: number
25
+ /** Largest contiguous free block in internal SRAM (0 on host).
26
+ * Allocations that must land in internal RAM (such as mbedTLS record
27
+ * buffers, ~16 KB each) fail when this drops below their size, even
28
+ * when `systemLargestFree` reports plenty of contiguous PSRAM. */
29
+ internalLargestFree: number
17
30
  }
18
31
 
19
32
  export interface Uptime {
@@ -2,6 +2,7 @@ import {Observable} from 'mikrojs/observable'
2
2
  import {err, ok} from 'mikrojs/result'
3
3
  import {Wifi as NativeWifi} from 'native:wifi'
4
4
 
5
+ import type {Subscriber} from '../observable/types.js'
5
6
  import type {Result} from '../result/types.js'
6
7
  import type {
7
8
  ApStartOptions,
@@ -37,22 +38,35 @@ const StatusFromCode = new Map<number, WifiStatus>(
37
38
 
38
39
  const native = new NativeWifi()
39
40
 
40
- /* One Observable per event type, each backed by a single native.on
41
- * registration. Subscribers share that registration via the multicast
42
- * source from Observable.withEmitters() — registering the listener once
43
- * keeps the C-level callback list at minimum size regardless of how many
44
- * JS subscribers are attached. */
45
- const _onConnect = Observable.withEmitters<WifiConnectionInfo>()
46
- const _onDisconnect = Observable.withEmitters<WifiDisconnectReason>()
47
- const _onRssiLow = Observable.withEmitters<number>()
48
- const _onStationConnect = Observable.withEmitters<ApStationInfo>()
49
- const _onStationDisconnect = Observable.withEmitters<ApStationInfo>()
50
-
51
- native.on('connect', (info) => _onConnect.next(info as WifiConnectionInfo))
52
- native.on('disconnect', (reason) => _onDisconnect.next(reason as WifiDisconnectReason))
53
- native.on('rssi-low', (rssi) => _onRssiLow.next(rssi as number))
54
- native.on('station-connect', (info) => _onStationConnect.next(info as ApStationInfo))
55
- native.on('station-disconnect', (info) => _onStationDisconnect.next(info as ApStationInfo))
41
+ /* Lazy-attach Observable: native.on is only called once a JS subscriber
42
+ * appears, and native.off runs when the last one unsubscribes. Code paths
43
+ * that import wifi but don't subscribe (e.g. a fetch-only flow) take the
44
+ * exact same internal-RAM path as before observables existed — important
45
+ * because mbedTLS handshake needs ~16 KB contiguous internal SRAM and
46
+ * each eager closure pinned at module load chips into that headroom. */
47
+ function lazyEvent<T>(eventName: string): Observable<T> {
48
+ const subscribers: Subscriber<T>[] = []
49
+ function handler(value: unknown): void {
50
+ /* Snapshot so unsubscribes during dispatch don't shift indices. */
51
+ const snapshot = subscribers.slice()
52
+ for (const s of snapshot) {
53
+ if (!s.closed) s.next(value as T)
54
+ }
55
+ }
56
+ return new Observable<T>((sub) => {
57
+ subscribers.push(sub)
58
+ if (subscribers.length === 1) {
59
+ native.on(eventName, handler)
60
+ }
61
+ sub.addTeardown(() => {
62
+ const i = subscribers.indexOf(sub)
63
+ if (i >= 0) subscribers.splice(i, 1)
64
+ if (subscribers.length === 0) {
65
+ native.off(eventName, handler)
66
+ }
67
+ })
68
+ })
69
+ }
56
70
 
57
71
  const ap: WifiAp = {
58
72
  start(options: ApStartOptions): Result<void, WifiError> {
@@ -88,8 +102,8 @@ const ap: WifiAp = {
88
102
  native.apSetInactiveTimeout(seconds)
89
103
  },
90
104
 
91
- onStationConnect: _onStationConnect.observable,
92
- onStationDisconnect: _onStationDisconnect.observable,
105
+ onStationConnect: lazyEvent<ApStationInfo>('station-connect'),
106
+ onStationDisconnect: lazyEvent<ApStationInfo>('station-disconnect'),
93
107
  }
94
108
 
95
109
  const MAX_CONNECT_RETRIES = 5
@@ -147,9 +161,9 @@ const wifi: Wifi = {
147
161
  return ok(asyncResult.value as ScanResult[])
148
162
  },
149
163
 
150
- onConnect: _onConnect.observable,
151
- onDisconnect: _onDisconnect.observable,
152
- onRssiLow: _onRssiLow.observable,
164
+ onConnect: lazyEvent<WifiConnectionInfo>('connect'),
165
+ onDisconnect: lazyEvent<WifiDisconnectReason>('disconnect'),
166
+ onRssiLow: lazyEvent<number>('rssi-low'),
153
167
 
154
168
  get mac(): string {
155
169
  const result = native.mac()
package/src/mem.cpp CHANGED
@@ -5,6 +5,8 @@
5
5
  #include <stdlib.h>
6
6
  #include <string.h>
7
7
 
8
+ #include "mikrojs/platform.h"
9
+
8
10
  /*
9
11
  * QuickJS uses malloc_usable_size to track memory consumption against its
10
12
  * memory limit. ESP-IDF lacks a standard malloc_usable_size, and the
@@ -61,3 +63,65 @@ void* mik__realloc(void* ptr, size_t size) {
61
63
  hdr_write(raw, size);
62
64
  return static_cast<char*>(raw) + HDR_SIZE;
63
65
  }
66
+
67
+ /* QuickJS-heap allocator. When the PSRAM flag is set, route through the
68
+ * platform's malloc_psram first; if the platform can't satisfy the request
69
+ * (no PSRAM, or PSRAM exhausted), fall back to libc malloc so the runtime
70
+ * keeps working. The fallback only matters on host builds and in PSRAM-OOM
71
+ * edge cases. Under normal ESP32 operation with CONFIG_SPIRAM=y, every
72
+ * allocation lands in PSRAM. */
73
+
74
+ static bool g_quickjs_heap_psram = false;
75
+
76
+ void mik__set_quickjs_heap_psram(bool enable) {
77
+ g_quickjs_heap_psram = enable;
78
+ }
79
+
80
+ bool mik__is_quickjs_heap_psram(void) {
81
+ return g_quickjs_heap_psram;
82
+ }
83
+
84
+ void* mik__js_malloc(size_t size) {
85
+ void* raw = nullptr;
86
+ if (g_quickjs_heap_psram) {
87
+ const MIKPlatform* p = MIK_GetPlatform();
88
+ if (p && p->malloc_psram) {
89
+ raw = p->malloc_psram(size + HDR_SIZE);
90
+ }
91
+ }
92
+ if (!raw) raw = malloc(size + HDR_SIZE);
93
+ if (!raw) return nullptr;
94
+ hdr_write(raw, size);
95
+ return static_cast<char*>(raw) + HDR_SIZE;
96
+ }
97
+
98
+ void* mik__js_calloc(size_t count, size_t size) {
99
+ if (size && count > SIZE_MAX / size) return nullptr;
100
+ size_t total = count * size;
101
+ void* raw = nullptr;
102
+ if (g_quickjs_heap_psram) {
103
+ const MIKPlatform* p = MIK_GetPlatform();
104
+ if (p && p->calloc_psram) {
105
+ raw = p->calloc_psram(1, total + HDR_SIZE);
106
+ }
107
+ }
108
+ if (!raw) raw = calloc(1, total + HDR_SIZE);
109
+ if (!raw) return nullptr;
110
+ hdr_write(raw, total);
111
+ return static_cast<char*>(raw) + HDR_SIZE;
112
+ }
113
+
114
+ void* mik__js_realloc(void* ptr, size_t size) {
115
+ void* raw = ptr ? static_cast<char*>(ptr) - HDR_SIZE : nullptr;
116
+ void* new_raw = nullptr;
117
+ if (g_quickjs_heap_psram) {
118
+ const MIKPlatform* p = MIK_GetPlatform();
119
+ if (p && p->realloc_psram) {
120
+ new_raw = p->realloc_psram(raw, size + HDR_SIZE);
121
+ }
122
+ }
123
+ if (!new_raw) new_raw = realloc(raw, size + HDR_SIZE);
124
+ if (!new_raw) return nullptr;
125
+ hdr_write(new_raw, size);
126
+ return static_cast<char*>(new_raw) + HDR_SIZE;
127
+ }
@@ -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
  }
package/src/mik_sys.cpp CHANGED
@@ -47,6 +47,10 @@ static JSValue mik__sys_memory_usage(JSContext* ctx, JSValue this_val, int argc,
47
47
  JS_NewInt64(ctx, (int64_t)platform->get_total_system_mem()));
48
48
  JS_SetPropertyStr(ctx, obj, "systemLargestFree",
49
49
  JS_NewInt64(ctx, (int64_t)platform->get_largest_free_system_mem()));
50
+ JS_SetPropertyStr(ctx, obj, "internalFree",
51
+ JS_NewInt64(ctx, (int64_t)platform->get_free_internal_mem()));
52
+ JS_SetPropertyStr(ctx, obj, "internalLargestFree",
53
+ JS_NewInt64(ctx, (int64_t)platform->get_largest_free_internal_mem()));
50
54
  return obj;
51
55
  }
52
56
 
package/src/mikrojs.cpp CHANGED
@@ -17,16 +17,18 @@
17
17
 
18
18
  #define MIK__DEFAULT_STACK_SIZE 1024 * 1024 // 1 MB
19
19
 
20
- /* JS malloc functions */
20
+ /* JS malloc functions. Routed through the mik__js_* family so the QuickJS
21
+ * heap can be relocated to PSRAM on chips with both internal SRAM and PSRAM,
22
+ * leaving internal SRAM free for mbedTLS, WiFi/BLE, and DMA buffers. */
21
23
 
22
24
  static void* mik__mf_calloc(void* opaque, size_t count, size_t size) {
23
25
  (void)opaque;
24
- return mik__calloc(count, size);
26
+ return mik__js_calloc(count, size);
25
27
  }
26
28
 
27
29
  static void* mik__mf_malloc(void* opaque, size_t size) {
28
30
  (void)opaque;
29
- return mik__malloc(size);
31
+ return mik__js_malloc(size);
30
32
  }
31
33
 
32
34
  static void mik__mf_free(void* opaque, void* ptr) {
@@ -36,7 +38,7 @@ static void mik__mf_free(void* opaque, void* ptr) {
36
38
 
37
39
  static void* mik__mf_realloc(void* opaque, void* ptr, size_t size) {
38
40
  (void)opaque;
39
- return mik__realloc(ptr, size);
41
+ return mik__js_realloc(ptr, size);
40
42
  }
41
43
 
42
44
  static const JSMallocFunctions mik_mf = {
@@ -199,7 +201,11 @@ static void mik__promise_rejection_tracker(JSContext* ctx, JSValue promise, JSVa
199
201
  }
200
202
 
201
203
  void MIK_DefaultOptions(MIKRunOptions* options) {
202
- static MIKRunOptions default_options = {.mem_limit = 0, .stack_size = MIK__DEFAULT_STACK_SIZE};
204
+ static MIKRunOptions default_options = {
205
+ .mem_limit = 0,
206
+ .stack_size = MIK__DEFAULT_STACK_SIZE,
207
+ .use_psram_heap = false,
208
+ };
203
209
 
204
210
  memcpy(options, &default_options, sizeof(*options));
205
211
  }
@@ -228,6 +234,12 @@ MIKRuntime* MIK_NewRuntimeInternal(MIKRunOptions* options) {
228
234
  memcpy(&mik_rt->options, options, sizeof(*options));
229
235
  MIK_DefaultConfig(&mik_rt->config);
230
236
 
237
+ /* Switch the QuickJS heap to PSRAM before constructing the runtime,
238
+ * so JS_NewRuntime2 and every subsequent QuickJS allocation lands
239
+ * in PSRAM. The MIKRuntime struct itself stays in internal SRAM:
240
+ * it was allocated before this point. */
241
+ mik__set_quickjs_heap_psram(options->use_psram_heap);
242
+
231
243
  rt = JS_NewRuntime2(&mik_mf, NULL);
232
244
  CHECK_NOT_NULL(rt);
233
245
  mik_rt->rt = rt;
@@ -45,6 +45,31 @@ static size_t posix_get_largest_free_system_mem(void) {
45
45
  return 0; /* Not available on desktop */
46
46
  }
47
47
 
48
+ static size_t posix_get_free_internal_mem(void) {
49
+ return 0; /* No internal/PSRAM distinction on desktop */
50
+ }
51
+
52
+ static size_t posix_get_largest_free_internal_mem(void) {
53
+ return 0; /* No internal/PSRAM distinction on desktop */
54
+ }
55
+
56
+ static void* posix_malloc_psram(size_t size) {
57
+ (void)size;
58
+ return NULL; /* No PSRAM on desktop; caller falls back to libc */
59
+ }
60
+
61
+ static void* posix_calloc_psram(size_t count, size_t size) {
62
+ (void)count;
63
+ (void)size;
64
+ return NULL;
65
+ }
66
+
67
+ static void* posix_realloc_psram(void* ptr, size_t size) {
68
+ (void)ptr;
69
+ (void)size;
70
+ return NULL;
71
+ }
72
+
48
73
  static bool posix_get_fs_info(const char* label, size_t* total, size_t* used) {
49
74
  (void)label;
50
75
  (void)total;
@@ -111,6 +136,11 @@ static const MIKPlatform posix_platform = {
111
136
  .get_min_free_system_mem = posix_get_min_free_system_mem,
112
137
  .get_total_system_mem = posix_get_total_system_mem,
113
138
  .get_largest_free_system_mem = posix_get_largest_free_system_mem,
139
+ .get_free_internal_mem = posix_get_free_internal_mem,
140
+ .get_largest_free_internal_mem = posix_get_largest_free_internal_mem,
141
+ .malloc_psram = posix_malloc_psram,
142
+ .calloc_psram = posix_calloc_psram,
143
+ .realloc_psram = posix_realloc_psram,
114
144
  .get_fs_info = posix_get_fs_info,
115
145
  .log = posix_log,
116
146
  .stdout_write = posix_stdout_write,