@mikrojs/native 0.22.0 → 0.23.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.
Files changed (46) hide show
  1. package/CMakeLists.txt +19 -0
  2. package/dist/runtime/result/native-result.node-shim.d.ts +2 -0
  3. package/dist/runtime/result/native-result.node-shim.d.ts.map +1 -1
  4. package/dist/runtime/result/native-result.node-shim.js +57 -0
  5. package/dist/runtime/result/native-result.node-shim.js.map +1 -1
  6. package/dist/runtime/result/types.d.ts +33 -0
  7. package/dist/runtime/result/types.d.ts.map +1 -1
  8. package/include/mikrojs/app_store.h +16 -0
  9. package/include/mikrojs/ble_port.h +50 -0
  10. package/include/mikrojs/build_install.h +36 -0
  11. package/include/mikrojs/device_store.h +84 -0
  12. package/include/mikrojs/http_native.h +52 -0
  13. package/include/mikrojs/mikrojs.h +19 -9
  14. package/include/mikrojs/ota.h +75 -0
  15. package/include/mikrojs/ota_env.h +1 -1
  16. package/include/mikrojs/platform.h +29 -0
  17. package/include/mikrojs/private.h +4 -1
  18. package/include/mikrojs/utils.h +5 -0
  19. package/package.json +4 -4
  20. package/prebuilds/darwin-arm64/mikrojs.napi.node +0 -0
  21. package/prebuilds/linux-arm64/mikrojs.napi.node +0 -0
  22. package/prebuilds/linux-x64/mikrojs.napi.node +0 -0
  23. package/runtime/http/server-impl.ts +5 -60
  24. package/runtime/http/server.ts +0 -1
  25. package/runtime/internal.d.ts +2 -0
  26. package/runtime/result/native-result.node-shim.ts +60 -0
  27. package/runtime/result/result.ts +1 -1
  28. package/runtime/result/types.ts +61 -0
  29. package/runtime/sys/sys.ts +1 -2
  30. package/src/mik_app_store.cpp +32 -0
  31. package/src/mik_ble.cpp +1573 -0
  32. package/src/mik_build_install.cpp +785 -0
  33. package/src/mik_config.cpp +330 -0
  34. package/src/mik_console.cpp +15 -24
  35. package/src/mik_deploy.cpp +759 -0
  36. package/src/mik_ota.cpp +1403 -0
  37. package/src/mik_ota_client_module.cpp +1142 -0
  38. package/src/mik_ota_env.cpp +318 -0
  39. package/src/mik_repl.cpp +12 -4
  40. package/src/mik_result.cpp +159 -0
  41. package/src/mik_sys.cpp +25 -51
  42. package/src/mik_test_supervisor.cpp +151 -0
  43. package/src/mik_watchdog.cpp +4 -5
  44. package/src/mik_wifi_client.cpp +1 -2
  45. package/src/mikrojs.cpp +15 -6
  46. package/src/modules.cpp +1 -1
@@ -0,0 +1,151 @@
1
+ #include <errno.h>
2
+ #include <stdio.h>
3
+ #include <stdlib.h>
4
+ #include <string.h>
5
+
6
+ #include "mikrojs/mikrojs.h"
7
+ #include "mikrojs/platform.h"
8
+ #include "mikrojs/private.h"
9
+
10
+ static const char* TAG = "mikrojs";
11
+
12
+ /* Fixed sizes instead of PATH_MAX scaling — on newlib PATH_MAX can be 4096,
13
+ * which is absurd for our purposes. These fit any realistic on-device test
14
+ * path. */
15
+ #define MIK_SUP_PATH_MAX 384
16
+ /* Exception text captured by MIK_RunEntryErr when a test file fails to
17
+ * evaluate. */
18
+ #define MIK_SUP_ERR_MAX 192
19
+
20
+ /* Minimal JSON string-escape into a bounded buffer. Handles `"`, `\`, and
21
+ * control characters; everything else copies verbatim. Returns bytes
22
+ * written (excluding NUL) on success, or -1 on overflow. Used to synthesize
23
+ * test-event JSON frames from raw C strings where the path may contain
24
+ * characters that would otherwise corrupt the frame. */
25
+ static int mik__json_escape(char* dst, size_t dst_size, const char* src) {
26
+ if (dst_size == 0) return -1;
27
+ size_t w = 0;
28
+ for (const unsigned char* p = (const unsigned char*)src; *p; p++) {
29
+ unsigned char c = *p;
30
+ if (c == '"' || c == '\\') {
31
+ if (w + 2 >= dst_size) return -1;
32
+ dst[w++] = '\\';
33
+ dst[w++] = (char)c;
34
+ } else if (c < 0x20) {
35
+ if (w + 7 >= dst_size) return -1;
36
+ int n = snprintf(dst + w, dst_size - w, "\\u%04x", c);
37
+ if (n < 0 || (size_t)n >= dst_size - w) return -1;
38
+ w += (size_t)n;
39
+ } else {
40
+ if (w + 1 >= dst_size) return -1;
41
+ dst[w++] = (char)c;
42
+ }
43
+ }
44
+ if (w >= dst_size) return -1;
45
+ dst[w] = '\0';
46
+ return (int)w;
47
+ }
48
+
49
+ /* Supervisor frame buffers live on these noinline helpers' stacks, called
50
+ * only while no JS runs. Not in the caller's frame: it stays live under the
51
+ * eval chain, and 1-2 KB of locals there faulted in mik_module_normalizer.
52
+ * Not on the heap: that held ~3 KB through every test file's network calls. */
53
+
54
+ /* Announce the file about to run so the CLI can confirm the supervisor's
55
+ * iteration matches its own testFiles order. The MSG_DEBUG frame is
56
+ * rendered as a dim log line. */
57
+ [[gnu::noinline]] static void mik__sup_announce(MIKReplTransport* transport, size_t i, size_t count,
58
+ const char* path) {
59
+ char dbg[MIK_SUP_PATH_MAX + 64];
60
+ int n = snprintf(dbg, sizeof(dbg), "[supervisor] running %zu/%zu: %s", i + 1, count, path);
61
+ if (n > 0 && n < (int)sizeof(dbg)) {
62
+ mik__proto_send(transport, MIK_MSG_DEBUG, dbg, n);
63
+ }
64
+ }
65
+
66
+ /* Synthesize a failing test + run_done so the CLI accounts for this file
67
+ * instead of stalling waiting for a run_done the runtime will never emit.
68
+ * `err` is the captured exception text, or null. */
69
+ [[gnu::noinline]] static void mik__sup_report_failure(MIKReplTransport* transport,
70
+ const char* path, const char* reason,
71
+ const char* err) {
72
+ char esc[MIK_SUP_PATH_MAX * 2 + 8];
73
+ char err_esc[MIK_SUP_ERR_MAX * 2 + 8];
74
+ char buf[MIK_SUP_PATH_MAX * 2 + 512];
75
+
76
+ const MIKPlatform* platform = MIK_GetPlatform();
77
+ if (platform && platform->log) platform->log(MIK_LOG_ERROR, TAG, "%s: %s", reason, path);
78
+ /* Escape the path so any `"` or `\` in it doesn't corrupt the JSON frame. */
79
+ if (mik__json_escape(esc, sizeof(esc), path) < 0) {
80
+ /* Path too long to fit even escaped — fall back to basename so the
81
+ * frame at least identifies something. */
82
+ const char* base = strrchr(path, '/');
83
+ if (!base || mik__json_escape(esc, sizeof(esc), base + 1) < 0) {
84
+ esc[0] = '?';
85
+ esc[1] = '\0';
86
+ }
87
+ }
88
+ /* Append the captured exception text (escaped) so the CLI shows the
89
+ * actual error, not just "Evaluation threw". */
90
+ err_esc[0] = '\0';
91
+ if (err && err[0] != '\0') {
92
+ if (mik__json_escape(err_esc, sizeof(err_esc), err) < 0) {
93
+ err_esc[0] = '\0';
94
+ }
95
+ }
96
+ int n;
97
+ if (err_esc[0] != '\0') {
98
+ n = snprintf(buf, sizeof(buf),
99
+ "{\"e\":3,\"s\":\"<load>\",\"t\":\"%s\",\"d\":0,\"m\":\"%s: %s\"}", esc,
100
+ reason, err_esc);
101
+ } else {
102
+ n = snprintf(buf, sizeof(buf),
103
+ "{\"e\":3,\"s\":\"<load>\",\"t\":\"%s\",\"d\":0,\"m\":\"%s\"}", esc, reason);
104
+ }
105
+ if (n > 0 && n < (int)sizeof(buf)) {
106
+ mik__proto_send(transport, MIK_MSG_TEST, buf, n);
107
+ }
108
+ static const char kRunDone[] = "{\"e\":6,\"p\":0,\"f\":1,\"k\":0,\"o\":0,\"d\":0}";
109
+ mik__proto_send(transport, MIK_MSG_TEST, kRunDone, sizeof(kRunDone) - 1);
110
+ }
111
+
112
+ void MIK_RunTestManifest(MIKReplTransport* transport, char** paths, size_t count,
113
+ MIKRuntime* (*create)(void* opaque), void* opaque) {
114
+ for (size_t i = 0; i < count; i++) {
115
+ mik__sup_announce(transport, i, count, paths[i]);
116
+ MIKRuntime* rt = create(opaque);
117
+ MIK_EnableTestHelpers(rt);
118
+ MIK_ProtocolAttach(rt);
119
+ /* Held only across the entry eval; a null buffer just drops the
120
+ * exception text. */
121
+ auto* err = static_cast<char*>(malloc(MIK_SUP_ERR_MAX));
122
+ int rc = MIK_RunEntryErr(rt, paths[i], err, err ? MIK_SUP_ERR_MAX : 0);
123
+ const char* fail_reason = nullptr;
124
+ if (rc == -ENOENT) {
125
+ fail_reason = "Test file not found";
126
+ } else if (rc == -EFAULT) {
127
+ fail_reason = "Evaluation threw";
128
+ } else {
129
+ free(err);
130
+ err = nullptr;
131
+ /* Entry eval returned successfully (rc == 0). The test module
132
+ * may still asynchronously reject (e.g. top-level throw in a
133
+ * module eval'd as a Promise) — that's caught after ServeLoop
134
+ * by inspecting MIK_IsStopRequested below. */
135
+ MIK_ProtocolServeLoop();
136
+ if (MIK_IsStopRequested(rt)) {
137
+ fail_reason = "Unhandled rejection";
138
+ }
139
+ }
140
+ if (fail_reason) {
141
+ mik__sup_report_failure(transport, paths[i], fail_reason, err);
142
+ }
143
+ free(err);
144
+ MIK_ProtocolDetach();
145
+ MIK_FreeRuntime(rt);
146
+ }
147
+
148
+ /* Signal end-of-manifest so the CLI can finalize its report
149
+ * without waiting on a silent stream. */
150
+ mik__proto_send(transport, MIK_MSG_MANIFEST_DONE, nullptr, 0);
151
+ }
@@ -19,8 +19,8 @@ void mik__blocking_begin(MIKRuntime* mik_rt) {
19
19
  mik_rt->blocking_armed = true;
20
20
  }
21
21
 
22
- int mik__watchdog_interrupt(JSRuntime* rt, void* opaque) {
23
- (void)rt;
22
+ int mik__watchdog_interrupt(JSContext* ctx, void* opaque) {
23
+ (void)ctx;
24
24
  MIKRuntime* mik_rt = static_cast<MIKRuntime*>(opaque);
25
25
  int budget_ms = mik_rt->config.blocking_timeout_ms;
26
26
  if (budget_ms <= 0 || !mik_rt->blocking_armed) {
@@ -109,9 +109,8 @@ void mik__watchdog_check(MIKRuntime* mik_rt) {
109
109
  mik__print_error_line("[watchdog] WATCHDOG TRIGGERED: time since last feed() exceeded "
110
110
  "configured limit of %d ms",
111
111
  mik_rt->config.feed_timeout_ms);
112
- /* No JS frame is active here, so uncatchable marking is moot: the
113
- * JS_HasException branch in MIK_Loop routes this before any user
114
- * code runs. */
112
+ /* No JS frame is active here: the JS_HasException branch in
113
+ * MIK_Loop routes this before any user code runs. */
115
114
  JS_ThrowInternalError(mik_rt->ctx,
116
115
  "watchdog: time since last feed() exceeded configured limit "
117
116
  "of %d ms",
@@ -634,8 +634,7 @@ int wc_module_init(JSContext* ctx, JSModuleDef* m) {
634
634
  JS_FreeValue(ctx, ns);
635
635
  if (JS_IsException(wifi_ctor)) return -1;
636
636
 
637
- /* One native instance per runtime, constructed at import (TS parity:
638
- * `new NativeWifi()` ran at module scope and its throw failed the import). */
637
+ /* One native instance per runtime, constructed at import. */
639
638
  JSValue nat = JS_CallConstructor(ctx, wifi_ctor, 0, nullptr);
640
639
  JS_FreeValue(ctx, wifi_ctor);
641
640
  if (JS_IsException(nat)) return -1;
package/src/mikrojs.cpp CHANGED
@@ -103,7 +103,7 @@ static const char* const sys_exports[] = {
103
103
  "gc", "setTime", "uptime", "restart",
104
104
  "version", "board", "firmware", "deviceId",
105
105
  "deviceName", "setDeviceName", "resetReason", "activeTimers",
106
- "unloadNamespace", "isUnloadableNamespace"};
106
+ "unloadNamespace", "isUnloadableNamespace", "getWakeupCause"};
107
107
 
108
108
  static int mik__sys_module_init(JSContext* ctx, JSModuleDef* m) {
109
109
  JSValue ns = JS_NewObjectProto(ctx, JS_NULL);
@@ -187,11 +187,10 @@ void mik__flush_unhandled_rejections(JSContext* ctx) {
187
187
  std::vector<MIKRejectedPromise> rejected;
188
188
  rejected.swap(mik_rt->pending_rejections);
189
189
 
190
- /* The same reason can reach reporting twice: the entry-eval path reads a
191
- * rejected module promise by value and the host dumps it via the throw
192
- * path while the promise itself stays queued here. mik__report_uncaught
193
- * dedups by reason-object identity, so the duplicate is a no-op; only act
194
- * on a fresh report so the host bridge and the halt don't fire twice.
190
+ /* Several queued promises can share one reason (`p.then(a); p.then(b)` on a
191
+ * rejected `p`). mik__report_uncaught dedups by reason-object identity, so
192
+ * the loop acts only on a fresh report, and the host bridge and the halt
193
+ * fire once.
195
194
  * (quickjs-libc reports every entry; we collapse same-error duplicates.) */
196
195
  for (const MIKRejectedPromise& rp : rejected) {
197
196
  if (mik__report_uncaught(ctx, rp.reason, true)) {
@@ -317,6 +316,7 @@ MIKRuntime* MIK_NewRuntimeInternal(MIKRunOptions* options) {
317
316
  mik_rt->result_ok_void_singleton = JS_UNDEFINED;
318
317
  mik_rt->http_body_consumed_ctor = JS_UNDEFINED;
319
318
  mik_rt->inspect_cache.object_proto = JS_UNDEFINED;
319
+ mik_rt->reported_atom = JS_ATOM_NULL;
320
320
 
321
321
  /* Default fs read cap: 64 KiB. Large enough for typical config/JSON
322
322
  * payloads on MCU; small enough that a runaway readFile() can't
@@ -349,6 +349,13 @@ MIKRuntime* MIK_NewRuntimeInternal(MIKRunOptions* options) {
349
349
 
350
350
  /* unhandled promise rejection tracker */
351
351
  JS_SetHostPromiseRejectionTracker(rt, mik__promise_rejection_tracker, NULL);
352
+ JSValue reported_sym = JS_NewPrivateSymbol(ctx, "mik_reported");
353
+ if (JS_IsException(reported_sym)) {
354
+ JS_FreeValue(ctx, JS_GetException(ctx));
355
+ } else {
356
+ mik_rt->reported_atom = JS_ValueToAtom(ctx, reported_sym);
357
+ JS_FreeValue(ctx, reported_sym);
358
+ }
352
359
 
353
360
  /* Blocking watchdog: polled every 10k branches/calls. */
354
361
  JS_SetInterruptHandler(rt, mik__watchdog_interrupt, mik_rt);
@@ -455,6 +462,8 @@ void MIK_FreeRuntime(MIKRuntime* mik_rt) {
455
462
  JS_FreeValue(mik_rt->ctx, ic.object_proto);
456
463
  ic.object_proto = JS_UNDEFINED;
457
464
  JS_FreeContext(mik_rt->ctx);
465
+ JS_FreeAtomRT(mik_rt->rt, mik_rt->reported_atom);
466
+ mik_rt->reported_atom = JS_ATOM_NULL;
458
467
  MIKResultAtoms& ra = mik_rt->result_atoms;
459
468
  JS_FreeAtomsRT(mik_rt->rt, ra.ok, ra.value, ra.error, ra.err, ra.code, ra.message, ra.name);
460
469
  ra = {};
package/src/modules.cpp CHANGED
@@ -1021,7 +1021,7 @@ JSValue mik__load_module_ns(JSContext* ctx, const char* importer, const char* sp
1021
1021
  JSAtom name = JS_NewAtom(ctx, specifier);
1022
1022
  JSModuleDef* m = JS_FindLoadedModule(ctx, name);
1023
1023
  if (!m) {
1024
- JSValue p = JS_LoadModule(ctx, importer, specifier);
1024
+ JSValue p = JS_LoadModule(ctx, importer, specifier, JS_UNDEFINED);
1025
1025
  if (JS_IsException(p)) {
1026
1026
  /* Propagate the loader's real error (e.g. a syntax error in a
1027
1027
  * virtual stub), not a generic resolution failure. */