koffi 2.2.2-beta.6 → 2.2.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.
Files changed (35) hide show
  1. package/ChangeLog.md +14 -0
  2. package/doc/callbacks.md +9 -2
  3. package/package.json +2 -2
  4. package/src/koffi/build/2.2.2/koffi_darwin_arm64.tar.gz +0 -0
  5. package/src/koffi/build/2.2.2/koffi_darwin_x64.tar.gz +0 -0
  6. package/src/koffi/build/2.2.2/koffi_freebsd_arm64.tar.gz +0 -0
  7. package/src/koffi/build/2.2.2/koffi_freebsd_ia32.tar.gz +0 -0
  8. package/src/koffi/build/2.2.2/koffi_freebsd_x64.tar.gz +0 -0
  9. package/src/koffi/build/2.2.2/koffi_linux_arm32hf.tar.gz +0 -0
  10. package/src/koffi/build/2.2.2/koffi_linux_arm64.tar.gz +0 -0
  11. package/src/koffi/build/2.2.2/koffi_linux_ia32.tar.gz +0 -0
  12. package/src/koffi/build/2.2.2/koffi_linux_riscv64hf64.tar.gz +0 -0
  13. package/src/koffi/build/2.2.2/koffi_linux_x64.tar.gz +0 -0
  14. package/src/koffi/build/2.2.2/koffi_openbsd_ia32.tar.gz +0 -0
  15. package/src/koffi/build/2.2.2/koffi_openbsd_x64.tar.gz +0 -0
  16. package/src/koffi/build/2.2.2/koffi_win32_arm64.tar.gz +0 -0
  17. package/src/koffi/build/2.2.2/koffi_win32_ia32.tar.gz +0 -0
  18. package/src/koffi/build/2.2.2/koffi_win32_x64.tar.gz +0 -0
  19. package/src/koffi/src/call.cc +3 -0
  20. package/src/koffi/src/ffi.cc +4 -4
  21. package/src/koffi/build/2.2.2-beta.6/koffi_darwin_arm64.tar.gz +0 -0
  22. package/src/koffi/build/2.2.2-beta.6/koffi_darwin_x64.tar.gz +0 -0
  23. package/src/koffi/build/2.2.2-beta.6/koffi_freebsd_arm64.tar.gz +0 -0
  24. package/src/koffi/build/2.2.2-beta.6/koffi_freebsd_ia32.tar.gz +0 -0
  25. package/src/koffi/build/2.2.2-beta.6/koffi_freebsd_x64.tar.gz +0 -0
  26. package/src/koffi/build/2.2.2-beta.6/koffi_linux_arm32hf.tar.gz +0 -0
  27. package/src/koffi/build/2.2.2-beta.6/koffi_linux_arm64.tar.gz +0 -0
  28. package/src/koffi/build/2.2.2-beta.6/koffi_linux_ia32.tar.gz +0 -0
  29. package/src/koffi/build/2.2.2-beta.6/koffi_linux_riscv64hf64.tar.gz +0 -0
  30. package/src/koffi/build/2.2.2-beta.6/koffi_linux_x64.tar.gz +0 -0
  31. package/src/koffi/build/2.2.2-beta.6/koffi_openbsd_ia32.tar.gz +0 -0
  32. package/src/koffi/build/2.2.2-beta.6/koffi_openbsd_x64.tar.gz +0 -0
  33. package/src/koffi/build/2.2.2-beta.6/koffi_win32_arm64.tar.gz +0 -0
  34. package/src/koffi/build/2.2.2-beta.6/koffi_win32_ia32.tar.gz +0 -0
  35. package/src/koffi/build/2.2.2-beta.6/koffi_win32_x64.tar.gz +0 -0
package/ChangeLog.md CHANGED
@@ -2,6 +2,20 @@
2
2
 
3
3
  ## History
4
4
 
5
+ ### Koffi 2.2.2
6
+
7
+ **Main fixes:**
8
+
9
+ - Support transparent [asynchronous callbacks](callbacks.md#asynchronous-callbacks)
10
+ - Expand from a maximum of 16+16 to 1024 callbacks running in parallel
11
+
12
+ **Other fixes:**
13
+
14
+ - Fix bundler support by removing shebang from index.js
15
+ - Fix bugs when loading Koffi multiples times in same process (context aware module)
16
+ - Check N-API version when module is loaded
17
+ - Optimize callback unregistration
18
+
5
19
  ### Koffi 2.2.1
6
20
 
7
21
  **Main fixes:**
package/doc/callbacks.md CHANGED
@@ -172,9 +172,16 @@ console.log(array); // Prints ['123', 'bar', 'foo', 'foobar']
172
172
 
173
173
  *New in Koffi 2.2.2*
174
174
 
175
- In Koffi, [asynchronous native calls](functions.md#asynchronous-calls) happen on a secondary thread. However, JS execution is inherently single-threaded, callbacks must run on the main thread.
175
+ JS execution is inherently single-threaded, so JS callbacks must run on the main thread. There are two ways you may want to call a callback function from another thread:
176
176
 
177
- Koffi deals with this by running the JS callback function in the Node.js event loop. This means the callback cannot run while the engine is busy running synchronous code.
177
+ - Call the callback from an asynchronous FFI call (e.g. `waitpid.async`)
178
+ - Inside a synchronous FFI call, pass the callback to another thread
179
+
180
+ In both cases, Koffi will queue the call back to JS to run on the main thread, as soon as the JS event loop has a chance to run (for example when you await a promise).
181
+
182
+ ```{warning}
183
+ Be careful, you can easily get into a deadlock situation if you call a callback from a secondary thread and your main thread never lets the JS event loop run (for example, if the main thread waits for the secondary thread to finish something itself).
184
+ ```
178
185
 
179
186
  ## Handling of exceptions
180
187
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koffi",
3
- "version": "2.2.2-beta.6",
4
- "stable": "2.2.1",
3
+ "version": "2.2.2",
4
+ "stable": "2.2.2",
5
5
  "description": "Fast and simple C FFI (foreign function interface) for Node.js",
6
6
  "keywords": [
7
7
  "foreign",
@@ -81,6 +81,9 @@ CallData::~CallData()
81
81
  void CallData::RelaySafe(Size idx, uint8_t *own_sp, uint8_t *caller_sp, BackRegisters *out_reg)
82
82
  {
83
83
  if (std::this_thread::get_id() != instance->main_thread_id) {
84
+ // JS/V8 is single-threaded, and runs on main_thread_id. Forward the call
85
+ // to the JS event loop.
86
+
84
87
  RelayContext ctx;
85
88
 
86
89
  ctx.call = this;
@@ -1065,9 +1065,6 @@ static Napi::Value TranslateNormalCall(const Napi::CallbackInfo &info)
1065
1065
  InstanceMemory *mem = instance->memories[0];
1066
1066
  CallData call(env, instance, mem);
1067
1067
 
1068
- RG_DEFER_C(prev_call = exec_call) { exec_call = prev_call; };
1069
- exec_call = &call;
1070
-
1071
1068
  if (!RG_UNLIKELY(call.Prepare(func, info)))
1072
1069
  return env.Null();
1073
1070
 
@@ -1113,7 +1110,7 @@ static Napi::Value TranslateVariadicCall(const Napi::CallbackInfo &info)
1113
1110
  for (Size i = func.parameters.len; i < (Size)info.Length(); i += 2) {
1114
1111
  ParameterInfo param = {};
1115
1112
 
1116
- param.type = ResolveType(info[i], &param.directions);
1113
+ param.type = ResolveType(info[(uint32_t)i], &param.directions);
1117
1114
 
1118
1115
  if (RG_UNLIKELY(!param.type))
1119
1116
  return env.Null();
@@ -1893,6 +1890,9 @@ extern "C" void RelayCallback(Size idx, uint8_t *own_sp, uint8_t *caller_sp, Bac
1893
1890
  if (RG_LIKELY(exec_call)) {
1894
1891
  exec_call->RelaySafe(idx, own_sp, caller_sp, out_reg);
1895
1892
  } else {
1893
+ // This happens if the callback pointer is called from a different thread
1894
+ // than the one that runs the FFI call (sync or async).
1895
+
1896
1896
  TrampolineInfo *trampoline = &shared.trampolines[idx];
1897
1897
 
1898
1898
  Napi::Env env = trampoline->func.Env();