koffi 2.3.12 → 2.3.14

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 (41) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/build/2.3.14/koffi_darwin_arm64/koffi.node +0 -0
  3. package/build/2.3.14/koffi_darwin_x64/koffi.node +0 -0
  4. package/build/2.3.14/koffi_freebsd_arm64/koffi.node +0 -0
  5. package/build/2.3.14/koffi_freebsd_ia32/koffi.node +0 -0
  6. package/build/2.3.14/koffi_freebsd_x64/koffi.node +0 -0
  7. package/build/2.3.14/koffi_linux_arm32hf/koffi.node +0 -0
  8. package/build/2.3.14/koffi_linux_arm64/koffi.node +0 -0
  9. package/build/2.3.14/koffi_linux_ia32/koffi.node +0 -0
  10. package/build/2.3.14/koffi_linux_riscv64hf64/koffi.node +0 -0
  11. package/build/{2.3.12 → 2.3.14}/koffi_linux_x64/koffi.node +0 -0
  12. package/build/2.3.14/koffi_openbsd_ia32/koffi.node +0 -0
  13. package/build/2.3.14/koffi_openbsd_x64/koffi.node +0 -0
  14. package/build/2.3.14/koffi_win32_arm64/koffi.node +0 -0
  15. package/build/2.3.14/koffi_win32_ia32/koffi.node +0 -0
  16. package/build/{2.3.12 → 2.3.14}/koffi_win32_x64/koffi.node +0 -0
  17. package/doc/misc.md +22 -0
  18. package/doc/pointers.md +4 -0
  19. package/package.json +2 -2
  20. package/src/index.d.ts +11 -4
  21. package/src/koffi/src/errno.inc +480 -0
  22. package/src/koffi/src/ffi.cc +73 -4
  23. package/build/2.3.12/koffi_darwin_arm64/koffi.node +0 -0
  24. package/build/2.3.12/koffi_darwin_x64/koffi.node +0 -0
  25. package/build/2.3.12/koffi_freebsd_arm64/koffi.node +0 -0
  26. package/build/2.3.12/koffi_freebsd_ia32/koffi.node +0 -0
  27. package/build/2.3.12/koffi_freebsd_x64/koffi.node +0 -0
  28. package/build/2.3.12/koffi_linux_arm32hf/koffi.node +0 -0
  29. package/build/2.3.12/koffi_linux_arm64/koffi.node +0 -0
  30. package/build/2.3.12/koffi_linux_ia32/koffi.node +0 -0
  31. package/build/2.3.12/koffi_linux_riscv64hf64/koffi.node +0 -0
  32. package/build/2.3.12/koffi_openbsd_ia32/koffi.node +0 -0
  33. package/build/2.3.12/koffi_openbsd_x64/koffi.node +0 -0
  34. package/build/2.3.12/koffi_win32_arm64/koffi.node +0 -0
  35. package/build/2.3.12/koffi_win32_ia32/koffi.node +0 -0
  36. /package/build/{2.3.12 → 2.3.14}/koffi_win32_arm64/koffi.exp +0 -0
  37. /package/build/{2.3.12 → 2.3.14}/koffi_win32_arm64/koffi.lib +0 -0
  38. /package/build/{2.3.12 → 2.3.14}/koffi_win32_ia32/koffi.exp +0 -0
  39. /package/build/{2.3.12 → 2.3.14}/koffi_win32_ia32/koffi.lib +0 -0
  40. /package/build/{2.3.12 → 2.3.14}/koffi_win32_x64/koffi.exp +0 -0
  41. /package/build/{2.3.12 → 2.3.14}/koffi_win32_x64/koffi.lib +0 -0
package/CHANGELOG.md CHANGED
@@ -4,6 +4,19 @@
4
4
 
5
5
  ### Koffi 2.3
6
6
 
7
+ #### Koffi 2.3.14
8
+
9
+ **Main changes:**
10
+
11
+ - Add `koffi.errno()` function to get and set current errno value
12
+ - Add `koffi.os.errno` object with valid errno codes
13
+
14
+ #### Koffi 2.3.13
15
+
16
+ **Main changes:**
17
+
18
+ - Add `koffi.address()` to get the raw value of a wrapper pointer
19
+
7
20
  #### Koffi 2.3.12
8
21
 
9
22
  **Main fixes:**
package/doc/misc.md CHANGED
@@ -101,3 +101,25 @@ max_type_size | 64 MiB | Maximum size of Koffi types (for arrays and str
101
101
  *New in Koffi 2.3.2*
102
102
 
103
103
  You can use `koffi.stats()` to get a few statistics related to Koffi.
104
+
105
+ ## POSIX error codes
106
+
107
+ *New in Koffi 2.3.14*
108
+
109
+ You can use `koffi.errno()` to the current errno value, and `koffi.errno(value)` to change it.
110
+
111
+ The standard POSIX error codes are available in `koffi.os.errno`, as in the example below:
112
+
113
+ ```js
114
+ const assert = require('assert');
115
+ const koffi = require('koffi');
116
+
117
+ const lib = koffi.load('libc.so.6');
118
+
119
+ const close = lib.func('int close(int fd)');
120
+
121
+ close(-1);
122
+ assert.equal(koffi.errno(), koffi.os.errno.EBADF);
123
+
124
+ console.log('close() with invalid FD is POSIX compliant!');
125
+ ```
package/doc/pointers.md CHANGED
@@ -133,3 +133,7 @@ By default, just like for objects, array arguments are copied from JS to C but n
133
133
  Disposable types allow you to register a function that will automatically called after each C to JS conversion performed by Koffi. This can be used to avoid leaking heap-allocated strings, for example.
134
134
 
135
135
  Read the documentation for [disposable types](calls.md#heap-allocated-values) on the page about function calls.
136
+
137
+ ## Unwrap pointers
138
+
139
+ You can use `koffi.address(ptr)` on a pointer to get the numeric value as a [BigInt object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt).
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koffi",
3
- "version": "2.3.12",
4
- "stable": "2.3.12",
3
+ "version": "2.3.14",
4
+ "stable": "2.3.14",
5
5
  "description": "Fast and simple C FFI (foreign function interface) for Node.js",
6
6
  "keywords": [
7
7
  "foreign",
package/src/index.d.ts CHANGED
@@ -101,8 +101,6 @@ declare module 'koffi' {
101
101
  export function out(value: TypeSpec): IKoffiCType;
102
102
  export function inout(value: TypeSpec): IKoffiCType;
103
103
 
104
- export function as(value: any, type: TypeSpec): IKoffiPointerCast;
105
-
106
104
  export function disposable(type: TypeSpec): IKoffiCType;
107
105
  export function disposable(name: string, type: TypeSpec): IKoffiCType;
108
106
  export function disposable(name: string, type: TypeSpec, freeFunction: Function): IKoffiCType;
@@ -114,10 +112,12 @@ declare module 'koffi' {
114
112
  export function register(thisValue: any, callback: Function, type: TypeSpec): IKoffiRegisteredCallback;
115
113
  export function unregister(callback: IKoffiRegisteredCallback): void;
116
114
 
115
+ export function as(value: any, type: TypeSpec): IKoffiPointerCast;
117
116
  export function decode(value: any, type: TypeSpec): any;
118
117
  export function decode(value: any, type: TypeSpec, len: number): any;
119
118
  export function decode(value: any, offset: number, type: TypeSpec): any;
120
119
  export function decode(value: any, offset: number, type: TypeSpec, len: number): any;
120
+ export function address(value: any): bigint;
121
121
 
122
122
  export function sizeof(type: TypeSpec): number;
123
123
  export function alignof(type: TypeSpec): number;
@@ -131,6 +131,13 @@ declare module 'koffi' {
131
131
  export function config(cfg: Record<string, unknown>): Record<string, unknown>;
132
132
  export function stats(): Record<string, unknown>;
133
133
 
134
- export let internal: Boolean;
135
- export let extension: String;
134
+ export function errno(): number;
135
+ export function errno(value: number): number;
136
+
137
+ export const internal: Boolean;
138
+ export const extension: String;
139
+
140
+ export const os: {
141
+ errno: Record<string, number>
142
+ };
136
143
  }
@@ -0,0 +1,480 @@
1
+ // Copyright 2023 Niels Martignène <niels.martignene@protonmail.com>
2
+ //
3
+ // Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ // this software and associated documentation files (the “Software”), to deal in
5
+ // the Software without restriction, including without limitation the rights to use,
6
+ // copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
7
+ // Software, and to permit persons to whom the Software is furnished to do so,
8
+ // subject to the following conditions:
9
+ //
10
+ // The above copyright notice and this permission notice shall be included in all
11
+ // copies or substantial portions of the Software.
12
+ //
13
+ // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
14
+ // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15
+ // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16
+ // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17
+ // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18
+ // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19
+ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20
+ // OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ #include <errno.h>
23
+
24
+ struct ErrnoCodeInfo {
25
+ const char *name;
26
+ int value;
27
+ };
28
+
29
+ const ErrnoCodeInfo ErrnoCodes[] = {
30
+ #ifdef E2BIG
31
+ {"E2BIG", E2BIG },
32
+ #endif
33
+ #ifdef EACCES
34
+ {"EACCES", EACCES },
35
+ #endif
36
+ #ifdef EADDRINUSE
37
+ {"EADDRINUSE", EADDRINUSE },
38
+ #endif
39
+ #ifdef EADDRNOTAVAIL
40
+ {"EADDRNOTAVAIL", EADDRNOTAVAIL },
41
+ #endif
42
+ #ifdef EADV
43
+ {"EADV", EADV },
44
+ #endif
45
+ #ifdef EAFNOSUPPORT
46
+ {"EAFNOSUPPORT", EAFNOSUPPORT },
47
+ #endif
48
+ #ifdef EAGAIN
49
+ {"EAGAIN", EAGAIN },
50
+ #endif
51
+ #ifdef EALREADY
52
+ {"EALREADY", EALREADY },
53
+ #endif
54
+ #ifdef EAUTH
55
+ {"EAUTH", EAUTH },
56
+ #endif
57
+ #ifdef EBADE
58
+ {"EBADE", EBADE },
59
+ #endif
60
+ #ifdef EBADF
61
+ {"EBADF", EBADF },
62
+ #endif
63
+ #ifdef EBADFD
64
+ {"EBADFD", EBADFD },
65
+ #endif
66
+ #ifdef EBADMSG
67
+ {"EBADMSG", EBADMSG },
68
+ #endif
69
+ #ifdef EBADR
70
+ {"EBADR", EBADR },
71
+ #endif
72
+ #ifdef EBADRPC
73
+ {"EBADRPC", EBADRPC },
74
+ #endif
75
+ #ifdef EBADRQC
76
+ {"EBADRQC", EBADRQC },
77
+ #endif
78
+ #ifdef EBADSLT
79
+ {"EBADSLT", EBADSLT },
80
+ #endif
81
+ #ifdef EBFONT
82
+ {"EBFONT", EBFONT },
83
+ #endif
84
+ #ifdef EBUSY
85
+ {"EBUSY", EBUSY },
86
+ #endif
87
+ #ifdef ECANCELED
88
+ {"ECANCELED", ECANCELED },
89
+ #endif
90
+ #ifdef ECAPMODE
91
+ {"ECAPMODE", ECAPMODE },
92
+ #endif
93
+ #ifdef ECHILD
94
+ {"ECHILD", ECHILD },
95
+ #endif
96
+ #ifdef ECHRNG
97
+ {"ECHRNG", ECHRNG },
98
+ #endif
99
+ #ifdef ECOMM
100
+ {"ECOMM", ECOMM },
101
+ #endif
102
+ #ifdef ECONNABORTED
103
+ {"ECONNABORTED", ECONNABORTED },
104
+ #endif
105
+ #ifdef ECONNREFUSED
106
+ {"ECONNREFUSED", ECONNREFUSED },
107
+ #endif
108
+ #ifdef ECONNRESET
109
+ {"ECONNRESET", ECONNRESET },
110
+ #endif
111
+ #ifdef EDEADLK
112
+ {"EDEADLK", EDEADLK },
113
+ #endif
114
+ #ifdef EDEADLOCK
115
+ {"EDEADLOCK", EDEADLOCK },
116
+ #endif
117
+ #ifdef EDESTADDRREQ
118
+ {"EDESTADDRREQ", EDESTADDRREQ },
119
+ #endif
120
+ #ifdef EDOM
121
+ {"EDOM", EDOM },
122
+ #endif
123
+ #ifdef EDOOFUS
124
+ {"EDOOFUS", EDOOFUS },
125
+ #endif
126
+ #ifdef EDOTDOT
127
+ {"EDOTDOT", EDOTDOT },
128
+ #endif
129
+ #ifdef EDQUOT
130
+ {"EDQUOT", EDQUOT },
131
+ #endif
132
+ #ifdef EEXIST
133
+ {"EEXIST", EEXIST },
134
+ #endif
135
+ #ifdef EFAULT
136
+ {"EFAULT", EFAULT },
137
+ #endif
138
+ #ifdef EFBIG
139
+ {"EFBIG", EFBIG },
140
+ #endif
141
+ #ifdef EFTYPE
142
+ {"EFTYPE", EFTYPE },
143
+ #endif
144
+ #ifdef EHOSTDOWN
145
+ {"EHOSTDOWN", EHOSTDOWN },
146
+ #endif
147
+ #ifdef EHOSTUNREACH
148
+ {"EHOSTUNREACH", EHOSTUNREACH },
149
+ #endif
150
+ #ifdef EHWPOISON
151
+ {"EHWPOISON", EHWPOISON },
152
+ #endif
153
+ #ifdef EIDRM
154
+ {"EIDRM", EIDRM },
155
+ #endif
156
+ #ifdef EILSEQ
157
+ {"EILSEQ", EILSEQ },
158
+ #endif
159
+ #ifdef EINPROGRESS
160
+ {"EINPROGRESS", EINPROGRESS },
161
+ #endif
162
+ #ifdef EINTEGRITY
163
+ {"EINTEGRITY", EINTEGRITY },
164
+ #endif
165
+ #ifdef EINTR
166
+ {"EINTR", EINTR },
167
+ #endif
168
+ #ifdef EINVAL
169
+ {"EINVAL", EINVAL },
170
+ #endif
171
+ #ifdef EIO
172
+ {"EIO", EIO },
173
+ #endif
174
+ #ifdef EISCONN
175
+ {"EISCONN", EISCONN },
176
+ #endif
177
+ #ifdef EISDIR
178
+ {"EISDIR", EISDIR },
179
+ #endif
180
+ #ifdef EISNAM
181
+ {"EISNAM", EISNAM },
182
+ #endif
183
+ #ifdef EKEYEXPIRED
184
+ {"EKEYEXPIRED", EKEYEXPIRED },
185
+ #endif
186
+ #ifdef EKEYREJECTED
187
+ {"EKEYREJECTED", EKEYREJECTED },
188
+ #endif
189
+ #ifdef EKEYREVOKED
190
+ {"EKEYREVOKED", EKEYREVOKED },
191
+ #endif
192
+ #ifdef EL2HLT
193
+ {"EL2HLT", EL2HLT },
194
+ #endif
195
+ #ifdef EL2NSYNC
196
+ {"EL2NSYNC", EL2NSYNC },
197
+ #endif
198
+ #ifdef EL3HLT
199
+ {"EL3HLT", EL3HLT },
200
+ #endif
201
+ #ifdef EL3RST
202
+ {"EL3RST", EL3RST },
203
+ #endif
204
+ #ifdef ELIBACC
205
+ {"ELIBACC", ELIBACC },
206
+ #endif
207
+ #ifdef ELIBBAD
208
+ {"ELIBBAD", ELIBBAD },
209
+ #endif
210
+ #ifdef ELIBEXEC
211
+ {"ELIBEXEC", ELIBEXEC },
212
+ #endif
213
+ #ifdef ELIBMAX
214
+ {"ELIBMAX", ELIBMAX },
215
+ #endif
216
+ #ifdef ELIBSCN
217
+ {"ELIBSCN", ELIBSCN },
218
+ #endif
219
+ #ifdef ELNRNG
220
+ {"ELNRNG", ELNRNG },
221
+ #endif
222
+ #ifdef ELOOP
223
+ {"ELOOP", ELOOP },
224
+ #endif
225
+ #ifdef EMEDIUMTYPE
226
+ {"EMEDIUMTYPE", EMEDIUMTYPE },
227
+ #endif
228
+ #ifdef EMFILE
229
+ {"EMFILE", EMFILE },
230
+ #endif
231
+ #ifdef EMLINK
232
+ {"EMLINK", EMLINK },
233
+ #endif
234
+ #ifdef EMSGSIZE
235
+ {"EMSGSIZE", EMSGSIZE },
236
+ #endif
237
+ #ifdef EMULTIHOP
238
+ {"EMULTIHOP", EMULTIHOP },
239
+ #endif
240
+ #ifdef ENAMETOOLONG
241
+ {"ENAMETOOLONG", ENAMETOOLONG },
242
+ #endif
243
+ #ifdef ENAVAIL
244
+ {"ENAVAIL", ENAVAIL },
245
+ #endif
246
+ #ifdef ENEEDAUTH
247
+ {"ENEEDAUTH", ENEEDAUTH },
248
+ #endif
249
+ #ifdef ENETDOWN
250
+ {"ENETDOWN", ENETDOWN },
251
+ #endif
252
+ #ifdef ENETRESET
253
+ {"ENETRESET", ENETRESET },
254
+ #endif
255
+ #ifdef ENETUNREACH
256
+ {"ENETUNREACH", ENETUNREACH },
257
+ #endif
258
+ #ifdef ENFILE
259
+ {"ENFILE", ENFILE },
260
+ #endif
261
+ #ifdef ENOANO
262
+ {"ENOANO", ENOANO },
263
+ #endif
264
+ #ifdef ENOATTR
265
+ {"ENOATTR", ENOATTR },
266
+ #endif
267
+ #ifdef ENOBUFS
268
+ {"ENOBUFS", ENOBUFS },
269
+ #endif
270
+ #ifdef ENOCSI
271
+ {"ENOCSI", ENOCSI },
272
+ #endif
273
+ #ifdef ENODATA
274
+ {"ENODATA", ENODATA },
275
+ #endif
276
+ #ifdef ENODEV
277
+ {"ENODEV", ENODEV },
278
+ #endif
279
+ #ifdef ENOENT
280
+ {"ENOENT", ENOENT },
281
+ #endif
282
+ #ifdef ENOEXEC
283
+ {"ENOEXEC", ENOEXEC },
284
+ #endif
285
+ #ifdef ENOKEY
286
+ {"ENOKEY", ENOKEY },
287
+ #endif
288
+ #ifdef ENOLCK
289
+ {"ENOLCK", ENOLCK },
290
+ #endif
291
+ #ifdef ENOLINK
292
+ {"ENOLINK", ENOLINK },
293
+ #endif
294
+ #ifdef ENOMEDIUM
295
+ {"ENOMEDIUM", ENOMEDIUM },
296
+ #endif
297
+ #ifdef ENOMEM
298
+ {"ENOMEM", ENOMEM },
299
+ #endif
300
+ #ifdef ENOMSG
301
+ {"ENOMSG", ENOMSG },
302
+ #endif
303
+ #ifdef ENONET
304
+ {"ENONET", ENONET },
305
+ #endif
306
+ #ifdef ENOPKG
307
+ {"ENOPKG", ENOPKG },
308
+ #endif
309
+ #ifdef ENOPROTOOPT
310
+ {"ENOPROTOOPT", ENOPROTOOPT },
311
+ #endif
312
+ #ifdef ENOSPC
313
+ {"ENOSPC", ENOSPC },
314
+ #endif
315
+ #ifdef ENOSR
316
+ {"ENOSR", ENOSR },
317
+ #endif
318
+ #ifdef ENOSTR
319
+ {"ENOSTR", ENOSTR },
320
+ #endif
321
+ #ifdef ENOSYS
322
+ {"ENOSYS", ENOSYS },
323
+ #endif
324
+ #ifdef ENOTBLK
325
+ {"ENOTBLK", ENOTBLK },
326
+ #endif
327
+ #ifdef ENOTCAPABLE
328
+ {"ENOTCAPABLE", ENOTCAPABLE },
329
+ #endif
330
+ #ifdef ENOTCONN
331
+ {"ENOTCONN", ENOTCONN },
332
+ #endif
333
+ #ifdef ENOTDIR
334
+ {"ENOTDIR", ENOTDIR },
335
+ #endif
336
+ #ifdef ENOTEMPTY
337
+ {"ENOTEMPTY", ENOTEMPTY },
338
+ #endif
339
+ #ifdef ENOTNAM
340
+ {"ENOTNAM", ENOTNAM },
341
+ #endif
342
+ #ifdef ENOTRECOVERABLE
343
+ {"ENOTRECOVERABLE", ENOTRECOVERABLE },
344
+ #endif
345
+ #ifdef ENOTSOCK
346
+ {"ENOTSOCK", ENOTSOCK },
347
+ #endif
348
+ #ifdef ENOTSUP
349
+ {"ENOTSUP", ENOTSUP },
350
+ #endif
351
+ #ifdef ENOTTY
352
+ {"ENOTTY", ENOTTY },
353
+ #endif
354
+ #ifdef ENOTUNIQ
355
+ {"ENOTUNIQ", ENOTUNIQ },
356
+ #endif
357
+ #ifdef ENXIO
358
+ {"ENXIO", ENXIO },
359
+ #endif
360
+ #ifdef EOPNOTSUPP
361
+ {"EOPNOTSUPP", EOPNOTSUPP },
362
+ #endif
363
+ #ifdef EOTHER
364
+ {"EOTHER", EOTHER },
365
+ #endif
366
+ #ifdef EOVERFLOW
367
+ {"EOVERFLOW", EOVERFLOW },
368
+ #endif
369
+ #ifdef EOWNERDEAD
370
+ {"EOWNERDEAD", EOWNERDEAD },
371
+ #endif
372
+ #ifdef EPERM
373
+ {"EPERM", EPERM },
374
+ #endif
375
+ #ifdef EPFNOSUPPORT
376
+ {"EPFNOSUPPORT", EPFNOSUPPORT },
377
+ #endif
378
+ #ifdef EPIPE
379
+ {"EPIPE", EPIPE },
380
+ #endif
381
+ #ifdef EPROCLIM
382
+ {"EPROCLIM", EPROCLIM },
383
+ #endif
384
+ #ifdef EPROCUNAVAIL
385
+ {"EPROCUNAVAIL", EPROCUNAVAIL },
386
+ #endif
387
+ #ifdef EPROGMISMATCH
388
+ {"EPROGMISMATCH", EPROGMISMATCH },
389
+ #endif
390
+ #ifdef EPROGUNAVAIL
391
+ {"EPROGUNAVAIL", EPROGUNAVAIL },
392
+ #endif
393
+ #ifdef EPROTO
394
+ {"EPROTO", EPROTO },
395
+ #endif
396
+ #ifdef EPROTONOSUPPORT
397
+ {"EPROTONOSUPPORT", EPROTONOSUPPORT },
398
+ #endif
399
+ #ifdef EPROTOTYPE
400
+ {"EPROTOTYPE", EPROTOTYPE },
401
+ #endif
402
+ #ifdef ERANGE
403
+ {"ERANGE", ERANGE },
404
+ #endif
405
+ #ifdef EREMCHG
406
+ {"EREMCHG", EREMCHG },
407
+ #endif
408
+ #ifdef EREMOTE
409
+ {"EREMOTE", EREMOTE },
410
+ #endif
411
+ #ifdef EREMOTEIO
412
+ {"EREMOTEIO", EREMOTEIO },
413
+ #endif
414
+ #ifdef ERESTART
415
+ {"ERESTART", ERESTART },
416
+ #endif
417
+ #ifdef ERFKILL
418
+ {"ERFKILL", ERFKILL },
419
+ #endif
420
+ #ifdef EROFS
421
+ {"EROFS", EROFS },
422
+ #endif
423
+ #ifdef ERPCMISMATCH
424
+ {"ERPCMISMATCH", ERPCMISMATCH },
425
+ #endif
426
+ #ifdef ESHUTDOWN
427
+ {"ESHUTDOWN", ESHUTDOWN },
428
+ #endif
429
+ #ifdef ESOCKTNOSUPPORT
430
+ {"ESOCKTNOSUPPORT", ESOCKTNOSUPPORT },
431
+ #endif
432
+ #ifdef ESPIPE
433
+ {"ESPIPE", ESPIPE },
434
+ #endif
435
+ #ifdef ESRCH
436
+ {"ESRCH", ESRCH },
437
+ #endif
438
+ #ifdef ESRMNT
439
+ {"ESRMNT", ESRMNT },
440
+ #endif
441
+ #ifdef ESTALE
442
+ {"ESTALE", ESTALE },
443
+ #endif
444
+ #ifdef ESTRPIPE
445
+ {"ESTRPIPE", ESTRPIPE },
446
+ #endif
447
+ #ifdef ETIME
448
+ {"ETIME", ETIME },
449
+ #endif
450
+ #ifdef ETIMEDOUT
451
+ {"ETIMEDOUT", ETIMEDOUT },
452
+ #endif
453
+ #ifdef ETOOMANYREFS
454
+ {"ETOOMANYREFS", ETOOMANYREFS },
455
+ #endif
456
+ #ifdef ETXTBSY
457
+ {"ETXTBSY", ETXTBSY },
458
+ #endif
459
+ #ifdef EUCLEAN
460
+ {"EUCLEAN", EUCLEAN },
461
+ #endif
462
+ #ifdef EUNATCH
463
+ {"EUNATCH", EUNATCH },
464
+ #endif
465
+ #ifdef EUSERS
466
+ {"EUSERS", EUSERS },
467
+ #endif
468
+ #ifdef EWOULDBLOCK
469
+ {"EWOULDBLOCK", EWOULDBLOCK },
470
+ #endif
471
+ #ifdef EXDEV
472
+ {"EXDEV", EXDEV },
473
+ #endif
474
+ #ifdef EXFULL
475
+ {"EXFULL", EXFULL },
476
+ #endif
477
+ #ifdef STRUNCATE
478
+ {"STRUNCATE", STRUNCATE },
479
+ #endif
480
+ };
@@ -27,6 +27,7 @@
27
27
  #ifdef _WIN32
28
28
  #include "win32.hh"
29
29
  #endif
30
+ #include "errno.inc"
30
31
 
31
32
  #ifdef _WIN32
32
33
  #ifndef NOMINMAX
@@ -722,19 +723,30 @@ static Napi::Value CreateDisposableType(const Napi::CallbackInfo &info)
722
723
  return WrapType(env, instance, type);
723
724
  }
724
725
 
726
+ static inline bool CheckExternalPointer(Napi::Env env, Napi::Value value)
727
+ {
728
+ InstanceData *instance = env.GetInstanceData<InstanceData>();
729
+
730
+ if (!value.IsExternal() || CheckValueTag(instance, value, &TypeInfoMarker) ||
731
+ CheckValueTag(instance, value, &CastMarker) ||
732
+ CheckValueTag(instance, value, &MagicUnionMarker)) {
733
+ ThrowError<Napi::TypeError>(env, "Unexpected %1 value for ptr, expected external pointer", GetValueType(instance, value));
734
+ return false;
735
+ }
736
+
737
+ return true;
738
+ }
739
+
725
740
  static Napi::Value CallFree(const Napi::CallbackInfo &info)
726
741
  {
727
742
  Napi::Env env = info.Env();
728
- InstanceData *instance = env.GetInstanceData<InstanceData>();
729
743
 
730
744
  if (info.Length() < 1) {
731
745
  ThrowError<Napi::TypeError>(env, "Expected 1 argument, got %1", info.Length());
732
746
  return env.Null();
733
747
  }
734
- if (!info[0].IsExternal() || CheckValueTag(instance, info[0], &TypeInfoMarker)) {
735
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value for ptr, expected external", GetValueType(instance, info[0]));
748
+ if (!CheckExternalPointer(env, info[0]))
736
749
  return env.Null();
737
- }
738
750
 
739
751
  Napi::External<void> external = info[0].As<Napi::External<void>>();
740
752
  void *ptr = external.Data();
@@ -744,6 +756,26 @@ static Napi::Value CallFree(const Napi::CallbackInfo &info)
744
756
  return env.Undefined();
745
757
  }
746
758
 
759
+ static Napi::Value GetOrSetErrNo(const Napi::CallbackInfo &info)
760
+ {
761
+ Napi::Env env = info.Env();
762
+ InstanceData *instance = env.GetInstanceData<InstanceData>();
763
+
764
+ if (info.Length() >= 1) {
765
+ Napi::Number value = info[0].As<Napi::Number>();
766
+
767
+ if (!value.IsNumber()) {
768
+ ThrowError<Napi::TypeError>(env, "Unexpected %1 value for errno, expected integer", GetValueType(instance, value));
769
+ return env.Null();
770
+ }
771
+
772
+ errno = value;
773
+ }
774
+
775
+ Napi::Number ret = Napi::Number::New(env, errno);
776
+ return ret;
777
+ }
778
+
747
779
  static Napi::Value CreateArrayType(const Napi::CallbackInfo &info)
748
780
  {
749
781
  Napi::Env env = info.Env();
@@ -1973,6 +2005,26 @@ static Napi::Value DecodeValue(const Napi::CallbackInfo &info)
1973
2005
  return ret;
1974
2006
  }
1975
2007
 
2008
+ static Napi::Value GetPointerAddress(const Napi::CallbackInfo &info)
2009
+ {
2010
+ Napi::Env env = info.Env();
2011
+
2012
+ if (info.Length() < 1) {
2013
+ ThrowError<Napi::TypeError>(env, "Expected 1 argument, got %1", info.Length());
2014
+ return env.Null();
2015
+ }
2016
+ if (!CheckExternalPointer(env, info[0]))
2017
+ return env.Null();
2018
+
2019
+ Napi::External<void> external = info[0].As<Napi::External<void>>();
2020
+ void *ptr = external.Data();
2021
+
2022
+ uint64_t ptr64 = (uint64_t)(uintptr_t)ptr;
2023
+ Napi::BigInt bigint = Napi::BigInt::New(env, ptr64);
2024
+
2025
+ return bigint;
2026
+ }
2027
+
1976
2028
  extern "C" void RelayCallback(Size idx, uint8_t *own_sp, uint8_t *caller_sp, BackRegisters *out_reg)
1977
2029
  {
1978
2030
  if (RG_LIKELY(exec_call)) {
@@ -2038,6 +2090,23 @@ static void SetExports(Napi::Env env, Func func)
2038
2090
 
2039
2091
  func("as", Napi::Function::New(env, CastValue));
2040
2092
  func("decode", Napi::Function::New(env, DecodeValue));
2093
+ func("address", Napi::Function::New(env, GetPointerAddress));
2094
+
2095
+ func("errno", Napi::Function::New(env, GetOrSetErrNo));
2096
+
2097
+ Napi::Object os = Napi::Object::New(env);
2098
+ func("os", os);
2099
+
2100
+ // Init constants mapping
2101
+ {
2102
+ Napi::Object codes = Napi::Object::New(env);
2103
+
2104
+ for (const ErrnoCodeInfo &info: ErrnoCodes) {
2105
+ codes.Set(info.name, Napi::Number::New(env, info.value));
2106
+ }
2107
+
2108
+ os.Set("errno", codes);
2109
+ }
2041
2110
 
2042
2111
  #if defined(_WIN32)
2043
2112
  func("extension", Napi::String::New(env, ".dll"));