koffi 1.0.5 → 1.1.0-beta.2
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/CMakeLists.txt +4 -0
- package/README.md +54 -24
- package/build/qemu/1.1.0-beta.2/koffi_darwin_x64.tar.gz +0 -0
- package/build/qemu/1.1.0-beta.2/koffi_freebsd_arm64.tar.gz +0 -0
- package/build/qemu/1.1.0-beta.2/koffi_freebsd_ia32.tar.gz +0 -0
- package/build/qemu/1.1.0-beta.2/koffi_freebsd_x64.tar.gz +0 -0
- package/build/qemu/1.1.0-beta.2/koffi_linux_arm.tar.gz +0 -0
- package/build/qemu/1.1.0-beta.2/koffi_linux_arm64.tar.gz +0 -0
- package/build/qemu/1.1.0-beta.2/koffi_linux_ia32.tar.gz +0 -0
- package/build/qemu/1.1.0-beta.2/koffi_linux_x64.tar.gz +0 -0
- package/build/qemu/1.1.0-beta.2/koffi_win32_ia32.tar.gz +0 -0
- package/build/qemu/1.1.0-beta.2/koffi_win32_x64.tar.gz +0 -0
- package/package.json +1 -1
- package/qemu/qemu.js +12 -5
- package/qemu/registry/machines.json +20 -10
- package/src/abi_arm32.cc +40 -51
- package/src/abi_arm64.cc +71 -138
- package/src/abi_x64_sysv.cc +37 -13
- package/src/abi_x64_win.cc +16 -6
- package/src/abi_x86.cc +16 -6
- package/src/call.cc +564 -58
- package/src/call.hh +32 -44
- package/src/ffi.cc +218 -19
- package/src/ffi.hh +27 -11
- package/src/parser.cc +4 -0
- package/src/util.cc +72 -0
- package/src/util.hh +2 -0
- package/test/misc.c +16 -10
- package/build/qemu/1.0.5/koffi_darwin_x64.tar.gz +0 -0
- package/build/qemu/1.0.5/koffi_freebsd_arm64.tar.gz +0 -0
- package/build/qemu/1.0.5/koffi_freebsd_ia32.tar.gz +0 -0
- package/build/qemu/1.0.5/koffi_freebsd_x64.tar.gz +0 -0
- package/build/qemu/1.0.5/koffi_linux_arm.tar.gz +0 -0
- package/build/qemu/1.0.5/koffi_linux_arm64.tar.gz +0 -0
- package/build/qemu/1.0.5/koffi_linux_ia32.tar.gz +0 -0
- package/build/qemu/1.0.5/koffi_linux_x64.tar.gz +0 -0
- package/build/qemu/1.0.5/koffi_win32_ia32.tar.gz +0 -0
- package/build/qemu/1.0.5/koffi_win32_x64.tar.gz +0 -0
- package/test/misc.js +0 -227
package/test/misc.c
CHANGED
|
@@ -60,8 +60,7 @@ typedef struct Float2 {
|
|
|
60
60
|
} Float2;
|
|
61
61
|
typedef struct Float3 {
|
|
62
62
|
float a;
|
|
63
|
-
float b;
|
|
64
|
-
float c;
|
|
63
|
+
float b[2];
|
|
65
64
|
} Float3;
|
|
66
65
|
|
|
67
66
|
typedef struct Double2 {
|
|
@@ -70,8 +69,10 @@ typedef struct Double2 {
|
|
|
70
69
|
} Double2;
|
|
71
70
|
typedef struct Double3 {
|
|
72
71
|
double a;
|
|
73
|
-
|
|
74
|
-
|
|
72
|
+
struct {
|
|
73
|
+
double b;
|
|
74
|
+
double c;
|
|
75
|
+
} s;
|
|
75
76
|
} Double3;
|
|
76
77
|
|
|
77
78
|
typedef struct IJK1 { int8_t i; int8_t j; int8_t k; } IJK1;
|
|
@@ -185,13 +186,18 @@ EXPORT Float3 PackFloat3(float a, float b, float c, Float3 *out)
|
|
|
185
186
|
Float3 ret;
|
|
186
187
|
|
|
187
188
|
ret.a = a;
|
|
188
|
-
ret.b = b;
|
|
189
|
-
ret.
|
|
189
|
+
ret.b[0] = b;
|
|
190
|
+
ret.b[1] = c;
|
|
190
191
|
*out = ret;
|
|
191
192
|
|
|
192
193
|
return ret;
|
|
193
194
|
}
|
|
194
195
|
|
|
196
|
+
EXPORT Float3 ThroughFloat3(Float3 f3)
|
|
197
|
+
{
|
|
198
|
+
return f3;
|
|
199
|
+
}
|
|
200
|
+
|
|
195
201
|
EXPORT Double2 PackDouble2(double a, double b, Double2 *out)
|
|
196
202
|
{
|
|
197
203
|
Double2 ret;
|
|
@@ -208,8 +214,8 @@ EXPORT Double3 PackDouble3(double a, double b, double c, Double3 *out)
|
|
|
208
214
|
Double3 ret;
|
|
209
215
|
|
|
210
216
|
ret.a = a;
|
|
211
|
-
ret.b = b;
|
|
212
|
-
ret.c = c;
|
|
217
|
+
ret.s.b = b;
|
|
218
|
+
ret.s.c = c;
|
|
213
219
|
*out = ret;
|
|
214
220
|
|
|
215
221
|
return ret;
|
|
@@ -275,7 +281,7 @@ EXPORT BFG STDCALL MakeBFG(BFG *p, int x, double y, const char *str)
|
|
|
275
281
|
{
|
|
276
282
|
BFG bfg;
|
|
277
283
|
|
|
278
|
-
char buf[64];
|
|
284
|
+
static char buf[64];
|
|
279
285
|
snprintf(buf, sizeof(buf), "X/%s/X", str);
|
|
280
286
|
|
|
281
287
|
bfg.a = x;
|
|
@@ -294,7 +300,7 @@ EXPORT PackedBFG FASTCALL MakePackedBFG(int x, double y, PackedBFG *p, const cha
|
|
|
294
300
|
{
|
|
295
301
|
PackedBFG bfg;
|
|
296
302
|
|
|
297
|
-
char buf[64];
|
|
303
|
+
static char buf[64];
|
|
298
304
|
snprintf(buf, sizeof(buf), "X/%s/X", str);
|
|
299
305
|
|
|
300
306
|
bfg.a = x;
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/test/misc.js
DELETED
|
@@ -1,227 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
// This program is free software: you can redistribute it and/or modify
|
|
4
|
-
// it under the terms of the GNU Affero General Public License as published by
|
|
5
|
-
// the Free Software Foundation, either version 3 of the License, or
|
|
6
|
-
// (at your option) any later version.
|
|
7
|
-
//
|
|
8
|
-
// This program is distributed in the hope that it will be useful,
|
|
9
|
-
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10
|
-
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11
|
-
// GNU Affero General Public License for more details.
|
|
12
|
-
//
|
|
13
|
-
// You should have received a copy of the GNU Affero General Public License
|
|
14
|
-
// along with this program. If not, see https://www.gnu.org/licenses/.
|
|
15
|
-
|
|
16
|
-
const koffi = require('./build/koffi.node');
|
|
17
|
-
const assert = require('assert');
|
|
18
|
-
const path = require('path');
|
|
19
|
-
|
|
20
|
-
const Pack1 = koffi.struct('Pack1', {
|
|
21
|
-
a: 'int'
|
|
22
|
-
});
|
|
23
|
-
const Pack2 = koffi.struct('Pack2', {
|
|
24
|
-
a: 'int',
|
|
25
|
-
b: 'int'
|
|
26
|
-
});
|
|
27
|
-
const Pack3 = koffi.struct('Pack3', {
|
|
28
|
-
a: 'int',
|
|
29
|
-
b: 'int',
|
|
30
|
-
c: 'int'
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
const Float2 = koffi.struct('Float2', {
|
|
34
|
-
a: 'float',
|
|
35
|
-
b: 'float'
|
|
36
|
-
});
|
|
37
|
-
const Float3 = koffi.struct('Float3', {
|
|
38
|
-
a: 'float',
|
|
39
|
-
b: 'float',
|
|
40
|
-
c: 'float'
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
const Double2 = koffi.struct('Double2', {
|
|
44
|
-
a: 'double',
|
|
45
|
-
b: 'double'
|
|
46
|
-
});
|
|
47
|
-
const Double3 = koffi.struct('Double3', {
|
|
48
|
-
a: 'double',
|
|
49
|
-
b: 'double',
|
|
50
|
-
c: 'double'
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
const BFG = koffi.struct('BFG', {
|
|
54
|
-
a: 'int8_t',
|
|
55
|
-
b: 'int64_t',
|
|
56
|
-
c: 'char',
|
|
57
|
-
d: 'string',
|
|
58
|
-
e: 'short',
|
|
59
|
-
inner: koffi.struct({
|
|
60
|
-
f: 'float',
|
|
61
|
-
g: 'double'
|
|
62
|
-
})
|
|
63
|
-
});
|
|
64
|
-
const PackedBFG = koffi.pack('PackedBFG', {
|
|
65
|
-
a: 'int8_t',
|
|
66
|
-
b: 'int64_t',
|
|
67
|
-
c: 'char',
|
|
68
|
-
d: 'string',
|
|
69
|
-
e: 'short',
|
|
70
|
-
inner: koffi.pack({
|
|
71
|
-
f: 'float',
|
|
72
|
-
g: 'double'
|
|
73
|
-
})
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
main();
|
|
77
|
-
|
|
78
|
-
async function main() {
|
|
79
|
-
try {
|
|
80
|
-
await test();
|
|
81
|
-
console.log('Success!');
|
|
82
|
-
} catch (err) {
|
|
83
|
-
console.error(err);
|
|
84
|
-
process.exit(1);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
async function test() {
|
|
89
|
-
let lib_filename = path.dirname(__filename) + '/build/misc' + koffi.extension;
|
|
90
|
-
let lib = koffi.load(lib_filename);
|
|
91
|
-
|
|
92
|
-
const FillPack1 = lib.func('FillPack1', 'void', ['int', koffi.out(koffi.pointer(Pack1))]);
|
|
93
|
-
const RetPack1 = lib.func('RetPack1', Pack1, ['int']);
|
|
94
|
-
const AddPack1 = lib.fastcall('AddPack1', 'void', ['int', koffi.inout(koffi.pointer(Pack1))]);
|
|
95
|
-
const FillPack2 = lib.func('FillPack2', 'void', ['int', 'int', koffi.out(koffi.pointer(Pack2))]);
|
|
96
|
-
const RetPack2 = lib.func('RetPack2', Pack2, ['int', 'int']);
|
|
97
|
-
const AddPack2 = lib.fastcall('AddPack2', 'void', ['int', 'int', koffi.inout(koffi.pointer(Pack2))]);
|
|
98
|
-
const FillPack3 = lib.func('FillPack3', 'void', ['int', 'int', 'int', koffi.out(koffi.pointer(Pack3))]);
|
|
99
|
-
const RetPack3 = lib.func('RetPack3', Pack3, ['int', 'int', 'int']);
|
|
100
|
-
const AddPack3 = lib.fastcall('AddPack3', 'void', ['int', 'int', 'int', koffi.inout(koffi.pointer(Pack3))]);
|
|
101
|
-
const PackFloat2 = lib.func('Float2 PackFloat2(float a, float b, _Out_ Float2 *out)');
|
|
102
|
-
const PackFloat3 = lib.func('Float3 PackFloat3(float a, float b, float c, _Out_ Float3 *out)');
|
|
103
|
-
const PackDouble2 = lib.func('Double2 PackDouble2(double a, double b, _Out_ Double2 *out)');
|
|
104
|
-
const PackDouble3 = lib.func('Double3 PackDouble3(double a, double b, double c, _Out_ Double3 *out)');
|
|
105
|
-
const ConcatenateToInt1 = lib.func('ConcatenateToInt1', 'int64_t', Array(12).fill('int8_t'));
|
|
106
|
-
const ConcatenateToInt4 = lib.func('ConcatenateToInt4', 'int64_t', Array(12).fill('int32_t'));
|
|
107
|
-
const ConcatenateToInt8 = lib.func('ConcatenateToInt8', 'int64_t', Array(12).fill('int64_t'));
|
|
108
|
-
const ConcatenateToStr1 = lib.func('ConcatenateToStr1', 'string', [...Array(8).fill('int8_t'), koffi.struct('IJK1', {i: 'int8_t', j: 'int8_t', k: 'int8_t'}), 'int8_t']);
|
|
109
|
-
const ConcatenateToStr4 = lib.func('ConcatenateToStr4', 'string', [...Array(8).fill('int32_t'), koffi.pointer(koffi.struct('IJK4', {i: 'int32_t', j: 'int32_t', k: 'int32_t'})), 'int32_t']);
|
|
110
|
-
const ConcatenateToStr8 = lib.func('ConcatenateToStr8', 'string', [...Array(8).fill('int64_t'), koffi.struct('IJK8', {i: 'int64_t', j: 'int64_t', k: 'int64_t'}), 'int64_t']);
|
|
111
|
-
const MakeBFG = lib.func('BFG __stdcall MakeBFG(_Out_ BFG *p, int x, double y, const char *str)');
|
|
112
|
-
const MakePackedBFG = lib.func('PackedBFG __fastcall MakePackedBFG(int x, double y, _Out_ PackedBFG *p, const char *str)');
|
|
113
|
-
const ReturnBigString = process.platform == 'win32' ?
|
|
114
|
-
lib.stdcall(1, 'string', ['string']) :
|
|
115
|
-
lib.func('const char * __stdcall ReturnBigString(const char *str)');
|
|
116
|
-
const PrintFmt = lib.func('const char *PrintFmt(const char *fmt, ...)');
|
|
117
|
-
const Concat16 = lib.func('const char16_t *Concat16(const char16_t *str1, const char16_t *str2)')
|
|
118
|
-
|
|
119
|
-
// Simple tests with Pack1
|
|
120
|
-
{
|
|
121
|
-
let p = {};
|
|
122
|
-
|
|
123
|
-
FillPack1(777, p);
|
|
124
|
-
assert.deepEqual(p, { a: 777 });
|
|
125
|
-
|
|
126
|
-
let q = RetPack1(6);
|
|
127
|
-
assert.deepEqual(q, { a: 6 });
|
|
128
|
-
|
|
129
|
-
AddPack1(6, p);
|
|
130
|
-
assert.deepEqual(p, { a: 783 });
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// Simple tests with Pack2
|
|
134
|
-
{
|
|
135
|
-
let p = {};
|
|
136
|
-
|
|
137
|
-
FillPack2(123, 456, p);
|
|
138
|
-
assert.deepEqual(p, { a: 123, b: 456 });
|
|
139
|
-
|
|
140
|
-
let q = RetPack2(6, 9);
|
|
141
|
-
assert.deepEqual(q, { a: 6, b: 9 });
|
|
142
|
-
|
|
143
|
-
AddPack2(6, 9, p);
|
|
144
|
-
assert.deepEqual(p, { a: 129, b: 465 });
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
// Simple tests with Pack3
|
|
148
|
-
{
|
|
149
|
-
let p = {};
|
|
150
|
-
|
|
151
|
-
FillPack3(1, 2, 3, p);
|
|
152
|
-
assert.deepEqual(p, { a: 1, b: 2, c: 3 });
|
|
153
|
-
|
|
154
|
-
let q = RetPack3(6, 9, -12);
|
|
155
|
-
assert.deepEqual(q, { a: 6, b: 9, c: -12 });
|
|
156
|
-
|
|
157
|
-
AddPack3(6, 9, -12, p);
|
|
158
|
-
assert.deepEqual(p, { a: 7, b: 11, c: -9 });
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
// HFA tests
|
|
162
|
-
{
|
|
163
|
-
let f2p = {};
|
|
164
|
-
let f2 = PackFloat2(1.5, 3.0, f2p);
|
|
165
|
-
assert.deepEqual(f2, { a: 1.5, b: 3.0 });
|
|
166
|
-
assert.deepEqual(f2, f2p);
|
|
167
|
-
|
|
168
|
-
let f3p = {};
|
|
169
|
-
let f3 = PackFloat3(20.0, 30.0, 40.0, f3p);
|
|
170
|
-
assert.deepEqual(f3, { a: 20.0, b: 30.0, c: 40.0 });
|
|
171
|
-
assert.deepEqual(f3, f3p);
|
|
172
|
-
|
|
173
|
-
let d2p = {};
|
|
174
|
-
let d2 = PackDouble2(1.0, 2.0, d2p);
|
|
175
|
-
assert.deepEqual(d2, { a: 1.0, b: 2.0 });
|
|
176
|
-
assert.deepEqual(d2, d2p);
|
|
177
|
-
|
|
178
|
-
let d3p = {};
|
|
179
|
-
let d3 = PackDouble3(0.5, 10.0, 5.0, d3p);
|
|
180
|
-
assert.deepEqual(d3, { a: 0.5, b: 10.0, c: 5.0 });
|
|
181
|
-
assert.deepEqual(d3, d3p);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
// Many parameters
|
|
185
|
-
{
|
|
186
|
-
assert.equal(ConcatenateToInt1(5, 6, 1, 2, 3, 9, 4, 4, 0, 6, 8, 7), 561239440687n);
|
|
187
|
-
assert.equal(ConcatenateToInt4(5, 6, 1, 2, 3, 9, 4, 4, 0, 6, 8, 7), 561239440687n);
|
|
188
|
-
assert.equal(ConcatenateToInt8(5, 6, 1, 2, 3, 9, 4, 4, 0, 6, 8, 7), 561239440687n);
|
|
189
|
-
assert.equal(ConcatenateToStr1(5, 6, 1, 2, 3, 9, 4, 4, {i: 0, j: 6, k: 8}, 7), '561239440687');
|
|
190
|
-
assert.equal(ConcatenateToStr4(5, 6, 1, 2, 3, 9, 4, 4, {i: 0, j: 6, k: 8}, 7), '561239440687');
|
|
191
|
-
assert.equal(ConcatenateToStr8(5, 6, 1, 2, 3, 9, 4, 4, {i: 0, j: 6, k: 8}, 7), '561239440687');
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
// Big struct
|
|
195
|
-
{
|
|
196
|
-
let out = {};
|
|
197
|
-
let bfg = MakeBFG(out, 2, 7, '__Hello123456789++++foobarFOOBAR!__');
|
|
198
|
-
assert.deepEqual(bfg, { a: 2, b: 4, c: -25, d: 'X/__Hello123456789++++foobarFOOBAR!__/X', e: 54, inner: { f: 14, g: 5 } });
|
|
199
|
-
assert.deepEqual(out, bfg);
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
// Packed struct
|
|
203
|
-
{
|
|
204
|
-
let out = {};
|
|
205
|
-
let bfg = MakePackedBFG(2, 7, out, '__Hello123456789++++foobarFOOBAR!__');
|
|
206
|
-
assert.deepEqual(bfg, { a: 2, b: 4, c: -25, d: 'X/__Hello123456789++++foobarFOOBAR!__/X', e: 54, inner: { f: 14, g: 5 } });
|
|
207
|
-
assert.deepEqual(out, bfg);
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
// Big string
|
|
211
|
-
{
|
|
212
|
-
let str = 'fooBAR!'.repeat(1024 * 1024);
|
|
213
|
-
assert.equal(ReturnBigString(str), str);
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
// Variadic
|
|
217
|
-
{
|
|
218
|
-
let str = PrintFmt('foo %d %g %s', 'int', 200, 'double', 1.5, 'string', 'BAR');
|
|
219
|
-
assert.equal(str, 'foo 200 1.5 BAR');
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
// UTF-16LE strings
|
|
223
|
-
{
|
|
224
|
-
let str = Concat16('Hello ', 'World!');
|
|
225
|
-
assert.equal(str, 'Hello World!');
|
|
226
|
-
}
|
|
227
|
-
}
|