koffi 2.5.8 → 2.5.9

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 (53) hide show
  1. package/CHANGELOG.md +11 -99
  2. package/build/{2.5.8 → koffi}/darwin_arm64/koffi.node +0 -0
  3. package/build/{2.5.8 → koffi}/darwin_x64/koffi.node +0 -0
  4. package/build/{2.5.8 → koffi}/freebsd_arm64/koffi.node +0 -0
  5. package/build/{2.5.8 → koffi}/freebsd_ia32/koffi.node +0 -0
  6. package/build/{2.5.8 → koffi}/freebsd_x64/koffi.node +0 -0
  7. package/build/koffi/linux_arm32hf/koffi.node +0 -0
  8. package/build/{2.5.8 → koffi}/linux_arm64/koffi.node +0 -0
  9. package/build/{2.5.8 → koffi}/linux_ia32/koffi.node +0 -0
  10. package/build/{2.5.8 → koffi}/linux_riscv64hf64/koffi.node +0 -0
  11. package/build/{2.5.8 → koffi}/linux_x64/koffi.node +0 -0
  12. package/build/{2.5.8 → koffi}/openbsd_ia32/koffi.node +0 -0
  13. package/build/{2.5.8 → koffi}/openbsd_x64/koffi.node +0 -0
  14. package/build/{2.5.8 → koffi}/win32_arm64/koffi.node +0 -0
  15. package/build/{2.5.8 → koffi}/win32_ia32/koffi.node +0 -0
  16. package/build/{2.5.8 → koffi}/win32_x64/koffi.node +0 -0
  17. package/doc/conf.py +2 -1
  18. package/doc/index.rst +1 -0
  19. package/doc/packaging.md +60 -0
  20. package/doc/start.md +1 -36
  21. package/package.json +2 -2
  22. package/src/cnoke/assets/FindCNoke.cmake +16 -0
  23. package/src/index.d.ts +4 -0
  24. package/src/index.js +50 -21
  25. package/src/koffi/CMakeLists.txt +6 -0
  26. package/src/koffi/examples/electron-builder/README.md +23 -0
  27. package/src/koffi/examples/electron-builder/package.json +21 -0
  28. package/src/koffi/examples/electron-builder/src/app.js +20 -0
  29. package/src/koffi/examples/electron-builder/src/index.html +143 -0
  30. package/src/koffi/examples/electron-builder/src/preload.js +5 -0
  31. package/src/koffi/examples/electron-forge/README.md +25 -0
  32. package/src/koffi/examples/electron-forge/forge.config.js +43 -0
  33. package/src/koffi/examples/electron-forge/package.json +37 -0
  34. package/src/koffi/examples/electron-forge/src/index.css +7 -0
  35. package/src/koffi/examples/electron-forge/src/index.html +143 -0
  36. package/src/koffi/examples/electron-forge/src/main.js +52 -0
  37. package/src/koffi/examples/electron-forge/src/preload.js +5 -0
  38. package/src/koffi/examples/electron-forge/src/renderer.js +31 -0
  39. package/src/koffi/examples/electron-forge/webpack.main.config.js +11 -0
  40. package/src/koffi/examples/electron-forge/webpack.renderer.config.js +13 -0
  41. package/src/koffi/examples/electron-forge/webpack.rules.js +35 -0
  42. package/src/koffi/examples/nwjs/README.md +20 -0
  43. package/src/koffi/examples/nwjs/package.json +11 -0
  44. package/src/koffi/examples/nwjs/src/index.html +145 -0
  45. package/src/koffi/examples/nwjs/src/package.json +9 -0
  46. package/src/koffi/src/ffi.cc +1 -0
  47. package/build/2.5.8/linux_arm32hf/koffi.node +0 -0
  48. /package/build/{2.5.8 → koffi}/win32_arm64/koffi.exp +0 -0
  49. /package/build/{2.5.8 → koffi}/win32_arm64/koffi.lib +0 -0
  50. /package/build/{2.5.8 → koffi}/win32_ia32/koffi.exp +0 -0
  51. /package/build/{2.5.8 → koffi}/win32_ia32/koffi.lib +0 -0
  52. /package/build/{2.5.8 → koffi}/win32_x64/koffi.exp +0 -0
  53. /package/build/{2.5.8 → koffi}/win32_x64/koffi.lib +0 -0
package/CHANGELOG.md CHANGED
@@ -4,61 +4,51 @@
4
4
 
5
5
  ### Koffi 2.5
6
6
 
7
- #### Koffi 2.5.8
7
+ #### Koffi 2.5.9
8
8
 
9
9
  **Main changes:**
10
10
 
11
+ - Use bundler-friendly static require calls in Koffi
12
+ - Add packaging examples to documentation
13
+
14
+ **Other changes:**
15
+
16
+ - Add missing unload() export to TS file
17
+ - Add export for koffi.types in TS file
18
+
19
+ #### Koffi 2.5.8
20
+
11
21
  - Add more search paths for native Koffi modules
12
22
  - Add section [about bundling](start.md#bundling-koffi) to documentation
13
23
 
14
24
  #### Koffi 2.5.7
15
25
 
16
- **Main changes:**
17
-
18
26
  - Point package to new repository
19
27
 
20
28
  #### Koffi 2.5.6
21
29
 
22
- **Main changes:**
23
-
24
30
  - Increase limit of output parameters from 16 to 32
25
31
 
26
32
  #### Koffi 2.5.5
27
33
 
28
- **Main changes:**
29
-
30
34
  - Support decoding non-char null-terminated arrays
31
35
 
32
36
  #### Koffi 2.5.4
33
37
 
34
- **Main changes:**
35
-
36
38
  - Fix `koffi.pointer()` not accepting disposable types
37
-
38
- **Other changes:**
39
-
40
39
  - Fix potential issues when making pointers to anonymous types
41
40
 
42
41
  #### Koffi 2.5.3
43
42
 
44
- **Main changes:**
45
-
46
43
  - Add missing union exports in TS definition file
47
-
48
- **Other changes:**
49
-
50
44
  - Fix some parameter names in TS definition file
51
45
 
52
46
  #### Koffi 2.5.2
53
47
 
54
- **Main changes:**
55
-
56
48
  - Default initialize unset object/struct members
57
49
 
58
50
  #### Koffi 2.5.1
59
51
 
60
- **Main changes:**
61
-
62
52
  - Fix crash with some struct types in System V 64 ABI
63
53
  - Always use direct passthrough for buffer arguments
64
54
 
@@ -77,12 +67,7 @@
77
67
 
78
68
  #### Koffi 2.4.2
79
69
 
80
- **Main changes:**
81
-
82
70
  - Support calling variadic function pointers
83
-
84
- **Other changes:**
85
-
86
71
  - Add documentation for function pointers
87
72
 
88
73
  #### Koffi 2.4.1
@@ -104,64 +89,43 @@
104
89
 
105
90
  #### Koffi 2.3.20
106
91
 
107
- **Main fixes:**
108
-
109
92
  - Support explicit library unloading with `lib.unload()`
110
93
 
111
94
  #### Koffi 2.3.19
112
95
 
113
- **Main fixes:**
114
-
115
96
  - Actually allow non-ambiguous [string] values for `void *` arguments
116
97
 
117
98
  #### Koffi 2.3.18
118
99
 
119
- **Main fixes:**
120
-
121
100
  - Fix possible crash on exit caused by unregistered callbacks
122
101
 
123
102
  #### Koffi 2.3.17
124
103
 
125
- **Main changes:**
126
-
127
104
  - Allow strings for input `void *`, `int8_t *` and `int16_t *` pointer arguments
128
105
  - Support using `[string]` (single-element string arrays) for polymorphic input/output arguments
129
106
 
130
107
  #### Koffi 2.3.16
131
108
 
132
- **Main changes:**
133
-
134
109
  - Fix Windows ARM64 build to work with official Node.js version
135
110
  - Compile Windows builds with Visual Studio 2022 17.5.3
136
-
137
- **Other changes:**
138
-
139
111
  - Support null in `koffi.free()` and `koffi.address()`
140
112
 
141
113
  #### Koffi 2.3.15
142
114
 
143
- **Main changes:**
144
-
145
115
  - Improve manual decoding of 0-terminated strings
146
116
  - Add checks around array conversion hints
147
117
 
148
118
  #### Koffi 2.3.14
149
119
 
150
- **Main changes:**
151
-
152
120
  - Add `koffi.errno()` function to get and set current errno value
153
121
  - Add `koffi.os.errno` object with valid errno codes
154
122
 
155
123
  #### Koffi 2.3.13
156
124
 
157
- **Main changes:**
158
-
159
125
  - Add `koffi.address()` to get the raw value of a wrapper pointer
160
126
 
161
127
  #### Koffi 2.3.12
162
128
 
163
- **Main fixes:**
164
-
165
129
  - Fix broken syntax in TS definition file
166
130
  - Add missing exported properties in TS file
167
131
 
@@ -179,29 +143,21 @@
179
143
 
180
144
  #### Koffi 2.3.9
181
145
 
182
- **Main changes:**
183
-
184
146
  - Relicense under MIT license
185
147
 
186
148
  #### Koffi 2.3.8
187
149
 
188
- **Main fixes:**
189
-
190
150
  - Disable non-ready union support
191
151
  - Simplify Windows stack allocation and drop NOACCESS and GUARD pages
192
152
  - Adjust Windows TEB SEH chain and GuaranteedStackBytes for Koffi calls
193
153
 
194
154
  #### Koffi 2.3.7
195
155
 
196
- **Main fixes:**
197
-
198
156
  - Fix missing require in index.js ([@gastonFrecceroNapse](https://github.com/gastonFrecceroNapse))
199
157
  - Reduce NPM package bloat (from 65 MB to 20 MB) caused by changes in 2.3.6
200
158
 
201
159
  #### Koffi 2.3.6
202
160
 
203
- **Main changes:**
204
-
205
161
  - Fix broken TS definition file
206
162
  - Keep all prebuilt binaries and load correct one at runtime
207
163
 
@@ -223,8 +179,6 @@
223
179
 
224
180
  #### Koffi 2.3.4
225
181
 
226
- **Main fixes:**
227
-
228
182
  - Fix error when installing Koffi on Windows (2.3.2)
229
183
 
230
184
  #### Koffi 2.3.2
@@ -242,8 +196,6 @@
242
196
 
243
197
  #### Koffi 2.3.1
244
198
 
245
- **Main changes:**
246
-
247
199
  - Error out when trying to use ambiguous `void *` arguments (input and/or output)
248
200
  - Adjust TypeScript definitions ([@insraq](https://github.com/insraq))
249
201
  - Fix possible crash when parsing invalid prototype
@@ -269,28 +221,16 @@
269
221
 
270
222
  #### Koffi 2.2.5
271
223
 
272
- **Main changes:**
273
-
274
224
  - Relicense Koffi under LGPL 3.0
275
225
 
276
226
  #### Koffi 2.2.4
277
227
 
278
- **Main fixes:**
279
-
280
228
  - Fix memory leak on Windows (in Koffi 2.2.3) when running many async calls
281
-
282
- **Other changes:**
283
-
284
229
  - Reorganize documentation pages
285
230
 
286
231
  #### Koffi 2.2.3
287
232
 
288
- **Main fixes:**
289
-
290
233
  - Support native code that uses [Structured Exception Handling (SEH)](https://learn.microsoft.com/en-us/cpp/cpp/structured-exception-handling-c-cpp) on Windows (x86, x64 and ARM64)
291
-
292
- **Other changes:**
293
-
294
234
  - Try to use ebp/rbp as frame pointer in x86/x64 ASM code
295
235
 
296
236
  #### Koffi 2.2.2
@@ -309,8 +249,6 @@
309
249
 
310
250
  #### Koffi 2.2.1
311
251
 
312
- **Main fixes:**
313
-
314
252
  - Fix crash when [calling callback again after FFI call inside previous callback](https://github.com/Koromix/rygel/issues/15)
315
253
 
316
254
  #### Koffi 2.2.0
@@ -331,8 +269,6 @@
331
269
 
332
270
  #### Koffi 2.1.5
333
271
 
334
- **Main fixes:**
335
-
336
272
  - Add missing README.md file to NPM package
337
273
 
338
274
  #### Koffi 2.1.4
@@ -348,20 +284,14 @@
348
284
 
349
285
  #### Koffi 2.1.3
350
286
 
351
- **Main changes:**
352
-
353
287
  - Support up to 16 output parameters (instead of 8)
354
288
 
355
289
  #### Koffi 2.1.2
356
290
 
357
- **Main changes:**
358
-
359
291
  - Support up to 8 output parameters (instead of 4)
360
292
 
361
293
  #### Koffi 2.1.1
362
294
 
363
- **Main fixes:**
364
-
365
295
  - Fix potential memory allocation bugs
366
296
 
367
297
  #### Koffi 2.1.0
@@ -386,8 +316,6 @@
386
316
 
387
317
  #### Koffi 2.0.1
388
318
 
389
- **Main changes:**
390
-
391
319
  - Return `undefined` (instead of null) for `void` functions
392
320
 
393
321
  #### Koffi 2.0.0
@@ -418,14 +346,10 @@ Consult the [migration guide](migration.md) for more information.
418
346
 
419
347
  #### Koffi 1.3.12
420
348
 
421
- **Main fixes:**
422
-
423
349
  - Fix support for Yarn package manager
424
350
 
425
351
  #### Koffi 1.3.11
426
352
 
427
- **Main fixes:**
428
-
429
353
  - Fix broken parsing of `void *` when used for first parameter
430
354
 
431
355
  #### Koffi 1.3.10
@@ -442,8 +366,6 @@ Consult the [migration guide](migration.md) for more information.
442
366
 
443
367
  #### Koffi 1.3.9
444
368
 
445
- **Main fixes:**
446
-
447
369
  - Fix prebuild compatibility with Electron on Windows x64
448
370
 
449
371
  #### Koffi 1.3.8
@@ -496,8 +418,6 @@ Consult the [migration guide](migration.md) for more information.
496
418
 
497
419
  #### Koffi 1.3.4
498
420
 
499
- **Main fixes:**
500
-
501
421
  - Fix possible OpenBSD i386 crash with `(void)` functions
502
422
 
503
423
  #### Koffi 1.3.3
@@ -514,15 +434,11 @@ Consult the [migration guide](migration.md) for more information.
514
434
 
515
435
  #### Koffi 1.3.2
516
436
 
517
- **Main fixes:**
518
-
519
437
  - Support compilation in C++14 mode (graceful degradation)
520
438
  - Support older toolchains on Linux (tested on Debian 9)
521
439
 
522
440
  #### Koffi 1.3.1
523
441
 
524
- **Main fixes:**
525
-
526
442
  - The prebuilt binary is tested when Koffi is installed, and a rebuild happens if it fails to load
527
443
 
528
444
  #### Koffi 1.3.0
@@ -545,14 +461,10 @@ Consult the [migration guide](migration.md) for more information.
545
461
 
546
462
  #### Koffi 1.2.4
547
463
 
548
- **New features:**
549
-
550
464
  - Windows ARM64 is now supported
551
465
 
552
466
  #### Koffi 1.2.3
553
467
 
554
- **New features:**
555
-
556
468
  - A prebuilt binary for macOS ARM64 (M1) is now included
557
469
 
558
470
  #### Koffi 1.2.1
package/doc/conf.py CHANGED
@@ -112,5 +112,6 @@ myst_number_code_blocks = ['c', 'js', 'sh', 'batch']
112
112
  # -- Redirections -------------------------------------------------
113
113
 
114
114
  rediraffe_redirects = {
115
- "changes.md": "changelog.md"
115
+ "changes.md": "changelog.md",
116
+ "bundling.md": "packaging.md"
116
117
  }
package/doc/index.rst CHANGED
@@ -30,6 +30,7 @@ Table of contents
30
30
  parameters
31
31
  callbacks
32
32
  misc
33
+ packaging
33
34
  benchmarks
34
35
  contribute
35
36
  changelog
@@ -0,0 +1,60 @@
1
+ # Packaging
2
+
3
+ ## Bundling and native modules
4
+
5
+ *Simplified in Koffi 2.5.9*
6
+
7
+ Koffi uses native modules to work. The NPM package contains binaries for various platforms and architectures, and the appropriate module is selected at runtime.
8
+
9
+ In theory, the bundlers should be able to find recorgnize all native modules because they are explictly listed in the Javascript file, as static strings.
10
+
11
+ If that is not the case, you can manually arrange to copy the `node_modules/koffi/build/koffi` directory next to your bundled script.
12
+
13
+ Here is an example that would work:
14
+
15
+ ```
16
+ koffi/
17
+ win32_x64/
18
+ koffi.node
19
+ linux_x64/
20
+ koffi.node
21
+ ...
22
+ MyBundle.js
23
+ ```
24
+
25
+ When running in Electron, Koffi will also try to find the native module in `process.resourcesPath`. For an Electron app you could do something like this
26
+
27
+ ```
28
+ locales/
29
+ resources/
30
+ koffi/
31
+ darwin_arm64/
32
+ koffi.node
33
+ darwin_x64/
34
+ koffi.node
35
+ MyApp.exe
36
+ ```
37
+
38
+ ## Packaging examples
39
+
40
+ ### Electron with electron-builder
41
+
42
+ Packaging with electron-builder should work as-is:
43
+
44
+ You can also go take a look at the full [working example in the repository](https://github.com/Koromix/rygel/tree/master/src/koffi/examples/electron-builder).
45
+
46
+ ### Electron Forge
47
+
48
+ Packaging with Electron Force should work as-is, even when using webpack as configured initially when you run:
49
+
50
+ ```sh
51
+ npm init electron-app@latest my-app -- --template=webpack
52
+ ```
53
+
54
+ You can also go take a look at the full [working example in the repository](https://github.com/Koromix/rygel/tree/master/src/koffi/examples/electron-forge).
55
+
56
+ ### NW.js
57
+
58
+ Packagers such as nw-builder should work as-is.
59
+
60
+ You can find a full [working example in the repository](https://github.com/Koromix/rygel/tree/master/src/koffi/examples/nwjs).
package/doc/start.md CHANGED
@@ -109,39 +109,4 @@ if (ret == IDYES)
109
109
 
110
110
  ## Bundling Koffi
111
111
 
112
- *Improved in Koffi 2.5.8*
113
-
114
- Koffi uses native modules to work. The NPM package contains binaries for various platforms and architectures, and the appropriate module is selected at runtime.
115
-
116
- If you use a bundler, you must make sure that the {{ "`node_modules/koffi/build/" ~ stable ~ "`" }} directory is available in your bundled script.
117
-
118
- Here is an example that would work:
119
-
120
- ```{eval-rst}
121
- .. parsed-literal::
122
- koffi/
123
- |version|/
124
- win32_x64/
125
- koffi.node
126
- linux_x64/
127
- koffi.node
128
- ...
129
- MyBundle.js
130
- ```
131
-
132
- When running in Electron, Koffi will also try to find the native module in `process.resourcesPath`. For an Electron app you could do something like this
133
-
134
- ```{eval-rst}
135
- .. parsed-literal::
136
- locales/
137
- resources/
138
- koffi/
139
- |version|/
140
- darwin_arm64/
141
- koffi.node
142
- darwin_x64/
143
- koffi.node
144
- MyApp.exe
145
- ```
146
-
147
- You must configure your bundler and/or your packager to make this work.
112
+ Please read the [dedicated page](packaging.md) for information about bundling and packaging applications using Koffi.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koffi",
3
- "version": "2.5.8",
4
- "stable": "2.5.8",
3
+ "version": "2.5.9",
4
+ "stable": "2.5.9",
5
5
  "description": "Fast and simple C FFI (foreign function interface) for Node.js",
6
6
  "keywords": [
7
7
  "foreign",
@@ -48,8 +48,12 @@ if(USE_UNITY_BUILDS)
48
48
  set(unity_file_cpp "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_unity.cpp")
49
49
  file(REMOVE ${unity_file_c} ${unity_file_cpp})
50
50
 
51
+ set(c_definitions "")
52
+ set(cpp_definitions "")
53
+
51
54
  foreach(src ${sources})
52
55
  get_source_file_property(language ${src} LANGUAGE)
56
+ get_property(definitions SOURCE ${src} PROPERTY COMPILE_DEFINITIONS)
53
57
  if(IS_ABSOLUTE ${src})
54
58
  set(src_full ${src})
55
59
  else()
@@ -58,17 +62,29 @@ if(USE_UNITY_BUILDS)
58
62
  if(language STREQUAL "C")
59
63
  set_source_files_properties(${src} PROPERTIES HEADER_FILE_ONLY 1)
60
64
  file(APPEND ${unity_file_c} "#include \"${src_full}\"\n")
65
+ if (definitions)
66
+ set(c_definitions "${c_definitions} ${definitions}")
67
+ endif()
61
68
  elseif(language STREQUAL "CXX")
62
69
  set_source_files_properties(${src} PROPERTIES HEADER_FILE_ONLY 1)
63
70
  file(APPEND ${unity_file_cpp} "#include \"${src_full}\"\n")
71
+ if (definitions)
72
+ set(cpp_definitions "${cpp_definitions} ${definitions}")
73
+ endif()
64
74
  endif()
65
75
  endforeach()
66
76
 
67
77
  if(EXISTS ${unity_file_c})
68
78
  target_sources(${TARGET} PRIVATE ${unity_file_c})
79
+ if(c_definitions)
80
+ set_source_files_properties(${unity_file_c} PROPERTIES COMPILE_DEFINITIONS ${c_definitions})
81
+ endif()
69
82
  endif()
70
83
  if(EXISTS ${unity_file_cpp})
71
84
  target_sources(${TARGET} PRIVATE ${unity_file_cpp})
85
+ if(cpp_definitions)
86
+ set_source_files_properties(${unity_file_cpp} PROPERTIES COMPILE_DEFINITIONS ${cpp_definitions})
87
+ endif()
72
88
  endif()
73
89
 
74
90
  target_compile_definitions(${TARGET} PRIVATE -DUNITY_BUILD=1)
package/src/index.d.ts CHANGED
@@ -79,6 +79,8 @@ declare module 'koffi' {
79
79
 
80
80
  thiscall(definition: string): KoffiFunction;
81
81
  thiscall(name: string, result: TypeSpec, arguments: TypeSpec[]): KoffiFunction;
82
+
83
+ unload(): void;
82
84
  }
83
85
 
84
86
  export function struct(name: string, def: Record<string, TypeSpecWithAlignment>): IKoffiCType;
@@ -149,4 +151,6 @@ declare module 'koffi' {
149
151
  export const os: {
150
152
  errno: Record<string, number>
151
153
  };
154
+
155
+ export const types: Record<string, IKoffiCType>;
152
156
  }
package/src/index.js CHANGED
@@ -38,32 +38,61 @@ if (process.versions.napi == null || process.versions.napi < pkg.cnoke.napi) {
38
38
  }
39
39
 
40
40
  let arch = cnoke.determine_arch();
41
+ let triplet = `${process.platform}_${arch}`;
41
42
 
42
- let filenames = [
43
- __dirname + `/../build/${pkg.version}/${process.platform}_${arch}/koffi.node`,
44
- __dirname + `/build/koffi/${pkg.version}/${process.platform}_${arch}/koffi.node`,
45
- __dirname + `/koffi/${pkg.version}/${process.platform}_${arch}/koffi.node`,
46
- __dirname + `/../build/${pkg.version}/koffi_${process.platform}_${arch}/koffi.node`,
47
- __dirname + `/build/koffi/${pkg.version}/koffi_${process.platform}_${arch}/koffi.node`,
48
- __dirname + `/koffi/${pkg.version}/koffi_${process.platform}_${arch}/koffi.node`
49
- ];
43
+ let native = null;
50
44
 
51
- if (process.resourcesPath != null) {
52
- filenames.push(
53
- process.resourcesPath + `/build/${pkg.version}/${process.platform}_${arch}/koffi.node`,
54
- process.resourcesPath + `/build/koffi/${pkg.version}/${process.platform}_${arch}/koffi.node`,
55
- process.resourcesPath + `/koffi/${pkg.version}/${process.platform}_${arch}/koffi.node`,
56
- process.resourcesPath + `/build/${pkg.version}/koffi_${process.platform}_${arch}/koffi.node`,
57
- process.resourcesPath + `/build/koffi/${pkg.version}/koffi_${process.platform}_${arch}/koffi.node`,
58
- process.resourcesPath + `/koffi/${pkg.version}/koffi_${process.platform}_${arch}/koffi.node`
59
- );
45
+ // Try an explicit list with static strings to help bundlers
46
+ try {
47
+ switch (triplet) {
48
+ case 'darwin_arm64': { native = require('../build/koffi/darwin_arm64/koffi.node'); } break;
49
+ case 'darwin_x64': { native = require('../build/koffi/darwin_x64/koffi.node'); } break;
50
+ case 'freebsd_arm46': { native = require('../build/koffi/freebsd_arm46/koffi.node'); } break;
51
+ case 'freebsd_ia32': { native = require('../build/koffi/freebsd_ia32/koffi.node'); } break;
52
+ case 'freebsd_x64': { native = require('../build/koffi/freebsd_x64/koffi.node'); } break;
53
+ case 'linux_arm32hf': { native = require('../build/koffi/linux_arm32hf/koffi.node'); } break;
54
+ case 'linux_arm64': { native = require('../build/koffi/linux_arm64/koffi.node'); } break;
55
+ case 'linux_ia32': { native = require('../build/koffi/linux_ia32/koffi.node'); } break;
56
+ case 'linux_risc64hf64': { native = require('../build/koffi/linux_risc64hf64/koffi.node'); } break;
57
+ case 'linux_x64': { native = require('../build/koffi/linux_x64/koffi.node'); } break;
58
+ case 'openbsd_ia32': { native = require('../build/koffi/openbsd_ia32/koffi.node'); } break;
59
+ case 'openbsd_x64': { native = require('../build/koffi/openbsd_x64/koffi.node'); } break;
60
+ case 'win32_arm64': { native = require('../build/koffi/win32_arm64/koffi.node'); } break;
61
+ case 'win32_ia32': { native = require('../build/koffi/win32_ia32/koffi.node'); } break;
62
+ case 'win32_x64': { native = require('../build/koffi/win32_x64/koffi.node'); } break;
63
+ }
64
+ } catch (err) {
65
+ // Go on!
60
66
  }
61
67
 
62
- let filename = filenames.find(filename => fs.existsSync(filename));
63
- if (filename == null)
64
- throw new Error('Cannot find the native Koffi module; did you bundle it correctly?');
68
+ // And now, search everywhere we know
69
+ if (native == null) {
70
+ let names = [
71
+ `/koffi/${process.platform}_${arch}/koffi.node`,
72
+ `/node_modules/koffi/build/koffi/${process.platform}_${arch}/koffi.node`
73
+ ];
74
+
75
+ for (let name of names) {
76
+ if (fs.existsSync(__dirname + name)) {
77
+ native = require(__dirname + name);
78
+ break;
79
+ }
80
+ }
65
81
 
66
- let native = require(filename);
82
+ if (native == null && process.resourcesPath != null) {
83
+ for (let name of names) {
84
+ if (fs.existsSync(process.resourcesPath + name)) {
85
+ native = require(process.resourcesPath + name);
86
+ break;
87
+ }
88
+ }
89
+ }
90
+ }
91
+
92
+ if (native == null)
93
+ throw new Error('Cannot find the native Koffi module; did you bundle it correctly?');
94
+ if (native.version != pkg.version)
95
+ throw new Error('Mismatched native Koffi modules');
67
96
 
68
97
  module.exports = {
69
98
  ...native,
@@ -47,6 +47,12 @@ endif()
47
47
 
48
48
  # ---- Koffi ----
49
49
 
50
+ file(READ ${CMAKE_CURRENT_SOURCE_DIR}/package.json PKG)
51
+ string(REGEX MATCH "\"version\": \"([^\"]+)\"" XX "${PKG}")
52
+ set(KOFFI_VERSION ${CMAKE_MATCH_1})
53
+
54
+ set_source_files_properties(src/ffi.cc PROPERTIES COMPILE_DEFINITIONS VERSION=${KOFFI_VERSION})
55
+
50
56
  set(KOFFI_SRC
51
57
  src/call.cc
52
58
  src/ffi.cc
@@ -0,0 +1,23 @@
1
+ This is a simple example shows:
2
+
3
+ - How to communicate with Koffi from a renderer window
4
+ - How to use electron-builder with an application that uses Koffi
5
+
6
+ One of the important piece for the packaging is in `package.json`, specifically this part:
7
+
8
+ ```json
9
+ "build": {
10
+ "extraResources": [
11
+ { "from": "node_modules/koffi/build", "to": "koffi" }
12
+ ]
13
+ }
14
+ ```
15
+
16
+ This instructs electron-builder to copy the native koffi modules to a place where your application will find them.
17
+
18
+ Use the following commands to package the app for your system:
19
+
20
+ ```sh
21
+ npm install
22
+ npm run dist
23
+ ```