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
package/test/sync.js CHANGED
@@ -15,7 +15,6 @@
15
15
 
16
16
  const koffi = require('./build/koffi.node');
17
17
  const assert = require('assert');
18
- const path = require('path');
19
18
 
20
19
  const Pack1 = koffi.struct('Pack1', {
21
20
  a: 'int'
@@ -62,7 +61,7 @@ const IntFloat = koffi.struct('IntFloat', {
62
61
 
63
62
  const BFG = koffi.struct('BFG', {
64
63
  a: 'int8_t',
65
- b: 'int64_t',
64
+ b: [16, 'int64_t'],
66
65
  c: 'char',
67
66
  d: 'const char *',
68
67
  e: 'short',
@@ -115,6 +114,21 @@ const StrStruct = koffi.struct('StrStruct', {
115
114
  str16: koffi.types.string16
116
115
  });
117
116
 
117
+ const EndianInts = koffi.struct('EndianInts', {
118
+ i16le: 'int16_le_t',
119
+ i16be: 'int16_be_t',
120
+ u16le: 'uint16_le_t',
121
+ u16be: 'uint16_be_t',
122
+ i32le: 'int32_le_t',
123
+ i32be: 'int32_be_t',
124
+ u32le: 'uint32_le_t',
125
+ u32be: 'uint32_be_t',
126
+ i64le: 'int64_le_t',
127
+ i64be: 'int64_be_t',
128
+ u64le: 'uint64_le_t',
129
+ u64be: 'uint64_be_t'
130
+ });
131
+
118
132
  main();
119
133
 
120
134
  async function main() {
@@ -128,7 +142,7 @@ async function main() {
128
142
  }
129
143
 
130
144
  async function test() {
131
- let lib_filename = path.dirname(__filename) + '/build/misc' + koffi.extension;
145
+ let lib_filename = __dirname + '/build/misc' + koffi.extension;
132
146
  let lib = koffi.load(lib_filename);
133
147
 
134
148
  const GetMinusOne1 = lib.func('int8_t GetMinusOne1(void)');
@@ -160,6 +174,7 @@ async function test() {
160
174
  const ConcatenateToStr8 = lib.func('ConcatenateToStr8', 'str', [...Array(8).fill('int64_t'), koffi.struct('IJK8', {i: 'int64_t', j: 'int64_t', k: 'int64_t'}), 'int64_t']);
161
175
  const MakeBFG = lib.func('BFG __stdcall MakeBFG(_Out_ BFG *p, int x, double y, const char *str)');
162
176
  const MakePackedBFG = lib.func('AliasBFG __fastcall MakePackedBFG(int x, double y, _Out_ PackedBFG *p, const char *str)');
177
+ const MakePolymorphBFG = lib.func('void MakePolymorphBFG(int type, int x, double y, const char *str, _Out_ void *p)');
163
178
  const ReturnBigString = process.platform == 'win32' ?
164
179
  lib.stdcall(1, koffi.disposable('str', koffi.free), ['str']) :
165
180
  lib.func('const char * __stdcall ReturnBigString(const char *str)');
@@ -186,6 +201,24 @@ async function test() {
186
201
  const MultiplyIntegers = lib.func('void MultiplyIntegers(int multiplier, _Inout_ int *values, int len)');
187
202
  const ThroughStr = lib.func('str ThroughStr(StrStruct s)');
188
203
  const ThroughStr16 = lib.func('str16 ThroughStr16(StrStruct s)');
204
+ const ReverseBytes = lib.func('void ReverseBytes(_Inout_ void *array, int len)');
205
+ const CopyEndianInts1 = lib.func('void CopyEndianInts1(EndianInts ints, _Out_ uint8_t *buf)');
206
+ const CopyEndianInts2 = lib.func('void CopyEndianInts2(int16_le_t i16le, int16_be_t i16be, uint16_le_t u16le, uint16_be_t u16be, ' +
207
+ 'int32_le_t i32le, int32_be_t i32be, uint32_le_t u32le, uint32_be_t u32be, ' +
208
+ 'int64_le_t i64le, int64_be_t i64be, uint64_le_t u64le, uint64_be_t u64be, ' +
209
+ '_Out_ void *out)');
210
+ const ReturnEndianInt2SL = lib.func('int16_le_t ReturnEndianInt2(int16_be_t v)');
211
+ const ReturnEndianInt2SB = lib.func('int16_be_t ReturnEndianInt2(int16_le_t v)');
212
+ const ReturnEndianInt2UL = lib.func('uint16_le_t ReturnEndianInt2(uint16_be_t v)');
213
+ const ReturnEndianInt2UB = lib.func('uint16_be_t ReturnEndianInt2(uint16_le_t v)');
214
+ const ReturnEndianInt4SL = lib.func('int32_le_t ReturnEndianInt4(int32_be_t v)');
215
+ const ReturnEndianInt4SB = lib.func('int32_be_t ReturnEndianInt4(int32_le_t v)');
216
+ const ReturnEndianInt4UL = lib.func('uint32_le_t ReturnEndianInt4(uint32_be_t v)');
217
+ const ReturnEndianInt4UB = lib.func('uint32_be_t ReturnEndianInt4(uint32_le_t v)');
218
+ const ReturnEndianInt8SL = lib.func('int64_le_t ReturnEndianInt8(int64_be_t v)');
219
+ const ReturnEndianInt8SB = lib.func('int64_be_t ReturnEndianInt8(int64_le_t v)');
220
+ const ReturnEndianInt8UL = lib.func('uint64_le_t ReturnEndianInt8(uint64_be_t v)');
221
+ const ReturnEndianInt8UB = lib.func('uint64_be_t ReturnEndianInt8(uint64_le_t v)');
189
222
 
190
223
  // Simple signed value returns
191
224
  assert.equal(GetMinusOne1(), -1);
@@ -295,6 +328,17 @@ async function test() {
295
328
  assert.deepEqual(out, bfg);
296
329
  }
297
330
 
331
+ // Polymorph pointer
332
+ {
333
+ let bfg = {};
334
+
335
+ MakePolymorphBFG(0, 2, 7, 'boo', koffi.as(bfg, 'BFG *'));
336
+ assert.deepEqual(bfg, { a: 2, b: 4, c: -25, d: 'X/boo/X', e: 54, inner: { f: 14, g: 5 }});
337
+
338
+ MakePolymorphBFG(1, 2, 7, 'bies', koffi.as(bfg, 'PackedBFG *'));
339
+ assert.deepEqual(bfg, { a: 2, b: 4, c: -25, d: 'X/bies/X', e: 54, inner: { f: 14, g: 5 }});
340
+ }
341
+
298
342
  // Big string
299
343
  {
300
344
  let str = 'fooBAR!'.repeat(1024 * 1024);
@@ -394,4 +438,65 @@ async function test() {
394
438
  assert.equal(ThroughStr16({ str: null, str16: 'World!' }), 'World!');
395
439
  assert.equal(ThroughStr16({ str: 'World!', str16: null }), null);
396
440
  }
441
+
442
+ // Transparent typed arrays for void pointers
443
+ {
444
+ let arr8 = Uint8Array.from([1, 2, 3, 4, 5]);
445
+ let arr16 = Int16Array.from([1, 2, 3, 4, 5]);
446
+
447
+ ReverseBytes(arr8, arr8.byteLength);
448
+ assert.deepEqual(arr8, Uint8Array.from([5, 4, 3, 2, 1]));
449
+
450
+ ReverseBytes(arr16, arr16.byteLength);
451
+ assert.deepEqual(arr16, Int16Array.from([1280, 1024, 768, 512, 256]));
452
+ }
453
+
454
+ // Endian-sensitive integer types
455
+ {
456
+ let ints = {
457
+ i16le: 0x7BCD,
458
+ i16be: 0x7BCD,
459
+ u16le: 0x7BCD,
460
+ u16be: 0x7BCD,
461
+ i32le: 0x4EADBEEF,
462
+ i32be: 0x4EADBEEF,
463
+ u32le: 0x4EADBEEF,
464
+ u32be: 0x4EADBEEF,
465
+ i64le: 0x0123456789ABCDEFn,
466
+ i64be: 0x0123456789ABCDEFn,
467
+ u64le: 0x0123456789ABCDEFn,
468
+ u64be: 0x0123456789ABCDEFn
469
+ };
470
+
471
+ let out1 = new Uint8Array(56);
472
+ let out2 = new Uint8Array(56);
473
+
474
+ CopyEndianInts1(ints, out1);
475
+ CopyEndianInts2(ints.i16le, ints.i16be, ints.u16le, ints.u16be,
476
+ ints.i32le, ints.i32be, ints.u32le, ints.u32be,
477
+ ints.u64le, ints.u64be, ints.u64le, ints.u64be, out2);
478
+
479
+ assert.deepEqual(out1, Uint8Array.from([
480
+ 0xCD, 0x7B, 0x7B, 0xCD,
481
+ 0xCD, 0x7B, 0x7B, 0xCD,
482
+ 0xEF, 0xBE, 0xAD, 0x4E, 0x4E, 0xAD, 0xBE, 0xEF,
483
+ 0xEF, 0xBE, 0xAD, 0x4E, 0x4E, 0xAD, 0xBE, 0xEF,
484
+ 0xEF, 0xCD, 0xAB, 0x89, 0x67, 0x45, 0x23, 0x01, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
485
+ 0xEF, 0xCD, 0xAB, 0x89, 0x67, 0x45, 0x23, 0x01, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF
486
+ ]));
487
+ assert.deepEqual(out2, out1);
488
+
489
+ assert.equal(ReturnEndianInt2SL(0x7B6D), 0x6D7B);
490
+ assert.equal(ReturnEndianInt2SB(0x7B6D), 0x6D7B);
491
+ assert.equal(ReturnEndianInt2UL(0x7B6D), 0x6D7B);
492
+ assert.equal(ReturnEndianInt2UB(0x7B6D), 0x6D7B);
493
+ assert.equal(ReturnEndianInt4SL(0x4EADBE4F), 0x4FBEAD4E);
494
+ assert.equal(ReturnEndianInt4SB(0x4EADBE4F), 0x4FBEAD4E);
495
+ assert.equal(ReturnEndianInt4UL(0x4EADBE4F), 0x4FBEAD4E);
496
+ assert.equal(ReturnEndianInt4UB(0x4EADBE4F), 0x4FBEAD4E);
497
+ assert.equal(ReturnEndianInt8SL(0x0123456789ABCD3Fn), 0x3FCDAB8967452301n);
498
+ assert.equal(ReturnEndianInt8SB(0x0123456789ABCD3Fn), 0x3FCDAB8967452301n);
499
+ assert.equal(ReturnEndianInt8UL(0x0123456789ABCD3Fn), 0x3FCDAB8967452301n);
500
+ assert.equal(ReturnEndianInt8UB(0x0123456789ABCD3Fn), 0x3FCDAB8967452301n);
501
+ }
397
502
  }