@mikrojs/native 0.21.1-next.20260927212749 → 0.22.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.
@@ -45,10 +45,29 @@ static inline JSValue new_result_obj(JSContext* ctx) {
45
45
  return JS_NewObjectProto(ctx, proto);
46
46
  }
47
47
 
48
+ /* Created by mik__result_init, which runs before anything can build a Result. */
49
+ static inline const MIKResultAtoms& result_atoms(JSContext* ctx) {
50
+ return MIK_GetRuntime(ctx)->result_atoms;
51
+ }
52
+
53
+ static inline void define_prop(JSContext* ctx, JSValue obj, JSAtom atom, JSValue val) {
54
+ JS_DefinePropertyValue(ctx, obj, atom, val, JS_PROP_C_W_E);
55
+ }
56
+
57
+ /* {ok: false, error} on the shared prototype; consumes `error` */
58
+ static JSValue new_err_result(JSContext* ctx, JSValue error) {
59
+ const MIKResultAtoms& a = result_atoms(ctx);
60
+ JSValue obj = new_result_obj(ctx);
61
+ define_prop(ctx, obj, a.ok, JS_FALSE);
62
+ define_prop(ctx, obj, a.error, error);
63
+ return obj;
64
+ }
65
+
48
66
  JSValue mik__result_ok(JSContext* ctx, JSValue value) {
67
+ const MIKResultAtoms& a = result_atoms(ctx);
49
68
  JSValue obj = new_result_obj(ctx);
50
- JS_DefinePropertyValueStr(ctx, obj, "ok", JS_TRUE, JS_PROP_C_W_E);
51
- JS_DefinePropertyValueStr(ctx, obj, "value", value, JS_PROP_C_W_E);
69
+ define_prop(ctx, obj, a.ok, JS_TRUE);
70
+ define_prop(ctx, obj, a.value, value);
52
71
  return obj;
53
72
  }
54
73
 
@@ -70,18 +89,15 @@ JSValue mik__result_err(JSContext* ctx, int code, int platform_errno, const char
70
89
  vsnprintf(msg, sizeof(msg), fmt, ap);
71
90
  va_end(ap);
72
91
 
92
+ const MIKResultAtoms& a = result_atoms(ctx);
73
93
  JSValue error = JS_NewObject(ctx);
74
- JS_DefinePropertyValueStr(ctx, error, "code", JS_NewInt32(ctx, code), JS_PROP_C_W_E);
75
- JS_DefinePropertyValueStr(ctx, error, "message", JS_NewString(ctx, msg), JS_PROP_C_W_E);
94
+ define_prop(ctx, error, a.code, JS_NewInt32(ctx, code));
95
+ define_prop(ctx, error, a.message, JS_NewString(ctx, msg));
76
96
  if (platform_errno) {
77
97
  JS_DefinePropertyValueStr(ctx, error, "errno", JS_NewInt32(ctx, platform_errno),
78
98
  JS_PROP_C_W_E);
79
99
  }
80
-
81
- JSValue obj = new_result_obj(ctx);
82
- JS_DefinePropertyValueStr(ctx, obj, "ok", JS_FALSE, JS_PROP_C_W_E);
83
- JS_DefinePropertyValueStr(ctx, obj, "error", error, JS_PROP_C_W_E);
84
- return obj;
100
+ return new_err_result(ctx, error);
85
101
  }
86
102
 
87
103
  static JSValue result_err_named_v(JSContext* ctx, const char* name, const char* fmt,
@@ -91,14 +107,11 @@ static JSValue result_err_named_v(JSContext* ctx, const char* name, const char*
91
107
  char msg[384];
92
108
  vsnprintf(msg, sizeof(msg), fmt, ap);
93
109
 
110
+ const MIKResultAtoms& a = result_atoms(ctx);
94
111
  JSValue error = JS_NewObject(ctx);
95
- JS_DefinePropertyValueStr(ctx, error, "name", JS_NewString(ctx, name), JS_PROP_C_W_E);
96
- JS_DefinePropertyValueStr(ctx, error, "message", JS_NewString(ctx, msg), JS_PROP_C_W_E);
97
-
98
- JSValue obj = new_result_obj(ctx);
99
- JS_DefinePropertyValueStr(ctx, obj, "ok", JS_FALSE, JS_PROP_C_W_E);
100
- JS_DefinePropertyValueStr(ctx, obj, "error", error, JS_PROP_C_W_E);
101
- return obj;
112
+ define_prop(ctx, error, a.name, JS_NewString(ctx, name));
113
+ define_prop(ctx, error, a.message, JS_NewString(ctx, msg));
114
+ return new_err_result(ctx, error);
102
115
  }
103
116
 
104
117
  JSValue mik__result_err_named(JSContext* ctx, const char* name, const char* fmt, ...) {
@@ -123,19 +136,12 @@ JSValue MIK_ResultErrNamed(JSContext* ctx, const char* name, const char* fmt, ..
123
136
 
124
137
  JSValue mik__result_err_tag(JSContext* ctx, const char* name) {
125
138
  JSValue error = JS_NewObject(ctx);
126
- JS_DefinePropertyValueStr(ctx, error, "name", JS_NewString(ctx, name), JS_PROP_C_W_E);
127
-
128
- JSValue obj = new_result_obj(ctx);
129
- JS_DefinePropertyValueStr(ctx, obj, "ok", JS_FALSE, JS_PROP_C_W_E);
130
- JS_DefinePropertyValueStr(ctx, obj, "error", error, JS_PROP_C_W_E);
131
- return obj;
139
+ define_prop(ctx, error, result_atoms(ctx).name, JS_NewString(ctx, name));
140
+ return new_err_result(ctx, error);
132
141
  }
133
142
 
134
143
  JSValue mik__result_err_obj(JSContext* ctx, JSValue error_obj) {
135
- JSValue obj = new_result_obj(ctx);
136
- JS_DefinePropertyValueStr(ctx, obj, "ok", JS_FALSE, JS_PROP_C_W_E);
137
- JS_DefinePropertyValueStr(ctx, obj, "error", error_obj, JS_PROP_C_W_E);
138
- return obj;
144
+ return new_err_result(ctx, error_obj);
139
145
  }
140
146
 
141
147
  /* ── Factory functions exported as `ok` / `err` ─────────────────── */
@@ -148,29 +154,24 @@ static JSValue js_result_ok(JSContext* ctx, JSValue this_val, int argc, JSValue*
148
154
  }
149
155
 
150
156
  static JSValue js_result_err(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) {
151
- JSValue obj = new_result_obj(ctx);
152
- JS_DefinePropertyValueStr(ctx, obj, "ok", JS_FALSE, JS_PROP_C_W_E);
153
- JS_DefinePropertyValueStr(ctx, obj, "error",
154
- argc > 0 ? JS_DupValue(ctx, argv[0]) : JS_UNDEFINED,
155
- JS_PROP_C_W_E);
156
- return obj;
157
+ return new_err_result(ctx, argc > 0 ? JS_DupValue(ctx, argv[0]) : JS_UNDEFINED);
157
158
  }
158
159
 
159
160
  /* ── Prototype method helpers ───────────────────────────────────── */
160
161
 
161
162
  static inline bool result_is_ok(JSContext* ctx, JSValue this_val) {
162
- JSValue ok_val = JS_GetPropertyStr(ctx, this_val, "ok");
163
+ JSValue ok_val = JS_GetProperty(ctx, this_val, result_atoms(ctx).ok);
163
164
  bool is_ok = JS_ToBool(ctx, ok_val) != 0;
164
165
  JS_FreeValue(ctx, ok_val);
165
166
  return is_ok;
166
167
  }
167
168
 
168
169
  static JSValue result_get_value(JSContext* ctx, JSValue this_val) {
169
- return JS_GetPropertyStr(ctx, this_val, "value");
170
+ return JS_GetProperty(ctx, this_val, result_atoms(ctx).value);
170
171
  }
171
172
 
172
173
  static JSValue result_get_error(JSContext* ctx, JSValue this_val) {
173
- return JS_GetPropertyStr(ctx, this_val, "error");
174
+ return JS_GetProperty(ctx, this_val, result_atoms(ctx).error);
174
175
  }
175
176
 
176
177
  /* ── Prototype methods ──────────────────────────────────────────── */
@@ -202,10 +203,7 @@ static JSValue js_result_map_err(JSContext* ctx, JSValue this_val, int argc, JSV
202
203
  JSValue mapped = JS_Call(ctx, argv[0], JS_UNDEFINED, 1, &error);
203
204
  JS_FreeValue(ctx, error);
204
205
  if (JS_IsException(mapped)) return JS_EXCEPTION;
205
- JSValue obj = new_result_obj(ctx);
206
- JS_DefinePropertyValueStr(ctx, obj, "ok", JS_FALSE, JS_PROP_C_W_E);
207
- JS_DefinePropertyValueStr(ctx, obj, "error", mapped, JS_PROP_C_W_E);
208
- return obj;
206
+ return new_err_result(ctx, mapped);
209
207
  }
210
208
 
211
209
  /* .andThen(fn) — if ok, call fn(value) and return its result; else pass through */
@@ -227,14 +225,15 @@ static JSValue js_result_match(JSContext* ctx, JSValue this_val, int argc, JSVal
227
225
  if (argc < 1 || !JS_IsObject(argv[0])) {
228
226
  return JS_ThrowTypeError(ctx, "Result.match: expected {ok, err} handlers");
229
227
  }
230
- const char* key = result_is_ok(ctx, this_val) ? "ok" : "err";
231
- JSValue handler = JS_GetPropertyStr(ctx, argv[0], key);
228
+ const MIKResultAtoms& a = result_atoms(ctx);
229
+ bool is_ok = result_is_ok(ctx, this_val);
230
+ const char* key = is_ok ? "ok" : "err";
231
+ JSValue handler = JS_GetProperty(ctx, argv[0], is_ok ? a.ok : a.err);
232
232
  if (!JS_IsFunction(ctx, handler)) {
233
233
  JS_FreeValue(ctx, handler);
234
234
  return JS_ThrowTypeError(ctx, "Result.match: missing '%s' handler", key);
235
235
  }
236
- JSValue payload =
237
- result_is_ok(ctx, this_val) ? result_get_value(ctx, this_val) : result_get_error(ctx, this_val);
236
+ JSValue payload = is_ok ? result_get_value(ctx, this_val) : result_get_error(ctx, this_val);
238
237
  JSValue ret = JS_Call(ctx, handler, JS_UNDEFINED, 1, &payload);
239
238
  JS_FreeValue(ctx, payload);
240
239
  JS_FreeValue(ctx, handler);
@@ -345,6 +344,15 @@ static int mik__result_module_init(JSContext* ctx, JSModuleDef* m) {
345
344
  JSModuleDef* mik__result_init(JSContext* ctx) {
346
345
  MIKRuntime* rt = MIK_GetRuntime(ctx);
347
346
  if (rt && JS_IsUndefined(rt->result_proto)) {
347
+ MIKResultAtoms& a = rt->result_atoms;
348
+ a.ok = JS_NewAtom(ctx, "ok");
349
+ a.value = JS_NewAtom(ctx, "value");
350
+ a.error = JS_NewAtom(ctx, "error");
351
+ a.err = JS_NewAtom(ctx, "err");
352
+ a.code = JS_NewAtom(ctx, "code");
353
+ a.message = JS_NewAtom(ctx, "message");
354
+ a.name = JS_NewAtom(ctx, "name");
355
+
348
356
  JSValue proto = JS_NewObject(ctx);
349
357
  mik__result_install_methods(ctx, proto);
350
358
  rt->result_proto = proto;
package/src/mik_sys.cpp CHANGED
@@ -171,7 +171,7 @@ static JSValue mik__sys_restart(JSContext* ctx, JSValue this_val, int argc, JSVa
171
171
  return JS_UNDEFINED;
172
172
  }
173
173
 
174
- const char* mik__board_name(void) {
174
+ const char* mik__firmware_name(void) {
175
175
  #ifdef MIK_BOARD_NAME
176
176
  return MIK_BOARD_NAME;
177
177
  #elif defined(CONFIG_IDF_TARGET)
@@ -181,6 +181,36 @@ const char* mik__board_name(void) {
181
181
  #endif
182
182
  }
183
183
 
184
+ #ifdef CONFIG_IDF_TARGET
185
+ /* The board name `mikro flash` writes into a copy of the image when it flashes
186
+ * the generic firmware for a board (packages/mikro/src/cli/lib/boardName.ts).
187
+ * ESP-IDF places .rodata_custom_desc right after esp_app_desc_t, at image
188
+ * offset 0x120. Empty in the built image. */
189
+ struct MIKBoardDesc {
190
+ uint32_t magic;
191
+ char board[64];
192
+ };
193
+ static const MIKBoardDesc s_board_desc
194
+ __attribute__((section(".rodata_custom_desc"), used)) = {0x424b494d /* "MIKB" */, {}};
195
+
196
+ /* Copied through volatile reads: the compiler knows the empty initializer and
197
+ * would otherwise fold every read of it. */
198
+ static const char* mik__patched_board_name(void) {
199
+ static char name[sizeof(s_board_desc.board)];
200
+ const volatile char* src = s_board_desc.board;
201
+ for (size_t i = 0; i + 1 < sizeof(name) && src[i] != '\0'; i++) name[i] = src[i];
202
+ return name;
203
+ }
204
+ #endif
205
+
206
+ const char* mik__board_name(void) {
207
+ #ifdef CONFIG_IDF_TARGET
208
+ static const char* patched = mik__patched_board_name();
209
+ if (patched[0] != '\0') return patched;
210
+ #endif
211
+ return mik__firmware_name();
212
+ }
213
+
184
214
  static JSValue mik__sys_board(JSContext* ctx) {
185
215
  JSValue obj = JS_NewObject(ctx);
186
216
  JS_SetPropertyStr(ctx, obj, "name", JS_NewString(ctx, mik__board_name()));
@@ -248,6 +278,7 @@ static JSValue mik__sys_board(JSContext* ctx) {
248
278
 
249
279
  static JSValue mik__sys_firmware(JSContext* ctx) {
250
280
  JSValue obj = JS_NewObject(ctx);
281
+ JS_SetPropertyStr(ctx, obj, "name", JS_NewString(ctx, mik__firmware_name()));
251
282
  #ifdef CONFIG_IDF_TARGET
252
283
  const esp_app_desc_t* desc = esp_app_get_description();
253
284
  char elf_hash[65];