koffi 2.3.18 → 2.3.20
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 +12 -0
- package/build/2.3.20/koffi_darwin_arm64/koffi.node +0 -0
- package/build/2.3.20/koffi_darwin_x64/koffi.node +0 -0
- package/build/2.3.20/koffi_freebsd_arm64/koffi.node +0 -0
- package/build/2.3.20/koffi_freebsd_ia32/koffi.node +0 -0
- package/build/2.3.20/koffi_freebsd_x64/koffi.node +0 -0
- package/build/2.3.20/koffi_linux_arm32hf/koffi.node +0 -0
- package/build/2.3.20/koffi_linux_arm64/koffi.node +0 -0
- package/build/2.3.20/koffi_linux_ia32/koffi.node +0 -0
- package/build/2.3.20/koffi_linux_riscv64hf64/koffi.node +0 -0
- package/build/2.3.20/koffi_linux_x64/koffi.node +0 -0
- package/build/2.3.20/koffi_openbsd_ia32/koffi.node +0 -0
- package/build/2.3.20/koffi_openbsd_x64/koffi.node +0 -0
- package/build/2.3.20/koffi_win32_arm64/koffi.node +0 -0
- package/build/{2.3.18 → 2.3.20}/koffi_win32_ia32/koffi.node +0 -0
- package/build/2.3.20/koffi_win32_x64/koffi.node +0 -0
- package/doc/functions.md +19 -7
- package/package.json +2 -2
- package/src/koffi/src/call.cc +13 -8
- package/src/koffi/src/ffi.cc +15 -1
- package/src/koffi/src/ffi.hh +3 -1
- package/build/2.3.18/koffi_darwin_arm64/koffi.node +0 -0
- package/build/2.3.18/koffi_darwin_x64/koffi.node +0 -0
- package/build/2.3.18/koffi_freebsd_arm64/koffi.node +0 -0
- package/build/2.3.18/koffi_freebsd_ia32/koffi.node +0 -0
- package/build/2.3.18/koffi_freebsd_x64/koffi.node +0 -0
- package/build/2.3.18/koffi_linux_arm32hf/koffi.node +0 -0
- package/build/2.3.18/koffi_linux_arm64/koffi.node +0 -0
- package/build/2.3.18/koffi_linux_ia32/koffi.node +0 -0
- package/build/2.3.18/koffi_linux_riscv64hf64/koffi.node +0 -0
- package/build/2.3.18/koffi_linux_x64/koffi.node +0 -0
- package/build/2.3.18/koffi_openbsd_ia32/koffi.node +0 -0
- package/build/2.3.18/koffi_openbsd_x64/koffi.node +0 -0
- package/build/2.3.18/koffi_win32_arm64/koffi.node +0 -0
- package/build/2.3.18/koffi_win32_x64/koffi.node +0 -0
- /package/build/{2.3.18 → 2.3.20}/koffi_win32_arm64/koffi.exp +0 -0
- /package/build/{2.3.18 → 2.3.20}/koffi_win32_arm64/koffi.lib +0 -0
- /package/build/{2.3.18 → 2.3.20}/koffi_win32_ia32/koffi.exp +0 -0
- /package/build/{2.3.18 → 2.3.20}/koffi_win32_ia32/koffi.lib +0 -0
- /package/build/{2.3.18 → 2.3.20}/koffi_win32_x64/koffi.exp +0 -0
- /package/build/{2.3.18 → 2.3.20}/koffi_win32_x64/koffi.lib +0 -0
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,18 @@
|
|
|
4
4
|
|
|
5
5
|
### Koffi 2.3
|
|
6
6
|
|
|
7
|
+
#### Koffi 2.3.20
|
|
8
|
+
|
|
9
|
+
**Main fixes:**
|
|
10
|
+
|
|
11
|
+
- Support explicit library unloading with `lib.unload()`
|
|
12
|
+
|
|
13
|
+
#### Koffi 2.3.19
|
|
14
|
+
|
|
15
|
+
**Main fixes:**
|
|
16
|
+
|
|
17
|
+
- Actually allow non-ambiguous [string] values for `void *` arguments
|
|
18
|
+
|
|
7
19
|
#### Koffi 2.3.18
|
|
8
20
|
|
|
9
21
|
**Main fixes:**
|
|
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/doc/functions.md
CHANGED
|
@@ -1,20 +1,32 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Library functions
|
|
2
2
|
|
|
3
|
-
##
|
|
3
|
+
## Loading libraries
|
|
4
4
|
|
|
5
5
|
To declare functions, start by loading the shared library with `koffi.load(filename)`.
|
|
6
6
|
|
|
7
7
|
```js
|
|
8
8
|
const koffi = require('koffi');
|
|
9
9
|
const lib = koffi.load('/path/to/shared/library'); // File extension depends on platforms: .so, .dll, .dylib, etc.
|
|
10
|
+
````
|
|
11
|
+
|
|
12
|
+
This library will be automatically unloaded once all references to it (including all the functions that use it, as described below).
|
|
13
|
+
|
|
14
|
+
Starting with *Koffi 2.3.20*, you can explicitly unload a library by calling `lib.unload()`. Any attempt to find or call a function from this library after unloading it will crash.
|
|
15
|
+
|
|
16
|
+
```{note}
|
|
17
|
+
On some platforms (such as with the [musl C library on Linux](https://wiki.musl-libc.org/functional-differences-from-glibc.html#Unloading-libraries)), shared libraries cannot be unloaded, so the library will remain loaded and memory mapped after the call to `lib.unload()`.
|
|
10
18
|
```
|
|
11
19
|
|
|
12
|
-
|
|
20
|
+
## Function definitions
|
|
21
|
+
|
|
22
|
+
### Definition syntax
|
|
23
|
+
|
|
24
|
+
Use the object returned by `koffi.load()` to load C functions from the library. To do so, you can use two syntaxes:
|
|
13
25
|
|
|
14
26
|
- The classic syntax, inspired by node-ffi
|
|
15
27
|
- C-like prototypes
|
|
16
28
|
|
|
17
|
-
|
|
29
|
+
#### Classic syntax
|
|
18
30
|
|
|
19
31
|
To declare a function, you need to specify its non-mangled name, its return type, and its parameters. Use an ellipsis as the last parameter for variadic functions.
|
|
20
32
|
|
|
@@ -25,7 +37,7 @@ const atoi = lib.func('atoi', 'int', ['str']);
|
|
|
25
37
|
|
|
26
38
|
Koffi automatically tries mangled names for non-standard x86 calling conventions. See the section on [calling conventions](#calling-conventions) for more information on this subject.
|
|
27
39
|
|
|
28
|
-
|
|
40
|
+
#### C-like prototypes
|
|
29
41
|
|
|
30
42
|
If you prefer, you can declare functions using simple C-like prototype strings, as shown below:
|
|
31
43
|
|
|
@@ -36,7 +48,7 @@ const atoi = lib.func('int atoi(str)'); // The parameter name is not used by Kof
|
|
|
36
48
|
|
|
37
49
|
You can use `()` or `(void)` for functions that take no argument.
|
|
38
50
|
|
|
39
|
-
|
|
51
|
+
### Variadic functions
|
|
40
52
|
|
|
41
53
|
Variadic functions are declared with an ellipsis as the last argument.
|
|
42
54
|
|
|
@@ -51,7 +63,7 @@ printf('Integer %d, double %g, str %s', 'int', 6, 'double', 8.5, 'str', 'THE END
|
|
|
51
63
|
|
|
52
64
|
On x86 platforms, only the Cdecl convention can be used for variadic functions.
|
|
53
65
|
|
|
54
|
-
|
|
66
|
+
### Calling conventions
|
|
55
67
|
|
|
56
68
|
By default, calling a C function happens synchronously.
|
|
57
69
|
|
package/package.json
CHANGED
package/src/koffi/src/call.cc
CHANGED
|
@@ -981,19 +981,24 @@ bool CallData::PushPointer(Napi::Value value, const TypeInfo *type, int directio
|
|
|
981
981
|
Size out_max_len = -1;
|
|
982
982
|
|
|
983
983
|
if (value.IsArray()) {
|
|
984
|
-
if (RG_UNLIKELY(!type->ref.type->size)) {
|
|
985
|
-
ThrowError<Napi::TypeError>(env, "Cannot pass %1 value to void *, use koffi.as()",
|
|
986
|
-
type->ref.type != instance->void_type ? "opaque" : "ambiguous");
|
|
987
|
-
return false;
|
|
988
|
-
}
|
|
989
|
-
|
|
990
984
|
Napi::Array array = value.As<Napi::Array>();
|
|
991
985
|
Size len = PushIndirectString(array, type->ref.type, &ptr);
|
|
992
986
|
|
|
993
987
|
if (len >= 0) {
|
|
988
|
+
if (RG_UNLIKELY(!type->ref.type->size && type->ref.type != instance->void_type)) {
|
|
989
|
+
ThrowError<Napi::TypeError>(env, "Cannot pass [string] value to %1", type->name);
|
|
990
|
+
return false;
|
|
991
|
+
}
|
|
992
|
+
|
|
994
993
|
out_kind = (type->ref.type->size == 2) ? OutArgument::Kind::String16 : OutArgument::Kind::String;
|
|
995
994
|
out_max_len = len;
|
|
996
995
|
} else {
|
|
996
|
+
if (RG_UNLIKELY(!type->ref.type->size)) {
|
|
997
|
+
ThrowError<Napi::TypeError>(env, "Cannot pass %1 value to %2, use koffi.as()",
|
|
998
|
+
type->ref.type != instance->void_type ? "opaque" : "ambiguous", type->name);
|
|
999
|
+
return false;
|
|
1000
|
+
}
|
|
1001
|
+
|
|
997
1002
|
Size len = (Size)array.Length();
|
|
998
1003
|
Size size = len * type->ref.type->size;
|
|
999
1004
|
|
|
@@ -1026,8 +1031,8 @@ bool CallData::PushPointer(Napi::Value value, const TypeInfo *type, int directio
|
|
|
1026
1031
|
} else if (RG_LIKELY(type->ref.type->primitive == PrimitiveKind::Record ||
|
|
1027
1032
|
type->ref.type->primitive == PrimitiveKind::Union)) {
|
|
1028
1033
|
if (RG_UNLIKELY(!type->ref.type->size)) {
|
|
1029
|
-
ThrowError<Napi::TypeError>(env, "Cannot pass %1 value to
|
|
1030
|
-
type->ref.type != instance->void_type ? "opaque" : "ambiguous");
|
|
1034
|
+
ThrowError<Napi::TypeError>(env, "Cannot pass %1 value to %2, use koffi.as()",
|
|
1035
|
+
type->ref.type != instance->void_type ? "opaque" : "ambiguous", type->name);
|
|
1031
1036
|
return false;
|
|
1032
1037
|
}
|
|
1033
1038
|
|
package/src/koffi/src/ffi.cc
CHANGED
|
@@ -1532,6 +1532,16 @@ static Napi::Value FindLibraryFunction(const Napi::CallbackInfo &info, CallConve
|
|
|
1532
1532
|
return wrapper;
|
|
1533
1533
|
}
|
|
1534
1534
|
|
|
1535
|
+
static Napi::Value UnloadLibrary(const Napi::CallbackInfo &info)
|
|
1536
|
+
{
|
|
1537
|
+
Napi::Env env = info.Env();
|
|
1538
|
+
LibraryHolder *lib = (LibraryHolder *)info.Data();
|
|
1539
|
+
|
|
1540
|
+
lib->Unload();
|
|
1541
|
+
|
|
1542
|
+
return env.Undefined();
|
|
1543
|
+
}
|
|
1544
|
+
|
|
1535
1545
|
static Napi::Value LoadSharedLibrary(const Napi::CallbackInfo &info)
|
|
1536
1546
|
{
|
|
1537
1547
|
Napi::Env env = info.Env();
|
|
@@ -1610,6 +1620,8 @@ static Napi::Value LoadSharedLibrary(const Napi::CallbackInfo &info)
|
|
|
1610
1620
|
|
|
1611
1621
|
#undef ADD_CONVENTION
|
|
1612
1622
|
|
|
1623
|
+
obj.Set("unload", Napi::Function::New(env, UnloadLibrary, "unload", (void *)lib->Ref()));
|
|
1624
|
+
|
|
1613
1625
|
return obj;
|
|
1614
1626
|
}
|
|
1615
1627
|
|
|
@@ -1721,7 +1733,7 @@ static Napi::Value UnregisterCallback(const Napi::CallbackInfo &info)
|
|
|
1721
1733
|
return env.Undefined();
|
|
1722
1734
|
}
|
|
1723
1735
|
|
|
1724
|
-
LibraryHolder
|
|
1736
|
+
void LibraryHolder::Unload()
|
|
1725
1737
|
{
|
|
1726
1738
|
#ifdef _WIN32
|
|
1727
1739
|
if (module && module != GetModuleHandle(nullptr)) {
|
|
@@ -1732,6 +1744,8 @@ LibraryHolder::~LibraryHolder()
|
|
|
1732
1744
|
dlclose(module);
|
|
1733
1745
|
}
|
|
1734
1746
|
#endif
|
|
1747
|
+
|
|
1748
|
+
module = nullptr;
|
|
1735
1749
|
}
|
|
1736
1750
|
|
|
1737
1751
|
const LibraryHolder *LibraryHolder::Ref() const
|
package/src/koffi/src/ffi.hh
CHANGED
|
@@ -155,7 +155,9 @@ struct LibraryHolder {
|
|
|
155
155
|
mutable std::atomic_int refcount { 1 };
|
|
156
156
|
|
|
157
157
|
LibraryHolder(void *module) : module(module) {}
|
|
158
|
-
~LibraryHolder();
|
|
158
|
+
~LibraryHolder() { Unload(); }
|
|
159
|
+
|
|
160
|
+
void Unload();
|
|
159
161
|
|
|
160
162
|
const LibraryHolder *Ref() const;
|
|
161
163
|
void Unref() const;
|
|
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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|