@songtonyli/dsh-cli 0.1.24 → 0.1.26

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.
@@ -7,6 +7,14 @@
7
7
 
8
8
  ### Koffi 3.3
9
9
 
10
+ #### Koffi 3.3.1
11
+
12
+ *Released on 2026-09-19*
13
+
14
+ - Fix broken HFA support for some union types (ARM64, RISC-V 64, LoongArch64 abd Power64)
15
+ - Reduce slight overhead with pointer arguments introduced in Koffi 3.3.0
16
+ - Fix invalid detection of mmap() failure
17
+
10
18
  #### Koffi 3.3.0
11
19
 
12
20
  *Released on 2026-09-14*
@@ -22,16 +22,16 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
22
22
  mod
23
23
  ));
24
24
 
25
- // ../cnoke/cnoke.js
25
+ // ../../src/cnoke/cnoke.js
26
26
  var import_node_fs4 = __toESM(require("node:fs"), 1);
27
27
 
28
- // ../cnoke/src/builder.js
28
+ // ../../src/cnoke/src/builder.js
29
29
  var import_node_fs3 = __toESM(require("node:fs"), 1);
30
30
  var import_node_os = __toESM(require("node:os"), 1);
31
31
  var import_node_path = __toESM(require("node:path"), 1);
32
32
  var import_node_child_process = require("node:child_process");
33
33
 
34
- // ../cnoke/src/abi.js
34
+ // ../../src/cnoke/src/abi.js
35
35
  var import_node_fs = __toESM(require("node:fs"), 1);
36
36
  function determineAbi() {
37
37
  let abi = process.arch.toString();
@@ -168,7 +168,7 @@ var FileHandle = class {
168
168
  }
169
169
  };
170
170
 
171
- // ../cnoke/src/util.js
171
+ // ../../src/cnoke/src/util.js
172
172
  var import_node_fs2 = __toESM(require("node:fs"), 1);
173
173
  function pathIsAbsolute(path2) {
174
174
  if (process.platform == "win32" && path2.match(/^[a-zA-Z]:/))
@@ -241,7 +241,7 @@ function compareVersions(ver1, ver2) {
241
241
  return cmp;
242
242
  }
243
243
 
244
- // ../cnoke/src/assets.js
244
+ // ../../src/cnoke/src/assets.js
245
245
  var FIND_CNOKE_CMAKE = `# SPDX-License-Identifier: MIT
246
246
  # SPDX-FileCopyrightText: 2026 Niels Martign\xE8ne <niels.martignene@protonmail.com>
247
247
 
@@ -408,7 +408,7 @@ const PfnDliHook __pfnDliNotifyHook2 = self_exe_hook;
408
408
  #endif
409
409
  `;
410
410
 
411
- // ../cnoke/src/builder.js
411
+ // ../../src/cnoke/src/builder.js
412
412
  var DefaultOptions = {
413
413
  mode: "RelWithDebInfo"
414
414
  };
@@ -701,7 +701,7 @@ function Builder(config = {}) {
701
701
  }
702
702
  }
703
703
 
704
- // ../cnoke/cnoke.js
704
+ // ../../src/cnoke/cnoke.js
705
705
  var VALID_COMMANDS = ["build", "configure", "clean"];
706
706
  main();
707
707
  async function main() {
@@ -204,6 +204,9 @@ void MallocAllocator::Release(const void *ptr, Size)
204
204
 
205
205
  LinkedAllocator& LinkedAllocator::operator=(LinkedAllocator &&other)
206
206
  {
207
+ if (&other == this) [[unlikely]]
208
+ return *this;
209
+
207
210
  ReleaseAll();
208
211
 
209
212
  allocator = other.allocator;
@@ -328,6 +331,8 @@ void LinkedAllocator::Release(const void *ptr, [[maybe_unused]] Size size)
328
331
 
329
332
  void LinkedAllocator::GiveTo(LinkedAllocator *alloc)
330
333
  {
334
+ K_ASSERT(alloc->allocator == allocator);
335
+
331
336
  Bucket *other = alloc->list;
332
337
 
333
338
  if (other && list) {
@@ -351,6 +356,9 @@ LinkedAllocator::Bucket *LinkedAllocator::PointerToBucket(void *ptr)
351
356
 
352
357
  BlockAllocator& BlockAllocator::operator=(BlockAllocator &&other)
353
358
  {
359
+ if (&other == this) [[unlikely]]
360
+ return *this;
361
+
354
362
  allocator.operator=(std::move(other.allocator));
355
363
 
356
364
  block_size = other.block_size;
@@ -397,7 +405,8 @@ void *BlockAllocator::Allocate(Size size)
397
405
  uint8_t *ptr = bucket_ptr;
398
406
 
399
407
  // Fast path
400
- if (uint8_t *new_ptr = AlignUp(ptr + size, 8); new_ptr <= bucket_end) [[likely]] {
408
+ // The ugly casts are necessary to avoid null pointer arithmethic UB
409
+ if (uint8_t *new_ptr = AlignUp((uint8_t *)((uintptr_t)ptr + size), 8); new_ptr <= bucket_end) [[likely]] {
401
410
  bucket_ptr = new_ptr;
402
411
 
403
412
  last_alloc = ptr;
@@ -431,7 +440,8 @@ void *BlockAllocator::Resize(void *ptr, Size old_size, Size new_size)
431
440
  }
432
441
 
433
442
  // Fast path
434
- if (uint8_t *new_ptr = AlignUp((uint8_t *)ptr + new_size, 8); ptr == last_alloc && new_ptr <= bucket_end) {
443
+ // The ugly casts are necessary to avoid null pointer arithmethic UB
444
+ if (uint8_t *new_ptr = AlignUp((uint8_t *)((uintptr_t)ptr + new_size), 8); ptr == last_alloc && new_ptr <= bucket_end) {
435
445
  K_ASSERT(ptr);
436
446
 
437
447
  bucket_ptr = new_ptr;
@@ -6100,16 +6110,15 @@ error:
6100
6110
  return false;
6101
6111
  }
6102
6112
 
6103
- static std::mutex fork_mutex;
6104
- static int64_t fork_generation;
6113
+ static std::atomic_uint fork_generation;
6105
6114
 
6106
- bool DetectFork(int64_t *marker)
6115
+ bool DetectFork(unsigned int *marker)
6107
6116
  {
6108
6117
  static void *addr = []() -> void * {
6109
6118
  size_t page_size = (size_t)GetPageSize();
6110
6119
 
6111
6120
  void *addr = mmap(nullptr, page_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
6112
- if (!addr)
6121
+ if (addr == MAP_FAILED)
6113
6122
  return nullptr;
6114
6123
  K_DEFER_N(err_guard) { munmap(addr, page_size); };
6115
6124
 
@@ -6131,14 +6140,21 @@ bool DetectFork(int64_t *marker)
6131
6140
  }();
6132
6141
 
6133
6142
  if (addr) {
6134
- std::atomic<int64_t> *ptr = (std::atomic<int64_t> *)addr;
6135
- int64_t generation = ptr->load();
6143
+ #if __cpp_lib_atomic_ref
6144
+ std::atomic_ref<unsigned int> ref(*(unsigned int *)addr);
6145
+ std::atomic_ref<unsigned int> *ptr = &ref;
6146
+ #else
6147
+ std::atomic_uint *ptr = (std::atomic_uint *)addr;
6148
+ #endif
6149
+
6150
+ unsigned int generation = ptr->load(std::memory_order_relaxed);
6136
6151
 
6137
6152
  if (!generation) {
6138
- std::lock_guard<std::mutex> lock(fork_mutex);
6153
+ // Multiple threads may end up fighting around this, which would cause spurious fork detections.
6154
+ // It's okay. I think.
6139
6155
 
6140
6156
  generation = ++fork_generation;
6141
- ptr->store(generation);
6157
+ ptr->store(generation, std::memory_order_relaxed);
6142
6158
  }
6143
6159
 
6144
6160
  if (*marker != generation) [[unlikely]] {
@@ -6148,7 +6164,7 @@ bool DetectFork(int64_t *marker)
6148
6164
  return false;
6149
6165
  }
6150
6166
  } else {
6151
- int64_t pid = (int64_t)getpid();
6167
+ unsigned int pid = (unsigned int)getpid();
6152
6168
 
6153
6169
  if (*marker != pid) [[unlikely]] {
6154
6170
  *marker = pid;
@@ -6914,7 +6930,7 @@ bool ParseVersion(Span<const char> str, int parts, int multiplier,
6914
6930
  static thread_local Size rnd_remain;
6915
6931
  static thread_local int64_t rnd_clock;
6916
6932
  #if !defined(_WIN32)
6917
- static thread_local int64_t rnd_generation;
6933
+ static thread_local unsigned int rnd_generation;
6918
6934
  #endif
6919
6935
  static thread_local uint32_t rnd_state[16];
6920
6936
  static thread_local uint8_t rnd_buf[64];
@@ -1323,6 +1323,9 @@ public:
1323
1323
  HeapChain(HeapChain &&other) { *this = std::move(other); }
1324
1324
  HeapChain& operator=(HeapChain &&other)
1325
1325
  {
1326
+ if (&other == this) [[unlikely]]
1327
+ return *this;
1328
+
1326
1329
  ReleaseAll();
1327
1330
 
1328
1331
  list = other.list;
@@ -1421,6 +1424,9 @@ public:
1421
1424
  }
1422
1425
  RetainPtr &operator=(const RetainPtr &other)
1423
1426
  {
1427
+ if (&other == this) [[unlikely]]
1428
+ return *this;
1429
+
1424
1430
  if (p && !p->Unref()) {
1425
1431
  p->delete_func((std::remove_const_t<T> *)p);
1426
1432
  }
@@ -2222,6 +2228,9 @@ public:
2222
2228
  HeapArray(HeapArray &&other) { *this = std::move(other); }
2223
2229
  HeapArray &operator=(HeapArray &&other)
2224
2230
  {
2231
+ if (&other == this) [[unlikely]]
2232
+ return *this;
2233
+
2225
2234
  Clear();
2226
2235
  MemMove(this, &other, K_SIZE(other));
2227
2236
  MemSet(&other, 0, K_SIZE(other));
@@ -2230,6 +2239,9 @@ public:
2230
2239
  HeapArray(const HeapArray &other) { *this = other; }
2231
2240
  HeapArray &operator=(const HeapArray &other)
2232
2241
  {
2242
+ if (&other == this) [[unlikely]]
2243
+ return *this;
2244
+
2233
2245
  RemoveFrom(0);
2234
2246
  Grow(other.capacity);
2235
2247
  if constexpr(!std::is_trivial<T>::value) {
@@ -2546,6 +2558,9 @@ public:
2546
2558
  BucketList(BucketList &&other) { *this = std::move(other); }
2547
2559
  BucketList &operator=(BucketList &&other)
2548
2560
  {
2561
+ if (&other == this) [[unlikely]]
2562
+ return *this;
2563
+
2549
2564
  ClearBucketsAndValues();
2550
2565
  MemMove(this, &other, K_SIZE(other));
2551
2566
  MemSet(&other, 0, K_SIZE(other));
@@ -2844,6 +2859,9 @@ public:
2844
2859
  HashTable(HashTable &&other) { *this = std::move(other); }
2845
2860
  HashTable &operator=(HashTable &&other)
2846
2861
  {
2862
+ if (&other == this) [[unlikely]]
2863
+ return *this;
2864
+
2847
2865
  Clear();
2848
2866
  MemMove(this, &other, K_SIZE(other));
2849
2867
  MemSet(&other, 0, K_SIZE(other));
@@ -2852,6 +2870,9 @@ public:
2852
2870
  HashTable(const HashTable &other) { *this = other; }
2853
2871
  HashTable &operator=(const HashTable &other)
2854
2872
  {
2873
+ if (&other == this) [[unlikely]]
2874
+ return *this;
2875
+
2855
2876
  Clear();
2856
2877
  for (const ValueType &value: other) {
2857
2878
  Set(value);
@@ -4290,8 +4311,6 @@ public:
4290
4311
  template<typename... Args>
4291
4312
  void SetFmt(const char *fmt, Args... args) { SetFmt(1ll, 0ll, fmt, args...); }
4292
4313
 
4293
- template<typename... Args>
4294
-
4295
4314
  void operator()(int64_t value, int64_t min, int64_t max) { Set(value, min, max); }
4296
4315
  void operator()(int64_t value, int64_t max) { Set(value, 0, max); }
4297
4316
  void operator()() { Set(0, 0); }
@@ -4702,7 +4721,7 @@ int GetCoreCount();
4702
4721
  #if !defined(_WIN32) && !defined(__wasi__)
4703
4722
  bool RaiseMaximumOpenFiles(int limit = -1);
4704
4723
  bool DropRootIdentity();
4705
- bool DetectFork(int64_t *marker);
4724
+ bool DetectFork(unsigned int *marker);
4706
4725
  #endif
4707
4726
 
4708
4727
  #if defined(__linux__)
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koffi",
3
- "version": "3.3.0",
3
+ "version": "3.3.1",
4
4
  "description": "Fast and easy-to-use dynamic C FFI (foreign function interface) for Node.js",
5
5
  "keywords": [
6
6
  "foreign",
@@ -34,7 +34,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
34
34
  ));
35
35
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
36
36
 
37
- // ../cnoke/src/abi.js
37
+ // ../../src/cnoke/src/abi.js
38
38
  function determineAbi() {
39
39
  let abi = process.arch.toString();
40
40
  if (abi == "riscv32" || abi == "riscv64") {
@@ -155,7 +155,7 @@ function openFile(filename, flags) {
155
155
  }
156
156
  var import_node_fs, FileHandle;
157
157
  var init_abi = __esm({
158
- "../cnoke/src/abi.js"() {
158
+ "../../src/cnoke/src/abi.js"() {
159
159
  import_node_fs = __toESM(require("node:fs"), 1);
160
160
  FileHandle = class {
161
161
  constructor(fd) {
@@ -176,15 +176,15 @@ var init_abi = __esm({
176
176
  }
177
177
  });
178
178
 
179
- // package.json
179
+ // ../../src/koffi/package.json
180
180
  var package_default;
181
181
  var init_package = __esm({
182
- "package.json"() {
183
- package_default = { name: "koffi", version: "3.3.0", cnoke: { api: "../../vendor/node-api-headers", output: "../../bin/Koffi/{{ toolchain }}", node: 16, napi: 8 } };
182
+ "../../src/koffi/package.json"() {
183
+ package_default = { name: "koffi", version: "3.3.1", cnoke: { api: "../../vendor/node-api-headers", output: "../../bin/Koffi/{{ toolchain }}", node: 16, napi: 8 } };
184
184
  }
185
185
  });
186
186
 
187
- // src/init.js
187
+ // ../../src/koffi/src/init.js
188
188
  var init_exports = {};
189
189
  __export(init_exports, {
190
190
  detectPlatform: () => detectPlatform,
@@ -276,7 +276,7 @@ function wrapNative(native2, version2) {
276
276
  }
277
277
  var import_node_util, import_node_fs2, import_node_module, require2;
278
278
  var init_init = __esm({
279
- "src/init.js"() {
279
+ "../../src/koffi/src/init.js"() {
280
280
  import_node_util = __toESM(require("node:util"));
281
281
  import_node_fs2 = __toESM(require("node:fs"));
282
282
  import_node_module = require("node:module");
@@ -286,7 +286,7 @@ var init_init = __esm({
286
286
  }
287
287
  });
288
288
 
289
- // index.cjs
289
+ // ../../src/koffi/index.cjs
290
290
  var { detectPlatform: detectPlatform2, loadDynamic: loadDynamic2, wrapNative: wrapNative2 } = (init_init(), __toCommonJS(init_exports));
291
291
  var { loadStatic } = require("./src/static.cjs");
292
292
  var [version, pkg, triplets] = detectPlatform2();
@@ -1,9 +1,9 @@
1
- // src/init.js
1
+ // ../../src/koffi/src/init.js
2
2
  import util from "node:util";
3
3
  import fs2 from "node:fs";
4
4
  import { createRequire } from "node:module";
5
5
 
6
- // ../cnoke/src/abi.js
6
+ // ../../src/cnoke/src/abi.js
7
7
  import fs from "node:fs";
8
8
  function determineAbi() {
9
9
  let abi = process.arch.toString();
@@ -140,10 +140,10 @@ var FileHandle = class {
140
140
  }
141
141
  };
142
142
 
143
- // package.json
144
- var package_default = { name: "koffi", version: "3.3.0", cnoke: { api: "../../vendor/node-api-headers", output: "../../bin/Koffi/{{ toolchain }}", node: 16, napi: 8 } };
143
+ // ../../src/koffi/package.json
144
+ var package_default = { name: "koffi", version: "3.3.1", cnoke: { api: "../../vendor/node-api-headers", output: "../../bin/Koffi/{{ toolchain }}", node: 16, napi: 8 } };
145
145
 
146
- // src/init.js
146
+ // ../../src/koffi/src/init.js
147
147
  var require2 = createRequire(import.meta.url);
148
148
  function detectPlatform() {
149
149
  if (process.versions.napi == null || process.versions.napi < package_default.cnoke.napi) {
@@ -229,7 +229,7 @@ function wrapNative(native2, version2) {
229
229
  native2.introspect = util.deprecate(native2.type, "The koffi.introspect() function was deprecated in Koffi 3.0, use koffi.type() instead", "KOFFI008");
230
230
  }
231
231
 
232
- // index.js
232
+ // ../../src/koffi/index.js
233
233
  import { loadStatic } from "./src/static.js";
234
234
  var [version, pkg, triplets] = detectPlatform();
235
235
  var native = loadStatic(pkg) ?? loadDynamic(import.meta.dirname, pkg, triplets);
@@ -34,7 +34,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
34
34
  ));
35
35
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
36
36
 
37
- // ../cnoke/src/abi.js
37
+ // ../../src/cnoke/src/abi.js
38
38
  function determineAbi() {
39
39
  let abi = process.arch.toString();
40
40
  if (abi == "riscv32" || abi == "riscv64") {
@@ -155,7 +155,7 @@ function openFile(filename, flags) {
155
155
  }
156
156
  var import_node_fs, FileHandle;
157
157
  var init_abi = __esm({
158
- "../cnoke/src/abi.js"() {
158
+ "../../src/cnoke/src/abi.js"() {
159
159
  import_node_fs = __toESM(require("node:fs"), 1);
160
160
  FileHandle = class {
161
161
  constructor(fd) {
@@ -176,15 +176,15 @@ var init_abi = __esm({
176
176
  }
177
177
  });
178
178
 
179
- // package.json
179
+ // ../../src/koffi/package.json
180
180
  var package_default;
181
181
  var init_package = __esm({
182
- "package.json"() {
183
- package_default = { name: "koffi", version: "3.3.0", cnoke: { api: "../../vendor/node-api-headers", output: "../../bin/Koffi/{{ toolchain }}", node: 16, napi: 8 } };
182
+ "../../src/koffi/package.json"() {
183
+ package_default = { name: "koffi", version: "3.3.1", cnoke: { api: "../../vendor/node-api-headers", output: "../../bin/Koffi/{{ toolchain }}", node: 16, napi: 8 } };
184
184
  }
185
185
  });
186
186
 
187
- // src/init.js
187
+ // ../../src/koffi/src/init.js
188
188
  var init_exports = {};
189
189
  __export(init_exports, {
190
190
  detectPlatform: () => detectPlatform,
@@ -276,7 +276,7 @@ function wrapNative(native2, version2) {
276
276
  }
277
277
  var import_node_util, import_node_fs2, import_node_module, require2;
278
278
  var init_init = __esm({
279
- "src/init.js"() {
279
+ "../../src/koffi/src/init.js"() {
280
280
  import_node_util = __toESM(require("node:util"));
281
281
  import_node_fs2 = __toESM(require("node:fs"));
282
282
  import_node_module = require("node:module");
@@ -286,7 +286,7 @@ var init_init = __esm({
286
286
  }
287
287
  });
288
288
 
289
- // indirect.cjs
289
+ // ../../src/koffi/indirect.cjs
290
290
  var { detectPlatform: detectPlatform2, loadDynamic: loadDynamic2, wrapNative: wrapNative2 } = (init_init(), __toCommonJS(init_exports));
291
291
  var [version, pkg, triplets] = detectPlatform2();
292
292
  var native = loadDynamic2(__dirname, pkg, triplets);
@@ -1,9 +1,9 @@
1
- // src/init.js
1
+ // ../../src/koffi/src/init.js
2
2
  import util from "node:util";
3
3
  import fs2 from "node:fs";
4
4
  import { createRequire } from "node:module";
5
5
 
6
- // ../cnoke/src/abi.js
6
+ // ../../src/cnoke/src/abi.js
7
7
  import fs from "node:fs";
8
8
  function determineAbi() {
9
9
  let abi = process.arch.toString();
@@ -140,10 +140,10 @@ var FileHandle = class {
140
140
  }
141
141
  };
142
142
 
143
- // package.json
144
- var package_default = { name: "koffi", version: "3.3.0", cnoke: { api: "../../vendor/node-api-headers", output: "../../bin/Koffi/{{ toolchain }}", node: 16, napi: 8 } };
143
+ // ../../src/koffi/package.json
144
+ var package_default = { name: "koffi", version: "3.3.1", cnoke: { api: "../../vendor/node-api-headers", output: "../../bin/Koffi/{{ toolchain }}", node: 16, napi: 8 } };
145
145
 
146
- // src/init.js
146
+ // ../../src/koffi/src/init.js
147
147
  var require2 = createRequire(import.meta.url);
148
148
  function detectPlatform() {
149
149
  if (process.versions.napi == null || process.versions.napi < package_default.cnoke.napi) {
@@ -229,7 +229,7 @@ function wrapNative(native2, version2) {
229
229
  native2.introspect = util.deprecate(native2.type, "The koffi.introspect() function was deprecated in Koffi 3.0, use koffi.type() instead", "KOFFI008");
230
230
  }
231
231
 
232
- // indirect.js
232
+ // ../../src/koffi/indirect.js
233
233
  var [version, pkg, triplets] = detectPlatform();
234
234
  var native = loadDynamic(import.meta.dirname, pkg, triplets);
235
235
  wrapNative(native, version);
@@ -45,6 +45,21 @@ public:
45
45
  int VecCount() const { return fpr_index; }
46
46
  };
47
47
 
48
+ static bool DetectUnion(const TypeInfo *type)
49
+ {
50
+ if (type->primitive == PrimitiveKind::Union)
51
+ return true;
52
+
53
+ if (type->primitive == PrimitiveKind::Record &&
54
+ std::any_of(type->members.begin(), type->members.end(),
55
+ [](const RecordMember &member) { return DetectUnion(member.type); }))
56
+ return true;
57
+ if (type->primitive == PrimitiveKind::Array && DetectUnion(type->ref.type))
58
+ return true;
59
+
60
+ return false;
61
+ }
62
+
48
63
  ClassResult ClassAnalyser::Analyse(const TypeInfo *type, bool variadic)
49
64
  {
50
65
  ClassResult ret = {};
@@ -63,7 +78,7 @@ ClassResult ClassAnalyser::Analyse(const TypeInfo *type, bool variadic)
63
78
  int fpr_avail = std::min(2, fpr_max - fpr_index);
64
79
 
65
80
  #if defined(__riscv_float_abi_double) || defined(__loongarch64)
66
- if (type->primitive != PrimitiveKind::Union && !variadic) {
81
+ if (!variadic && !DetectUnion(type)) {
67
82
  int gpr_count = 0;
68
83
  int fpr_count = 0;
69
84
  bool gpr_fpr = false;
@@ -334,8 +334,10 @@ Size CallData::PushStringValue(napi_value value, const char **out_str)
334
334
  char *ptr = (char *)heap.ptr;
335
335
 
336
336
  // Fast path for small strings
337
- if (available >= 4096) [[likely]] {
338
- napi_status status = napi_get_value_string_utf8(env, value, ptr, 4096, &len);
337
+ {
338
+ size_t small = std::min(available, (size_t)4096);
339
+
340
+ napi_status status = napi_get_value_string_utf8(env, value, ptr, small, &len);
339
341
  if (status == napi_string_expected)
340
342
  return -1;
341
343
  K_ASSERT(status == napi_ok);
@@ -344,9 +346,10 @@ Size CallData::PushStringValue(napi_value value, const char **out_str)
344
346
 
345
347
  // UTF-8 can take up to 4 bytes for a codepoint, so truncation may
346
348
  // result in a value that is several bytes less than the buffer size.
347
- // So len < 4096 - 4 should be enough, but exagerate a bit "just in case" :)
349
+ // So len < len - 4 should be enough, but exagerate a bit "just in case" :)
350
+ // Avoid substraction because we're using size_t (unsigned).
348
351
 
349
- if ((Size)len < 4096 - 8) {
352
+ if (len + 8 < small) {
350
353
  heap.ptr += (Size)AlignLen(len, 16);
351
354
 
352
355
  *out_str = ptr;
@@ -938,12 +941,16 @@ bool CallData::PushPointer(napi_value value, const TypeInfo *type, int direction
938
941
 
939
942
  void *ptr = nullptr;
940
943
  Size len = 0;
941
- napi_valuetype kind = napi_undefined;
942
944
 
943
945
  restart:
944
946
 
945
- if (TryPointer(env, value, &ptr, &len, &kind)) {
946
- if (type->ref.conversion != BufferConversion::None && kind == napi_object) [[unlikely]] {
947
+ if (TryPointer(env, value, &ptr, &len)) {
948
+ if (type->ref.conversion == BufferConversion::None) [[likely]] {
949
+ *out_ptr = ptr;
950
+ return true;
951
+ }
952
+
953
+ if (len >= 0) {
947
954
  if (directions & 2) {
948
955
  if (directions & 1) {
949
956
  ConvertBuffer(type->ref.conversion, ptr, len, type->ref.stride);
@@ -969,6 +976,9 @@ restart:
969
976
  return true;
970
977
  }
971
978
 
979
+ // When TryPointer fails, it stuffs the napi_valuetype (kind) in out_len to help
980
+ napi_valuetype kind = (napi_valuetype)len;
981
+
972
982
  if (kind == napi_external && CheckValueTag(env, value, &CastMarker)) {
973
983
  Napi::External<ValueCast> external = Napi::External<ValueCast>(env, value);
974
984
  ValueCast *cast = external.Data();
@@ -1129,15 +1139,17 @@ bool CallData::PushCallback(napi_value value, const TypeInfo *type, void **out_p
1129
1139
  {
1130
1140
  void *ptr = nullptr;
1131
1141
  Size len = 0;
1132
- napi_valuetype kind = napi_undefined;
1133
1142
 
1134
1143
  restart:
1135
1144
 
1136
- if (TryPointer(env, value, &ptr, &len, &kind)) {
1145
+ if (TryPointer(env, value, &ptr, &len)) {
1137
1146
  *out_ptr = ptr;
1138
1147
  return true;
1139
1148
  }
1140
1149
 
1150
+ // When TryPointer fails, it stuffs the napi_valuetype (kind) in out_len to help
1151
+ napi_valuetype kind = (napi_valuetype)len;
1152
+
1141
1153
  if (kind == napi_function) {
1142
1154
  Napi::Function func = Napi::Function(env, value);
1143
1155
 
@@ -1389,15 +1401,15 @@ static TRANSLATE_FUNC K_FORCE_INLINE napi_value TranslateNormalCall(CallData *ca
1389
1401
 
1390
1402
  static TRANSLATE_FUNC napi_value TranslateNormalCall(napi_env env, napi_callback_info info)
1391
1403
  {
1392
- static_assert(MaxParameters >= 8);
1404
+ static_assert(MaxParameters >= 6);
1393
1405
 
1394
1406
  CallData call(env);
1395
- size_t count = 8;
1407
+ size_t count = 6;
1396
1408
  FunctionInfo *func;
1397
1409
 
1398
1410
  NAPI_OK(napi_get_cb_info(env, info, &count, call.args, nullptr, (void **)&func));
1399
1411
 
1400
- if (count > 8) {
1412
+ if (count > 6) {
1401
1413
  NAPI_OK(napi_get_cb_info(env, info, &count, call.args, nullptr, nullptr));
1402
1414
  count = std::min(count, (size_t)MaxParameters);
1403
1415
  }
@@ -147,7 +147,6 @@ template <typename T>
147
147
  inline T *CallData::AllocStack(Size size)
148
148
  {
149
149
  K_ASSERT(AlignDown(stack.end, 16) == stack.end);
150
- K_ASSERT(AlignLen(size, 16) == size);
151
150
 
152
151
  uint8_t *ptr = stack.end - size;
153
152
  FillMemory(ptr, stack.end - ptr);
@@ -1945,9 +1945,8 @@ static Napi::Value DecodeValue(const Napi::CallbackInfo &info)
1945
1945
 
1946
1946
  const void *src = nullptr;
1947
1947
  Size src_len = 0;
1948
- napi_valuetype src_kind;
1949
1948
 
1950
- if (!TryPointer(env, ref, (void **)&src, &src_len, &src_kind)) {
1949
+ if (!TryPointer(env, ref, (void **)&src, &src_len)) {
1951
1950
  ThrowError<Napi::TypeError>(env, "Unexpected %1 value for reference, expected pointer", GetValueType(instance, ref));
1952
1951
  return env.Null();
1953
1952
  }
@@ -2298,9 +2297,8 @@ static Napi::Value EncodeValue(const Napi::CallbackInfo &info)
2298
2297
 
2299
2298
  void *dest = nullptr;
2300
2299
  Size dest_len = 0;
2301
- napi_valuetype dest_kind;
2302
2300
 
2303
- if (!TryPointer(env, ref, &dest, &dest_len, &dest_kind)) {
2301
+ if (!TryPointer(env, ref, &dest, &dest_len)) {
2304
2302
  ThrowError<Napi::TypeError>(env, "Unexpected %1 value for reference, expected pointer", GetValueType(instance, ref));
2305
2303
  return env.Null();
2306
2304
  }
@@ -2487,7 +2485,7 @@ void InstanceMemory::Allocate(Size stack_size, Size heap_size)
2487
2485
  stack.ptr = (uint8_t *)mmap(nullptr, stack_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON | MAP_STACK, -1, 0);
2488
2486
  stack.end = stack.ptr + stack_size;
2489
2487
 
2490
- K_CRITICAL(stack.ptr, "Failed to allocate %1 of memory", stack_size);
2488
+ K_CRITICAL(stack.ptr != MAP_FAILED, "Failed to allocate %1 of memory", stack_size);
2491
2489
  #endif
2492
2490
 
2493
2491
  #if defined(__OpenBSD__)
@@ -2504,7 +2502,7 @@ void InstanceMemory::Allocate(Size stack_size, Size heap_size)
2504
2502
  heap.ptr = (uint8_t *)mmap(nullptr, heap_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
2505
2503
  heap.end = heap.ptr + heap_size;
2506
2504
 
2507
- K_CRITICAL(heap.ptr, "Failed to allocate %1 of memory", heap_size);
2505
+ K_CRITICAL(heap.ptr != MAP_FAILED, "Failed to allocate %1 of memory", heap_size);
2508
2506
  #endif
2509
2507
  }
2510
2508
 
@@ -570,11 +570,12 @@ static int AnalyseFlatRec(const TypeInfo *type, int offset, int count, FunctionR
570
570
  }
571
571
  } else if (type->primitive == PrimitiveKind::Union) {
572
572
  for (int i = 0; i < count; i++) {
573
+ int next = offset;
573
574
  for (const RecordMember &member: type->members) {
574
- AnalyseFlatRec(member.type, offset, 1, func);
575
+ next = std::max(next, AnalyseFlatRec(member.type, offset, 1, func));
575
576
  }
577
+ offset = next;
576
578
  }
577
- offset += count;
578
579
  } else if (type->primitive == PrimitiveKind::Array) {
579
580
  count *= type->size / type->ref.type->size;
580
581
  offset = AnalyseFlatRec(type->ref.type, offset, count, func);