koffi 2.3.19 → 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.
Files changed (39) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/build/2.3.20/koffi_darwin_arm64/koffi.node +0 -0
  3. package/build/2.3.20/koffi_darwin_x64/koffi.node +0 -0
  4. package/build/2.3.20/koffi_freebsd_arm64/koffi.node +0 -0
  5. package/build/2.3.20/koffi_freebsd_ia32/koffi.node +0 -0
  6. package/build/2.3.20/koffi_freebsd_x64/koffi.node +0 -0
  7. package/build/2.3.20/koffi_linux_arm32hf/koffi.node +0 -0
  8. package/build/2.3.20/koffi_linux_arm64/koffi.node +0 -0
  9. package/build/2.3.20/koffi_linux_ia32/koffi.node +0 -0
  10. package/build/2.3.20/koffi_linux_riscv64hf64/koffi.node +0 -0
  11. package/build/{2.3.19 → 2.3.20}/koffi_linux_x64/koffi.node +0 -0
  12. package/build/2.3.20/koffi_openbsd_ia32/koffi.node +0 -0
  13. package/build/2.3.20/koffi_openbsd_x64/koffi.node +0 -0
  14. package/build/2.3.20/koffi_win32_arm64/koffi.node +0 -0
  15. package/build/{2.3.19 → 2.3.20}/koffi_win32_ia32/koffi.node +0 -0
  16. package/build/2.3.20/koffi_win32_x64/koffi.node +0 -0
  17. package/doc/functions.md +19 -7
  18. package/package.json +2 -2
  19. package/src/koffi/src/ffi.cc +15 -1
  20. package/src/koffi/src/ffi.hh +3 -1
  21. package/build/2.3.19/koffi_darwin_arm64/koffi.node +0 -0
  22. package/build/2.3.19/koffi_darwin_x64/koffi.node +0 -0
  23. package/build/2.3.19/koffi_freebsd_arm64/koffi.node +0 -0
  24. package/build/2.3.19/koffi_freebsd_ia32/koffi.node +0 -0
  25. package/build/2.3.19/koffi_freebsd_x64/koffi.node +0 -0
  26. package/build/2.3.19/koffi_linux_arm32hf/koffi.node +0 -0
  27. package/build/2.3.19/koffi_linux_arm64/koffi.node +0 -0
  28. package/build/2.3.19/koffi_linux_ia32/koffi.node +0 -0
  29. package/build/2.3.19/koffi_linux_riscv64hf64/koffi.node +0 -0
  30. package/build/2.3.19/koffi_openbsd_ia32/koffi.node +0 -0
  31. package/build/2.3.19/koffi_openbsd_x64/koffi.node +0 -0
  32. package/build/2.3.19/koffi_win32_arm64/koffi.node +0 -0
  33. package/build/2.3.19/koffi_win32_x64/koffi.node +0 -0
  34. /package/build/{2.3.19 → 2.3.20}/koffi_win32_arm64/koffi.exp +0 -0
  35. /package/build/{2.3.19 → 2.3.20}/koffi_win32_arm64/koffi.lib +0 -0
  36. /package/build/{2.3.19 → 2.3.20}/koffi_win32_ia32/koffi.exp +0 -0
  37. /package/build/{2.3.19 → 2.3.20}/koffi_win32_ia32/koffi.lib +0 -0
  38. /package/build/{2.3.19 → 2.3.20}/koffi_win32_x64/koffi.exp +0 -0
  39. /package/build/{2.3.19 → 2.3.20}/koffi_win32_x64/koffi.lib +0 -0
package/CHANGELOG.md CHANGED
@@ -4,6 +4,12 @@
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
+
7
13
  #### Koffi 2.3.19
8
14
 
9
15
  **Main fixes:**
package/doc/functions.md CHANGED
@@ -1,20 +1,32 @@
1
- # Function definitions
1
+ # Library functions
2
2
 
3
- ## Definition syntax
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
- You can use the returned object to load C functions from the library. To do so, you can use two syntaxes:
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
- ### Classic syntax
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
- ### C-like prototypes
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
- ## Variadic functions
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
- ## Calling conventions
66
+ ### Calling conventions
55
67
 
56
68
  By default, calling a C function happens synchronously.
57
69
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koffi",
3
- "version": "2.3.19",
4
- "stable": "2.3.19",
3
+ "version": "2.3.20",
4
+ "stable": "2.3.20",
5
5
  "description": "Fast and simple C FFI (foreign function interface) for Node.js",
6
6
  "keywords": [
7
7
  "foreign",
@@ -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::~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
@@ -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;