koffi 2.12.0 → 2.12.1

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.
package/CHANGELOG.md CHANGED
@@ -7,6 +7,12 @@
7
7
 
8
8
  ### Koffi 2.12
9
9
 
10
+ #### Koffi 2.12.1
11
+
12
+ *Released on 2025-07-05*
13
+
14
+ - Fix type errors when casting function pointers
15
+
10
16
  #### Koffi 2.12.0
11
17
 
12
18
  *Released on 2025-06-19*
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
package/index.js CHANGED
@@ -4,9 +4,9 @@ var __commonJS = (cb, mod3) => function __require() {
4
4
  return mod3 || (0, cb[__getOwnPropNames(cb)[0]])((mod3 = { exports: {} }).exports, mod3), mod3.exports;
5
5
  };
6
6
 
7
- // package/src/cnoke/src/tools.js
7
+ // ../../bin/Koffi/package/src/cnoke/src/tools.js
8
8
  var require_tools = __commonJS({
9
- "package/src/cnoke/src/tools.js"(exports2, module2) {
9
+ "../../bin/Koffi/package/src/cnoke/src/tools.js"(exports2, module2) {
10
10
  "use strict";
11
11
  var crypto = require("crypto");
12
12
  var fs2 = require("fs");
@@ -374,13 +374,13 @@ var require_tools = __commonJS({
374
374
  }
375
375
  });
376
376
 
377
- // package/src/koffi/package.json
377
+ // ../../bin/Koffi/package/src/koffi/package.json
378
378
  var require_package = __commonJS({
379
- "package/src/koffi/package.json"(exports2, module2) {
379
+ "../../bin/Koffi/package/src/koffi/package.json"(exports2, module2) {
380
380
  module2.exports = {
381
381
  name: "koffi",
382
- version: "2.12.0",
383
- stable: "2.12.0",
382
+ version: "2.12.1",
383
+ stable: "2.12.1",
384
384
  description: "Fast and simple C FFI (foreign function interface) for Node.js",
385
385
  keywords: [
386
386
  "foreign",
@@ -424,9 +424,9 @@ var require_package = __commonJS({
424
424
  }
425
425
  });
426
426
 
427
- // package/src/koffi/src/init.js
427
+ // ../../bin/Koffi/package/src/koffi/src/init.js
428
428
  var require_init = __commonJS({
429
- "package/src/koffi/src/init.js"(exports, module) {
429
+ "../../bin/Koffi/package/src/koffi/src/init.js"(exports, module) {
430
430
  var fs = require("fs");
431
431
  var path = require("path");
432
432
  var util = require("util");
@@ -509,7 +509,7 @@ var require_init = __commonJS({
509
509
  }
510
510
  });
511
511
 
512
- // package/src/koffi/index.js
512
+ // ../../bin/Koffi/package/src/koffi/index.js
513
513
  var { detect: detect2, init: init2 } = require_init();
514
514
  var triplet2 = detect2();
515
515
  var native2 = null;
package/indirect.js CHANGED
@@ -4,9 +4,9 @@ var __commonJS = (cb, mod3) => function __require() {
4
4
  return mod3 || (0, cb[__getOwnPropNames(cb)[0]])((mod3 = { exports: {} }).exports, mod3), mod3.exports;
5
5
  };
6
6
 
7
- // package/src/cnoke/src/tools.js
7
+ // ../../bin/Koffi/package/src/cnoke/src/tools.js
8
8
  var require_tools = __commonJS({
9
- "package/src/cnoke/src/tools.js"(exports2, module2) {
9
+ "../../bin/Koffi/package/src/cnoke/src/tools.js"(exports2, module2) {
10
10
  "use strict";
11
11
  var crypto = require("crypto");
12
12
  var fs2 = require("fs");
@@ -374,13 +374,13 @@ var require_tools = __commonJS({
374
374
  }
375
375
  });
376
376
 
377
- // package/src/koffi/package.json
377
+ // ../../bin/Koffi/package/src/koffi/package.json
378
378
  var require_package = __commonJS({
379
- "package/src/koffi/package.json"(exports2, module2) {
379
+ "../../bin/Koffi/package/src/koffi/package.json"(exports2, module2) {
380
380
  module2.exports = {
381
381
  name: "koffi",
382
- version: "2.12.0",
383
- stable: "2.12.0",
382
+ version: "2.12.1",
383
+ stable: "2.12.1",
384
384
  description: "Fast and simple C FFI (foreign function interface) for Node.js",
385
385
  keywords: [
386
386
  "foreign",
@@ -424,9 +424,9 @@ var require_package = __commonJS({
424
424
  }
425
425
  });
426
426
 
427
- // package/src/koffi/src/init.js
427
+ // ../../bin/Koffi/package/src/koffi/src/init.js
428
428
  var require_init = __commonJS({
429
- "package/src/koffi/src/init.js"(exports, module) {
429
+ "../../bin/Koffi/package/src/koffi/src/init.js"(exports, module) {
430
430
  var fs = require("fs");
431
431
  var path = require("path");
432
432
  var util = require("util");
@@ -509,7 +509,7 @@ var require_init = __commonJS({
509
509
  }
510
510
  });
511
511
 
512
- // package/src/koffi/indirect.js
512
+ // ../../bin/Koffi/package/src/koffi/indirect.js
513
513
  var { detect: detect2, init: init2 } = require_init();
514
514
  var triplet2 = detect2();
515
515
  var mod2 = init2(triplet2, null);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koffi",
3
- "version": "2.12.0",
4
- "stable": "2.12.0",
3
+ "version": "2.12.1",
4
+ "stable": "2.12.1",
5
5
  "description": "Fast and simple C FFI (foreign function interface) for Node.js",
6
6
  "keywords": [
7
7
  "foreign",
@@ -431,21 +431,8 @@ bool CallData::Prepare(const FunctionInfo *func, const Napi::CallbackInfo &info)
431
431
  } break;
432
432
  case PrimitiveKind::Callback: {
433
433
  void *ptr;
434
-
435
- if (value.IsFunction()) {
436
- Napi::Function func = value.As<Napi::Function>();
437
-
438
- ptr = ReserveTrampoline(param.type->ref.proto, func);
439
- if (!ptr) [[unlikely]]
440
- return false;
441
- } else if (CheckValueTag(instance, value, param.type->ref.marker)) {
442
- ptr = value.As<Napi::External<void>>().Data();
443
- } else if (IsNullOrUndefined(value)) {
444
- ptr = nullptr;
445
- } else {
446
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected %2", GetValueType(instance, value), param.type->name);
434
+ if (!PushCallback(value, param.type, &ptr)) [[unlikely]]
447
435
  return false;
448
- }
449
436
 
450
437
  *(void **)((param.gpr_count ? gpr_ptr : args_ptr)++) = ptr;
451
438
  } break;
@@ -538,21 +538,8 @@ bool CallData::Prepare(const FunctionInfo *func, const Napi::CallbackInfo &info)
538
538
  } break;
539
539
  case PrimitiveKind::Callback: {
540
540
  void *ptr;
541
-
542
- if (value.IsFunction()) {
543
- Napi::Function func = value.As<Napi::Function>();
544
-
545
- ptr = ReserveTrampoline(param.type->ref.proto, func);
546
- if (!ptr) [[unlikely]]
547
- return false;
548
- } else if (CheckValueTag(instance, value, param.type->ref.marker)) {
549
- ptr = value.As<Napi::External<void>>().Data();
550
- } else if (IsNullOrUndefined(value)) {
551
- ptr = nullptr;
552
- } else {
553
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected %2", GetValueType(instance, value), param.type->name);
541
+ if (!PushCallback(value, param.type, &ptr)) [[unlikely]]
554
542
  return false;
555
- }
556
543
 
557
544
  #ifdef __APPLE__
558
545
  args_ptr = param.gpr_count ? args_ptr : AlignUp(args_ptr, 8);
@@ -362,21 +362,8 @@ bool CallData::Prepare(const FunctionInfo *func, const Napi::CallbackInfo &info)
362
362
  } break;
363
363
  case PrimitiveKind::Callback: {
364
364
  void *ptr;
365
-
366
- if (value.IsFunction()) {
367
- Napi::Function func = value.As<Napi::Function>();
368
-
369
- ptr = ReserveTrampoline(param.type->ref.proto, func);
370
- if (!ptr) [[unlikely]]
371
- return false;
372
- } else if (CheckValueTag(instance, value, param.type->ref.marker)) {
373
- ptr = value.As<Napi::External<void>>().Data();
374
- } else if (IsNullOrUndefined(value)) {
375
- ptr = nullptr;
376
- } else {
377
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected %2", GetValueType(instance, value), param.type->name);
365
+ if (!PushCallback(value, param.type, &ptr)) [[unlikely]]
378
366
  return false;
379
- }
380
367
 
381
368
  *(void **)((param.gpr_count ? gpr_ptr : args_ptr)++) = ptr;
382
369
  } break;
@@ -403,21 +403,8 @@ bool CallData::Prepare(const FunctionInfo *func, const Napi::CallbackInfo &info)
403
403
  } break;
404
404
  case PrimitiveKind::Callback: {
405
405
  void *ptr;
406
-
407
- if (value.IsFunction()) {
408
- Napi::Function func = value.As<Napi::Function>();
409
-
410
- ptr = ReserveTrampoline(param.type->ref.proto, func);
411
- if (!ptr) [[unlikely]]
412
- return false;
413
- } else if (CheckValueTag(instance, value, param.type->ref.marker)) {
414
- ptr = value.As<Napi::External<void>>().Data();
415
- } else if (IsNullOrUndefined(value)) {
416
- ptr = nullptr;
417
- } else {
418
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected %2", GetValueType(instance, value), param.type->name);
406
+ if (!PushCallback(value, param.type, &ptr)) [[unlikely]]
419
407
  return false;
420
- }
421
408
 
422
409
  *(void **)((param.gpr_count ? gpr_ptr : args_ptr)++) = ptr;
423
410
  } break;
@@ -200,21 +200,8 @@ bool CallData::Prepare(const FunctionInfo *func, const Napi::CallbackInfo &info)
200
200
  } break;
201
201
  case PrimitiveKind::Callback: {
202
202
  void *ptr;
203
-
204
- if (value.IsFunction()) {
205
- Napi::Function func = value.As<Napi::Function>();
206
-
207
- ptr = ReserveTrampoline(param.type->ref.proto, func);
208
- if (!ptr) [[unlikely]]
209
- return false;
210
- } else if (CheckValueTag(instance, value, param.type->ref.marker)) {
211
- ptr = value.As<Napi::External<void>>().Data();
212
- } else if (IsNullOrUndefined(value)) {
213
- ptr = nullptr;
214
- } else {
215
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected %2", GetValueType(instance, value), param.type->name);
203
+ if (!PushCallback(value, param.type, &ptr)) [[unlikely]]
216
204
  return false;
217
- }
218
205
 
219
206
  *(void **)(args_ptr++) = ptr;
220
207
  } break;
@@ -294,21 +294,8 @@ bool CallData::Prepare(const FunctionInfo *func, const Napi::CallbackInfo &info)
294
294
  } break;
295
295
  case PrimitiveKind::Callback: {
296
296
  void *ptr;
297
-
298
- if (value.IsFunction()) {
299
- Napi::Function func = value.As<Napi::Function>();
300
-
301
- ptr = ReserveTrampoline(param.type->ref.proto, func);
302
- if (!ptr) [[unlikely]]
303
- return false;
304
- } else if (CheckValueTag(instance, value, param.type->ref.marker)) {
305
- ptr = value.As<Napi::External<void>>().Data();
306
- } else if (IsNullOrUndefined(value)) {
307
- ptr = nullptr;
308
- } else {
309
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected %2", GetValueType(instance, value), param.type->name);
297
+ if (!PushCallback(value, param.type, &ptr)) [[unlikely]]
310
298
  return false;
311
- }
312
299
 
313
300
  *(void **)((param.fast ? fast_ptr : args_ptr)++) = ptr;
314
301
  } break;
@@ -743,22 +743,8 @@ bool CallData::PushObject(Napi::Object obj, const TypeInfo *type, uint8_t *origi
743
743
  } break;
744
744
  case PrimitiveKind::Callback: {
745
745
  void *ptr;
746
-
747
- if (value.IsFunction()) {
748
- Napi::Function func = value.As<Napi::Function>();
749
-
750
- ptr = ReserveTrampoline(member.type->ref.proto, func);
751
- if (!ptr) [[unlikely]]
752
- return false;
753
- } else if (CheckValueTag(instance, value, member.type->ref.marker)) {
754
- Napi::External<void> external = value.As<Napi::External<void>>();
755
- ptr = external.Data();
756
- } else if (IsNullOrUndefined(value)) {
757
- ptr = nullptr;
758
- } else {
759
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected %2", GetValueType(instance, value), member.type->name);
746
+ if (!PushCallback(value, member.type, &ptr))
760
747
  return false;
761
- }
762
748
 
763
749
  *(void **)dest = ptr;
764
750
  } break;
@@ -991,22 +977,8 @@ bool CallData::PushNormalArray(Napi::Array array, Size len, const TypeInfo *type
991
977
  uint8_t *dest = origin + offset;
992
978
 
993
979
  void *ptr;
994
-
995
- if (value.IsFunction()) {
996
- Napi::Function func = value.As<Napi::Function>();
997
-
998
- ptr = ReserveTrampoline(ref->ref.proto, func);
999
- if (!ptr) [[unlikely]]
1000
- return false;
1001
- } else if (CheckValueTag(instance, value, ref->ref.marker)) {
1002
- Napi::External<void> external = value.As<Napi::External<void>>();
1003
- ptr = external.Data();
1004
- } else if (IsNullOrUndefined(value)) {
1005
- ptr = nullptr;
1006
- } else {
1007
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected %2", GetValueType(instance, value), ref->name);
980
+ if (!PushCallback(value, ref, &ptr))
1008
981
  return false;
1009
- }
1010
982
 
1011
983
  *(void **)dest = ptr;
1012
984
 
@@ -1274,6 +1246,41 @@ unexpected:
1274
1246
  return false;
1275
1247
  }
1276
1248
 
1249
+ bool CallData::PushCallback(Napi::Value value, const TypeInfo *type, void **out_ptr)
1250
+ {
1251
+ if (value.IsFunction()) {
1252
+ Napi::Function func = value.As<Napi::Function>();
1253
+
1254
+ void *ptr = ReserveTrampoline(type->ref.proto, func);
1255
+ if (!ptr) [[unlikely]]
1256
+ return false;
1257
+
1258
+ *out_ptr = ptr;
1259
+ } else if (CheckValueTag(instance, value, type->ref.marker)) {
1260
+ *out_ptr = value.As<Napi::External<void>>().Data();
1261
+ } else if (CheckValueTag(instance, value, &CastMarker)) {
1262
+ Napi::External<ValueCast> external = value.As<Napi::External<ValueCast>>();
1263
+ ValueCast *cast = external.Data();
1264
+
1265
+ value = cast->ref.Value();
1266
+
1267
+ if (!value.IsExternal() || cast->type != type)
1268
+ goto unexpected;
1269
+
1270
+ *out_ptr = value.As<Napi::External<void>>().Data();
1271
+ } else if (IsNullOrUndefined(value)) {
1272
+ *out_ptr = nullptr;
1273
+ } else {
1274
+ goto unexpected;
1275
+ }
1276
+
1277
+ return true;
1278
+
1279
+ unexpected:
1280
+ ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected %2", GetValueType(instance, value), type->name);
1281
+ return false;
1282
+ }
1283
+
1277
1284
  Size CallData::PushIndirectString(Napi::Array array, const TypeInfo *ref, uint8_t **out_ptr)
1278
1285
  {
1279
1286
  if (array.Length() != 1)
@@ -125,6 +125,7 @@ public:
125
125
  void PushBuffer(Span<const uint8_t> buffer, Size size, const TypeInfo *type, uint8_t *origin);
126
126
  bool PushStringArray(Napi::Value value, const TypeInfo *type, uint8_t *origin);
127
127
  bool PushPointer(Napi::Value value, const TypeInfo *type, int directions, void **out_ptr);
128
+ bool PushCallback(Napi::Value value, const TypeInfo *type, void **out_ptr);
128
129
  Size PushIndirectString(Napi::Array array, const TypeInfo *ref, uint8_t **out_ptr);
129
130
 
130
131
  void *ReserveTrampoline(const FunctionInfo *proto, Napi::Function func);