koffi 2.0.1 → 2.1.0-beta.3

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 (90) hide show
  1. package/CMakeLists.txt +2 -9
  2. package/ChangeLog.md +17 -0
  3. package/benchmark/atoi_koffi.js +12 -8
  4. package/benchmark/atoi_napi.js +12 -8
  5. package/benchmark/atoi_node_ffi.js +11 -10
  6. package/benchmark/raylib_cc.cc +12 -9
  7. package/benchmark/raylib_koffi.js +15 -13
  8. package/benchmark/raylib_node_ffi.js +15 -13
  9. package/benchmark/raylib_node_raylib.js +14 -11
  10. package/build/qemu/2.1.0-beta.3/koffi_darwin_arm64.tar.gz +0 -0
  11. package/build/qemu/2.1.0-beta.3/koffi_darwin_x64.tar.gz +0 -0
  12. package/build/qemu/2.1.0-beta.3/koffi_freebsd_arm64.tar.gz +0 -0
  13. package/build/qemu/2.1.0-beta.3/koffi_freebsd_ia32.tar.gz +0 -0
  14. package/build/qemu/2.1.0-beta.3/koffi_freebsd_x64.tar.gz +0 -0
  15. package/build/qemu/2.1.0-beta.3/koffi_linux_arm32hf.tar.gz +0 -0
  16. package/build/qemu/2.1.0-beta.3/koffi_linux_arm64.tar.gz +0 -0
  17. package/build/qemu/2.1.0-beta.3/koffi_linux_ia32.tar.gz +0 -0
  18. package/build/qemu/2.1.0-beta.3/koffi_linux_riscv64hf64.tar.gz +0 -0
  19. package/build/qemu/2.1.0-beta.3/koffi_linux_x64.tar.gz +0 -0
  20. package/build/qemu/2.1.0-beta.3/koffi_openbsd_ia32.tar.gz +0 -0
  21. package/build/qemu/2.1.0-beta.3/koffi_openbsd_x64.tar.gz +0 -0
  22. package/build/qemu/2.1.0-beta.3/koffi_win32_arm64.tar.gz +0 -0
  23. package/build/qemu/2.1.0-beta.3/koffi_win32_ia32.tar.gz +0 -0
  24. package/build/qemu/2.1.0-beta.3/koffi_win32_x64.tar.gz +0 -0
  25. package/doc/Makefile +1 -1
  26. package/doc/changes.md +12 -8
  27. package/doc/conf.py +5 -0
  28. package/doc/dist/doctrees/changes.doctree +0 -0
  29. package/doc/dist/doctrees/environment.pickle +0 -0
  30. package/doc/dist/doctrees/functions.doctree +0 -0
  31. package/doc/dist/doctrees/index.doctree +0 -0
  32. package/doc/dist/doctrees/types.doctree +0 -0
  33. package/doc/dist/html/.buildinfo +1 -1
  34. package/doc/dist/html/_sources/changes.md.txt +12 -8
  35. package/doc/dist/html/_sources/functions.md.txt +71 -5
  36. package/doc/dist/html/_sources/types.md.txt +147 -159
  37. package/doc/dist/html/benchmarks.html +2 -3
  38. package/doc/dist/html/changes.html +64 -35
  39. package/doc/dist/html/contribute.html +2 -3
  40. package/doc/dist/html/functions.html +73 -12
  41. package/doc/dist/html/genindex.html +2 -3
  42. package/doc/dist/html/index.html +6 -7
  43. package/doc/dist/html/memory.html +2 -3
  44. package/doc/dist/html/objects.inv +0 -0
  45. package/doc/dist/html/platforms.html +3 -4
  46. package/doc/dist/html/search.html +2 -3
  47. package/doc/dist/html/searchindex.js +1 -1
  48. package/doc/dist/html/start.html +2 -3
  49. package/doc/dist/html/types.html +238 -237
  50. package/doc/functions.md +71 -5
  51. package/doc/make.bat +1 -1
  52. package/doc/templates/badges.html +1 -2
  53. package/doc/types.md +149 -159
  54. package/package.json +3 -2
  55. package/qemu/qemu.js +1 -1
  56. package/src/abi_arm32.cc +208 -102
  57. package/src/abi_arm64.cc +239 -55
  58. package/src/abi_riscv64.cc +128 -40
  59. package/src/abi_x64_sysv.cc +135 -41
  60. package/src/abi_x64_win.cc +134 -40
  61. package/src/abi_x86.cc +182 -67
  62. package/src/call.cc +241 -26
  63. package/src/call.hh +15 -3
  64. package/src/ffi.cc +120 -31
  65. package/src/ffi.hh +19 -0
  66. package/src/index.js +4 -2
  67. package/src/parser.cc +3 -5
  68. package/src/util.cc +44 -1
  69. package/src/util.hh +4 -0
  70. package/test/async.js +1 -2
  71. package/test/callbacks.js +2 -3
  72. package/test/misc.c +64 -1
  73. package/test/raylib.js +1 -1
  74. package/test/sqlite.js +3 -3
  75. package/test/sync.js +108 -3
  76. package/build/qemu/2.0.1/koffi_darwin_arm64.tar.gz +0 -0
  77. package/build/qemu/2.0.1/koffi_darwin_x64.tar.gz +0 -0
  78. package/build/qemu/2.0.1/koffi_freebsd_arm64.tar.gz +0 -0
  79. package/build/qemu/2.0.1/koffi_freebsd_ia32.tar.gz +0 -0
  80. package/build/qemu/2.0.1/koffi_freebsd_x64.tar.gz +0 -0
  81. package/build/qemu/2.0.1/koffi_linux_arm32hf.tar.gz +0 -0
  82. package/build/qemu/2.0.1/koffi_linux_arm64.tar.gz +0 -0
  83. package/build/qemu/2.0.1/koffi_linux_ia32.tar.gz +0 -0
  84. package/build/qemu/2.0.1/koffi_linux_riscv64hf64.tar.gz +0 -0
  85. package/build/qemu/2.0.1/koffi_linux_x64.tar.gz +0 -0
  86. package/build/qemu/2.0.1/koffi_openbsd_ia32.tar.gz +0 -0
  87. package/build/qemu/2.0.1/koffi_openbsd_x64.tar.gz +0 -0
  88. package/build/qemu/2.0.1/koffi_win32_arm64.tar.gz +0 -0
  89. package/build/qemu/2.0.1/koffi_win32_ia32.tar.gz +0 -0
  90. package/build/qemu/2.0.1/koffi_win32_x64.tar.gz +0 -0
@@ -169,11 +169,17 @@ static Size ClassifyType(const TypeInfo *type, Size offset, Span<RegisterClass>
169
169
  case PrimitiveKind::Int8:
170
170
  case PrimitiveKind::UInt8:
171
171
  case PrimitiveKind::Int16:
172
+ case PrimitiveKind::Int16S:
172
173
  case PrimitiveKind::UInt16:
174
+ case PrimitiveKind::UInt16S:
173
175
  case PrimitiveKind::Int32:
176
+ case PrimitiveKind::Int32S:
174
177
  case PrimitiveKind::UInt32:
178
+ case PrimitiveKind::UInt32S:
175
179
  case PrimitiveKind::Int64:
180
+ case PrimitiveKind::Int64S:
176
181
  case PrimitiveKind::UInt64:
182
+ case PrimitiveKind::UInt64S:
177
183
  case PrimitiveKind::String:
178
184
  case PrimitiveKind::String16:
179
185
  case PrimitiveKind::Pointer:
@@ -298,6 +304,27 @@ bool CallData::Prepare(const Napi::CallbackInfo &info)
298
304
  *(uint8_t **)(gpr_ptr++) = return_ptr;
299
305
  }
300
306
 
307
+ #define PUSH_INTEGER(CType) \
308
+ do { \
309
+ if (RG_UNLIKELY(!value.IsNumber() && !value.IsBigInt())) { \
310
+ ThrowError<Napi::TypeError>(env, "Unexpected %1 value for argument %2, expected number", GetValueType(instance, value), param.offset + 1); \
311
+ return false; \
312
+ } \
313
+ \
314
+ CType v = CopyNumber<CType>(value); \
315
+ *((param.gpr_count ? gpr_ptr : args_ptr)++) = (uint64_t)v; \
316
+ } while (false)
317
+ #define PUSH_INTEGER_SWAP(CType) \
318
+ do { \
319
+ if (RG_UNLIKELY(!value.IsNumber() && !value.IsBigInt())) { \
320
+ ThrowError<Napi::TypeError>(env, "Unexpected %1 value for argument %2, expected number", GetValueType(instance, value), param.offset + 1); \
321
+ return false; \
322
+ } \
323
+ \
324
+ CType v = CopyNumber<CType>(value); \
325
+ *((param.gpr_count ? gpr_ptr : args_ptr)++) = (uint64_t)ReverseBytes(v); \
326
+ } while (false)
327
+
301
328
  // Push arguments
302
329
  for (Size i = 0; i < func->parameters.len; i++) {
303
330
  const ParameterInfo &param = func->parameters[i];
@@ -317,30 +344,20 @@ bool CallData::Prepare(const Napi::CallbackInfo &info)
317
344
  bool b = value.As<Napi::Boolean>();
318
345
  *((param.gpr_count ? gpr_ptr : args_ptr)++) = (uint64_t)b;
319
346
  } break;
320
- case PrimitiveKind::Int8:
321
- case PrimitiveKind::UInt8:
322
- case PrimitiveKind::Int16:
323
- case PrimitiveKind::UInt16:
324
- case PrimitiveKind::Int32:
325
- case PrimitiveKind::UInt32:
326
- case PrimitiveKind::Int64: {
327
- if (RG_UNLIKELY(!value.IsNumber() && !value.IsBigInt())) {
328
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value for argument %2, expected number", GetValueType(instance, value), param.offset + 1);
329
- return false;
330
- }
331
-
332
- int64_t v = CopyNumber<int64_t>(value);
333
- *(int64_t *)((param.gpr_count ? gpr_ptr : args_ptr)++) = v;
334
- } break;
335
- case PrimitiveKind::UInt64: {
336
- if (RG_UNLIKELY(!value.IsNumber() && !value.IsBigInt())) {
337
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value for argument %2, expected number", GetValueType(instance, value), param.offset + 1);
338
- return false;
339
- }
340
-
341
- uint64_t v = CopyNumber<uint64_t>(value);
342
- *((param.gpr_count ? gpr_ptr : args_ptr)++) = v;
343
- } break;
347
+ case PrimitiveKind::Int8: { PUSH_INTEGER(int8_t); } break;
348
+ case PrimitiveKind::UInt8: { PUSH_INTEGER(uint8_t); } break;
349
+ case PrimitiveKind::Int16: { PUSH_INTEGER(int16_t); } break;
350
+ case PrimitiveKind::Int16S: { PUSH_INTEGER_SWAP(int16_t); } break;
351
+ case PrimitiveKind::UInt16: { PUSH_INTEGER(uint16_t); } break;
352
+ case PrimitiveKind::UInt16S: { PUSH_INTEGER_SWAP(uint16_t); } break;
353
+ case PrimitiveKind::Int32: { PUSH_INTEGER(int32_t); } break;
354
+ case PrimitiveKind::Int32S: { PUSH_INTEGER_SWAP(int32_t); } break;
355
+ case PrimitiveKind::UInt32: { PUSH_INTEGER(uint32_t); } break;
356
+ case PrimitiveKind::UInt32S: { PUSH_INTEGER_SWAP(uint32_t); } break;
357
+ case PrimitiveKind::Int64: { PUSH_INTEGER(int64_t); } break;
358
+ case PrimitiveKind::Int64S: { PUSH_INTEGER_SWAP(int64_t); } break;
359
+ case PrimitiveKind::UInt64: { PUSH_INTEGER(uint64_t); } break;
360
+ case PrimitiveKind::UInt64S: { PUSH_INTEGER_SWAP(uint64_t); } break;
344
361
  case PrimitiveKind::String: {
345
362
  const char *str;
346
363
  if (RG_LIKELY(value.IsString())) {
@@ -463,6 +480,9 @@ bool CallData::Prepare(const Napi::CallbackInfo &info)
463
480
  }
464
481
  }
465
482
 
483
+ #undef PUSH_INTEGER_SWAP
484
+ #undef PUSH_INTEGER
485
+
466
486
  new_sp = mem->stack.end();
467
487
 
468
488
  return true;
@@ -486,11 +506,17 @@ void CallData::Execute()
486
506
  case PrimitiveKind::Int8:
487
507
  case PrimitiveKind::UInt8:
488
508
  case PrimitiveKind::Int16:
509
+ case PrimitiveKind::Int16S:
489
510
  case PrimitiveKind::UInt16:
511
+ case PrimitiveKind::UInt16S:
490
512
  case PrimitiveKind::Int32:
513
+ case PrimitiveKind::Int32S:
491
514
  case PrimitiveKind::UInt32:
492
- case PrimitiveKind::Int64:
515
+ case PrimitiveKind::UInt32S:
516
+ case PrimitiveKind::Int64:
517
+ case PrimitiveKind::Int64S:
493
518
  case PrimitiveKind::UInt64:
519
+ case PrimitiveKind::UInt64S:
494
520
  case PrimitiveKind::String:
495
521
  case PrimitiveKind::String16:
496
522
  case PrimitiveKind::Pointer:
@@ -536,11 +562,17 @@ Napi::Value CallData::Complete()
536
562
  case PrimitiveKind::Int8: return Napi::Number::New(env, (double)result.i8);
537
563
  case PrimitiveKind::UInt8: return Napi::Number::New(env, (double)result.u8);
538
564
  case PrimitiveKind::Int16: return Napi::Number::New(env, (double)result.i16);
565
+ case PrimitiveKind::Int16S: return Napi::Number::New(env, (double)ReverseBytes(result.i16));
539
566
  case PrimitiveKind::UInt16: return Napi::Number::New(env, (double)result.u16);
567
+ case PrimitiveKind::UInt16S: return Napi::Number::New(env, (double)ReverseBytes(result.u16));
540
568
  case PrimitiveKind::Int32: return Napi::Number::New(env, (double)result.i32);
569
+ case PrimitiveKind::Int32S: return Napi::Number::New(env, (double)ReverseBytes(result.i32));
541
570
  case PrimitiveKind::UInt32: return Napi::Number::New(env, (double)result.u32);
571
+ case PrimitiveKind::UInt32S: return Napi::Number::New(env, (double)ReverseBytes(result.u32));
542
572
  case PrimitiveKind::Int64: return NewBigInt(env, result.i64);
573
+ case PrimitiveKind::Int64S: return NewBigInt(env, ReverseBytes(result.i64));
543
574
  case PrimitiveKind::UInt64: return NewBigInt(env, result.u64);
575
+ case PrimitiveKind::UInt64S: return NewBigInt(env, ReverseBytes(result.u64));
544
576
  case PrimitiveKind::String: return result.ptr ? Napi::String::New(env, (const char *)result.ptr) : env.Null();
545
577
  case PrimitiveKind::String16: return result.ptr ? Napi::String::New(env, (const char16_t *)result.ptr) : env.Null();
546
578
  case PrimitiveKind::Pointer:
@@ -629,36 +661,76 @@ void CallData::Relay(Size idx, uint8_t *own_sp, uint8_t *caller_sp, BackRegister
629
661
  Napi::Value arg = Napi::Number::New(env, d);
630
662
  arguments.Append(arg);
631
663
  } break;
664
+ case PrimitiveKind::Int16S: {
665
+ int16_t v = *(int16_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
666
+ double d = (double)ReverseBytes(v);
667
+
668
+ Napi::Value arg = Napi::Number::New(env, d);
669
+ arguments.Append(arg);
670
+ } break;
632
671
  case PrimitiveKind::UInt16: {
633
672
  double d = (double)*(uint16_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
634
673
 
635
674
  Napi::Value arg = Napi::Number::New(env, d);
636
675
  arguments.Append(arg);
637
676
  } break;
677
+ case PrimitiveKind::UInt16S: {
678
+ uint16_t v = *(uint16_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
679
+ double d = (double)ReverseBytes(v);
680
+
681
+ Napi::Value arg = Napi::Number::New(env, d);
682
+ arguments.Append(arg);
683
+ } break;
638
684
  case PrimitiveKind::Int32: {
639
685
  double d = (double)*(int32_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
640
686
 
641
687
  Napi::Value arg = Napi::Number::New(env, d);
642
688
  arguments.Append(arg);
643
689
  } break;
690
+ case PrimitiveKind::Int32S: {
691
+ int32_t v = *(int32_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
692
+ double d = (double)ReverseBytes(v);
693
+
694
+ Napi::Value arg = Napi::Number::New(env, d);
695
+ arguments.Append(arg);
696
+ } break;
644
697
  case PrimitiveKind::UInt32: {
645
698
  double d = (double)*(uint32_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
646
699
 
647
700
  Napi::Value arg = Napi::Number::New(env, d);
648
701
  arguments.Append(arg);
649
702
  } break;
703
+ case PrimitiveKind::UInt32S: {
704
+ uint32_t v = *(uint32_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
705
+ double d = (double)ReverseBytes(v);
706
+
707
+ Napi::Value arg = Napi::Number::New(env, d);
708
+ arguments.Append(arg);
709
+ } break;
650
710
  case PrimitiveKind::Int64: {
651
711
  int64_t v = *(int64_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
652
712
 
653
713
  Napi::Value arg = NewBigInt(env, v);
654
714
  arguments.Append(arg);
655
715
  } break;
716
+ case PrimitiveKind::Int64S: {
717
+ int64_t v = *(int64_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
718
+
719
+ Napi::Value arg = NewBigInt(env, ReverseBytes(v));
720
+ arguments.Append(arg);
721
+ } break;
656
722
  case PrimitiveKind::UInt64: {
657
723
  uint64_t v = *(uint64_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
658
724
 
659
725
  Napi::Value arg = NewBigInt(env, v);
660
726
  arguments.Append(arg);
661
727
  } break;
728
+ case PrimitiveKind::UInt64S: {
729
+ uint64_t v = *(uint64_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
730
+
731
+ Napi::Value arg = NewBigInt(env, ReverseBytes(v));
732
+ arguments.Append(arg);
733
+ } break;
662
734
  case PrimitiveKind::String: {
663
735
  const char *str = *(const char **)((param.gpr_count ? gpr_ptr : args_ptr)++);
664
736
 
@@ -758,6 +830,27 @@ void CallData::Relay(Size idx, uint8_t *own_sp, uint8_t *caller_sp, BackRegister
758
830
  if (RG_UNLIKELY(env.IsExceptionPending()))
759
831
  return;
760
832
 
833
+ #define RETURN_INTEGER(CType) \
834
+ do { \
835
+ if (RG_UNLIKELY(!value.IsNumber() && !value.IsBigInt())) { \
836
+ ThrowError<Napi::TypeError>(env, "Unexpected %1 value for return value, expected number", GetValueType(instance, value)); \
837
+ return; \
838
+ } \
839
+ \
840
+ CType v = CopyNumber<CType>(value); \
841
+ out_reg->rax = (uint64_t)v; \
842
+ } while (false)
843
+ #define RETURN_INTEGER_SWAP(CType) \
844
+ do { \
845
+ if (RG_UNLIKELY(!value.IsNumber() && !value.IsBigInt())) { \
846
+ ThrowError<Napi::TypeError>(env, "Unexpected %1 value for return value, expected number", GetValueType(instance, value)); \
847
+ return; \
848
+ } \
849
+ \
850
+ CType v = CopyNumber<CType>(value); \
851
+ out_reg->rax = (uint64_t)ReverseBytes(v); \
852
+ } while (false)
853
+
761
854
  // Convert the result
762
855
  switch (type->primitive) {
763
856
  case PrimitiveKind::Void: {} break;
@@ -770,22 +863,20 @@ void CallData::Relay(Size idx, uint8_t *own_sp, uint8_t *caller_sp, BackRegister
770
863
  bool b = value.As<Napi::Boolean>();
771
864
  out_reg->rax = (uint64_t)b;
772
865
  } break;
773
- case PrimitiveKind::Int8:
774
- case PrimitiveKind::UInt8:
775
- case PrimitiveKind::Int16:
776
- case PrimitiveKind::UInt16:
777
- case PrimitiveKind::Int32:
778
- case PrimitiveKind::UInt32:
779
- case PrimitiveKind::Int64:
780
- case PrimitiveKind::UInt64: {
781
- if (RG_UNLIKELY(!value.IsNumber() && !value.IsBigInt())) {
782
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value for return value, expected number", GetValueType(instance, value));
783
- return;
784
- }
785
-
786
- int64_t v = CopyNumber<int64_t>(value);
787
- out_reg->rax = (uint64_t)v;
788
- } break;
866
+ case PrimitiveKind::Int8: { RETURN_INTEGER(int8_t); } break;
867
+ case PrimitiveKind::UInt8: { RETURN_INTEGER(uint8_t); } break;
868
+ case PrimitiveKind::Int16: { RETURN_INTEGER(int16_t); } break;
869
+ case PrimitiveKind::Int16S: { RETURN_INTEGER_SWAP(int16_t); } break;
870
+ case PrimitiveKind::UInt16: { RETURN_INTEGER(uint16_t); } break;
871
+ case PrimitiveKind::UInt16S: { RETURN_INTEGER_SWAP(uint16_t); } break;
872
+ case PrimitiveKind::Int32: { RETURN_INTEGER(int32_t); } break;
873
+ case PrimitiveKind::Int32S: { RETURN_INTEGER_SWAP(int32_t); } break;
874
+ case PrimitiveKind::UInt32: { RETURN_INTEGER(uint32_t); } break;
875
+ case PrimitiveKind::UInt32S: { RETURN_INTEGER_SWAP(uint32_t); } break;
876
+ case PrimitiveKind::Int64: { RETURN_INTEGER(int64_t); } break;
877
+ case PrimitiveKind::Int64S: { RETURN_INTEGER_SWAP(int64_t); } break;
878
+ case PrimitiveKind::UInt64: { RETURN_INTEGER(uint64_t); } break;
879
+ case PrimitiveKind::UInt64S: { RETURN_INTEGER_SWAP(uint64_t); } break;
789
880
  case PrimitiveKind::String: {
790
881
  const char *str;
791
882
  if (RG_LIKELY(value.IsString())) {
@@ -916,6 +1007,9 @@ void CallData::Relay(Size idx, uint8_t *own_sp, uint8_t *caller_sp, BackRegister
916
1007
  case PrimitiveKind::Prototype: { RG_UNREACHABLE(); } break;
917
1008
  }
918
1009
 
1010
+ #undef RETURN_INTEGER_SWAP
1011
+ #undef RETURN_INTEGER
1012
+
919
1013
  err_guard.Disable();
920
1014
  }
921
1015
 
@@ -141,6 +141,27 @@ bool CallData::Prepare(const Napi::CallbackInfo &info)
141
141
  *(uint8_t **)(args_ptr++) = return_ptr;
142
142
  }
143
143
 
144
+ #define PUSH_INTEGER(CType) \
145
+ do { \
146
+ if (RG_UNLIKELY(!value.IsNumber() && !value.IsBigInt())) { \
147
+ ThrowError<Napi::TypeError>(env, "Unexpected %1 value for argument %2, expected number", GetValueType(instance, value), param.offset + 1); \
148
+ return false; \
149
+ } \
150
+ \
151
+ CType v = CopyNumber<CType>(value); \
152
+ *(args_ptr++) = (uint64_t)v; \
153
+ } while (false)
154
+ #define PUSH_INTEGER_SWAP(CType) \
155
+ do { \
156
+ if (RG_UNLIKELY(!value.IsNumber() && !value.IsBigInt())) { \
157
+ ThrowError<Napi::TypeError>(env, "Unexpected %1 value for argument %2, expected number", GetValueType(instance, value), param.offset + 1); \
158
+ return false; \
159
+ } \
160
+ \
161
+ CType v = CopyNumber<CType>(value); \
162
+ *(args_ptr++) = (uint64_t)ReverseBytes(v); \
163
+ } while (false)
164
+
144
165
  // Push arguments
145
166
  for (Size i = 0; i < func->parameters.len; i++) {
146
167
  const ParameterInfo &param = func->parameters[i];
@@ -161,30 +182,20 @@ bool CallData::Prepare(const Napi::CallbackInfo &info)
161
182
 
162
183
  *(bool *)(args_ptr++) = b;
163
184
  } break;
164
- case PrimitiveKind::Int8:
165
- case PrimitiveKind::UInt8:
166
- case PrimitiveKind::Int16:
167
- case PrimitiveKind::UInt16:
168
- case PrimitiveKind::Int32:
169
- case PrimitiveKind::UInt32:
170
- case PrimitiveKind::Int64: {
171
- if (RG_UNLIKELY(!value.IsNumber() && !value.IsBigInt())) {
172
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value for argument %2, expected number", GetValueType(instance, value), param.offset + 1);
173
- return false;
174
- }
175
-
176
- int64_t v = CopyNumber<int64_t>(value);
177
- *(int64_t *)(args_ptr++) = v;
178
- } break;
179
- case PrimitiveKind::UInt64: {
180
- if (RG_UNLIKELY(!value.IsNumber() && !value.IsBigInt())) {
181
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value for argument %2, expected number", GetValueType(instance, value), param.offset + 1);
182
- return false;
183
- }
184
-
185
- uint64_t v = CopyNumber<uint64_t>(value);
186
- *(args_ptr++) = v;
187
- } break;
185
+ case PrimitiveKind::Int8: { PUSH_INTEGER(int8_t); } break;
186
+ case PrimitiveKind::UInt8: { PUSH_INTEGER(uint8_t); } break;
187
+ case PrimitiveKind::Int16: { PUSH_INTEGER(int16_t); } break;
188
+ case PrimitiveKind::Int16S: { PUSH_INTEGER_SWAP(int16_t); } break;
189
+ case PrimitiveKind::UInt16: { PUSH_INTEGER(uint16_t); } break;
190
+ case PrimitiveKind::UInt16S: { PUSH_INTEGER_SWAP(uint16_t); } break;
191
+ case PrimitiveKind::Int32: { PUSH_INTEGER(int32_t); } break;
192
+ case PrimitiveKind::Int32S: { PUSH_INTEGER_SWAP(int32_t); } break;
193
+ case PrimitiveKind::UInt32: { PUSH_INTEGER(uint32_t); } break;
194
+ case PrimitiveKind::UInt32S: { PUSH_INTEGER_SWAP(uint32_t); } break;
195
+ case PrimitiveKind::Int64: { PUSH_INTEGER(int64_t); } break;
196
+ case PrimitiveKind::Int64S: { PUSH_INTEGER_SWAP(int64_t); } break;
197
+ case PrimitiveKind::UInt64: { PUSH_INTEGER(uint64_t); } break;
198
+ case PrimitiveKind::UInt64S: { PUSH_INTEGER_SWAP(uint64_t); } break;
188
199
  case PrimitiveKind::String: {
189
200
  const char *str;
190
201
  if (RG_LIKELY(value.IsString())) {
@@ -286,6 +297,9 @@ bool CallData::Prepare(const Napi::CallbackInfo &info)
286
297
  }
287
298
  }
288
299
 
300
+ #undef PUSH_INTEGER_SWAP
301
+ #undef PUSH_INTEGER
302
+
289
303
  new_sp = mem->stack.end();
290
304
 
291
305
  return true;
@@ -308,11 +322,17 @@ void CallData::Execute()
308
322
  case PrimitiveKind::Int8:
309
323
  case PrimitiveKind::UInt8:
310
324
  case PrimitiveKind::Int16:
325
+ case PrimitiveKind::Int16S:
311
326
  case PrimitiveKind::UInt16:
327
+ case PrimitiveKind::UInt16S:
312
328
  case PrimitiveKind::Int32:
329
+ case PrimitiveKind::Int32S:
313
330
  case PrimitiveKind::UInt32:
331
+ case PrimitiveKind::UInt32S:
314
332
  case PrimitiveKind::Int64:
333
+ case PrimitiveKind::Int64S:
315
334
  case PrimitiveKind::UInt64:
335
+ case PrimitiveKind::UInt64S:
316
336
  case PrimitiveKind::String:
317
337
  case PrimitiveKind::String16:
318
338
  case PrimitiveKind::Pointer:
@@ -344,11 +364,17 @@ Napi::Value CallData::Complete()
344
364
  case PrimitiveKind::Int8: return Napi::Number::New(env, (double)result.i8);
345
365
  case PrimitiveKind::UInt8: return Napi::Number::New(env, (double)result.u8);
346
366
  case PrimitiveKind::Int16: return Napi::Number::New(env, (double)result.i16);
367
+ case PrimitiveKind::Int16S: return Napi::Number::New(env, (double)ReverseBytes(result.i16));
347
368
  case PrimitiveKind::UInt16: return Napi::Number::New(env, (double)result.u16);
369
+ case PrimitiveKind::UInt16S: return Napi::Number::New(env, (double)ReverseBytes(result.u16));
348
370
  case PrimitiveKind::Int32: return Napi::Number::New(env, (double)result.i32);
371
+ case PrimitiveKind::Int32S: return Napi::Number::New(env, (double)ReverseBytes(result.i32));
349
372
  case PrimitiveKind::UInt32: return Napi::Number::New(env, (double)result.u32);
373
+ case PrimitiveKind::UInt32S: return Napi::Number::New(env, (double)ReverseBytes(result.u32));
350
374
  case PrimitiveKind::Int64: return NewBigInt(env, result.i64);
375
+ case PrimitiveKind::Int64S: return NewBigInt(env, ReverseBytes(result.i64));
351
376
  case PrimitiveKind::UInt64: return NewBigInt(env, result.u64);
377
+ case PrimitiveKind::UInt64S: return NewBigInt(env, ReverseBytes(result.u64));
352
378
  case PrimitiveKind::String: return result.ptr ? Napi::String::New(env, (const char *)result.ptr) : env.Null();
353
379
  case PrimitiveKind::String16: return result.ptr ? Napi::String::New(env, (const char16_t *)result.ptr) : env.Null();
354
380
  case PrimitiveKind::Pointer:
@@ -440,6 +466,14 @@ void CallData::Relay(Size idx, uint8_t *own_sp, uint8_t *caller_sp, BackRegister
440
466
  Napi::Value arg = Napi::Number::New(env, d);
441
467
  arguments.Append(arg);
442
468
  } break;
469
+ case PrimitiveKind::Int16S: {
470
+ int16_t v = *(int16_t *)(j < 4 ? gpr_ptr + j : args_ptr);
471
+ double d = (double)ReverseBytes(v);
472
+ args_ptr += (j >= 4);
473
+
474
+ Napi::Value arg = Napi::Number::New(env, d);
475
+ arguments.Append(arg);
476
+ } break;
443
477
  case PrimitiveKind::UInt16: {
444
478
  double d = (double)*(uint16_t *)(j < 4 ? gpr_ptr + j : args_ptr);
445
479
  args_ptr += (j >= 4);
@@ -447,6 +481,14 @@ void CallData::Relay(Size idx, uint8_t *own_sp, uint8_t *caller_sp, BackRegister
447
481
  Napi::Value arg = Napi::Number::New(env, d);
448
482
  arguments.Append(arg);
449
483
  } break;
484
+ case PrimitiveKind::UInt16S: {
485
+ uint16_t v = *(uint16_t *)(j < 4 ? gpr_ptr + j : args_ptr);
486
+ double d = (double)ReverseBytes(v);
487
+ args_ptr += (j >= 4);
488
+
489
+ Napi::Value arg = Napi::Number::New(env, d);
490
+ arguments.Append(arg);
491
+ } break;
450
492
  case PrimitiveKind::Int32: {
451
493
  double d = (double)*(int32_t *)(j < 4 ? gpr_ptr + j : args_ptr);
452
494
  args_ptr += (j >= 4);
@@ -454,6 +496,14 @@ void CallData::Relay(Size idx, uint8_t *own_sp, uint8_t *caller_sp, BackRegister
454
496
  Napi::Value arg = Napi::Number::New(env, d);
455
497
  arguments.Append(arg);
456
498
  } break;
499
+ case PrimitiveKind::Int32S: {
500
+ int32_t v = *(int32_t *)(j < 4 ? gpr_ptr + j : args_ptr);
501
+ double d = (double)ReverseBytes(v);
502
+ args_ptr += (j >= 4);
503
+
504
+ Napi::Value arg = Napi::Number::New(env, d);
505
+ arguments.Append(arg);
506
+ } break;
457
507
  case PrimitiveKind::UInt32: {
458
508
  double d = (double)*(uint32_t *)(j < 4 ? gpr_ptr + j : args_ptr);
459
509
  args_ptr += (j >= 4);
@@ -461,6 +511,14 @@ void CallData::Relay(Size idx, uint8_t *own_sp, uint8_t *caller_sp, BackRegister
461
511
  Napi::Value arg = Napi::Number::New(env, d);
462
512
  arguments.Append(arg);
463
513
  } break;
514
+ case PrimitiveKind::UInt32S: {
515
+ uint32_t v = *(uint32_t *)(j < 4 ? gpr_ptr + j : args_ptr);
516
+ double d = (double)ReverseBytes(v);
517
+ args_ptr += (j >= 4);
518
+
519
+ Napi::Value arg = Napi::Number::New(env, d);
520
+ arguments.Append(arg);
521
+ } break;
464
522
  case PrimitiveKind::Int64: {
465
523
  int64_t v = *(int64_t *)(j < 4 ? gpr_ptr + j : args_ptr);
466
524
  args_ptr += (j >= 4);
@@ -468,6 +526,13 @@ void CallData::Relay(Size idx, uint8_t *own_sp, uint8_t *caller_sp, BackRegister
468
526
  Napi::Value arg = NewBigInt(env, v);
469
527
  arguments.Append(arg);
470
528
  } break;
529
+ case PrimitiveKind::Int64S: {
530
+ int64_t v = *(int64_t *)(j < 4 ? gpr_ptr + j : args_ptr);
531
+ args_ptr += (j >= 4);
532
+
533
+ Napi::Value arg = NewBigInt(env, ReverseBytes(v));
534
+ arguments.Append(arg);
535
+ } break;
471
536
  case PrimitiveKind::UInt64: {
472
537
  uint64_t v = *(uint64_t *)(j < 4 ? gpr_ptr + j : args_ptr);
473
538
  args_ptr += (j >= 4);
@@ -475,6 +540,13 @@ void CallData::Relay(Size idx, uint8_t *own_sp, uint8_t *caller_sp, BackRegister
475
540
  Napi::Value arg = NewBigInt(env, v);
476
541
  arguments.Append(arg);
477
542
  } break;
543
+ case PrimitiveKind::UInt64S: {
544
+ uint64_t v = *(uint64_t *)(j < 4 ? gpr_ptr + j : args_ptr);
545
+ args_ptr += (j >= 4);
546
+
547
+ Napi::Value arg = NewBigInt(env, ReverseBytes(v));
548
+ arguments.Append(arg);
549
+ } break;
478
550
  case PrimitiveKind::String: {
479
551
  const char *str = *(const char **)(j < 4 ? gpr_ptr + j : args_ptr);
480
552
  args_ptr += (j >= 4);
@@ -557,6 +629,27 @@ void CallData::Relay(Size idx, uint8_t *own_sp, uint8_t *caller_sp, BackRegister
557
629
  if (RG_UNLIKELY(env.IsExceptionPending()))
558
630
  return;
559
631
 
632
+ #define RETURN_INTEGER(CType) \
633
+ do { \
634
+ if (RG_UNLIKELY(!value.IsNumber() && !value.IsBigInt())) { \
635
+ ThrowError<Napi::TypeError>(env, "Unexpected %1 value for return value, expected number", GetValueType(instance, value)); \
636
+ return; \
637
+ } \
638
+ \
639
+ CType v = CopyNumber<CType>(value); \
640
+ out_reg->rax = (uint64_t)v; \
641
+ } while (false)
642
+ #define RETURN_INTEGER_SWAP(CType) \
643
+ do { \
644
+ if (RG_UNLIKELY(!value.IsNumber() && !value.IsBigInt())) { \
645
+ ThrowError<Napi::TypeError>(env, "Unexpected %1 value for return value, expected number", GetValueType(instance, value)); \
646
+ return; \
647
+ } \
648
+ \
649
+ CType v = CopyNumber<CType>(value); \
650
+ out_reg->rax = (uint64_t)ReverseBytes(v); \
651
+ } while (false)
652
+
560
653
  switch (type->primitive) {
561
654
  case PrimitiveKind::Void: {} break;
562
655
  case PrimitiveKind::Bool: {
@@ -568,22 +661,20 @@ void CallData::Relay(Size idx, uint8_t *own_sp, uint8_t *caller_sp, BackRegister
568
661
  bool b = value.As<Napi::Boolean>();
569
662
  out_reg->rax = (uint64_t)b;
570
663
  } break;
571
- case PrimitiveKind::Int8:
572
- case PrimitiveKind::UInt8:
573
- case PrimitiveKind::Int16:
574
- case PrimitiveKind::UInt16:
575
- case PrimitiveKind::Int32:
576
- case PrimitiveKind::UInt32:
577
- case PrimitiveKind::Int64:
578
- case PrimitiveKind::UInt64: {
579
- if (RG_UNLIKELY(!value.IsNumber() && !value.IsBigInt())) {
580
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value for return value, expected number", GetValueType(instance, value));
581
- return;
582
- }
583
-
584
- int64_t v = CopyNumber<int64_t>(value);
585
- out_reg->rax = (uint64_t)v;
586
- } break;
664
+ case PrimitiveKind::Int8: { RETURN_INTEGER(int8_t); } break;
665
+ case PrimitiveKind::UInt8: { RETURN_INTEGER(uint8_t); } break;
666
+ case PrimitiveKind::Int16: { RETURN_INTEGER(int16_t); } break;
667
+ case PrimitiveKind::Int16S: { RETURN_INTEGER_SWAP(int16_t); } break;
668
+ case PrimitiveKind::UInt16: { RETURN_INTEGER(uint16_t); } break;
669
+ case PrimitiveKind::UInt16S: { RETURN_INTEGER_SWAP(uint16_t); } break;
670
+ case PrimitiveKind::Int32: { RETURN_INTEGER(int32_t); } break;
671
+ case PrimitiveKind::Int32S: { RETURN_INTEGER_SWAP(int32_t); } break;
672
+ case PrimitiveKind::UInt32: { RETURN_INTEGER(uint32_t); } break;
673
+ case PrimitiveKind::UInt32S: { RETURN_INTEGER_SWAP(uint32_t); } break;
674
+ case PrimitiveKind::Int64: { RETURN_INTEGER(int64_t); } break;
675
+ case PrimitiveKind::Int64S: { RETURN_INTEGER_SWAP(int64_t); } break;
676
+ case PrimitiveKind::UInt64: { RETURN_INTEGER(uint64_t); } break;
677
+ case PrimitiveKind::UInt64S: { RETURN_INTEGER_SWAP(uint64_t); } break;
587
678
  case PrimitiveKind::String: {
588
679
  const char *str;
589
680
  if (RG_LIKELY(value.IsString())) {
@@ -696,6 +787,9 @@ void CallData::Relay(Size idx, uint8_t *own_sp, uint8_t *caller_sp, BackRegister
696
787
  case PrimitiveKind::Prototype: { RG_UNREACHABLE(); } break;
697
788
  }
698
789
 
790
+ #undef RETURN_INTEGER_SWAP
791
+ #undef RETURN_INTEGER
792
+
699
793
  err_guard.Disable();
700
794
  }
701
795