koffi 3.0.0-alpha.5 → 3.0.0-alpha.6

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 (43) hide show
  1. package/{build → bin}/koffi/darwin_arm64/koffi.node +0 -0
  2. package/bin/koffi/darwin_x64/koffi.node +0 -0
  3. package/bin/koffi/freebsd_arm64/koffi.node +0 -0
  4. package/bin/koffi/freebsd_ia32/koffi.node +0 -0
  5. package/bin/koffi/freebsd_x64/koffi.node +0 -0
  6. package/{build → bin}/koffi/linux_arm64/koffi.node +0 -0
  7. package/{build → bin}/koffi/linux_ia32/koffi.node +0 -0
  8. package/bin/koffi/linux_x64/koffi.node +0 -0
  9. package/{build → bin}/koffi/musl_arm64/koffi.node +0 -0
  10. package/{build → bin}/koffi/musl_x64/koffi.node +0 -0
  11. package/bin/koffi/openbsd_ia32/koffi.node +0 -0
  12. package/bin/koffi/openbsd_x64/koffi.node +0 -0
  13. package/{build → bin}/koffi/win32_ia32/koffi.exp +0 -0
  14. package/{build → bin}/koffi/win32_ia32/koffi.node +0 -0
  15. package/{build → bin}/koffi/win32_x64/koffi.exp +0 -0
  16. package/{build → bin}/koffi/win32_x64/koffi.node +0 -0
  17. package/doc/contribute.md +4 -2
  18. package/doc/packaging.md +1 -1
  19. package/index.d.ts +0 -2
  20. package/package.json +15 -4
  21. package/src/koffi/index.cjs +116 -55
  22. package/src/koffi/index.js +24 -67
  23. package/src/koffi/indirect.cjs +115 -55
  24. package/src/koffi/indirect.js +10 -53
  25. package/src/koffi/src/abi/arm64.cc +3 -0
  26. package/src/koffi/src/abi/x64win.cc +3 -0
  27. package/src/koffi/src/abi/x86.cc +3 -0
  28. package/src/koffi/src/call.cc +59 -30
  29. package/src/koffi/src/call.hh +1 -1
  30. package/src/koffi/src/ffi.cc +0 -16
  31. package/src/koffi/src/ffi.hh +2 -3
  32. package/src/koffi/src/util.cc +0 -26
  33. package/src/koffi/src/util.hh +43 -13
  34. package/build/koffi/darwin_x64/koffi.node +0 -0
  35. package/build/koffi/freebsd_arm64/koffi.node +0 -0
  36. package/build/koffi/freebsd_ia32/koffi.node +0 -0
  37. package/build/koffi/freebsd_x64/koffi.node +0 -0
  38. package/build/koffi/linux_x64/koffi.node +0 -0
  39. package/build/koffi/openbsd_ia32/koffi.node +0 -0
  40. package/build/koffi/openbsd_x64/koffi.node +0 -0
  41. /package/{build → bin}/koffi/win32_ia32/koffi.lib +0 -0
  42. /package/{build → bin}/koffi/win32_x64/koffi.lib +0 -0
  43. /package/{src/build.js → build.js} +0 -0
Binary file
Binary file
Binary file
Binary file
package/doc/contribute.md CHANGED
@@ -145,8 +145,10 @@ cd src/koffi
145
145
  node tools/brew.js test # If not done before
146
146
  node tools/brew.js build
147
147
 
148
- cd ../../bin/Koffi/package
149
- npm publish
148
+ cd ../../bin/koffi/packages
149
+
150
+ ./prepare.sh
151
+ ./publish.sh
150
152
  ```
151
153
 
152
154
  Some platforms are emulated so this can take a few minutes until the pre-built binaries are ready. Go grab a cup of coffee, come back and execute the `npm publish` command!
package/doc/packaging.md CHANGED
@@ -11,7 +11,7 @@ Koffi uses native modules to work. The NPM package contains binaries for various
11
11
 
12
12
  In theory, the **packagers/bundlers should be able to find all native modules** because they are explictly listed in the Javascript file (as static strings) and package them somehow.
13
13
 
14
- If that is not the case, you can manually arrange to copy the `node_modules/koffi/build/koffi` directory next to your bundled script.
14
+ If that is not the case, you can manually arrange to copy the `node_modules/koffi/bin/koffi` directory next to your bundled script.
15
15
 
16
16
  Here is an example that would work:
17
17
 
package/index.d.ts CHANGED
@@ -152,8 +152,6 @@ type KoffiConfig = {
152
152
  resident_async_pools?: number;
153
153
  max_async_calls?: number;
154
154
  max_type_size?: number;
155
- fast_pointers?: boolean;
156
- fast_callbacks?: boolean;
157
155
  };
158
156
  type KoffiStats = {
159
157
  disposed: number
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koffi",
3
- "version": "3.0.0-alpha.5",
3
+ "version": "3.0.0-alpha.6",
4
4
  "description": "Fast and simple C FFI (foreign function interface) for Node.js",
5
5
  "keywords": [
6
6
  "foreign",
@@ -21,18 +21,29 @@
21
21
  "email": "niels.martignene@protonmail.com",
22
22
  "url": "https://koromix.dev/"
23
23
  },
24
- "module": "./index.js",
25
24
  "scripts": {
26
- "install": "node src/build.js -P . -D src/koffi --prebuild --release"
25
+ "install": "node ./build.js -P . -D src/koffi --prebuild --release"
27
26
  },
28
27
  "license": "MIT",
29
28
  "cnoke": {
30
29
  "api": "../../vendor/node-api-headers",
31
- "output": "build/koffi/{{ toolchain }}",
30
+ "output": "bin/koffi/{{ toolchain }}",
32
31
  "node": 16,
33
32
  "napi": 8
34
33
  },
35
34
  "funding": "https://liberapay.com/Koromix",
35
+ "type": "module",
36
36
  "main": "./index.cjs",
37
+ "module": "./index.js",
38
+ "exports": {
39
+ ".": {
40
+ "import": "./index.js",
41
+ "require": "./index.cjs"
42
+ },
43
+ "./indirect": {
44
+ "import": "./indirect.js",
45
+ "require": "./indirect.cjs"
46
+ }
47
+ },
37
48
  "types": "./index.d.ts"
38
49
  }
@@ -7,6 +7,9 @@ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
7
  var __esm = (fn, res) => function __init() {
8
8
  return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
9
9
  };
10
+ var __commonJS = (cb, mod) => function __require() {
11
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
12
+ };
10
13
  var __export = (target, all) => {
11
14
  for (var name in all)
12
15
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -128,47 +131,7 @@ var init_abi = __esm({
128
131
  var package_default;
129
132
  var init_package = __esm({
130
133
  "package.json"() {
131
- package_default = {
132
- name: "koffi",
133
- version: "3.0.0-alpha.5",
134
- description: "Fast and simple C FFI (foreign function interface) for Node.js",
135
- keywords: [
136
- "foreign",
137
- "function",
138
- "interface",
139
- "ffi",
140
- "binding",
141
- "c",
142
- "napi"
143
- ],
144
- repository: {
145
- type: "git",
146
- url: "https://github.com/Koromix/koffi"
147
- },
148
- homepage: "https://koffi.dev/",
149
- author: {
150
- name: "Niels Martign\xE8ne",
151
- email: "niels.martignene@protonmail.com",
152
- url: "https://koromix.dev/"
153
- },
154
- main: "./index.cjs",
155
- module: "./index.js",
156
- types: "./index.d.ts",
157
- scripts: {
158
- test: "node tools/brew.js test",
159
- prepack: `echo 'Use "npm run package" instead' && false`,
160
- prepublishOnly: `echo 'Use "npm run package" instead' && false`,
161
- package: "node tools/brew.js build"
162
- },
163
- license: "MIT",
164
- cnoke: {
165
- api: "../../vendor/node-api-headers",
166
- output: "../../bin/Koffi/{{ toolchain }}",
167
- node: 16,
168
- napi: 8
169
- },
170
- funding: "https://liberapay.com/Koromix"
171
- };
134
+ package_default = { name: "koffi", version: "3.0.0-alpha.6", cnoke: { api: "../../vendor/node-api-headers", output: "../../bin/koffi/{{ toolchain }}", node: 16, napi: 8 } };
172
135
  }
173
136
  });
174
137
 
@@ -195,19 +158,17 @@ function loadDynamic(root, pkg2, triplets2) {
195
158
  let roots = [root];
196
159
  let native2 = null;
197
160
  let err = null;
198
- if (process.resourcesPath != null)
161
+ if (process.resourcesPath != null) {
199
162
  roots.push(process.resourcesPath);
200
- let filenames = roots.flatMap((dir) => triplets2.flatMap((triplet) => [
201
- `${dir}/build/koffi/${triplet}/koffi.node`,
202
- `${dir}/bin/Koffi/${triplet}/koffi.node`,
203
- `${dir}/node_modules/koffi/build/koffi/${triplet}/koffi.node`,
204
- `${dir}/../@koromix/koffi-${pkg2}/${triplet}/koffi.node`
205
- ]));
206
- for (let filename of filenames) {
207
- if (!import_fs2.default.existsSync(filename))
208
- continue;
163
+ roots.push(process.resourcesPath + "/node_modules/koffi");
164
+ }
165
+ let names = [
166
+ `@koromix/koffi-${pkg2}`,
167
+ ...triplets2.flatMap((triplet) => roots.map((dir) => `${dir}/bin/koffi/${triplet}/koffi.node`))
168
+ ];
169
+ for (let name of names) {
209
170
  try {
210
- native2 = requireNative(filename);
171
+ native2 = requireNative(name);
211
172
  break;
212
173
  } catch (e) {
213
174
  err ??= e;
@@ -230,11 +191,10 @@ function wrapNative(native2) {
230
191
  return lib;
231
192
  };
232
193
  }
233
- var import_util, import_fs2, import_node_module, requireNative;
194
+ var import_util, import_node_module, requireNative;
234
195
  var init_init = __esm({
235
196
  "src/init.js"() {
236
197
  import_util = __toESM(require("util"));
237
- import_fs2 = __toESM(require("fs"));
238
198
  import_node_module = require("node:module");
239
199
  init_abi();
240
200
  init_package();
@@ -242,10 +202,111 @@ var init_init = __esm({
242
202
  }
243
203
  });
244
204
 
205
+ // src/static.js
206
+ var require_static = __commonJS({
207
+ "src/static.js"(exports2, module2) {
208
+ var { createRequire: createRequire2 } = require("node:module");
209
+ var requireNative2 = createRequire2(__dirname);
210
+ function loadStatic2(pkg2) {
211
+ let native2 = null;
212
+ if (native2 == null && pkg2 == "linux-arm64") {
213
+ try {
214
+ native2 = requireNative2("../../bin/koffi/linux_arm64/koffi.node");
215
+ } catch (err) {
216
+ }
217
+ }
218
+ if (native2 == null && pkg2 == "linux-arm64") {
219
+ try {
220
+ native2 = requireNative2("../../bin/koffi/musl_arm64/koffi.node");
221
+ } catch (err) {
222
+ }
223
+ }
224
+ if (native2 == null && pkg2 == "linux-ia32") {
225
+ try {
226
+ native2 = requireNative2("../../bin/koffi/linux_ia32/koffi.node");
227
+ } catch (err) {
228
+ }
229
+ }
230
+ if (native2 == null && pkg2 == "linux-x64") {
231
+ try {
232
+ native2 = requireNative2("../../bin/koffi/linux_x64/koffi.node");
233
+ } catch (err) {
234
+ }
235
+ }
236
+ if (native2 == null && pkg2 == "linux-x64") {
237
+ try {
238
+ native2 = requireNative2("../../bin/koffi/musl_x64/koffi.node");
239
+ } catch (err) {
240
+ }
241
+ }
242
+ if (native2 == null && pkg2 == "freebsd-ia32") {
243
+ try {
244
+ native2 = requireNative2("../../bin/koffi/freebsd_ia32/koffi.node");
245
+ } catch (err) {
246
+ }
247
+ }
248
+ if (native2 == null && pkg2 == "freebsd-x64") {
249
+ try {
250
+ native2 = requireNative2("../../bin/koffi/freebsd_x64/koffi.node");
251
+ } catch (err) {
252
+ }
253
+ }
254
+ if (native2 == null && pkg2 == "freebsd-arm64") {
255
+ try {
256
+ native2 = requireNative2("../../bin/koffi/freebsd_arm64/koffi.node");
257
+ } catch (err) {
258
+ }
259
+ }
260
+ if (native2 == null && pkg2 == "openbsd-ia32") {
261
+ try {
262
+ native2 = requireNative2("../../bin/koffi/openbsd_ia32/koffi.node");
263
+ } catch (err) {
264
+ }
265
+ }
266
+ if (native2 == null && pkg2 == "openbsd-x64") {
267
+ try {
268
+ native2 = requireNative2("../../bin/koffi/openbsd_x64/koffi.node");
269
+ } catch (err) {
270
+ }
271
+ }
272
+ if (native2 == null && pkg2 == "win32-ia32") {
273
+ try {
274
+ native2 = requireNative2("../../bin/koffi/win32_ia32/koffi.node");
275
+ } catch (err) {
276
+ }
277
+ }
278
+ if (native2 == null && pkg2 == "win32-x64") {
279
+ try {
280
+ native2 = requireNative2("../../bin/koffi/win32_x64/koffi.node");
281
+ } catch (err) {
282
+ }
283
+ }
284
+ if (native2 == null && pkg2 == "darwin-x64") {
285
+ try {
286
+ native2 = requireNative2("../../bin/koffi/darwin_x64/koffi.node");
287
+ } catch (err) {
288
+ }
289
+ }
290
+ if (native2 == null && pkg2 == "darwin-arm64") {
291
+ try {
292
+ native2 = requireNative2("../../bin/koffi/darwin_arm64/koffi.node");
293
+ } catch (err) {
294
+ }
295
+ }
296
+ return native2;
297
+ }
298
+ module2.exports = { loadStatic: loadStatic2 };
299
+ }
300
+ });
301
+
245
302
  // index.cjs
246
303
  var { detectPlatform: detectPlatform2, loadDynamic: loadDynamic2, wrapNative: wrapNative2 } = (init_init(), __toCommonJS(init_exports));
304
+ var { loadStatic } = require_static();
247
305
  var [version, pkg, triplets] = detectPlatform2();
248
- var native = loadDynamic2(__dirname + "/../..", pkg, triplets);
306
+ var native = null;
307
+ STATIC: native = loadStatic(pkg);
308
+ if (native == null)
309
+ native = loadDynamic2(__dirname + "/../..", pkg, triplets);
249
310
  if (native.version != version)
250
311
  throw new Error("Mismatched native Koffi modules");
251
312
  wrapNative2(native);
@@ -1,6 +1,5 @@
1
1
  // src/init.js
2
2
  import util from "util";
3
- import fs2 from "fs";
4
3
  import { createRequire } from "node:module";
5
4
 
6
5
  // ../cnoke/src/abi.js
@@ -94,47 +93,7 @@ function decodeElfHeader(buf) {
94
93
  }
95
94
 
96
95
  // package.json
97
- var package_default = {
98
- name: "koffi",
99
- version: "3.0.0-alpha.5",
100
- description: "Fast and simple C FFI (foreign function interface) for Node.js",
101
- keywords: [
102
- "foreign",
103
- "function",
104
- "interface",
105
- "ffi",
106
- "binding",
107
- "c",
108
- "napi"
109
- ],
110
- repository: {
111
- type: "git",
112
- url: "https://github.com/Koromix/koffi"
113
- },
114
- homepage: "https://koffi.dev/",
115
- author: {
116
- name: "Niels Martign\xE8ne",
117
- email: "niels.martignene@protonmail.com",
118
- url: "https://koromix.dev/"
119
- },
120
- main: "./index.cjs",
121
- module: "./index.js",
122
- types: "./index.d.ts",
123
- scripts: {
124
- test: "node tools/brew.js test",
125
- prepack: `echo 'Use "npm run package" instead' && false`,
126
- prepublishOnly: `echo 'Use "npm run package" instead' && false`,
127
- package: "node tools/brew.js build"
128
- },
129
- license: "MIT",
130
- cnoke: {
131
- api: "../../vendor/node-api-headers",
132
- output: "../../bin/Koffi/{{ toolchain }}",
133
- node: 16,
134
- napi: 8
135
- },
136
- funding: "https://liberapay.com/Koromix"
137
- };
96
+ var package_default = { name: "koffi", version: "3.0.0-alpha.6", cnoke: { api: "../../vendor/node-api-headers", output: "../../bin/koffi/{{ toolchain }}", node: 16, napi: 8 } };
138
97
 
139
98
  // src/init.js
140
99
  var requireNative = createRequire(import.meta.dirname);
@@ -154,19 +113,17 @@ function loadDynamic(root, pkg2, triplets2) {
154
113
  let roots = [root];
155
114
  let native2 = null;
156
115
  let err = null;
157
- if (process.resourcesPath != null)
116
+ if (process.resourcesPath != null) {
158
117
  roots.push(process.resourcesPath);
159
- let filenames = roots.flatMap((dir) => triplets2.flatMap((triplet) => [
160
- `${dir}/build/koffi/${triplet}/koffi.node`,
161
- `${dir}/bin/Koffi/${triplet}/koffi.node`,
162
- `${dir}/node_modules/koffi/build/koffi/${triplet}/koffi.node`,
163
- `${dir}/../@koromix/koffi-${pkg2}/${triplet}/koffi.node`
164
- ]));
165
- for (let filename of filenames) {
166
- if (!fs2.existsSync(filename))
167
- continue;
118
+ roots.push(process.resourcesPath + "/node_modules/koffi");
119
+ }
120
+ let names = [
121
+ `@koromix/koffi-${pkg2}`,
122
+ ...triplets2.flatMap((triplet) => roots.map((dir) => `${dir}/bin/koffi/${triplet}/koffi.node`))
123
+ ];
124
+ for (let name of names) {
168
125
  try {
169
- native2 = requireNative(filename);
126
+ native2 = requireNative(name);
170
127
  break;
171
128
  } catch (e) {
172
129
  err ??= e;
@@ -197,85 +154,85 @@ function loadStatic(pkg2) {
197
154
  let native2 = null;
198
155
  if (native2 == null && pkg2 == "linux-arm64") {
199
156
  try {
200
- native2 = requireNative2("../../build/koffi/linux_arm64/koffi.node");
157
+ native2 = requireNative2("../../bin/koffi/linux_arm64/koffi.node");
201
158
  } catch (err) {
202
159
  }
203
160
  }
204
161
  if (native2 == null && pkg2 == "linux-arm64") {
205
162
  try {
206
- native2 = requireNative2("../../build/koffi/musl_arm64/koffi.node");
163
+ native2 = requireNative2("../../bin/koffi/musl_arm64/koffi.node");
207
164
  } catch (err) {
208
165
  }
209
166
  }
210
167
  if (native2 == null && pkg2 == "linux-ia32") {
211
168
  try {
212
- native2 = requireNative2("../../build/koffi/linux_ia32/koffi.node");
169
+ native2 = requireNative2("../../bin/koffi/linux_ia32/koffi.node");
213
170
  } catch (err) {
214
171
  }
215
172
  }
216
173
  if (native2 == null && pkg2 == "linux-x64") {
217
174
  try {
218
- native2 = requireNative2("../../build/koffi/linux_x64/koffi.node");
175
+ native2 = requireNative2("../../bin/koffi/linux_x64/koffi.node");
219
176
  } catch (err) {
220
177
  }
221
178
  }
222
179
  if (native2 == null && pkg2 == "linux-x64") {
223
180
  try {
224
- native2 = requireNative2("../../build/koffi/musl_x64/koffi.node");
181
+ native2 = requireNative2("../../bin/koffi/musl_x64/koffi.node");
225
182
  } catch (err) {
226
183
  }
227
184
  }
228
185
  if (native2 == null && pkg2 == "freebsd-ia32") {
229
186
  try {
230
- native2 = requireNative2("../../build/koffi/freebsd_ia32/koffi.node");
187
+ native2 = requireNative2("../../bin/koffi/freebsd_ia32/koffi.node");
231
188
  } catch (err) {
232
189
  }
233
190
  }
234
191
  if (native2 == null && pkg2 == "freebsd-x64") {
235
192
  try {
236
- native2 = requireNative2("../../build/koffi/freebsd_x64/koffi.node");
193
+ native2 = requireNative2("../../bin/koffi/freebsd_x64/koffi.node");
237
194
  } catch (err) {
238
195
  }
239
196
  }
240
197
  if (native2 == null && pkg2 == "freebsd-arm64") {
241
198
  try {
242
- native2 = requireNative2("../../build/koffi/freebsd_arm64/koffi.node");
199
+ native2 = requireNative2("../../bin/koffi/freebsd_arm64/koffi.node");
243
200
  } catch (err) {
244
201
  }
245
202
  }
246
203
  if (native2 == null && pkg2 == "openbsd-ia32") {
247
204
  try {
248
- native2 = requireNative2("../../build/koffi/openbsd_ia32/koffi.node");
205
+ native2 = requireNative2("../../bin/koffi/openbsd_ia32/koffi.node");
249
206
  } catch (err) {
250
207
  }
251
208
  }
252
209
  if (native2 == null && pkg2 == "openbsd-x64") {
253
210
  try {
254
- native2 = requireNative2("../../build/koffi/openbsd_x64/koffi.node");
211
+ native2 = requireNative2("../../bin/koffi/openbsd_x64/koffi.node");
255
212
  } catch (err) {
256
213
  }
257
214
  }
258
215
  if (native2 == null && pkg2 == "win32-ia32") {
259
216
  try {
260
- native2 = requireNative2("../../build/koffi/win32_ia32/koffi.node");
217
+ native2 = requireNative2("../../bin/koffi/win32_ia32/koffi.node");
261
218
  } catch (err) {
262
219
  }
263
220
  }
264
221
  if (native2 == null && pkg2 == "win32-x64") {
265
222
  try {
266
- native2 = requireNative2("../../build/koffi/win32_x64/koffi.node");
223
+ native2 = requireNative2("../../bin/koffi/win32_x64/koffi.node");
267
224
  } catch (err) {
268
225
  }
269
226
  }
270
227
  if (native2 == null && pkg2 == "darwin-x64") {
271
228
  try {
272
- native2 = requireNative2("../../build/koffi/darwin_x64/koffi.node");
229
+ native2 = requireNative2("../../bin/koffi/darwin_x64/koffi.node");
273
230
  } catch (err) {
274
231
  }
275
232
  }
276
233
  if (native2 == null && pkg2 == "darwin-arm64") {
277
234
  try {
278
- native2 = requireNative2("../../build/koffi/darwin_arm64/koffi.node");
235
+ native2 = requireNative2("../../bin/koffi/darwin_arm64/koffi.node");
279
236
  } catch (err) {
280
237
  }
281
238
  }
@@ -7,6 +7,9 @@ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
7
  var __esm = (fn, res) => function __init() {
8
8
  return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
9
9
  };
10
+ var __commonJS = (cb, mod) => function __require() {
11
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
12
+ };
10
13
  var __export = (target, all) => {
11
14
  for (var name in all)
12
15
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -128,47 +131,7 @@ var init_abi = __esm({
128
131
  var package_default;
129
132
  var init_package = __esm({
130
133
  "package.json"() {
131
- package_default = {
132
- name: "koffi",
133
- version: "3.0.0-alpha.5",
134
- description: "Fast and simple C FFI (foreign function interface) for Node.js",
135
- keywords: [
136
- "foreign",
137
- "function",
138
- "interface",
139
- "ffi",
140
- "binding",
141
- "c",
142
- "napi"
143
- ],
144
- repository: {
145
- type: "git",
146
- url: "https://github.com/Koromix/koffi"
147
- },
148
- homepage: "https://koffi.dev/",
149
- author: {
150
- name: "Niels Martign\xE8ne",
151
- email: "niels.martignene@protonmail.com",
152
- url: "https://koromix.dev/"
153
- },
154
- main: "./index.cjs",
155
- module: "./index.js",
156
- types: "./index.d.ts",
157
- scripts: {
158
- test: "node tools/brew.js test",
159
- prepack: `echo 'Use "npm run package" instead' && false`,
160
- prepublishOnly: `echo 'Use "npm run package" instead' && false`,
161
- package: "node tools/brew.js build"
162
- },
163
- license: "MIT",
164
- cnoke: {
165
- api: "../../vendor/node-api-headers",
166
- output: "../../bin/Koffi/{{ toolchain }}",
167
- node: 16,
168
- napi: 8
169
- },
170
- funding: "https://liberapay.com/Koromix"
171
- };
134
+ package_default = { name: "koffi", version: "3.0.0-alpha.6", cnoke: { api: "../../vendor/node-api-headers", output: "../../bin/koffi/{{ toolchain }}", node: 16, napi: 8 } };
172
135
  }
173
136
  });
174
137
 
@@ -195,19 +158,17 @@ function loadDynamic(root, pkg2, triplets2) {
195
158
  let roots = [root];
196
159
  let native2 = null;
197
160
  let err = null;
198
- if (process.resourcesPath != null)
161
+ if (process.resourcesPath != null) {
199
162
  roots.push(process.resourcesPath);
200
- let filenames = roots.flatMap((dir) => triplets2.flatMap((triplet) => [
201
- `${dir}/build/koffi/${triplet}/koffi.node`,
202
- `${dir}/bin/Koffi/${triplet}/koffi.node`,
203
- `${dir}/node_modules/koffi/build/koffi/${triplet}/koffi.node`,
204
- `${dir}/../@koromix/koffi-${pkg2}/${triplet}/koffi.node`
205
- ]));
206
- for (let filename of filenames) {
207
- if (!import_fs2.default.existsSync(filename))
208
- continue;
163
+ roots.push(process.resourcesPath + "/node_modules/koffi");
164
+ }
165
+ let names = [
166
+ `@koromix/koffi-${pkg2}`,
167
+ ...triplets2.flatMap((triplet) => roots.map((dir) => `${dir}/bin/koffi/${triplet}/koffi.node`))
168
+ ];
169
+ for (let name of names) {
209
170
  try {
210
- native2 = requireNative(filename);
171
+ native2 = requireNative(name);
211
172
  break;
212
173
  } catch (e) {
213
174
  err ??= e;
@@ -230,11 +191,10 @@ function wrapNative(native2) {
230
191
  return lib;
231
192
  };
232
193
  }
233
- var import_util, import_fs2, import_node_module, requireNative;
194
+ var import_util, import_node_module, requireNative;
234
195
  var init_init = __esm({
235
196
  "src/init.js"() {
236
197
  import_util = __toESM(require("util"));
237
- import_fs2 = __toESM(require("fs"));
238
198
  import_node_module = require("node:module");
239
199
  init_abi();
240
200
  init_package();
@@ -242,10 +202,110 @@ var init_init = __esm({
242
202
  }
243
203
  });
244
204
 
205
+ // src/static.js
206
+ var require_static = __commonJS({
207
+ "src/static.js"(exports2, module2) {
208
+ var { createRequire: createRequire2 } = require("node:module");
209
+ var requireNative2 = createRequire2(__dirname);
210
+ function loadStatic2(pkg2) {
211
+ let native2 = null;
212
+ if (native2 == null && pkg2 == "linux-arm64") {
213
+ try {
214
+ native2 = requireNative2("../../bin/koffi/linux_arm64/koffi.node");
215
+ } catch (err) {
216
+ }
217
+ }
218
+ if (native2 == null && pkg2 == "linux-arm64") {
219
+ try {
220
+ native2 = requireNative2("../../bin/koffi/musl_arm64/koffi.node");
221
+ } catch (err) {
222
+ }
223
+ }
224
+ if (native2 == null && pkg2 == "linux-ia32") {
225
+ try {
226
+ native2 = requireNative2("../../bin/koffi/linux_ia32/koffi.node");
227
+ } catch (err) {
228
+ }
229
+ }
230
+ if (native2 == null && pkg2 == "linux-x64") {
231
+ try {
232
+ native2 = requireNative2("../../bin/koffi/linux_x64/koffi.node");
233
+ } catch (err) {
234
+ }
235
+ }
236
+ if (native2 == null && pkg2 == "linux-x64") {
237
+ try {
238
+ native2 = requireNative2("../../bin/koffi/musl_x64/koffi.node");
239
+ } catch (err) {
240
+ }
241
+ }
242
+ if (native2 == null && pkg2 == "freebsd-ia32") {
243
+ try {
244
+ native2 = requireNative2("../../bin/koffi/freebsd_ia32/koffi.node");
245
+ } catch (err) {
246
+ }
247
+ }
248
+ if (native2 == null && pkg2 == "freebsd-x64") {
249
+ try {
250
+ native2 = requireNative2("../../bin/koffi/freebsd_x64/koffi.node");
251
+ } catch (err) {
252
+ }
253
+ }
254
+ if (native2 == null && pkg2 == "freebsd-arm64") {
255
+ try {
256
+ native2 = requireNative2("../../bin/koffi/freebsd_arm64/koffi.node");
257
+ } catch (err) {
258
+ }
259
+ }
260
+ if (native2 == null && pkg2 == "openbsd-ia32") {
261
+ try {
262
+ native2 = requireNative2("../../bin/koffi/openbsd_ia32/koffi.node");
263
+ } catch (err) {
264
+ }
265
+ }
266
+ if (native2 == null && pkg2 == "openbsd-x64") {
267
+ try {
268
+ native2 = requireNative2("../../bin/koffi/openbsd_x64/koffi.node");
269
+ } catch (err) {
270
+ }
271
+ }
272
+ if (native2 == null && pkg2 == "win32-ia32") {
273
+ try {
274
+ native2 = requireNative2("../../bin/koffi/win32_ia32/koffi.node");
275
+ } catch (err) {
276
+ }
277
+ }
278
+ if (native2 == null && pkg2 == "win32-x64") {
279
+ try {
280
+ native2 = requireNative2("../../bin/koffi/win32_x64/koffi.node");
281
+ } catch (err) {
282
+ }
283
+ }
284
+ if (native2 == null && pkg2 == "darwin-x64") {
285
+ try {
286
+ native2 = requireNative2("../../bin/koffi/darwin_x64/koffi.node");
287
+ } catch (err) {
288
+ }
289
+ }
290
+ if (native2 == null && pkg2 == "darwin-arm64") {
291
+ try {
292
+ native2 = requireNative2("../../bin/koffi/darwin_arm64/koffi.node");
293
+ } catch (err) {
294
+ }
295
+ }
296
+ return native2;
297
+ }
298
+ module2.exports = { loadStatic: loadStatic2 };
299
+ }
300
+ });
301
+
245
302
  // index.cjs
246
303
  var { detectPlatform: detectPlatform2, loadDynamic: loadDynamic2, wrapNative: wrapNative2 } = (init_init(), __toCommonJS(init_exports));
304
+ var { loadStatic } = require_static();
247
305
  var [version, pkg, triplets] = detectPlatform2();
248
- var native = loadDynamic2(__dirname + "/../..", pkg, triplets);
306
+ var native = null;
307
+ if (native == null)
308
+ native = loadDynamic2(__dirname + "/../..", pkg, triplets);
249
309
  if (native.version != version)
250
310
  throw new Error("Mismatched native Koffi modules");
251
311
  wrapNative2(native);
@@ -1,6 +1,5 @@
1
1
  // src/init.js
2
2
  import util from "util";
3
- import fs2 from "fs";
4
3
  import { createRequire } from "node:module";
5
4
 
6
5
  // ../cnoke/src/abi.js
@@ -94,47 +93,7 @@ function decodeElfHeader(buf) {
94
93
  }
95
94
 
96
95
  // package.json
97
- var package_default = {
98
- name: "koffi",
99
- version: "3.0.0-alpha.5",
100
- description: "Fast and simple C FFI (foreign function interface) for Node.js",
101
- keywords: [
102
- "foreign",
103
- "function",
104
- "interface",
105
- "ffi",
106
- "binding",
107
- "c",
108
- "napi"
109
- ],
110
- repository: {
111
- type: "git",
112
- url: "https://github.com/Koromix/koffi"
113
- },
114
- homepage: "https://koffi.dev/",
115
- author: {
116
- name: "Niels Martign\xE8ne",
117
- email: "niels.martignene@protonmail.com",
118
- url: "https://koromix.dev/"
119
- },
120
- main: "./index.cjs",
121
- module: "./index.js",
122
- types: "./index.d.ts",
123
- scripts: {
124
- test: "node tools/brew.js test",
125
- prepack: `echo 'Use "npm run package" instead' && false`,
126
- prepublishOnly: `echo 'Use "npm run package" instead' && false`,
127
- package: "node tools/brew.js build"
128
- },
129
- license: "MIT",
130
- cnoke: {
131
- api: "../../vendor/node-api-headers",
132
- output: "../../bin/Koffi/{{ toolchain }}",
133
- node: 16,
134
- napi: 8
135
- },
136
- funding: "https://liberapay.com/Koromix"
137
- };
96
+ var package_default = { name: "koffi", version: "3.0.0-alpha.6", cnoke: { api: "../../vendor/node-api-headers", output: "../../bin/koffi/{{ toolchain }}", node: 16, napi: 8 } };
138
97
 
139
98
  // src/init.js
140
99
  var requireNative = createRequire(import.meta.dirname);
@@ -154,19 +113,17 @@ function loadDynamic(root, pkg2, triplets2) {
154
113
  let roots = [root];
155
114
  let native2 = null;
156
115
  let err = null;
157
- if (process.resourcesPath != null)
116
+ if (process.resourcesPath != null) {
158
117
  roots.push(process.resourcesPath);
159
- let filenames = roots.flatMap((dir) => triplets2.flatMap((triplet) => [
160
- `${dir}/build/koffi/${triplet}/koffi.node`,
161
- `${dir}/bin/Koffi/${triplet}/koffi.node`,
162
- `${dir}/node_modules/koffi/build/koffi/${triplet}/koffi.node`,
163
- `${dir}/../@koromix/koffi-${pkg2}/${triplet}/koffi.node`
164
- ]));
165
- for (let filename of filenames) {
166
- if (!fs2.existsSync(filename))
167
- continue;
118
+ roots.push(process.resourcesPath + "/node_modules/koffi");
119
+ }
120
+ let names = [
121
+ `@koromix/koffi-${pkg2}`,
122
+ ...triplets2.flatMap((triplet) => roots.map((dir) => `${dir}/bin/koffi/${triplet}/koffi.node`))
123
+ ];
124
+ for (let name of names) {
168
125
  try {
169
- native2 = requireNative(filename);
126
+ native2 = requireNative(name);
170
127
  break;
171
128
  } catch (e) {
172
129
  err ??= e;
@@ -602,6 +602,9 @@ namespace {
602
602
  [&]() { \
603
603
  TEB *teb = GetTEB(); \
604
604
  \
605
+ K_DEFER { call->instance->last_error = teb->LastErrorValue; }; \
606
+ teb->LastErrorValue = call->instance->last_error; \
607
+ \
605
608
  ADJUST_TEB(teb, call->mem->stack0.ptr, call->mem->stack0.end); \
606
609
  \
607
610
  return (Expr); \
@@ -237,6 +237,9 @@ namespace {
237
237
  [&]() { \
238
238
  TEB *teb = GetTEB(); \
239
239
  \
240
+ K_DEFER { call->instance->last_error = teb->LastErrorValue; }; \
241
+ teb->LastErrorValue = call->instance->last_error; \
242
+ \
240
243
  ADJUST_TEB(teb, call->mem->stack0.ptr, call->mem->stack0.end); \
241
244
  \
242
245
  return (Expr); \
@@ -307,6 +307,9 @@ napi_value RunLoop(CallData *call, napi_value *args, uint32_t *base, const AbiIn
307
307
  [&]() { \
308
308
  TEB *teb = GetTEB(); \
309
309
  \
310
+ K_DEFER { call->instance->last_error = teb->LastErrorValue; }; \
311
+ teb->LastErrorValue = call->instance->last_error; \
312
+ \
310
313
  ADJUST_TEB(teb, call->mem->stack0.ptr, call->mem->stack0.end); \
311
314
  \
312
315
  return (Expr); \
@@ -866,12 +866,13 @@ bool CallData::PushPointer(napi_value value, const TypeInfo *type, int direction
866
866
  // just made things worse. Oh, well.
867
867
 
868
868
  void *ptr = nullptr;
869
+ napi_valuetype kind = napi_undefined;
869
870
 
870
871
  restart:
871
872
 
872
- // Fast path
873
- if (bool external = false; TryPointer(env, value, &ptr, &external)) {
874
- if (external && CheckValueTag(env, value, &CastMarker)) {
873
+ if (TryPointer(env, value, &ptr, &kind)) {
874
+ #if defined(EXTERNAL_POINTERS)
875
+ if (kind == napi_external && CheckValueTag(env, value, &CastMarker)) {
875
876
  Napi::External<ValueCast> external = Napi::External<ValueCast>(env, value);
876
877
  ValueCast *cast = external.Data();
877
878
 
@@ -880,21 +881,55 @@ restart:
880
881
 
881
882
  goto restart;
882
883
  }
884
+ #endif
883
885
 
884
886
  *out_ptr = ptr;
885
887
  return true;
886
888
  }
887
889
 
888
- return PushPointerSlow(value, type, directions, out_ptr);
890
+ #if !defined(EXTERNAL_POINTERS)
891
+ if (kind == napi_external && CheckValueTag(env, value, &CastMarker)) {
892
+ Napi::External<ValueCast> external = Napi::External<ValueCast>(env, value);
893
+ ValueCast *cast = external.Data();
894
+
895
+ napi_get_reference_value(env, cast->ref, &value);
896
+ type = cast->type;
897
+
898
+ goto restart;
899
+ }
900
+ #endif
901
+
902
+ return PushPointerSlow(value, kind, type, directions, out_ptr);
889
903
  }
890
904
 
891
- bool CallData::PushPointerSlow(napi_value value, const TypeInfo *type, int directions, void **out_ptr)
905
+ bool CallData::PushPointerSlow(napi_value value, napi_valuetype kind, const TypeInfo *type, int directions, void **out_ptr)
892
906
  {
893
907
  const TypeInfo *ref = type->ref.type;
894
908
 
895
909
  void *ptr = nullptr;
896
910
 
897
- if (IsArray(env, value)) {
911
+ if (kind == napi_string) {
912
+ K_ASSERT(type->primitive == PrimitiveKind::Pointer);
913
+
914
+ if (directions & 2) [[unlikely]]
915
+ goto unexpected;
916
+
917
+ if (ref == instance->void_type) {
918
+ PushStringValue(value, (const char **)out_ptr);
919
+ return true;
920
+ } else if (ref->primitive == PrimitiveKind::Int8) {
921
+ PushStringValue(value, (const char **)out_ptr);
922
+ return true;
923
+ } else if (ref->primitive == PrimitiveKind::Int16) {
924
+ PushString16Value(value, (const char16_t **)out_ptr);
925
+ return true;
926
+ } else if (ref->primitive == PrimitiveKind::Int32) {
927
+ PushString32Value(value, (const char32_t **)out_ptr);
928
+ return true;
929
+ } else {
930
+ goto unexpected;
931
+ }
932
+ } else if (IsArray(env, value)) {
898
933
  Napi::Array array = Napi::Array(env, value);
899
934
  Size len = PushIndirectString(array, ref, &ptr);
900
935
 
@@ -981,27 +1016,6 @@ bool CallData::PushPointerSlow(napi_value value, const TypeInfo *type, int direc
981
1016
 
982
1017
  *out_ptr = ptr;
983
1018
  return true;
984
- } else if (napi_valuetype kind = GetKindOf(env, value); kind == napi_string) {
985
- K_ASSERT(type->primitive == PrimitiveKind::Pointer);
986
-
987
- if (directions & 2) [[unlikely]]
988
- goto unexpected;
989
-
990
- if (ref == instance->void_type) {
991
- PushStringValue(value, (const char **)out_ptr);
992
- return true;
993
- } else if (ref->primitive == PrimitiveKind::Int8) {
994
- PushStringValue(value, (const char **)out_ptr);
995
- return true;
996
- } else if (ref->primitive == PrimitiveKind::Int16) {
997
- PushString16Value(value, (const char16_t **)out_ptr);
998
- return true;
999
- } else if (ref->primitive == PrimitiveKind::Int32) {
1000
- PushString32Value(value, (const char32_t **)out_ptr);
1001
- return true;
1002
- } else {
1003
- goto unexpected;
1004
- }
1005
1019
  } else if (kind == napi_function) {
1006
1020
  if (type->primitive != PrimitiveKind::Callback) [[unlikely]] {
1007
1021
  ThrowError<Napi::TypeError>(env, "Cannot pass function to type %1", type->name);
@@ -1026,11 +1040,13 @@ unexpected:
1026
1040
  bool CallData::PushCallback(napi_value value, const TypeInfo *type, void **out_ptr)
1027
1041
  {
1028
1042
  void *ptr = nullptr;
1043
+ napi_valuetype kind = napi_undefined;
1029
1044
 
1030
1045
  restart:
1031
1046
 
1032
- if (bool external = false; TryPointer(env, value, &ptr, &external)) {
1033
- if (external && CheckValueTag(env, value, &CastMarker)) {
1047
+ if (TryPointer(env, value, &ptr, &kind)) {
1048
+ #if defined(EXTERNAL_POINTERS)
1049
+ if (kind == napi_external && CheckValueTag(env, value, &CastMarker)) {
1034
1050
  Napi::External<ValueCast> external = Napi::External<ValueCast>(env, value);
1035
1051
  ValueCast *cast = external.Data();
1036
1052
 
@@ -1039,10 +1055,13 @@ restart:
1039
1055
 
1040
1056
  goto restart;
1041
1057
  }
1058
+ #endif
1042
1059
 
1043
1060
  *out_ptr = ptr;
1044
1061
  return true;
1045
- } else if (GetKindOf(env, value) == napi_function) {
1062
+ }
1063
+
1064
+ if (kind == napi_function) {
1046
1065
  Napi::Function func = Napi::Function(env, value);
1047
1066
 
1048
1067
  ptr = ReserveTrampoline(type->ref.proto, func);
@@ -1051,6 +1070,16 @@ restart:
1051
1070
 
1052
1071
  *out_ptr = ptr;
1053
1072
  return true;
1073
+ #if !defined(EXTERNAL_POINTERS)
1074
+ } else if (kind == napi_external && CheckValueTag(env, value, &CastMarker)) {
1075
+ Napi::External<ValueCast> external = Napi::External<ValueCast>(env, value);
1076
+ ValueCast *cast = external.Data();
1077
+
1078
+ napi_get_reference_value(env, cast->ref, &value);
1079
+ type = cast->type;
1080
+
1081
+ goto restart;
1082
+ #endif
1054
1083
  }
1055
1084
 
1056
1085
  ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected %2", GetValueType(instance, value), type->name);
@@ -87,7 +87,7 @@ struct alignas(8) CallData {
87
87
  INLINE_UNITY void PushBuffer(Span<const uint8_t> buffer, const TypeInfo *type, uint8_t *origin);
88
88
  bool PushStringArray(napi_value value, const TypeInfo *type, uint8_t *origin);
89
89
  INLINE_UNITY bool PushPointer(napi_value value, const TypeInfo *type, int directions, void **out_ptr);
90
- bool PushPointerSlow(napi_value value, const TypeInfo *type, int directions, void **out_ptr);
90
+ bool PushPointerSlow(napi_value value, napi_valuetype kind, const TypeInfo *type, int directions, void **out_ptr);
91
91
  INLINE_UNITY bool PushCallback(napi_value value, const TypeInfo *type, void **out_ptr);
92
92
  Size PushIndirectString(Napi::Array array, const TypeInfo *ref, void **out_ptr);
93
93
 
@@ -143,20 +143,6 @@ static Napi::Value GetSetConfig(const Napi::CallbackInfo &info)
143
143
  } else if (key == "max_type_size") {
144
144
  if (!ChangeSize(key.c_str(), value, 32, Mebibytes(512), &new_config.max_type_size))
145
145
  return env.Null();
146
- } else if (key == "fast_pointers") {
147
- if (!value.IsBoolean()) {
148
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value for '%2', expected boolean", GetValueType(instance, value), key.c_str());
149
- return env.Null();
150
- }
151
-
152
- new_config.fast_pointers = value.As<Napi::Boolean>();
153
- } else if (key == "fast_callbacks") {
154
- if (!value.IsBoolean()) {
155
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value for '%2', expected boolean", GetValueType(instance, value), key.c_str());
156
- return env.Null();
157
- }
158
-
159
- new_config.fast_callbacks = value.As<Napi::Boolean>();
160
146
  } else {
161
147
  ThrowError<Napi::Error>(env, "Unexpected config member '%1'", key.c_str());
162
148
  return env.Null();
@@ -181,8 +167,6 @@ static Napi::Value GetSetConfig(const Napi::CallbackInfo &info)
181
167
  obj.Set("resident_async_pools", instance->config.resident_async_pools);
182
168
  obj.Set("max_async_calls", instance->config.resident_async_pools + instance->config.max_temporaries);
183
169
  obj.Set("max_type_size", instance->config.max_type_size);
184
- obj.Set("fast_pointers", instance->config.fast_pointers);
185
- obj.Set("fast_callbacks", instance->config.fast_callbacks);
186
170
 
187
171
  return obj;
188
172
  }
@@ -292,6 +292,8 @@ struct InstanceData {
292
292
  #if defined(_WIN32)
293
293
  void *main_stack_max;
294
294
  void *main_stack_min;
295
+
296
+ uint32_t last_error = 0;
295
297
  #endif
296
298
 
297
299
  BucketArray<LinkedAllocator> encode_allocators;
@@ -309,9 +311,6 @@ struct InstanceData {
309
311
  int resident_async_pools = DefaultResidentAsyncPools;
310
312
  int max_temporaries = DefaultMaxAsyncCalls - DefaultResidentAsyncPools;
311
313
  Size max_type_size = DefaultMaxTypeSize;
312
-
313
- bool fast_pointers = true;
314
- bool fast_callbacks = true;
315
314
  } config;
316
315
 
317
316
  struct {
@@ -1768,32 +1768,6 @@ Napi::Function WrapFunction(Napi::Env env, const FunctionInfo *func)
1768
1768
  return wrapper;
1769
1769
  }
1770
1770
 
1771
- Napi::Value WrapPointer(Napi::Env env, const TypeInfo *ref, void *ptr)
1772
- {
1773
- InstanceData *instance = env.GetInstanceData<InstanceData>();
1774
-
1775
- if (instance->config.fast_pointers) {
1776
- Napi::BigInt big = Napi::BigInt::New(env, (uint64_t)(uintptr_t)ptr);
1777
- return big;
1778
- } else {
1779
- Napi::External<void> external = Napi::External<void>::New(env, ptr);
1780
- return external;
1781
- }
1782
- }
1783
-
1784
- Napi::Value WrapCallback(Napi::Env env, const TypeInfo *ref, void *ptr)
1785
- {
1786
- InstanceData *instance = env.GetInstanceData<InstanceData>();
1787
-
1788
- if (instance->config.fast_callbacks) {
1789
- Napi::BigInt big = Napi::BigInt::New(env, (uint64_t)(uintptr_t)ptr);
1790
- return big;
1791
- } else {
1792
- Napi::External<void> external = Napi::External<void>::New(env, ptr);
1793
- return external;
1794
- }
1795
- }
1796
-
1797
1771
  bool DetectCallConvention(Span<const char> name, CallConvention *out_convention)
1798
1772
  {
1799
1773
  if (name == "__cdecl") {
@@ -10,6 +10,8 @@
10
10
 
11
11
  namespace K {
12
12
 
13
+ // #define EXTERNAL_POINTERS
14
+
13
15
  #if defined(_MSC_VER)
14
16
  #define FORCE_INLINE __forceinline
15
17
  #else
@@ -243,6 +245,9 @@ static FORCE_INLINE bool TryPointer(napi_env env, napi_value value, void **out_p
243
245
  if (uintptr_t ptr = 0; TryNumber(env, value, &ptr)) {
244
246
  *out_ptr = (void *)ptr;
245
247
  return true;
248
+ } else if (IsTypedArray(env, value)) {
249
+ napi_get_typedarray_info(env, value, nullptr, nullptr, out_ptr, nullptr, nullptr);
250
+ return true;
246
251
  }
247
252
 
248
253
  napi_valuetype kind = GetKindOf(env, value);
@@ -250,14 +255,13 @@ static FORCE_INLINE bool TryPointer(napi_env env, napi_value value, void **out_p
250
255
  if (IsNullOrUndefined(kind)) {
251
256
  *out_ptr = nullptr;
252
257
  return true;
253
- } else if (IsTypedArray(env, value)) {
254
- napi_get_typedarray_info(env, value, nullptr, nullptr, out_ptr, nullptr, nullptr);
255
- return true;
258
+ #if defined(EXTERNAL_POINTERS)
256
259
  } else if (kind == napi_external) {
257
260
  Napi::External<void> external = Napi::External<void>(env, value);
258
261
 
259
262
  *out_ptr = (void *)external.Data();
260
263
  return true;
264
+ #endif
261
265
  } else if (IsArrayBuffer(env, value)) {
262
266
  Napi::ArrayBuffer buffer = Napi::ArrayBuffer(env, value);
263
267
 
@@ -268,11 +272,17 @@ static FORCE_INLINE bool TryPointer(napi_env env, napi_value value, void **out_p
268
272
  return false;
269
273
  }
270
274
 
271
- static FORCE_INLINE bool TryPointer(napi_env env, napi_value value, void **out_ptr, bool *out_external)
275
+ // The output value type is not accurate: napi_number instead of napi_bing, napi_null instead of napi_undefined
276
+ static FORCE_INLINE bool TryPointer(napi_env env, napi_value value, void **out_ptr, napi_valuetype *out_kind)
272
277
  {
273
278
  if (uintptr_t ptr = 0; TryNumber(env, value, &ptr)) {
274
279
  *out_ptr = (void *)ptr;
275
- *out_external = false;
280
+ *out_kind = napi_number;
281
+
282
+ return true;
283
+ } else if (IsTypedArray(env, value)) {
284
+ napi_get_typedarray_info(env, value, nullptr, nullptr, out_ptr, nullptr, nullptr);
285
+ *out_kind = napi_object;
276
286
 
277
287
  return true;
278
288
  }
@@ -281,30 +291,28 @@ static FORCE_INLINE bool TryPointer(napi_env env, napi_value value, void **out_p
281
291
 
282
292
  if (IsNullOrUndefined(kind)) {
283
293
  *out_ptr = nullptr;
284
- *out_external = false;
285
-
286
- return true;
287
- } else if (IsTypedArray(env, value)) {
288
- napi_get_typedarray_info(env, value, nullptr, nullptr, out_ptr, nullptr, nullptr);
289
- *out_external = false;
294
+ *out_kind = napi_null;
290
295
 
291
296
  return true;
297
+ #if defined(EXTERNAL_POINTERS)
292
298
  } else if (kind == napi_external) {
293
299
  Napi::External<void> external = Napi::External<void>(env, value);
294
300
 
295
301
  *out_ptr = (void *)external.Data();
296
- *out_external = true;
302
+ *out_kind = napi_external;
297
303
 
298
304
  return true;
305
+ #endif
299
306
  } else if (IsArrayBuffer(env, value)) {
300
307
  Napi::ArrayBuffer buffer = Napi::ArrayBuffer(env, value);
301
308
 
302
309
  *out_ptr = (void *)buffer.Data();
303
- *out_external = false;
310
+ *out_kind = napi_object;
304
311
 
305
312
  return true;
306
313
  }
307
314
 
315
+ *out_kind = kind;
308
316
  return false;
309
317
  }
310
318
 
@@ -421,6 +429,28 @@ static FORCE_INLINE Napi::Array GetOwnPropertyNames(napi_env env, napi_value obj
421
429
 
422
430
  Napi::Function WrapFunction(Napi::Env env, const FunctionInfo *func);
423
431
 
432
+ static FORCE_INLINE Napi::Value WrapPointer(Napi::Env env, const TypeInfo *ref, void *ptr)
433
+ {
434
+ #if defined(EXTERNAL_POINTERS)
435
+ Napi::External<void> external = Napi::External<void>::New(env, ptr);
436
+ return external;
437
+ #else
438
+ Napi::BigInt big = Napi::BigInt::New(env, (uint64_t)(uintptr_t)ptr);
439
+ return big;
440
+ #endif
441
+ }
442
+
443
+ static FORCE_INLINE Napi::Value WrapCallback(Napi::Env env, const TypeInfo *ref, void *ptr)
444
+ {
445
+ #if defined(EXTERNAL_POINTERS)
446
+ Napi::External<void> external = Napi::External<void>::New(env, ptr);
447
+ return external;
448
+ #else
449
+ Napi::BigInt big = Napi::BigInt::New(env, (uint64_t)(uintptr_t)ptr);
450
+ return big;
451
+ #endif
452
+ }
453
+
424
454
  Napi::Value INLINE_UNITY WrapPointer(Napi::Env env, const TypeInfo *ref, void *ptr);
425
455
  Napi::Value INLINE_UNITY WrapCallback(Napi::Env env, const TypeInfo *ref, void *ptr);
426
456
 
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
File without changes
File without changes
File without changes