@mikrojs/native 0.21.1-next.20260928214050 → 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 (52) hide show
  1. package/CMakeLists.txt +21 -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 +35 -0
  17. package/include/mikrojs/private.h +13 -3
  18. package/include/mikrojs/utils.h +17 -7
  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/modules.json +1 -1
  27. package/runtime/result/native-result.node-shim.ts +60 -0
  28. package/runtime/result/result.ts +1 -1
  29. package/runtime/result/types.ts +61 -0
  30. package/runtime/sys/sys.ts +1 -2
  31. package/runtime/sys/types.ts +5 -2
  32. package/src/mik_app_store.cpp +32 -0
  33. package/src/mik_ble.cpp +1573 -0
  34. package/src/mik_build_install.cpp +785 -0
  35. package/src/mik_config.cpp +330 -0
  36. package/src/mik_console.cpp +15 -24
  37. package/src/mik_deploy.cpp +759 -0
  38. package/src/mik_ota.cpp +1403 -0
  39. package/src/mik_ota_client_module.cpp +1142 -0
  40. package/src/mik_ota_env.cpp +318 -0
  41. package/src/mik_repl.cpp +15 -6
  42. package/src/mik_result.cpp +159 -0
  43. package/src/mik_sys.cpp +57 -52
  44. package/src/mik_test.cpp +1334 -0
  45. package/src/mik_test_supervisor.cpp +151 -0
  46. package/src/mik_udp.cpp +4 -1
  47. package/src/mik_watchdog.cpp +4 -5
  48. package/src/mik_wifi_client.cpp +1 -2
  49. package/src/mikrojs.cpp +15 -6
  50. package/src/modules.cpp +2 -1
  51. package/src/utils.cpp +1 -2
  52. package/runtime/test/test.ts +0 -686
@@ -153,6 +153,9 @@ struct MIKRuntime {
153
153
  * module. Created lazily on first http module init, freed in
154
154
  * MIK_FreeRuntime. */
155
155
  JSValue http_body_consumed_ctor;
156
+ /* Private-symbol atom marking error objects mik__report_uncaught already
157
+ * reported. Created with the runtime, freed after JS_FreeContext. */
158
+ JSAtom reported_atom;
156
159
  JSValue env_obj; /* Frozen object with env vars, set on import.meta.env */
157
160
  struct {
158
161
  JSValue on_data; /* JS callback for stdin data, JS_UNDEFINED when not listening */
@@ -220,8 +223,12 @@ struct MIKRuntime {
220
223
  void mik__pub_fs_register(JSContext* ctx);
221
224
  void mik__stdio_init(JSContext* ctx, JSValue ns);
222
225
  void mik__sys_api_init(JSContext* ctx, JSValue ns);
223
- /* Board name of this build: MIK_BOARD_NAME if defined, else "<chip>-generic"
224
- * on device, "generic" on host. Static storage, never NULL. */
226
+ /* Name of this build: MIK_BOARD_NAME if defined, else "<chip>-generic" on
227
+ * device, "generic" on host. Static storage, never NULL. */
228
+ const char* mik__firmware_name(void);
229
+ /* The board: the name `mikro flash` wrote into the image when it flashed the
230
+ * generic firmware for a board, else mik__firmware_name(). Static storage,
231
+ * never NULL. */
225
232
  const char* mik__board_name(void);
226
233
  void mik__text_encoding_init(JSContext* ctx, JSValue global);
227
234
  /* UTF-8 pass shared by TextDecoder and the http body drain: each invalid byte
@@ -344,13 +351,16 @@ JSModuleDef* mik__observable_operators_load(JSContext* ctx);
344
351
  int mik__observable_multicast_new(JSContext* ctx, JSValue* observable, JSValue* next,
345
352
  JSValue* complete);
346
353
 
354
+ /* Test runner module (mik_test.cpp): mikro/test, C-module table in modules.cpp. */
355
+ JSModuleDef* mik__test_load(JSContext* ctx);
356
+
347
357
  /* Watchdog (mik_watchdog.cpp). */
348
358
  /* Start a fresh blocking budget: top of each MIK_Loop pass, eval entry
349
359
  * points, REPL eval, and on return from a deliberately blocking native
350
360
  * (lightSleep). One store; the interrupt handler stamps on its next poll. */
351
361
  void mik__blocking_begin(struct MIKRuntime* mik_rt);
352
362
  /* JS_SetInterruptHandler callback; opaque is the MIKRuntime*. */
353
- int mik__watchdog_interrupt(JSRuntime* rt, void* opaque);
363
+ int mik__watchdog_interrupt(JSContext* ctx, void* opaque);
354
364
  /* If the blocking deadline was hit, log the watchdog line and clear the
355
365
  * flag. Returns whether it did; callers print the error trace after. */
356
366
  bool mik__watchdog_report_blocking(struct MIKRuntime* mik_rt);
@@ -11,19 +11,24 @@
11
11
  struct AssertionInfo {
12
12
  const char* file_line; // filename:line
13
13
  const char* message;
14
- const char* function;
15
14
  };
16
15
 
17
- #define ERROR_AND_ABORT(expr) \
18
- do { \
19
- static const struct AssertionInfo args = {__FILE__ ":" STRINGIFY(__LINE__), #expr, \
20
- PRETTY_FUNCTION_NAME}; \
21
- mik_assert(args); \
16
+ /* Base name only: __FILE__ would put the build machine's absolute path in flash at every
17
+ * call site. */
18
+ #ifdef __FILE_NAME__
19
+ #define MIK__FILE_NAME __FILE_NAME__
20
+ #else
21
+ #define MIK__FILE_NAME __FILE__
22
+ #endif
23
+
24
+ #define ERROR_AND_ABORT(expr) \
25
+ do { \
26
+ static const struct AssertionInfo args = {MIK__FILE_NAME ":" STRINGIFY(__LINE__), #expr}; \
27
+ mik_assert(args); \
22
28
  } while (0)
23
29
 
24
30
  #define MIK__LIKELY(expr) __builtin_expect(!!(expr), 1)
25
31
  #define MIK__UNLIKELY(expr) __builtin_expect(!!(expr), 0)
26
- #define PRETTY_FUNCTION_NAME __PRETTY_FUNCTION__
27
32
 
28
33
  #define STRINGIFY_(x) #x
29
34
  #define STRINGIFY(x) STRINGIFY_(x)
@@ -152,3 +157,8 @@ JSValue mik__result_err_tag(JSContext* ctx, const char* name);
152
157
  * ownership of `error_obj`. For variants with extra fields beyond
153
158
  * {name, message} — path, code, limit, etc. */
154
159
  JSValue mik__result_err_obj(JSContext* ctx, JSValue error_obj);
160
+ /* Throw a PanicError, for a failure with no Result to return. Use it for
161
+ * hardware failures instead of JS_ThrowInternalError, which means the engine
162
+ * itself is in a bad state and which JS code cannot catch. */
163
+ JSValue mik__throw_panic(JSContext* ctx, const char* fmt, ...)
164
+ __attribute__((format(printf, 2, 3)));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikrojs/native",
3
- "version": "0.21.1-next.20260928214050",
3
+ "version": "0.23.0",
4
4
  "description": "Mikro.js C++ runtime library and Node.js native addon",
5
5
  "keywords": [
6
6
  "esp32",
@@ -86,14 +86,14 @@
86
86
  "./runtime/wifi/types": "./runtime/wifi/types.ts"
87
87
  },
88
88
  "dependencies": {
89
- "@mikrojs/quickjs": "0.21.1-next.20260928214050+4d8353a",
89
+ "@mikrojs/quickjs": "0.23.0",
90
90
  "cmake-js": "^8.0.0",
91
91
  "node-addon-api": "^8.9.2",
92
92
  "node-gyp-build": "^4.8.4"
93
93
  },
94
94
  "devDependencies": {
95
- "@mikrojs/registry": "0.21.1-next.20260928214050+4d8353a",
96
- "@mikrojs/schema": "0.21.1-next.20260928214050+4d8353a",
95
+ "@mikrojs/registry": "0.23.0",
96
+ "@mikrojs/schema": "0.23.0",
97
97
  "@swc/core": "^1.16.2",
98
98
  "@types/node": "^24.12.2",
99
99
  "esbuild": "^0.28.2",
@@ -54,22 +54,9 @@ export interface ServerResponse {
54
54
 
55
55
  export type Handler = (req: ServerRequest) => ServerResponse | Promise<ServerResponse>
56
56
 
57
- /**
58
- * Called when a handler panics (throws or rejects) — an unexpected failure, not
59
- * the place for normal error handling. Receives the thrown value and the
60
- * request. Return a `ServerResponse` to send it; return nothing to fall back to
61
- * a plain 500. If the hook itself throws, the 500 fallback is used.
62
- */
63
- export type PanicHandler = (
64
- error: unknown,
65
- req: ServerRequest,
66
- ) => ServerResponse | undefined | void | Promise<ServerResponse | undefined | void>
67
-
68
57
  export interface ServerOptions {
69
58
  /** Cap on the buffered request body, in bytes (default 16 KiB). */
70
59
  maxBodySize?: number
71
- /** Hook invoked when a handler panics (throws). See {@link PanicHandler}. */
72
- onPanic?: PanicHandler
73
60
  }
74
61
 
75
62
  export interface Server {
@@ -201,10 +188,6 @@ async function streamResponse(
201
188
  // means the client disconnected, so stop pulling from the generator.
202
189
  if (!(await native.respondChunk(id, bytes))) break
203
190
  }
204
- } catch {
205
- // The generator threw after status + headers were already sent, so the
206
- // status can't change. End the response below to truncate it cleanly; the
207
- // server keeps running.
208
191
  } finally {
209
192
  // Only end a stream we actually started, so the worker is always released
210
193
  // exactly once.
@@ -230,58 +213,20 @@ async function sendResponse(
230
213
  }
231
214
  }
232
215
 
233
- const FALLBACK_500: ServerResponse = {status: 500, body: 'Internal Server Error'}
234
-
235
- // A handler that panics (throws) is isolated to its one request so the server
236
- // keeps serving: run the optional onPanic hook for a custom response (and
237
- // server-side visibility), else send a plain 500. The httpd worker is blocked
238
- // until we respond, so every branch must produce exactly one response.
239
- async function handlePanic(
240
- onPanic: PanicHandler | undefined,
241
- error: unknown,
242
- req: ServerRequest,
243
- ): Promise<ServerResponse> {
244
- if (!onPanic) return FALLBACK_500
245
- try {
246
- return (await onPanic(error, req)) ?? FALLBACK_500
247
- } catch {
248
- // The hook itself threw; don't let its bug hang the worker.
249
- return FALLBACK_500
250
- }
251
- }
252
-
253
216
  async function serveLoop(
254
217
  native: NativeHttpServerModule,
255
218
  handler: Handler,
256
219
  isRunning: () => boolean,
257
220
  cap: number,
258
- onPanic: PanicHandler | undefined,
259
221
  ): Promise<void> {
260
222
  while (isRunning()) {
261
- let desc: NativeRequestDescriptor | {closed: true}
262
- try {
263
- desc = await native.nextRequest()
264
- } catch {
265
- break
266
- }
223
+ const desc = await native.nextRequest()
267
224
  if ('closed' in desc) break
268
225
 
269
226
  const req = makeServerRequest(native, desc, cap)
270
- // The whole per-request cycle (handler + send) is isolated so neither a
271
- // throwing handler nor a failed send can break the serve loop and stall
272
- // the server.
273
- try {
274
- let response: ServerResponse
275
- try {
276
- response = await handler(req)
277
- } catch (e) {
278
- response = await handlePanic(onPanic, e, req)
279
- }
280
- await sendResponse(native, desc.id, response)
281
- } catch {
282
- // Sending the response failed; the request is already past the point
283
- // where we can change it. Keep serving the next request.
284
- }
227
+ // A throw from the handler or its body generator is a panic, like a throw
228
+ // anywhere else: it leaves this loop as an unhandled rejection.
229
+ await sendResponse(native, desc.id, await handler(req))
285
230
  }
286
231
  }
287
232
 
@@ -295,7 +240,7 @@ export function createServerFromNative(native: NativeHttpServerModule): CreateSe
295
240
  const started = native.start(port, opts.maxBodySize)
296
241
  if (!started.ok) return started
297
242
  running = true
298
- void serveLoop(native, handler, () => running, cap, opts.onPanic)
243
+ void serveLoop(native, handler, () => running, cap)
299
244
  return ok()
300
245
  },
301
246
  close() {
@@ -20,7 +20,6 @@ export const createServer = createServerFromNative(native)
20
20
  export type {
21
21
  CreateServer,
22
22
  Handler,
23
- PanicHandler,
24
23
  RequestHeaders,
25
24
  Server,
26
25
  ServerOptions,
@@ -59,6 +59,7 @@ declare module 'native:mikro/result' {
59
59
  export function ok(): OkResult<void>
60
60
  export function ok<T>(value: T): OkResult<T>
61
61
  export function err<E>(error: E): ErrResult<E>
62
+ export {safe, tryCall} from '@mikrojs/native/runtime/result/types'
62
63
  }
63
64
 
64
65
  declare module 'native:mikro/observable' {
@@ -92,6 +93,7 @@ declare module 'native:mikro/sys' {
92
93
  export function setTime(millisSinceEpoch: number): void
93
94
  export function uptime(): {boot: number; rtc: number}
94
95
  export function restart(): never
96
+ export function getWakeupCause(): string
95
97
  export const version: string
96
98
  export const board: {
97
99
  name: string
@@ -23,7 +23,6 @@
23
23
  {"name": "stdio"},
24
24
  {"name": "stream"},
25
25
  {"name": "sys"},
26
- {"name": "test"},
27
26
  {"name": "udp"},
28
27
  {"name": "watchdog"},
29
28
  {"name": "ble", "native": "ble", "feature": "ble", "when": "ble"},
@@ -38,6 +37,7 @@
38
37
  {"name": "http/helpers", "bytecode": false},
39
38
  {"name": "observable/operators", "bytecode": false},
40
39
  {"name": "inspect", "bytecode": false},
40
+ {"name": "test", "bytecode": false},
41
41
  {"name": "wifi", "bytecode": false, "native": "wifi", "feature": "wifi", "when": "wifi"}
42
42
  ]
43
43
  }
@@ -36,6 +36,66 @@ export function ok(value?: unknown): unknown {
36
36
  return r
37
37
  }
38
38
 
39
+ // atob/btoa are untyped in this tsconfig
40
+ const g = globalThis as unknown as Record<string, unknown>
41
+ const safeBuiltins: unknown[] = [
42
+ JSON.parse,
43
+ JSON.stringify,
44
+ g.atob,
45
+ g.btoa,
46
+ decodeURI,
47
+ decodeURIComponent,
48
+ encodeURI,
49
+ encodeURIComponent,
50
+ RegExp,
51
+ String.fromCodePoint,
52
+ ]
53
+
54
+ export function safe(fn: (...args: unknown[]) => unknown, ...args: unknown[]): unknown {
55
+ if (!safeBuiltins.includes(fn)) {
56
+ throw new TypeError(
57
+ 'safe: expected one of JSON.parse, JSON.stringify, atob, btoa, decodeURI, ' +
58
+ 'decodeURIComponent, encodeURI, encodeURIComponent, RegExp, String.fromCodePoint',
59
+ )
60
+ }
61
+ const name =
62
+ fn === JSON.parse
63
+ ? 'JSON.parse reviver'
64
+ : fn === JSON.stringify
65
+ ? 'JSON.stringify replacer'
66
+ : ''
67
+ const callArgs = name
68
+ ? args.slice(0, 3).map((a, i) =>
69
+ i === 1 && typeof a === 'function'
70
+ ? function (this: unknown, ...cbArgs: unknown[]) {
71
+ try {
72
+ return a.apply(this, cbArgs)
73
+ } catch (cause) {
74
+ throw new Error(`${name} threw`, {cause})
75
+ }
76
+ }
77
+ : a,
78
+ )
79
+ : args
80
+ try {
81
+ return ok(fn(...callArgs))
82
+ } catch (e) {
83
+ return err(e)
84
+ }
85
+ }
86
+
87
+ export function tryCall(fn: (() => unknown) | Promise<unknown>): unknown {
88
+ if (fn instanceof Promise) return fn.then(ok, err)
89
+ if (typeof fn !== 'function') throw new TypeError('tryCall: expected a function or a promise')
90
+ let value: unknown
91
+ try {
92
+ value = fn()
93
+ } catch (e) {
94
+ return err(e)
95
+ }
96
+ return value instanceof Promise ? value.then(ok, err) : ok(value)
97
+ }
98
+
39
99
  export function err(error: unknown): unknown {
40
100
  const r = Object.create(proto)
41
101
  r.ok = false
@@ -1,4 +1,4 @@
1
- export {err, ok} from 'native:mikro/result'
1
+ export {err, ok, safe, tryCall} from 'native:mikro/result'
2
2
 
3
3
  export class PanicError extends Error {
4
4
  constructor(message: string, options?: {cause?: unknown}) {
@@ -37,6 +37,67 @@ export declare function ok<T>(value: T): OkResult<T>
37
37
 
38
38
  export declare function err<E>(error: E): ErrResult<E>
39
39
 
40
+ /**
41
+ * Call a builtin that throws on bad input and return the outcome as a Result:
42
+ * `ok(value)`, or `err(error)` with what it threw. Synchronous.
43
+ *
44
+ * `fn` must be one of `JSON.parse`, `JSON.stringify`, `atob`, `btoa`,
45
+ * `decodeURI`, `decodeURIComponent`, `encodeURI`, `encodeURIComponent`,
46
+ * `RegExp` or `String.fromCodePoint`. Any other function is a TypeError.
47
+ * If a reviver or replacer throws, the error is an `Error` whose `cause` is
48
+ * what it threw, so a bug in it isn't mistaken for bad input.
49
+ *
50
+ * @example
51
+ * const body = safe(JSON.parse, text)
52
+ * if (!body.ok) return err(new Error('invalid body', {cause: body.error}))
53
+ */
54
+ export declare function safe(
55
+ fn: RegExpConstructor,
56
+ pattern: string | RegExp,
57
+ flags?: string,
58
+ ): Result<RegExp, Error>
59
+ export declare function safe<A extends unknown[], R>(
60
+ fn: (...args: A) => R,
61
+ ...args: A
62
+ ): Result<unknown extends R ? unknown : R, Error>
63
+ // After the generic one, so other builtins never match these: inference from an
64
+ // overloaded function sees only its last overload, which lacks the callback form
65
+ export declare function safe(
66
+ fn: typeof JSON.parse,
67
+ text: string,
68
+ reviver?: (this: any, key: string, value: any) => any,
69
+ ): Result<unknown, Error>
70
+ export declare function safe(
71
+ fn: typeof JSON.stringify,
72
+ value: unknown,
73
+ replacer?: ((this: any, key: string, value: any) => any) | (number | string)[] | null,
74
+ space?: string | number,
75
+ ): Result<string, Error>
76
+
77
+ /**
78
+ * Call `fn` and return the outcome as a Result: `ok(value)`, or `err(error)`
79
+ * with what it threw. For code that throws and that {@link safe} doesn't
80
+ * cover, such as a library.
81
+ *
82
+ * Given a promise, or a `fn` that returns one, it returns a promise of a
83
+ * Result that never rejects. Passing the promise directly misses a throw that
84
+ * happens before the promise exists; the callback form catches both.
85
+ *
86
+ * @example
87
+ * const doc = tryCall(() => yaml.parse(text))
88
+ * const body = await tryCall(() => lib.download(url))
89
+ */
90
+ export declare function tryCall<T>(promise: Promise<T>): Promise<Result<T, unknown>>
91
+ export declare function tryCall<T>(
92
+ fn: () => T,
93
+ ): 0 extends 1 & T
94
+ ? Result<any, unknown>
95
+ : [T] extends [never]
96
+ ? Result<never, unknown>
97
+ : T extends Promise<infer U>
98
+ ? Promise<Result<U, unknown>>
99
+ : Result<T, unknown>
100
+
40
101
  export declare function matchError<E extends {name: string}, R>(
41
102
  error: E,
42
103
  handlers: {[K in E['name']]: (error: Extract<E, {name: K}>) => R},
@@ -1,5 +1,4 @@
1
1
  import {err, ok, PanicError, type Result} from 'mikro/result'
2
- import {getWakeupCause as nativeGetWakeupCause} from 'native:mikro/sleep'
3
2
  import * as native from 'native:mikro/sys'
4
3
 
5
4
  import type {DeviceName} from './types.js'
@@ -80,7 +79,7 @@ export function restart(): never {
80
79
  }
81
80
 
82
81
  export function getWakeupCause(): string {
83
- return nativeGetWakeupCause()
82
+ return native.getWakeupCause()
84
83
  }
85
84
 
86
85
  export function exit(_exitCode?: number): never {
@@ -98,8 +98,9 @@ export declare const version: string
98
98
  export type BoardFeature = 'wifi' | 'ble' | 'bt' | 'ieee802154'
99
99
 
100
100
  export interface BoardInfo {
101
- /** Board name from the firmware build (e.g. "xiao-esp32c6"). A build for no
102
- * particular board is "<chip>-generic" (e.g. "esp32c6-generic"). */
101
+ /** The board (e.g. "@acme/devboard"). The generic firmware reports
102
+ * "<chip>-generic" (e.g. "esp32c6-generic"), or the name of the board it was
103
+ * flashed for. */
103
104
  name: string
104
105
  /** Chip target (e.g. "esp32c6") or "host" */
105
106
  chip: string
@@ -148,6 +149,8 @@ export declare function setDeviceName(value: DeviceName): void
148
149
 
149
150
  /** Firmware build identifiers */
150
151
  export declare const firmware: {
152
+ /** The build (e.g. "esp32c6-generic", "@acme/devboard") */
153
+ readonly name: string
151
154
  /** ELF SHA256 hash on ESP32, "dev" on host */
152
155
  readonly hash: string
153
156
  /** Build date and time */
@@ -3,6 +3,7 @@
3
3
  #include <dirent.h>
4
4
  #include <errno.h>
5
5
  #include <stdio.h>
6
+ #include <stdlib.h>
6
7
  #include <string.h>
7
8
  #include <sys/stat.h>
8
9
  #include <unistd.h>
@@ -178,3 +179,34 @@ void mik__app_recover(const char* base) {
178
179
  mik__rmdir_recursive(tmp);
179
180
  }
180
181
  }
182
+
183
+ long mik__file_size(const char* path) {
184
+ struct stat st;
185
+ if (stat(path, &st) != 0) return -1;
186
+ return (long)st.st_size;
187
+ }
188
+
189
+ bool mik__fs_space(long* total, long* free_bytes) {
190
+ const MIKPlatform* p = MIK_GetPlatform();
191
+ size_t t = 0;
192
+ size_t u = 0;
193
+ if (!p || !p->get_fs_info || !p->get_fs_info("user", &t, &u)) return false;
194
+ *total = (long)t;
195
+ *free_bytes = t > u ? (long)(t - u) : 0;
196
+ return true;
197
+ }
198
+
199
+ char* mik__join_paths(const char* base, const char* const* suffixes, size_t count,
200
+ const char** const* slots) {
201
+ const size_t base_len = strlen(base);
202
+ size_t total = 0;
203
+ for (size_t i = 0; i < count; i++) total += base_len + strlen(suffixes[i]) + 1;
204
+ char* block = static_cast<char*>(malloc(total));
205
+ if (!block) return nullptr;
206
+ size_t at = 0;
207
+ for (size_t i = 0; i < count; i++) {
208
+ *slots[i] = block + at;
209
+ at += (size_t)snprintf(block + at, total - at, "%s%s", base, suffixes[i]) + 1;
210
+ }
211
+ return block;
212
+ }