koffi 2.3.0-beta.2 → 2.3.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 +27 -0
- package/doc/callbacks.md +4 -2
- package/doc/calls.md +3 -4
- package/package.json +3 -2
- package/src/koffi/build/2.3.1/koffi_darwin_arm64.tar.gz +0 -0
- package/src/koffi/build/2.3.1/koffi_darwin_x64.tar.gz +0 -0
- package/src/koffi/build/2.3.1/koffi_freebsd_arm64.tar.gz +0 -0
- package/src/koffi/build/2.3.1/koffi_freebsd_ia32.tar.gz +0 -0
- package/src/koffi/build/2.3.1/koffi_freebsd_x64.tar.gz +0 -0
- package/src/koffi/build/2.3.1/koffi_linux_arm32hf.tar.gz +0 -0
- package/src/koffi/build/2.3.1/koffi_linux_arm64.tar.gz +0 -0
- package/src/koffi/build/2.3.1/koffi_linux_ia32.tar.gz +0 -0
- package/src/koffi/build/2.3.1/koffi_linux_riscv64hf64.tar.gz +0 -0
- package/src/koffi/build/2.3.1/koffi_linux_x64.tar.gz +0 -0
- package/src/koffi/build/2.3.1/koffi_openbsd_ia32.tar.gz +0 -0
- package/src/koffi/build/2.3.1/koffi_openbsd_x64.tar.gz +0 -0
- package/src/koffi/build/2.3.1/koffi_win32_arm64.tar.gz +0 -0
- package/src/koffi/build/2.3.1/koffi_win32_ia32.tar.gz +0 -0
- package/src/koffi/build/2.3.1/koffi_win32_x64.tar.gz +0 -0
- package/src/koffi/src/call.cc +12 -0
- package/src/koffi/src/index.d.ts +95 -0
- package/src/koffi/src/parser.cc +1 -0
- package/src/koffi/build/2.3.0-beta.2/koffi_darwin_arm64.tar.gz +0 -0
- package/src/koffi/build/2.3.0-beta.2/koffi_darwin_x64.tar.gz +0 -0
- package/src/koffi/build/2.3.0-beta.2/koffi_freebsd_arm64.tar.gz +0 -0
- package/src/koffi/build/2.3.0-beta.2/koffi_freebsd_ia32.tar.gz +0 -0
- package/src/koffi/build/2.3.0-beta.2/koffi_freebsd_x64.tar.gz +0 -0
- package/src/koffi/build/2.3.0-beta.2/koffi_linux_arm32hf.tar.gz +0 -0
- package/src/koffi/build/2.3.0-beta.2/koffi_linux_arm64.tar.gz +0 -0
- package/src/koffi/build/2.3.0-beta.2/koffi_linux_ia32.tar.gz +0 -0
- package/src/koffi/build/2.3.0-beta.2/koffi_linux_riscv64hf64.tar.gz +0 -0
- package/src/koffi/build/2.3.0-beta.2/koffi_linux_x64.tar.gz +0 -0
- package/src/koffi/build/2.3.0-beta.2/koffi_openbsd_ia32.tar.gz +0 -0
- package/src/koffi/build/2.3.0-beta.2/koffi_openbsd_x64.tar.gz +0 -0
- package/src/koffi/build/2.3.0-beta.2/koffi_win32_arm64.tar.gz +0 -0
- package/src/koffi/build/2.3.0-beta.2/koffi_win32_ia32.tar.gz +0 -0
- package/src/koffi/build/2.3.0-beta.2/koffi_win32_x64.tar.gz +0 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,33 @@
|
|
|
2
2
|
|
|
3
3
|
## Version history
|
|
4
4
|
|
|
5
|
+
### Koffi 2.3
|
|
6
|
+
|
|
7
|
+
#### Koffi 2.3.1
|
|
8
|
+
|
|
9
|
+
**Main changes:**
|
|
10
|
+
|
|
11
|
+
- Error out when trying to use ambiguous `void *` arguments (input and/or output)
|
|
12
|
+
- Adjust TypeScript definitions ([@insraq](https://github.com/insraq))
|
|
13
|
+
- Fix possible crash when parsing invalid prototype
|
|
14
|
+
|
|
15
|
+
#### Koffi 2.3.0
|
|
16
|
+
|
|
17
|
+
**Main changes:**
|
|
18
|
+
|
|
19
|
+
- Allow buffers (TypedArray or ArrayBuffer) values for input and/or output pointer arguments (for polymorphic arguments)
|
|
20
|
+
- Support opaque buffers (TypedArray or ArrayBuffer) values in `koffi.decode()` to [decode output buffers](calls.md#output-buffers)
|
|
21
|
+
- Decode non-string types as arrays when an [explicit length is passed to koffi.decode()](callbacks.md#pointer-arguments)
|
|
22
|
+
|
|
23
|
+
**Other changes:**
|
|
24
|
+
|
|
25
|
+
- Drop TypedArray type check for array and pointer values (only the size matters)
|
|
26
|
+
- Allow ArrayBuffer everywhere TypedArray is supported
|
|
27
|
+
- Add TypeScript definition for Koffi ([@insraq](https://github.com/insraq))
|
|
28
|
+
- Improve documentation about opaque and polymorphic structs
|
|
29
|
+
- Improve documentation for `koffi.decode()`
|
|
30
|
+
- Reduce NPM package size (from 100 MB to 25 MB) by removing test code and dependencies
|
|
31
|
+
|
|
5
32
|
### Koffi 2.2
|
|
6
33
|
|
|
7
34
|
#### Koffi 2.2.5
|
package/doc/callbacks.md
CHANGED
|
@@ -134,7 +134,9 @@ let cb1 = koffi.register(store.get, 'IntCallback *'); // If a C function calls c
|
|
|
134
134
|
let cb2 = koffi.register(store, store.get, 'IntCallback *'); // However in this case, this will match the store object
|
|
135
135
|
```
|
|
136
136
|
|
|
137
|
-
##
|
|
137
|
+
## Special considerations
|
|
138
|
+
|
|
139
|
+
### Decoding pointer arguments
|
|
138
140
|
|
|
139
141
|
*New in Koffi 2.2, changed in Koffi 2.3*
|
|
140
142
|
|
|
@@ -177,7 +179,7 @@ There is also an optional ending `length` argument that you can use in two cases
|
|
|
177
179
|
In Koffi 2.2 and earlier versions, the length argument is only used to decode strings and is ignored otherwise.
|
|
178
180
|
```
|
|
179
181
|
|
|
180
|
-
|
|
182
|
+
### Asynchronous callbacks
|
|
181
183
|
|
|
182
184
|
*New in Koffi 2.2.2*
|
|
183
185
|
|
package/doc/calls.md
CHANGED
|
@@ -159,7 +159,7 @@ console.log(out[0]);
|
|
|
159
159
|
|
|
160
160
|
## Polymorphic parameters
|
|
161
161
|
|
|
162
|
-
### Input
|
|
162
|
+
### Input polymorphism
|
|
163
163
|
|
|
164
164
|
*New in Koffi 2.1*
|
|
165
165
|
|
|
@@ -221,7 +221,7 @@ let hdr = {};
|
|
|
221
221
|
console.log('PNG header:', hdr);
|
|
222
222
|
```
|
|
223
223
|
|
|
224
|
-
### Output
|
|
224
|
+
### Output buffers
|
|
225
225
|
|
|
226
226
|
*New in Koffi 2.3*
|
|
227
227
|
|
|
@@ -261,7 +261,7 @@ console.log(vec1); // { x: 3, y: 2, z: 1 }
|
|
|
261
261
|
console.log(vec2); // { x: 1, y: 2, z: 3 }
|
|
262
262
|
```
|
|
263
263
|
|
|
264
|
-
See [pointer arguments](callbacks.md#pointer-arguments) for more information about the decode function.
|
|
264
|
+
See [pointer arguments](callbacks.md#decoding-pointer-arguments) for more information about the decode function.
|
|
265
265
|
|
|
266
266
|
## Heap-allocated values
|
|
267
267
|
|
|
@@ -287,7 +287,6 @@ The following example illustrates the use of a disposable type derived from *str
|
|
|
287
287
|
const koffi = require('koffi');
|
|
288
288
|
const lib = koffi.load('libc.so.6');
|
|
289
289
|
|
|
290
|
-
// You can also use: const strdup = lib.func('const char *! strdup(const char *str)')
|
|
291
290
|
const HeapStr = koffi.disposable('str');
|
|
292
291
|
const strdup = lib.cdecl('strdup', HeapStr, ['str']);
|
|
293
292
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "koffi",
|
|
3
|
-
"version": "2.3.
|
|
4
|
-
"stable": "2.
|
|
3
|
+
"version": "2.3.1",
|
|
4
|
+
"stable": "2.3.1",
|
|
5
5
|
"description": "Fast and simple C FFI (foreign function interface) for Node.js",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"foreign",
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
"homepage": "https://koffi.dev/",
|
|
20
20
|
"author": "Niels Martignène <niels.martignene@protonmail.com>",
|
|
21
21
|
"main": "src/koffi/src/index.js",
|
|
22
|
+
"types": "src/koffi/src/index.d.ts",
|
|
22
23
|
"scripts": {
|
|
23
24
|
"install": "cnoke --prebuild -d src/koffi"
|
|
24
25
|
},
|
|
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
|
|
Binary file
|
package/src/koffi/src/call.cc
CHANGED
|
@@ -909,6 +909,12 @@ bool CallData::PushPointer(Napi::Value value, const TypeInfo *type, int directio
|
|
|
909
909
|
uint8_t *ptr = nullptr;
|
|
910
910
|
|
|
911
911
|
if (value.IsArray()) {
|
|
912
|
+
if (RG_UNLIKELY(!type->ref.type->size)) {
|
|
913
|
+
ThrowError<Napi::TypeError>(env, "Cannot pass %1 value to void *, use koffi.as()",
|
|
914
|
+
type->ref.type != instance->void_type ? "opaque" : "ambiguous");
|
|
915
|
+
return false;
|
|
916
|
+
}
|
|
917
|
+
|
|
912
918
|
Napi::Array array = value.As<Napi::Array>();
|
|
913
919
|
|
|
914
920
|
Size len = (Size)array.Length();
|
|
@@ -936,6 +942,12 @@ bool CallData::PushPointer(Napi::Value value, const TypeInfo *type, int directio
|
|
|
936
942
|
directions = 1;
|
|
937
943
|
}
|
|
938
944
|
} else if (RG_LIKELY(type->ref.type->primitive == PrimitiveKind::Record)) {
|
|
945
|
+
if (RG_UNLIKELY(!type->ref.type->size)) {
|
|
946
|
+
ThrowError<Napi::TypeError>(env, "Cannot pass %1 value to void *, use koffi.as()",
|
|
947
|
+
type->ref.type != instance->void_type ? "opaque" : "ambiguous");
|
|
948
|
+
return false;
|
|
949
|
+
}
|
|
950
|
+
|
|
939
951
|
Napi::Object obj = value.As<Napi::Object>();
|
|
940
952
|
RG_ASSERT(IsObject(value));
|
|
941
953
|
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// This program is free software: you can redistribute it and/or modify
|
|
2
|
+
// it under the terms of the GNU Lesser General Public License as published by
|
|
3
|
+
// the Free Software Foundation, either version 3 of the License, or
|
|
4
|
+
// (at your option) any later version.
|
|
5
|
+
//
|
|
6
|
+
// This program is distributed in the hope that it will be useful,
|
|
7
|
+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
8
|
+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
9
|
+
// GNU Lesser General Public License for more details.
|
|
10
|
+
//
|
|
11
|
+
// You should have received a copy of the GNU Lesser General Public License
|
|
12
|
+
// along with this program. If not, see https://www.gnu.org/licenses/.
|
|
13
|
+
|
|
14
|
+
declare module 'koffi' {
|
|
15
|
+
export function load(path: string): IKoffiLib;
|
|
16
|
+
|
|
17
|
+
interface IKoffiCType { __brand: 'IKoffiCType' }
|
|
18
|
+
interface IKoffiPointerCast { __brand: 'IKoffiPointerCast' }
|
|
19
|
+
interface IKoffiRegisteredCallback { __brand: 'IKoffiRegisteredCallback' }
|
|
20
|
+
|
|
21
|
+
type TypeSpec = string | IKoffiCType;
|
|
22
|
+
type TypeSpecWithAlignment = TypeSpec | [number, TypeSpec];
|
|
23
|
+
type TypeInfo = {
|
|
24
|
+
name: string;
|
|
25
|
+
primitive: string;
|
|
26
|
+
size: number;
|
|
27
|
+
alignment: number;
|
|
28
|
+
length: number;
|
|
29
|
+
ref: IKoffiCType;
|
|
30
|
+
members: Record<string, { name: string, type: IKoffiCType, offset: number }>;
|
|
31
|
+
};
|
|
32
|
+
type KoffiFunction = Function & { async: Function };
|
|
33
|
+
|
|
34
|
+
export interface IKoffiLib {
|
|
35
|
+
func(definition: string): KoffiFunction;
|
|
36
|
+
func(name: string, result: TypeSpec, arguments: TypeSpec[]): KoffiFunction;
|
|
37
|
+
|
|
38
|
+
cdecl(definition: string): KoffiFunction;
|
|
39
|
+
cdecl(name: string, result: TypeSpec, arguments: TypeSpec[]): KoffiFunction;
|
|
40
|
+
|
|
41
|
+
stdcall(definition: string): KoffiFunction;
|
|
42
|
+
stdcall(name: string, result: TypeSpec, arguments: TypeSpec[]): KoffiFunction;
|
|
43
|
+
|
|
44
|
+
fastcall(definition: string): KoffiFunction;
|
|
45
|
+
fastcall(name: string, result: TypeSpec, arguments: TypeSpec[]): KoffiFunction;
|
|
46
|
+
|
|
47
|
+
thiscall(definition: string): KoffiFunction;
|
|
48
|
+
thiscall(name: string, result: TypeSpec, arguments: TypeSpec[]): KoffiFunction;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function struct(name: string, def: Record<string, TypeSpecWithAlignment>): IKoffiCType;
|
|
52
|
+
export function struct(def: Record<string, TypeSpecWithAlignment>): IKoffiCType;
|
|
53
|
+
|
|
54
|
+
export function pack(name: string, def: Record<string, TypeSpecWithAlignment>): IKoffiCType;
|
|
55
|
+
export function pack(def: Record<string, TypeSpecWithAlignment>): IKoffiCType;
|
|
56
|
+
|
|
57
|
+
export function opaque(name: string): IKoffiCType;
|
|
58
|
+
export function opaque(): IKoffiCType;
|
|
59
|
+
|
|
60
|
+
export function pointer(value: TypeSpec): IKoffiCType;
|
|
61
|
+
export function pointer(value: TypeSpec, asteriskCount: number): IKoffiCType;
|
|
62
|
+
export function pointer(name: string, value: TypeSpec, asteriskCount: number): IKoffiCType;
|
|
63
|
+
|
|
64
|
+
export function out(value: TypeSpec): IKoffiCType;
|
|
65
|
+
export function inout(value: TypeSpec): IKoffiCType;
|
|
66
|
+
|
|
67
|
+
export function as(value: any, type: TypeSpec): IKoffiPointerCast;
|
|
68
|
+
|
|
69
|
+
export function disposable(type: TypeSpec): IKoffiCType;
|
|
70
|
+
export function disposable(name: string, type: TypeSpec): IKoffiCType;
|
|
71
|
+
export function disposable(name: string, type: TypeSpec, freeFunction: Function): IKoffiCType;
|
|
72
|
+
|
|
73
|
+
export function callback(definition: string): IKoffiCType;
|
|
74
|
+
export function callback(name: string, result: TypeSpec, arguments: TypeSpec[]): IKoffiCType;
|
|
75
|
+
|
|
76
|
+
export function register(callback: Function, type: TypeSpec): IKoffiRegisteredCallback;
|
|
77
|
+
export function register(thisValue: any, callback: Function, type: TypeSpec): IKoffiRegisteredCallback;
|
|
78
|
+
export function unregister(callback: IKoffiRegisteredCallback): void;
|
|
79
|
+
|
|
80
|
+
export function decode(value: any, type: TypeSpec): any;
|
|
81
|
+
export function decode(value: any, type: TypeSpec, len: number): any;
|
|
82
|
+
export function decode(value: any, offset: number, type: TypeSpec): any;
|
|
83
|
+
export function decode(value: any, offset: number, type: TypeSpec, len: number): any;
|
|
84
|
+
|
|
85
|
+
export function sizeof(type: TypeSpec): number;
|
|
86
|
+
export function alignof(type: TypeSpec): number;
|
|
87
|
+
export function offsetof(type: TypeSpec): number;
|
|
88
|
+
export function resolve(type: TypeSpec): IKoffiCType;
|
|
89
|
+
export function introspect(type: TypeSpec): TypeInfo;
|
|
90
|
+
|
|
91
|
+
export function alias(name: string, type: TypeSpec): IKoffiCType;
|
|
92
|
+
|
|
93
|
+
export function config(): Record<string, unknown>;
|
|
94
|
+
export function config(cfg: Record<string, unknown>): Record<string, unknown>;
|
|
95
|
+
}
|
package/src/koffi/src/parser.cc
CHANGED
|
@@ -152,6 +152,7 @@ const TypeInfo *PrototypeParser::ParseType()
|
|
|
152
152
|
offset++;
|
|
153
153
|
}
|
|
154
154
|
offset += (offset < tokens.len && tokens[offset] == "!");
|
|
155
|
+
offset = std::min(tokens.len - 1, offset);
|
|
155
156
|
|
|
156
157
|
while (offset >= start) {
|
|
157
158
|
Span<const char> str = MakeSpan(tokens[start].ptr, tokens[offset].end() - tokens[start].ptr);
|
|
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
|
|
Binary file
|