@mikrojs/native 0.6.0-pr-70.g7bdc6dd → 0.6.0-pr-72.g464c29c
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/CMakeLists.txt +1 -3
- package/include/mikrojs/mem.h +17 -0
- package/include/mikrojs/mikrojs.h +11 -0
- package/include/mikrojs/platform.h +27 -1
- package/include/mikrojs/private.h +0 -3
- package/package.json +2 -4
- package/prebuilds/darwin-arm64/mikrojs.napi.node +0 -0
- package/prebuilds/linux-arm64/mikrojs.napi.node +0 -0
- package/prebuilds/linux-x64/mikrojs.napi.node +0 -0
- package/runtime/ble/ble.ts +8 -16
- package/runtime/ble/types.ts +14 -7
- package/runtime/internal.d.ts +0 -4
- package/runtime/sys/types.ts +14 -1
- package/runtime/udp/types.ts +2 -15
- package/runtime/udp/udp.ts +9 -45
- package/runtime/wifi/types.ts +15 -6
- package/runtime/wifi/wifi.ts +16 -24
- package/src/mem.cpp +64 -0
- package/src/mik_sys.cpp +4 -0
- package/src/mikrojs.cpp +17 -6
- package/src/platform_posix.cpp +30 -0
- package/runtime/observable/native-observable.node-shim.ts +0 -220
- package/runtime/observable/observable.ts +0 -1
- package/runtime/observable/operators.ts +0 -140
- package/runtime/observable/types.ts +0 -80
- package/src/mik_observable.cpp +0 -950
package/CMakeLists.txt
CHANGED
|
@@ -50,7 +50,6 @@ set(MIKROJS_CORE_SOURCES
|
|
|
50
50
|
src/mik_repl.cpp
|
|
51
51
|
src/mik_app_config.cpp
|
|
52
52
|
src/mik_udp.cpp
|
|
53
|
-
src/mik_observable.cpp
|
|
54
53
|
)
|
|
55
54
|
|
|
56
55
|
add_library(mikrojs STATIC
|
|
@@ -81,7 +80,7 @@ endif()
|
|
|
81
80
|
include(cmake/mikrojs_bytecode.cmake)
|
|
82
81
|
mikrojs_generate_bytecode(
|
|
83
82
|
RUNTIME_DIR "${CMAKE_CURRENT_SOURCE_DIR}/runtime"
|
|
84
|
-
MODULES cbor env result schema fs http/helpers http/request i2c kv/nvs kv/rtc kv/shared neopixel
|
|
83
|
+
MODULES cbor env result schema fs http/helpers http/request i2c kv/nvs kv/rtc kv/shared neopixel pin pwm reader sleep spi sntp stdio stream sys test uart udp wifi
|
|
85
84
|
MODULE_PREFIX "mikrojs"
|
|
86
85
|
SYMBOL_PREFIX "mikrojs"
|
|
87
86
|
TARGET gen_bytecode
|
|
@@ -162,7 +161,6 @@ if(BUILD_TESTING)
|
|
|
162
161
|
test/stream_test.cpp
|
|
163
162
|
test/runtime_recycle_test.cpp
|
|
164
163
|
test/udp_test.cpp
|
|
165
|
-
test/observable_test.cpp
|
|
166
164
|
)
|
|
167
165
|
|
|
168
166
|
target_link_libraries(mikrojs_tests PRIVATE mikrojs)
|
package/include/mikrojs/mem.h
CHANGED
|
@@ -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);
|
|
@@ -215,9 +215,6 @@ JSModuleDef* mik__result_init(JSContext* ctx);
|
|
|
215
215
|
/* UDP module (mik_udp.cpp) */
|
|
216
216
|
JSModuleDef* mik__udp_init(JSContext* ctx);
|
|
217
217
|
|
|
218
|
-
/* Observable module (mik_observable.cpp) */
|
|
219
|
-
JSModuleDef* mik__observable_init(JSContext* ctx);
|
|
220
|
-
|
|
221
218
|
bool mik__repl_is_evaluating(void);
|
|
222
219
|
|
|
223
220
|
/* REPL protocol mode (mik_repl.cpp) — used by mik_console.cpp, mik_stdio.cpp */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikrojs/native",
|
|
3
|
-
"version": "0.6.0-pr-
|
|
3
|
+
"version": "0.6.0-pr-72.g464c29c",
|
|
4
4
|
"description": "Mikro.js C++ runtime library and Node.js native addon",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"esp32",
|
|
@@ -56,8 +56,6 @@
|
|
|
56
56
|
"./runtime/kv/shared": "./runtime/kv/shared.ts",
|
|
57
57
|
"./runtime/kv/types": "./runtime/kv/types.ts",
|
|
58
58
|
"./runtime/neopixel/types": "./runtime/neopixel/types.ts",
|
|
59
|
-
"./runtime/observable/operators": "./runtime/observable/operators.ts",
|
|
60
|
-
"./runtime/observable/types": "./runtime/observable/types.ts",
|
|
61
59
|
"./runtime/pin/types": "./runtime/pin/types.ts",
|
|
62
60
|
"./runtime/pwm/types": "./runtime/pwm/types.ts",
|
|
63
61
|
"./runtime/reader/types": "./runtime/reader/types.ts",
|
|
@@ -78,7 +76,7 @@
|
|
|
78
76
|
"cmake-js": "^8.0.0",
|
|
79
77
|
"node-addon-api": "^8.7.0",
|
|
80
78
|
"node-gyp-build": "^4.8.4",
|
|
81
|
-
"@mikrojs/quickjs": "0.6.0-pr-
|
|
79
|
+
"@mikrojs/quickjs": "0.6.0-pr-72.g464c29c"
|
|
82
80
|
},
|
|
83
81
|
"devDependencies": {
|
|
84
82
|
"@swc/core": "^1.15.30",
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/runtime/ble/ble.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import {Observable} from 'mikrojs/observable'
|
|
2
1
|
import {err, ok} from 'mikrojs/result'
|
|
3
2
|
import {Ble as NativeBle} from 'native:ble'
|
|
4
3
|
|
|
@@ -10,9 +9,8 @@ import type {
|
|
|
10
9
|
BleError,
|
|
11
10
|
Characteristic,
|
|
12
11
|
CharacteristicProperty,
|
|
13
|
-
ConnectionInfo,
|
|
14
|
-
MtuInfo,
|
|
15
12
|
Peripheral,
|
|
13
|
+
PeripheralEventMap,
|
|
16
14
|
Service,
|
|
17
15
|
} from './types.js'
|
|
18
16
|
import {parseUuid} from './uuid.js'
|
|
@@ -142,16 +140,6 @@ function normalizeServices(services: Service[]) {
|
|
|
142
140
|
|
|
143
141
|
const native = new NativeBle()
|
|
144
142
|
|
|
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))
|
|
154
|
-
|
|
155
143
|
const ble: Ble = {
|
|
156
144
|
get name(): string {
|
|
157
145
|
return native.getName()
|
|
@@ -231,9 +219,13 @@ const peripheral: Peripheral = {
|
|
|
231
219
|
return ok(handle)
|
|
232
220
|
},
|
|
233
221
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
222
|
+
on<K extends keyof PeripheralEventMap>(event: K, listener: PeripheralEventMap[K]) {
|
|
223
|
+
native.on(event, listener as (...args: unknown[]) => void)
|
|
224
|
+
},
|
|
225
|
+
|
|
226
|
+
off<K extends keyof PeripheralEventMap>(event: K, listener: PeripheralEventMap[K]) {
|
|
227
|
+
native.off(event, listener as (...args: unknown[]) => void)
|
|
228
|
+
},
|
|
237
229
|
}
|
|
238
230
|
|
|
239
231
|
export {ble, peripheral}
|
package/runtime/ble/types.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type {Observable} from '../observable/types.js'
|
|
2
1
|
import type {Result} from '../result/types.js'
|
|
3
2
|
|
|
4
3
|
/** Advertising interval range in milliseconds. */
|
|
@@ -172,15 +171,23 @@ export interface MtuInfo {
|
|
|
172
171
|
mtu: number
|
|
173
172
|
}
|
|
174
173
|
|
|
174
|
+
export interface PeripheralEventMap {
|
|
175
|
+
connect: (info: ConnectionInfo) => void
|
|
176
|
+
disconnect: (info: ConnectionInfo) => void
|
|
177
|
+
mtu: (info: MtuInfo) => void
|
|
178
|
+
}
|
|
179
|
+
|
|
175
180
|
export interface Peripheral {
|
|
176
181
|
/** Start advertising. Returns a handle whose `stop()` ends this session. */
|
|
177
182
|
advertise(options?: AdvertiseOptions): Promise<Result<AdvertiseHandle, BleError>>
|
|
178
|
-
/**
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
183
|
+
/**
|
|
184
|
+
* Register a listener for a peripheral lifecycle event. Listeners are
|
|
185
|
+
* called on the JS loop thread in registration order. Safe to register
|
|
186
|
+
* before calling `advertise()`.
|
|
187
|
+
*/
|
|
188
|
+
on<K extends keyof PeripheralEventMap>(event: K, listener: PeripheralEventMap[K]): void
|
|
189
|
+
/** Remove a previously-registered listener. */
|
|
190
|
+
off<K extends keyof PeripheralEventMap>(event: K, listener: PeripheralEventMap[K]): void
|
|
184
191
|
}
|
|
185
192
|
|
|
186
193
|
export declare const ble: Ble
|
package/runtime/internal.d.ts
CHANGED
|
@@ -25,10 +25,6 @@ declare module 'native:result' {
|
|
|
25
25
|
export function err<E>(error: E): ErrResult<E>
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
declare module 'native:observable' {
|
|
29
|
-
export {Observable} from '@mikrojs/native/runtime/observable/types'
|
|
30
|
-
}
|
|
31
|
-
|
|
32
28
|
declare module 'native:sys' {
|
|
33
29
|
import type {JsMemoryUsage} from './sys/types.js'
|
|
34
30
|
export function evalScript(code: string): Promise<{value: unknown}>
|
package/runtime/sys/types.ts
CHANGED
|
@@ -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
|
|
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 {
|
package/runtime/udp/types.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type {Observable} from '../observable/types.js'
|
|
2
1
|
import type {Result} from '../result/types.js'
|
|
3
2
|
|
|
4
3
|
export type UdpFamily = 'ipv4' | 'ipv6'
|
|
@@ -32,31 +31,19 @@ export type UdpError =
|
|
|
32
31
|
| {name: 'Closed'}
|
|
33
32
|
| {name: 'NotBound'}
|
|
34
33
|
|
|
35
|
-
/** Inbound datagram delivered via `socket.onMessage`. */
|
|
36
|
-
export interface UdpMessage {
|
|
37
|
-
msg: Uint8Array
|
|
38
|
-
from: PeerAddress
|
|
39
|
-
}
|
|
40
|
-
|
|
41
34
|
export interface UdpSocket {
|
|
42
35
|
readonly port: number
|
|
43
36
|
readonly family: UdpFamily | 'dual'
|
|
44
37
|
/**
|
|
45
38
|
* Counter incremented each time an inbound datagram is dropped:
|
|
46
39
|
* - the per-socket queue is full,
|
|
47
|
-
* - no
|
|
40
|
+
* - no `onMessage` handler is set when a packet arrives,
|
|
48
41
|
* - or the datagram is larger than the 1500-byte receive buffer
|
|
49
42
|
* (typical Ethernet MTU; covers CoAP / mDNS / SNTP / DNS).
|
|
50
43
|
*/
|
|
51
44
|
dropped: number
|
|
52
45
|
|
|
53
|
-
|
|
54
|
-
* Stream of inbound datagrams. Subscribing attaches the native dispatch
|
|
55
|
-
* once the first subscriber is added; the last `unsubscribe()` detaches
|
|
56
|
-
* it again. Multiple subscribers fan out from one native registration.
|
|
57
|
-
* Completes when `close()` is called.
|
|
58
|
-
*/
|
|
59
|
-
readonly onMessage: Observable<UdpMessage>
|
|
46
|
+
onMessage: ((msg: Uint8Array, from: PeerAddress) => void) | null
|
|
60
47
|
|
|
61
48
|
send(data: Uint8Array | string, to: PeerAddress): Promise<Result<void, UdpError>>
|
|
62
49
|
joinMulticastGroup(group: MulticastGroup): Result<void, UdpError>
|
package/runtime/udp/udp.ts
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import {Observable} from 'mikrojs/observable'
|
|
2
1
|
import {ok} from 'mikrojs/result'
|
|
3
2
|
import {bind as nativeBind, type NativeUdpSocket} from 'native:udp'
|
|
4
3
|
|
|
5
|
-
import type {Subscriber} from '../observable/types.js'
|
|
6
4
|
import type {Result} from '../result/types.js'
|
|
7
|
-
import type {BindOptions, MulticastGroup,
|
|
5
|
+
import type {BindOptions, MulticastGroup, PeerAddress, UdpError, UdpSocket} from './types.js'
|
|
8
6
|
|
|
9
7
|
const utf8 = new TextEncoder()
|
|
10
8
|
|
|
@@ -13,37 +11,7 @@ function toBytes(data: Uint8Array | string): Uint8Array {
|
|
|
13
11
|
}
|
|
14
12
|
|
|
15
13
|
function makeSocket(handle: NativeUdpSocket): UdpSocket {
|
|
16
|
-
|
|
17
|
-
* JS subscriber is listening. With zero subscribers, the native side
|
|
18
|
-
* skips JS dispatch and increments the dropped counter for inbound
|
|
19
|
-
* packets — preserving the pre-Observable behavior of unset onMessage
|
|
20
|
-
* silently discarding traffic. */
|
|
21
|
-
const subscribers: Subscriber<UdpMessage>[] = []
|
|
22
|
-
let closed = false
|
|
23
|
-
|
|
24
|
-
const onMessage = new Observable<UdpMessage>((sub) => {
|
|
25
|
-
if (closed) {
|
|
26
|
-
sub.complete()
|
|
27
|
-
return
|
|
28
|
-
}
|
|
29
|
-
subscribers.push(sub)
|
|
30
|
-
if (subscribers.length === 1) {
|
|
31
|
-
handle.setOnMessage((msg, from) => {
|
|
32
|
-
/* Snapshot so unsubscribes during dispatch don't shift indices. */
|
|
33
|
-
const snapshot = subscribers.slice()
|
|
34
|
-
for (const s of snapshot) {
|
|
35
|
-
if (!s.closed) s.next({msg, from})
|
|
36
|
-
}
|
|
37
|
-
})
|
|
38
|
-
}
|
|
39
|
-
sub.addTeardown(() => {
|
|
40
|
-
const i = subscribers.indexOf(sub)
|
|
41
|
-
if (i >= 0) subscribers.splice(i, 1)
|
|
42
|
-
if (subscribers.length === 0 && !closed) {
|
|
43
|
-
handle.setOnMessage(null)
|
|
44
|
-
}
|
|
45
|
-
})
|
|
46
|
-
})
|
|
14
|
+
let onMessage: ((msg: Uint8Array, from: PeerAddress) => void) | null = null
|
|
47
15
|
|
|
48
16
|
const sock: UdpSocket = {
|
|
49
17
|
get port() {
|
|
@@ -58,7 +26,13 @@ function makeSocket(handle: NativeUdpSocket): UdpSocket {
|
|
|
58
26
|
set dropped(value: number) {
|
|
59
27
|
handle.dropped = value
|
|
60
28
|
},
|
|
61
|
-
onMessage
|
|
29
|
+
get onMessage() {
|
|
30
|
+
return onMessage
|
|
31
|
+
},
|
|
32
|
+
set onMessage(fn) {
|
|
33
|
+
onMessage = fn
|
|
34
|
+
handle.setOnMessage(fn)
|
|
35
|
+
},
|
|
62
36
|
send(data, to) {
|
|
63
37
|
return handle.send(toBytes(data), to)
|
|
64
38
|
},
|
|
@@ -69,16 +43,6 @@ function makeSocket(handle: NativeUdpSocket): UdpSocket {
|
|
|
69
43
|
return handle.leaveMulticastGroup(group.address)
|
|
70
44
|
},
|
|
71
45
|
close() {
|
|
72
|
-
if (closed) return
|
|
73
|
-
closed = true
|
|
74
|
-
handle.setOnMessage(null)
|
|
75
|
-
/* Complete any active subscriptions. Walk a snapshot in case a
|
|
76
|
-
* complete handler unsubscribes a sibling. */
|
|
77
|
-
const snapshot = subscribers.slice()
|
|
78
|
-
subscribers.length = 0
|
|
79
|
-
for (const s of snapshot) {
|
|
80
|
-
if (!s.closed) s.complete()
|
|
81
|
-
}
|
|
82
46
|
handle.close()
|
|
83
47
|
},
|
|
84
48
|
}
|
package/runtime/wifi/types.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type {Observable} from '../observable/types.js'
|
|
2
1
|
import type {Result} from '../result/types.js'
|
|
3
2
|
|
|
4
3
|
export type WifiStatus =
|
|
@@ -88,6 +87,17 @@ export interface ApStartOptions {
|
|
|
88
87
|
maxConnections?: number
|
|
89
88
|
}
|
|
90
89
|
|
|
90
|
+
export interface WifiEventMap {
|
|
91
|
+
connect: (info: WifiConnectionInfo) => void
|
|
92
|
+
disconnect: (reason: WifiDisconnectReason) => void
|
|
93
|
+
'rssi-low': (rssi: number) => void
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export interface ApEventMap {
|
|
97
|
+
'station-connect': (info: ApStationInfo) => void
|
|
98
|
+
'station-disconnect': (info: ApStationInfo) => void
|
|
99
|
+
}
|
|
100
|
+
|
|
91
101
|
export type WifiError =
|
|
92
102
|
| {name: 'InitFailed'; message: string}
|
|
93
103
|
| {name: 'CountryNotSet'}
|
|
@@ -112,8 +122,8 @@ export interface WifiAp {
|
|
|
112
122
|
readonly stations: ApStationInfo[]
|
|
113
123
|
deauthStation(mac: string): Result<void, WifiError>
|
|
114
124
|
inactiveTimeout: number
|
|
115
|
-
|
|
116
|
-
|
|
125
|
+
on<K extends keyof ApEventMap>(event: K, listener: ApEventMap[K]): void
|
|
126
|
+
off<K extends keyof ApEventMap>(event: K, listener: ApEventMap[K]): void
|
|
117
127
|
}
|
|
118
128
|
|
|
119
129
|
export interface Wifi {
|
|
@@ -125,9 +135,8 @@ export interface Wifi {
|
|
|
125
135
|
isConnected: boolean
|
|
126
136
|
scan(opts?: ScanOptions): Promise<Result<ScanResult[], WifiError>>
|
|
127
137
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
readonly onRssiLow: Observable<number>
|
|
138
|
+
on<K extends keyof WifiEventMap>(event: K, listener: WifiEventMap[K]): void
|
|
139
|
+
off<K extends keyof WifiEventMap>(event: K, listener: WifiEventMap[K]): void
|
|
131
140
|
|
|
132
141
|
readonly mac: string
|
|
133
142
|
hostname: string | undefined
|
package/runtime/wifi/wifi.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {Observable} from 'mikrojs/observable'
|
|
2
1
|
import {err, ok} from 'mikrojs/result'
|
|
3
2
|
import {Wifi as NativeWifi} from 'native:wifi'
|
|
4
3
|
|
|
5
4
|
import type {Result} from '../result/types.js'
|
|
6
5
|
import type {
|
|
6
|
+
ApEventMap,
|
|
7
7
|
ApStartOptions,
|
|
8
8
|
ApStationInfo,
|
|
9
9
|
IpConfig,
|
|
@@ -15,8 +15,8 @@ import type {
|
|
|
15
15
|
WifiAp,
|
|
16
16
|
WifiConnectionInfo,
|
|
17
17
|
WifiCountryCode,
|
|
18
|
-
WifiDisconnectReason,
|
|
19
18
|
WifiError,
|
|
19
|
+
WifiEventMap,
|
|
20
20
|
WifiStatus,
|
|
21
21
|
} from './types.js'
|
|
22
22
|
|
|
@@ -37,23 +37,6 @@ const StatusFromCode = new Map<number, WifiStatus>(
|
|
|
37
37
|
|
|
38
38
|
const native = new NativeWifi()
|
|
39
39
|
|
|
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))
|
|
56
|
-
|
|
57
40
|
const ap: WifiAp = {
|
|
58
41
|
start(options: ApStartOptions): Result<void, WifiError> {
|
|
59
42
|
return native.apStart(options as Parameters<typeof native.apStart>[0])
|
|
@@ -88,8 +71,13 @@ const ap: WifiAp = {
|
|
|
88
71
|
native.apSetInactiveTimeout(seconds)
|
|
89
72
|
},
|
|
90
73
|
|
|
91
|
-
|
|
92
|
-
|
|
74
|
+
on<K extends keyof ApEventMap>(event: K, listener: ApEventMap[K]) {
|
|
75
|
+
native.on(event, listener as (...args: unknown[]) => void)
|
|
76
|
+
},
|
|
77
|
+
|
|
78
|
+
off<K extends keyof ApEventMap>(event: K, listener: ApEventMap[K]) {
|
|
79
|
+
native.off(event, listener as (...args: unknown[]) => void)
|
|
80
|
+
},
|
|
93
81
|
}
|
|
94
82
|
|
|
95
83
|
const MAX_CONNECT_RETRIES = 5
|
|
@@ -147,9 +135,13 @@ const wifi: Wifi = {
|
|
|
147
135
|
return ok(asyncResult.value as ScanResult[])
|
|
148
136
|
},
|
|
149
137
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
138
|
+
on<K extends keyof WifiEventMap>(event: K, listener: WifiEventMap[K]) {
|
|
139
|
+
native.on(event, listener as (...args: unknown[]) => void)
|
|
140
|
+
},
|
|
141
|
+
|
|
142
|
+
off<K extends keyof WifiEventMap>(event: K, listener: WifiEventMap[K]) {
|
|
143
|
+
native.off(event, listener as (...args: unknown[]) => void)
|
|
144
|
+
},
|
|
153
145
|
|
|
154
146
|
get mac(): string {
|
|
155
147
|
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
|
+
}
|
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
|
|