@mikrojs/firmware 0.17.0 → 0.17.1-next.20260731001908

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.
@@ -117,6 +117,17 @@ target_compile_options(${COMPONENT_LIB} PUBLIC $<$<COMPILE_LANGUAGE:CXX>:-fpermi
117
117
  # own C/C++ can be bitten by aliasing UB during optimization.
118
118
  target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-implicit-fallthrough -Wno-maybe-uninitialized -Wno-missing-field-initializers -fno-strict-aliasing)
119
119
 
120
+ # NDEBUG for the QuickJS sources only: without it quickjs.c enables
121
+ # ENABLE_DUMPS, which adds a list_head (+8 bytes) to every JSString, links
122
+ # each string into rt->string_list, and keeps assert() live in the
123
+ # interpreter. ESP-IDF keeps assertions enabled globally, so NDEBUG is not
124
+ # set project-wide; scoping it here leaves IDF and mikrojs asserts intact.
125
+ # -Wno-unused-but-set-variable: several quickjs.c variables are only read
126
+ # inside assert(), which NDEBUG compiles away.
127
+ set_source_files_properties(${QUICKJS_SOURCES} PROPERTIES
128
+ COMPILE_DEFINITIONS NDEBUG
129
+ COMPILE_OPTIONS "-Wno-unused-but-set-variable")
130
+
120
131
  # ── Board and version metadata ──────────────────────────────────────
121
132
  execute_process(
122
133
  COMMAND node ${_MIK_RESOLVE} version
@@ -253,8 +253,38 @@ static void mik__wifi_event_handler(void* arg, esp_event_base_t event_base, int3
253
253
  }
254
254
  }
255
255
 
256
+ /* Shape the device's stored name into an RFC 1123 hostname label: extract the
257
+ * name from the `[rev, name]` JSON pair, lowercase it, map anything outside
258
+ * [a-z0-9] to `-`, collapse runs, trim hyphens at both ends, cap at 63 chars.
259
+ * Returns false when no usable label comes out. */
260
+ static bool mik__wifi_hostname_from_device_name(char* out, size_t out_size) {
261
+ const MIKPlatform* platform = MIK_GetPlatform();
262
+ if (!platform->get_device_name) return false;
263
+ const char* pair = platform->get_device_name();
264
+ if (!pair) return false;
265
+ const char* p = strchr(pair, '"');
266
+ if (!p) return false;
267
+ size_t n = 0;
268
+ for (p++; *p != '\0' && *p != '"'; p++) {
269
+ /* CLI-validated names contain no escapes; a backslash means this pair
270
+ * came from somewhere else, so don't guess at decoding it. */
271
+ if (*p == '\\') return false;
272
+ char c = *p;
273
+ if (c >= 'A' && c <= 'Z') c = static_cast<char>(c - 'A' + 'a');
274
+ if (!((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9'))) c = '-';
275
+ if (c == '-' && (n == 0 || out[n - 1] == '-')) continue;
276
+ if (n >= 63 || n + 1 >= out_size) break;
277
+ out[n++] = c;
278
+ }
279
+ while (n > 0 && out[n - 1] == '-') n--;
280
+ if (n == 0) return false;
281
+ out[n] = '\0';
282
+ return true;
283
+ }
284
+
256
285
  /* Pick the DHCP hostname for the STA netif: configured `wifi.hostname` from
257
- * mikro.config.json takes priority; otherwise default to `mikrojs-<device-id>`.
286
+ * mikro.config.json takes priority, then the device's stored name (shaped into
287
+ * a valid hostname label); otherwise default to `mikrojs-<device-id>`.
258
288
  * Returns true if a hostname was applied. */
259
289
  static bool mik__wifi_apply_hostname(JSContext* ctx) {
260
290
  if (!s_sta_netif) return false;
@@ -266,6 +296,9 @@ static bool mik__wifi_apply_hostname(JSContext* ctx) {
266
296
  hostname = mik_rt->config.wifi_hostname;
267
297
  }
268
298
  }
299
+ if (!hostname && mik__wifi_hostname_from_device_name(buf, sizeof(buf))) {
300
+ hostname = buf;
301
+ }
269
302
  if (!hostname) {
270
303
  const char* dev_id = MIK_GetPlatform()->get_device_id();
271
304
  if (!dev_id || !dev_id[0]) return false;
@@ -4,6 +4,7 @@
4
4
  #include "freertos/FreeRTOS.h"
5
5
  #include "freertos/task.h"
6
6
  #include "mikrojs.h"
7
+ #include "mikrojs/platform.h"
7
8
  #include "private.h"
8
9
  #include "quickjs.h"
9
10
  #include "unity.h"
@@ -427,6 +428,46 @@ TEST_CASE("Wifi hostname reflects configured wifi.hostname", "[wifi]") {
427
428
  teardown();
428
429
  }
429
430
 
431
+ TEST_CASE("Wifi hostname falls back to the device name", "[wifi]") {
432
+ /* No wifi.hostname configured, but a stored device name. The name pair is
433
+ * NVS-backed, so save and restore whatever the device really has. */
434
+ const MIKPlatform* platform = MIK_GetPlatform();
435
+ char saved[256] = {0};
436
+ const char* prev = platform->get_device_name();
437
+ bool had_name = prev != nullptr;
438
+ if (had_name) {
439
+ snprintf(saved, sizeof(saved), "%s", prev);
440
+ }
441
+ platform->set_device_name("[1,\"Living_Room.Sensor\"]");
442
+
443
+ esp_wifi_disconnect();
444
+ vTaskDelay(pdMS_TO_TICKS(100));
445
+ rt = MIK_NewRuntime();
446
+ ctx = MIK_GetJSContext(rt);
447
+ MIKConfig cfg;
448
+ MIK_DefaultConfig(&cfg);
449
+ snprintf(cfg.wifi_country, sizeof(cfg.wifi_country), "US");
450
+ MIK_SetConfig(rt, &cfg);
451
+
452
+ JSValue ret = eval_module(R"(
453
+ import { Wifi } from "native:mikro/wifi";
454
+ globalThis.__hostname = new Wifi().getHostname();
455
+ )");
456
+ /* Restore before any assertion: a failing TEST_ASSERT longjmps out of the
457
+ * test body, and the hostname was already applied during the eval above. */
458
+ platform->set_device_name(had_name ? saved : nullptr);
459
+ TEST_ASSERT_FALSE_MESSAGE(JS_IsException(ret), "Module eval should not throw");
460
+
461
+ JSValue global = JS_GetGlobalObject(ctx);
462
+ JSValue hnVal = JS_GetPropertyStr(ctx, global, "__hostname");
463
+ const char* hn = JS_ToCString(ctx, hnVal);
464
+ TEST_ASSERT_EQUAL_STRING("living-room-sensor", hn);
465
+ JS_FreeCString(ctx, hn);
466
+ JS_FreeValue(ctx, hnVal);
467
+ JS_FreeValue(ctx, global);
468
+ teardown();
469
+ }
470
+
430
471
  TEST_CASE("Wifi getIpConfig returns object with expected keys", "[wifi]") {
431
472
  setup();
432
473
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikrojs/firmware",
3
- "version": "0.17.0",
3
+ "version": "0.17.1-next.20260731001908",
4
4
  "description": "Mikro.js ESP32 firmware: ESP-IDF component, build tools, and project template",
5
5
  "keywords": [
6
6
  "esp-idf",
@@ -47,8 +47,8 @@
47
47
  },
48
48
  "dependencies": {
49
49
  "esbuild": "^0.28.0",
50
- "@mikrojs/quickjs": "0.17.0",
51
- "@mikrojs/native": "0.17.0"
50
+ "@mikrojs/native": "0.17.1-next.20260731001908+ba9853e",
51
+ "@mikrojs/quickjs": "0.17.1-next.20260731001908+ba9853e"
52
52
  },
53
53
  "engines": {
54
54
  "node": ">=24.0.0"
Binary file
Binary file
Binary file
Binary file
Binary file