koffi 2.5.18 → 2.5.19

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 CHANGED
@@ -4,6 +4,11 @@
4
4
 
5
5
  ### Koffi 2.5
6
6
 
7
+ #### Koffi 2.5.19 (2023-08-29)
8
+
9
+ - Create thread-safe function broker lazily
10
+ - Add [koffi.reset()](misc.md#reset-internal-state) for type names and async broker
11
+
7
12
  #### Koffi 2.5.18 (2023-08-27)
8
13
 
9
14
  - Fix compatibility with Electron
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/misc.md CHANGED
@@ -125,3 +125,18 @@ assert.equal(koffi.errno(), koffi.os.errno.EBADF);
125
125
 
126
126
  console.log('close() with invalid FD is POSIX compliant!');
127
127
  ```
128
+
129
+ ## Reset internal state
130
+
131
+ *New in Koffi 2.5.19*
132
+
133
+ You can use `koffi.reset()` to clear some Koffi internal state such as:
134
+
135
+ - Parser type names
136
+ - Asynchronous function broker (useful to avoid false positive with `jest --detectOpenHandles`)
137
+
138
+ This function is mainly intended for test code, when you execute the same code over and over and you need to reuse type names.
139
+
140
+ ```{warning}
141
+ Trying to use a function or a type defined before the reset is undefined behavior and will likely lead to a crash!
142
+ ```
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koffi",
3
- "version": "2.5.18",
4
- "stable": "2.5.18",
3
+ "version": "2.5.19",
4
+ "stable": "2.5.19",
5
5
  "description": "Fast and simple C FFI (foreign function interface) for Node.js",
6
6
  "keywords": [
7
7
  "foreign",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cnoke",
3
- "version": "4.0.1",
3
+ "version": "4.0.2",
4
4
  "description": "Build native Node addons based on CMake, without extra dependency",
5
5
  "keywords": [
6
6
  "native",
package/src/index.d.ts CHANGED
@@ -145,6 +145,8 @@ declare module 'koffi' {
145
145
  export function errno(): number;
146
146
  export function errno(value: number): number;
147
147
 
148
+ export function reset(): void;
149
+
148
150
  export const internal: Boolean;
149
151
  export const extension: String;
150
152
 
package/src/index.js CHANGED
@@ -378,8 +378,8 @@ var require_package = __commonJS({
378
378
  "build/dist/src/koffi/package.json"(exports2, module2) {
379
379
  module2.exports = {
380
380
  name: "koffi",
381
- version: "2.5.18",
382
- stable: "2.5.18",
381
+ version: "2.5.19",
382
+ stable: "2.5.19",
383
383
  description: "Fast and simple C FFI (foreign function interface) for Node.js",
384
384
  keywords: [
385
385
  "foreign",
@@ -47,6 +47,10 @@ endif()
47
47
 
48
48
  # ---- Koffi ----
49
49
 
50
+ # Recompute the version string after each commit
51
+ if(EXISTS "${CMAKE_SOURCE_DIR}/../../.git/logs/HEAD")
52
+ configure_file("${CMAKE_SOURCE_DIR}/../../.git/logs/HEAD" git_logs_HEAD COPYONLY)
53
+ endif()
50
54
  if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/package.json)
51
55
  file(READ ${CMAKE_CURRENT_SOURCE_DIR}/package.json PKG)
52
56
  else()
@@ -1236,6 +1236,17 @@ void CallData::PopOutArguments()
1236
1236
 
1237
1237
  void *CallData::ReserveTrampoline(const FunctionInfo *proto, Napi::Function func)
1238
1238
  {
1239
+ if (!instance->broker) {
1240
+ if (napi_create_threadsafe_function(env, nullptr, nullptr,
1241
+ Napi::String::New(env, "Koffi Async Callback Broker"),
1242
+ 0, 1, nullptr, nullptr, nullptr,
1243
+ CallData::RelayAsync, &instance->broker) != napi_ok) {
1244
+ LogError("Failed to create async callback broker");
1245
+ return nullptr;
1246
+ }
1247
+ napi_unref_threadsafe_function(env, instance->broker);
1248
+ }
1249
+
1239
1250
  int16_t idx;
1240
1251
  {
1241
1252
  std::lock_guard<std::mutex> lock(shared.mutex);
@@ -2068,6 +2068,25 @@ static Napi::Object InitBaseTypes(Napi::Env env)
2068
2068
  return types;
2069
2069
  }
2070
2070
 
2071
+ static Napi::Value ResetKoffi(const Napi::CallbackInfo &info)
2072
+ {
2073
+ Napi::Env env = info.Env();
2074
+ InstanceData *instance = env.GetInstanceData<InstanceData>();
2075
+
2076
+ if (instance->broker) {
2077
+ napi_release_threadsafe_function(instance->broker, napi_tsfn_abort);
2078
+ instance->broker = nullptr;
2079
+ }
2080
+
2081
+ instance->types.Clear();
2082
+ instance->types_map.Clear();
2083
+ instance->callbacks.Clear();
2084
+
2085
+ InitBaseTypes(env);
2086
+
2087
+ return env.Undefined();
2088
+ }
2089
+
2071
2090
  static InstanceData *CreateInstance(Napi::Env env)
2072
2091
  {
2073
2092
  InstanceData *instance = new InstanceData();
@@ -2078,15 +2097,6 @@ static InstanceData *CreateInstance(Napi::Env env)
2078
2097
  instance->debug = GetDebugFlag("DUMP_CALLS");
2079
2098
  FillRandomSafe(&instance->tag_lower, RG_SIZE(instance->tag_lower));
2080
2099
 
2081
- if (napi_create_threadsafe_function(env, nullptr, nullptr,
2082
- Napi::String::New(env, "Koffi Async Callback Broker"),
2083
- 0, 1, nullptr, nullptr, nullptr,
2084
- CallData::RelayAsync, &instance->broker) != napi_ok) {
2085
- LogError("Failed to create async callback broker");
2086
- return nullptr;
2087
- }
2088
- napi_unref_threadsafe_function(env, instance->broker);
2089
-
2090
2100
  #ifdef _WIN32
2091
2101
  TEB *teb = GetTEB();
2092
2102
 
@@ -2141,6 +2151,8 @@ static Napi::Object InitModule(Napi::Env env, Napi::Object exports)
2141
2151
  exports.Set("address", Napi::Function::New(env, GetPointerAddress));
2142
2152
  exports.Set("call", Napi::Function::New(env, CallPointerSync));
2143
2153
 
2154
+ exports.Set("reset", Napi::Function::New(env, ResetKoffi));
2155
+
2144
2156
  exports.Set("errno", Napi::Function::New(env, GetOrSetErrNo));
2145
2157
 
2146
2158
  Napi::Object os = Napi::Object::New(env);