koffi 2.16.1 → 3.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/CHANGELOG.md +2 -2
  2. package/README.md +8 -8
  3. package/build/koffi/darwin_arm64/koffi.node +0 -0
  4. package/build/koffi/darwin_x64/koffi.node +0 -0
  5. package/build/koffi/freebsd_arm64/koffi.node +0 -0
  6. package/build/koffi/freebsd_ia32/koffi.node +0 -0
  7. package/build/koffi/freebsd_x64/koffi.node +0 -0
  8. package/build/koffi/linux_arm64/koffi.node +0 -0
  9. package/build/koffi/linux_ia32/koffi.node +0 -0
  10. package/build/koffi/linux_x64/koffi.node +0 -0
  11. package/build/koffi/musl_arm64/koffi.node +0 -0
  12. package/build/koffi/musl_x64/koffi.node +0 -0
  13. package/build/koffi/openbsd_ia32/koffi.node +0 -0
  14. package/build/koffi/openbsd_x64/koffi.node +0 -0
  15. package/build/koffi/win32_ia32/koffi.node +0 -0
  16. package/build/koffi/win32_x64/koffi.node +0 -0
  17. package/doc/benchmarks.md +33 -33
  18. package/doc/benchmarks.xlsx +0 -0
  19. package/doc/contribute.md +37 -25
  20. package/doc/index.md +8 -8
  21. package/doc/variables.md +1 -1
  22. package/index.d.ts +11 -9
  23. package/index.js +1 -634
  24. package/indirect.js +1 -533
  25. package/lib/native/base/base.cc +63 -85
  26. package/lib/native/base/base.hh +178 -151
  27. package/package.json +3 -4
  28. package/src/build.js +1178 -0
  29. package/src/koffi/CMakeLists.txt +97 -34
  30. package/src/koffi/index.js +359 -0
  31. package/src/koffi/indirect.js +216 -0
  32. package/src/koffi/src/{abi_arm32.cc → abi/arm32.cc} +279 -275
  33. package/src/koffi/src/{abi_arm32_asm.S → abi/arm32_asm.S} +2 -5
  34. package/src/koffi/src/abi/arm64.cc +1511 -0
  35. package/src/koffi/src/{abi_arm64_asm.S → abi/arm64_asm.S} +44 -23
  36. package/src/koffi/src/{abi_arm64_asm.asm → abi/arm64_asm.asm} +32 -25
  37. package/src/koffi/src/{abi_loong64_asm.S → abi/loong64_asm.S} +1 -5
  38. package/src/koffi/src/{abi_riscv64.cc → abi/riscv64.cc} +279 -273
  39. package/src/koffi/src/{abi_riscv64_asm.S → abi/riscv64_asm.S} +1 -4
  40. package/src/koffi/src/abi/x64sysv.cc +1456 -0
  41. package/src/koffi/src/{abi_x64_sysv_asm.S → abi/x64sysv_asm.S} +30 -33
  42. package/src/koffi/src/abi/x64win.cc +1121 -0
  43. package/src/koffi/src/{abi_x64_win_asm.S → abi/x64win_asm.S} +8 -12
  44. package/src/koffi/src/{abi_x64_win_asm.asm → abi/x64win_asm.asm} +11 -14
  45. package/src/koffi/src/abi/x86.cc +1233 -0
  46. package/src/koffi/src/{abi_x86_asm.S → abi/x86_asm.S} +49 -25
  47. package/src/koffi/src/{abi_x86_asm.asm → abi/x86_asm.asm} +11 -14
  48. package/src/koffi/src/call.cc +458 -598
  49. package/src/koffi/src/call.hh +61 -82
  50. package/src/koffi/src/ffi.cc +595 -309
  51. package/src/koffi/src/ffi.hh +103 -89
  52. package/src/koffi/src/parser.cc +0 -4
  53. package/src/koffi/src/primitives.inc +39 -0
  54. package/src/koffi/{tools/write_trampolines.js → src/trampolines.js} +1 -1
  55. package/src/koffi/src/util.cc +503 -446
  56. package/src/koffi/src/util.hh +269 -57
  57. package/src/koffi/src/uv.cc +2 -2
  58. package/src/koffi/src/uv.def +3 -0
  59. package/src/koffi/src/win32.hh +30 -1
  60. package/vendor/node-addon-api/napi-inl.h +4 -0
  61. package/vendor/node-addon-api/napi.h +1 -0
  62. package/build/koffi/linux_armhf/koffi.node +0 -0
  63. package/build/koffi/linux_loong64/koffi.node +0 -0
  64. package/build/koffi/linux_riscv64d/koffi.node +0 -0
  65. package/build/koffi/win32_arm64/koffi.exp +0 -0
  66. package/build/koffi/win32_arm64/koffi.lib +0 -0
  67. package/build/koffi/win32_arm64/koffi.node +0 -0
  68. package/doc/platforms.md +0 -36
  69. package/src/cnoke/LICENSE.txt +0 -22
  70. package/src/cnoke/README.md +0 -99
  71. package/src/cnoke/assets/FindCNoke.cmake +0 -127
  72. package/src/cnoke/assets/toolchains.json +0 -126
  73. package/src/cnoke/assets/win_delay_hook.c +0 -36
  74. package/src/cnoke/cnoke.js +0 -170
  75. package/src/cnoke/package.json +0 -24
  76. package/src/cnoke/src/builder.js +0 -511
  77. package/src/cnoke/src/index.js +0 -10
  78. package/src/cnoke/src/tools.js +0 -407
  79. package/src/koffi/src/abi_arm64.cc +0 -1295
  80. package/src/koffi/src/abi_x64_sysv.cc +0 -939
  81. package/src/koffi/src/abi_x64_win.cc +0 -715
  82. package/src/koffi/src/abi_x86.cc +0 -860
  83. package/src/koffi/src/init.js +0 -105
  84. /package/src/koffi/src/{abi_loong64.cc → abi/loong64.cc} +0 -0
@@ -1,7 +1,7 @@
1
1
  # SPDX-License-Identifier: MIT
2
2
  # SPDX-FileCopyrightText: 2025 Niels Martignène <niels.martignene@protonmail.com>
3
3
 
4
- cmake_minimum_required(VERSION 3.10)
4
+ cmake_minimum_required(VERSION 3.11)
5
5
  cmake_policy(SET CMP0091 NEW)
6
6
 
7
7
  if(NOT NODE_JS_INCLUDE_DIRS)
@@ -23,7 +23,7 @@ if(MSVC)
23
23
  enable_language(ASM_MASM)
24
24
  endif()
25
25
  else()
26
- set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wno-missing-field-initializers -Wno-unused-parameter -Wno-class-memaccess -Wswitch -Werror=switch")
26
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wno-missing-field-initializers -Wno-unused-parameter -Wno-class-memaccess -Wno-c99-designator -Wswitch -Werror=switch")
27
27
 
28
28
  if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
29
29
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unknown-warning-option")
@@ -60,8 +60,8 @@ add_custom_command(OUTPUT trampolines/gnu.inc
60
60
  trampolines/masm64.inc
61
61
  COMMENT "Generate trampoline macros"
62
62
  COMMAND "${NODE_JS_EXECPATH}"
63
- ARGS "${CMAKE_CURRENT_SOURCE_DIR}/tools/write_trampolines.js" "${CMAKE_CURRENT_BINARY_DIR}/trampolines" 8192
64
- DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/tools/write_trampolines.js")
63
+ ARGS "${CMAKE_CURRENT_SOURCE_DIR}/src/trampolines.js" "${CMAKE_CURRENT_BINARY_DIR}/trampolines" 8192
64
+ DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/trampolines.js")
65
65
 
66
66
  set(KOFFI_SRC
67
67
  src/call.cc
@@ -75,8 +75,8 @@ set(KOFFI_SRC
75
75
  if(CMAKE_SIZEOF_VOID_P EQUAL 8)
76
76
  # CMAKE_SYSTEM_PROCESSOR is wrong on Windows ARM64
77
77
 
78
- if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch|arm" OR CMAKE_GENERATOR_PLATFORM STREQUAL "ARM64" OR CMAKE_OSX_ARCHITECTURES MATCHES "arm")
79
- if(WIN32)
78
+ if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch|arm|ARM|AARCH" OR CMAKE_GENERATOR_PLATFORM STREQUAL "ARM64" OR CMAKE_OSX_ARCHITECTURES MATCHES "arm")
79
+ if(MSVC)
80
80
  get_filename_component(cl_dir "${CMAKE_CXX_COMPILER}" DIRECTORY)
81
81
  file(TO_CMAKE_PATH "${cl_dir}/armasm64.exe" asm_compiler)
82
82
 
@@ -87,8 +87,8 @@ if(CMAKE_SIZEOF_VOID_P EQUAL 8)
87
87
 
88
88
  message(STATUS "Using ARMASM64 compiler: ${asm_compiler}")
89
89
 
90
- file(TO_CMAKE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/src/abi_arm64_asm.asm" asm_source)
91
- file(TO_CMAKE_PATH "${CMAKE_CURRENT_BINARY_DIR}/abi_arm64_asm.obj" asm_object)
90
+ file(TO_CMAKE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/src/abi/arm64_asm.asm" asm_source)
91
+ file(TO_CMAKE_PATH "${CMAKE_CURRENT_BINARY_DIR}/arm64_asm.obj" asm_object)
92
92
 
93
93
  add_custom_command(
94
94
  OUTPUT "${asm_object}"
@@ -99,33 +99,33 @@ if(CMAKE_SIZEOF_VOID_P EQUAL 8)
99
99
  )
100
100
  set_source_files_properties("${asm_object}" PROPERTIES EXTERNAL_OBJECT TRUE)
101
101
 
102
- list(APPEND KOFFI_SRC src/abi_arm64.cc "${asm_object}")
102
+ list(APPEND KOFFI_SRC src/abi/arm64.cc "${asm_object}")
103
103
  else()
104
- list(APPEND KOFFI_SRC src/abi_arm64.cc src/abi_arm64_asm.S)
104
+ list(APPEND KOFFI_SRC src/abi/arm64.cc src/abi/arm64_asm.S)
105
105
  endif()
106
106
  elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "riscv")
107
- list(APPEND KOFFI_SRC src/abi_riscv64.cc src/abi_riscv64_asm.S)
107
+ list(APPEND KOFFI_SRC src/abi/riscv64.cc src/abi/riscv64_asm.S)
108
108
  elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "loongarch64")
109
- list(APPEND KOFFI_SRC src/abi_riscv64.cc src/abi_loong64_asm.S)
109
+ list(APPEND KOFFI_SRC src/abi/riscv64.cc src/abi/loong64_asm.S)
110
110
  else()
111
111
  if(WIN32)
112
112
  if(MSVC)
113
- list(APPEND KOFFI_SRC src/abi_x64_win.cc src/abi_x64_win_asm.asm)
113
+ list(APPEND KOFFI_SRC src/abi/x64win.cc src/abi/x64win_asm.asm)
114
114
  else()
115
- list(APPEND KOFFI_SRC src/abi_x64_win.cc src/abi_x64_win_asm.S)
115
+ list(APPEND KOFFI_SRC src/abi/x64win.cc src/abi/x64win_asm.S)
116
116
  endif()
117
117
  else()
118
- list(APPEND KOFFI_SRC src/abi_x64_sysv.cc src/abi_x64_sysv_asm.S)
118
+ list(APPEND KOFFI_SRC src/abi/x64sysv.cc src/abi/x64sysv_asm.S)
119
119
  endif()
120
120
  endif()
121
121
  elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
122
122
  if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch|arm")
123
- list(APPEND KOFFI_SRC src/abi_arm32.cc src/abi_arm32_asm.S)
123
+ list(APPEND KOFFI_SRC src/abi/arm32.cc src/abi/arm32_asm.S)
124
124
  else()
125
- if(WIN32)
126
- list(APPEND KOFFI_SRC src/abi_x86.cc src/abi_x86_asm.asm)
125
+ if(MSVC)
126
+ list(APPEND KOFFI_SRC src/abi/x86.cc src/abi/x86_asm.asm)
127
127
  else()
128
- list(APPEND KOFFI_SRC src/abi_x86.cc src/abi_x86_asm.S)
128
+ list(APPEND KOFFI_SRC src/abi/x86.cc src/abi/x86_asm.S)
129
129
  endif()
130
130
  endif()
131
131
  endif()
@@ -134,24 +134,13 @@ add_node_addon(NAME koffi SOURCES ${KOFFI_SRC} trampolines/gnu.inc)
134
134
  target_include_directories(koffi PRIVATE . ../.. ../../vendor/node-addon-api "${CMAKE_CURRENT_BINARY_DIR}/trampolines")
135
135
 
136
136
  if(WIN32)
137
+ create_import_lib(uv.lib src/uv.def)
137
138
  set(UV_LINK_LIB "${CMAKE_CURRENT_BINARY_DIR}/uv.lib")
138
- if(MSVC)
139
- add_custom_command(OUTPUT uv.lib
140
- COMMAND ${CMAKE_AR} ${CMAKE_STATIC_LINKER_FLAGS}
141
- /def:src/uv.def /out:${UV_LINK_LIB}
142
- WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
143
- MAIN_DEPENDENCY src/uv.def)
144
- else()
145
- add_custom_command(OUTPUT uv.lib
146
- COMMAND ${CMAKE_DLLTOOL} -d src/uv.def -l ${UV_LINK_LIB}
147
- WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
148
- MAIN_DEPENDENCY src/uv.def)
149
- endif()
150
139
  target_sources(koffi PRIVATE uv.lib)
151
140
  target_link_libraries(koffi PRIVATE ${UV_LINK_LIB})
152
141
  endif()
153
142
 
154
- target_compile_definitions(koffi PRIVATE FELIX_TARGET=koffi NAPI_DISABLE_CPP_EXCEPTIONS NAPI_VERSION=8 CORE_NO_STATX)
143
+ target_compile_definitions(koffi PRIVATE FELIX_TARGET=koffi NAPI_DISABLE_CPP_EXCEPTIONS NAPI_VERSION=NAPI_VERSION_EXPERIMENTAL CORE_NO_STATX)
155
144
  if(WIN32)
156
145
  target_compile_definitions(koffi PRIVATE _CRT_SECURE_NO_WARNINGS _CRT_NONSTDC_NO_DEPRECATE)
157
146
  target_link_libraries(koffi PRIVATE ws2_32)
@@ -162,11 +151,28 @@ if(UNIX AND NOT APPLE)
162
151
  target_link_options(koffi PRIVATE -Wl,--gc-sections)
163
152
  endif()
164
153
 
154
+ # ---- Optimization ----
155
+
156
+ option(PGO_GENERATE "Build with PGO profile generation" "")
157
+ option(PGO_USE "Optimize with PGO profile" "")
158
+
159
+ if(MSVC AND (CMAKE_BUILD_TYPE STREQUAL "Release" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo"))
160
+ target_compile_options(koffi PRIVATE $<$<COMPILE_LANGUAGE:CXX>:/GS- /Ob2>)
161
+
162
+ if(CMAKE_C_COMPILER_ID MATCHES "[Cc]lang")
163
+ set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /clang:-O3")
164
+ endif()
165
+
166
+ if(CMAKE_BUILD_TYPE STREQUAL "Release")
167
+ target_compile_options(koffi PRIVATE $<$<COMPILE_LANGUAGE:CXX>:/Oy->)
168
+ endif()
169
+ endif()
170
+
165
171
  if(NOT MSVC OR CMAKE_C_COMPILER_ID MATCHES "[Cc]lang")
166
172
  # Restore C/C++ compiler sanity
167
173
 
168
174
  if(NOT MSVC)
169
- target_compile_options(koffi PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions -fno-strict-aliasing -fwrapv
175
+ target_compile_options(koffi PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-fno-strict-aliasing -fwrapv
170
176
  -fno-delete-null-pointer-checks>)
171
177
  else()
172
178
  target_compile_options(koffi PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-fno-strict-aliasing /clang:-fwrapv
@@ -177,6 +183,63 @@ if(NOT MSVC OR CMAKE_C_COMPILER_ID MATCHES "[Cc]lang")
177
183
  if(use_no_finite_loops)
178
184
  target_compile_options(koffi PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-fno-finite-loops>)
179
185
  endif()
186
+
187
+ if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
188
+ target_compile_options(koffi PRIVATE -fno-reorder-blocks-and-partition -Wno-maybe-musttail-local-addr)
189
+ endif()
190
+ endif()
191
+
192
+ if(PGO_GENERATE)
193
+ set_target_properties(koffi PROPERTIES INTERPROCEDURAL_OPTIMIZATION ON)
194
+ target_compile_options(koffi PRIVATE "-fprofile-generate=${PGO_GENERATE}")
195
+ target_link_options(koffi PRIVATE "-fprofile-generate=${PGO_GENERATE}")
196
+
197
+ message(STATUS "PGO: Building instrumented binary")
198
+ elseif(PGO_PROFILE)
199
+ if(NOT EXISTS "${PGO_PROFILE}")
200
+ message(FATAL_ERROR "Profile data not found at '${PGO_PROFILE}'")
201
+ endif()
202
+
203
+ set_target_properties(koffi PROPERTIES INTERPROCEDURAL_OPTIMIZATION ON)
204
+ target_compile_options(koffi PRIVATE "-fprofile-use=${PGO_PROFILE}")
205
+ target_link_options(koffi PRIVATE "-fprofile-use=${PGO_PROFILE}")
206
+
207
+ if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
208
+ target_compile_options(koffi PRIVATE -fprofile-correction)
209
+ endif()
210
+
211
+ message(STATUS "PGO: Building optimized binary with profile data")
212
+ else()
213
+ enable_unity_build(koffi EXCLUDE src/uv.cc)
180
214
  endif()
181
215
 
182
- enable_unity_build(koffi EXCLUDE src/uv.cc)
216
+ if(APPLE)
217
+ add_custom_command(
218
+ TARGET koffi POST_BUILD
219
+ DEPENDS koffi
220
+ COMMAND $<$<CONFIG:release>:${CMAKE_STRIP}>
221
+ ARGS -x $<TARGET_FILE:koffi>
222
+ )
223
+ elseif(UNIX)
224
+ add_custom_command(
225
+ TARGET koffi POST_BUILD
226
+ DEPENDS koffi
227
+ COMMAND $<$<CONFIG:release>:${CMAKE_STRIP}>
228
+ ARGS $<TARGET_FILE:koffi>
229
+ )
230
+ endif()
231
+
232
+ # ---- Debug ----
233
+
234
+ option(ASAN "Build with ASan" OFF)
235
+ option(UBSAN "Build with UBSan" OFF)
236
+
237
+ if(ASAN)
238
+ target_compile_options(koffi PRIVATE -fsanitize=address)
239
+ target_link_options(koffi BEFORE PRIVATE -fsanitize=address)
240
+ endif()
241
+
242
+ if(UBSAN)
243
+ target_compile_options(koffi PRIVATE -fsanitize=undefined)
244
+ target_link_options(koffi BEFORE PRIVATE -fsanitize=undefined)
245
+ endif()
@@ -0,0 +1,359 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __esm = (fn, res) => function __init() {
9
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
10
+ };
11
+ var __commonJS = (cb, mod3) => function __require() {
12
+ return mod3 || (0, cb[__getOwnPropNames(cb)[0]])((mod3 = { exports: {} }).exports, mod3), mod3.exports;
13
+ };
14
+ var __export = (target, all) => {
15
+ for (var name in all)
16
+ __defProp(target, name, { get: all[name], enumerable: true });
17
+ };
18
+ var __copyProps = (to, from, except, desc) => {
19
+ if (from && typeof from === "object" || typeof from === "function") {
20
+ for (let key of __getOwnPropNames(from))
21
+ if (!__hasOwnProp.call(to, key) && key !== except)
22
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
23
+ }
24
+ return to;
25
+ };
26
+ var __toESM = (mod3, isNodeMode, target) => (target = mod3 != null ? __create(__getProtoOf(mod3)) : {}, __copyProps(
27
+ // If the importer is in node compatibility mode or this is not an ESM
28
+ // file that has been converted to a CommonJS file using a Babel-
29
+ // compatible transform (i.e. "__esModule" has not been set), then set
30
+ // "default" to the CommonJS "module.exports" for node compatibility.
31
+ isNodeMode || !mod3 || !mod3.__esModule ? __defProp(target, "default", { value: mod3, enumerable: true }) : target,
32
+ mod3
33
+ ));
34
+ var __toCommonJS = (mod3) => __copyProps(__defProp({}, "__esModule", { value: true }), mod3);
35
+
36
+ // ../cnoke/src/abi.js
37
+ var abi_exports = {};
38
+ __export(abi_exports, {
39
+ determineAbi: () => determineAbi2
40
+ });
41
+ function determineAbi2() {
42
+ let abi = process.arch;
43
+ if (abi == "riscv32" || abi == "riscv64") {
44
+ let buf = readFileHeader(process.execPath, 512);
45
+ let header = decodeElfHeader(buf);
46
+ let float_abi = header.e_flags & 6;
47
+ switch (float_abi) {
48
+ case 0:
49
+ {
50
+ }
51
+ break;
52
+ case 2:
53
+ {
54
+ abi += "f";
55
+ }
56
+ break;
57
+ case 4:
58
+ {
59
+ abi += "d";
60
+ }
61
+ break;
62
+ case 6:
63
+ {
64
+ abi += "q";
65
+ }
66
+ break;
67
+ }
68
+ } else if (abi == "arm") {
69
+ let buf = readFileHeader(process.execPath, 512);
70
+ let header = decodeElfHeader(buf);
71
+ if (header.e_flags & 1024) {
72
+ abi += "hf";
73
+ } else if (header.e_flags & 512) {
74
+ abi += "sf";
75
+ } else {
76
+ throw new Error("Unknown ARM floating-point ABI");
77
+ }
78
+ }
79
+ return abi;
80
+ }
81
+ function readFileHeader(filename2, read) {
82
+ let fd = null;
83
+ try {
84
+ let fd2 = import_fs.default.openSync(filename2);
85
+ let buf = Buffer.allocUnsafe(read);
86
+ let len = import_fs.default.readSync(fd2, buf);
87
+ return buf.subarray(0, len);
88
+ } finally {
89
+ if (fd != null)
90
+ import_fs.default.closeSync(fd);
91
+ }
92
+ }
93
+ function decodeElfHeader(buf) {
94
+ let header = {};
95
+ if (buf.length < 16)
96
+ throw new Error("Truncated header");
97
+ if (buf[0] != 127 || buf[1] != 69 || buf[2] != 76 || buf[3] != 70)
98
+ throw new Error("Invalid magic number");
99
+ if (buf[6] != 1)
100
+ throw new Error("Invalid ELF version");
101
+ if (buf[5] != 1)
102
+ throw new Error("Big-endian architectures are not supported");
103
+ header.e_machine = buf.readUInt16LE(18);
104
+ switch (buf[4]) {
105
+ case 1:
106
+ {
107
+ buf = buf.subarray(0, 68);
108
+ if (buf.length < 68)
109
+ throw new Error("Truncated ELF header");
110
+ header.ei_class = 32;
111
+ header.e_flags = buf.readUInt32LE(36);
112
+ }
113
+ break;
114
+ case 2:
115
+ {
116
+ buf = buf.subarray(0, 120);
117
+ if (buf.length < 120)
118
+ throw new Error("Truncated ELF header");
119
+ header.ei_class = 64;
120
+ header.e_flags = buf.readUInt32LE(48);
121
+ }
122
+ break;
123
+ default:
124
+ throw new Error("Invalid ELF class");
125
+ }
126
+ return header;
127
+ }
128
+ var import_fs;
129
+ var init_abi = __esm({
130
+ "../cnoke/src/abi.js"() {
131
+ import_fs = __toESM(require("fs"), 1);
132
+ }
133
+ });
134
+
135
+ // package.json
136
+ var require_package = __commonJS({
137
+ "package.json"(exports2, module2) {
138
+ module2.exports = {
139
+ name: "koffi",
140
+ version: "3.0.0-alpha.2",
141
+ description: "Fast and simple C FFI (foreign function interface) for Node.js",
142
+ keywords: [
143
+ "foreign",
144
+ "function",
145
+ "interface",
146
+ "ffi",
147
+ "binding",
148
+ "c",
149
+ "napi"
150
+ ],
151
+ repository: {
152
+ type: "git",
153
+ url: "https://github.com/Koromix/koffi"
154
+ },
155
+ homepage: "https://koffi.dev/",
156
+ author: {
157
+ name: "Niels Martign\xE8ne",
158
+ email: "niels.martignene@protonmail.com",
159
+ url: "https://koromix.dev/"
160
+ },
161
+ main: "./index.js",
162
+ types: "./index.d.ts",
163
+ scripts: {
164
+ test: "node tools/brew.js test",
165
+ prepack: `echo 'Use "npm run package" instead' && false`,
166
+ prepublishOnly: `echo 'Use "npm run package" instead' && false`,
167
+ package: "node tools/brew.js build"
168
+ },
169
+ license: "MIT",
170
+ cnoke: {
171
+ api: "../../vendor/node-api-headers",
172
+ output: "../../bin/Koffi/{{ toolchain }}",
173
+ node: 16,
174
+ napi: 8
175
+ },
176
+ funding: "https://liberapay.com/Koromix"
177
+ };
178
+ }
179
+ });
180
+
181
+ // src/init.js
182
+ var require_init = __commonJS({
183
+ "src/init.js"(exports, module) {
184
+ "use strict";
185
+ var fs = require("fs");
186
+ var path = require("path");
187
+ var util = require("util");
188
+ var { determineAbi } = (init_abi(), __toCommonJS(abi_exports));
189
+ var PACKAGE = require_package();
190
+ function detect() {
191
+ if (process.versions.napi == null || process.versions.napi < PACKAGE.cnoke.napi) {
192
+ let major = parseInt(process.versions.node, 10);
193
+ throw new Error(`This engine is based on Node ${process.versions.node}, but ${PACKAGE.name} does not support the Node ${major}.x branch (N-API < ${PACKAGE.cnoke.napi})`);
194
+ }
195
+ let abi = determineAbi();
196
+ let pkg3 = `${process.platform}-${process.arch}`;
197
+ let triplets3 = [`${process.platform}_${abi}`];
198
+ if (process.platform == "linux")
199
+ triplets3.push(`musl_${abi}`);
200
+ return [pkg3, triplets3];
201
+ }
202
+ function init(root, pkg, triplets, native) {
203
+ let err = null;
204
+ if (native == null) {
205
+ let roots = [root];
206
+ if (process.resourcesPath != null)
207
+ roots.push(process.resourcesPath);
208
+ let filenames = roots.flatMap((dir) => triplets.flatMap((triplet) => [
209
+ `${dir}/build/koffi/${triplet}/koffi.node`,
210
+ `${dir}/bin/Koffi/${triplet}/koffi.node`,
211
+ `${dir}/node_modules/koffi/build/koffi/${triplet}/koffi.node`,
212
+ `${dir}/../@koromix/koffi-${pkg}/${triplet}/koffi.node`
213
+ ]));
214
+ for (let filename of filenames) {
215
+ if (!fs.existsSync(filename))
216
+ continue;
217
+ try {
218
+ native = eval("require")(filename);
219
+ break;
220
+ } catch (e) {
221
+ err ??= e;
222
+ }
223
+ }
224
+ }
225
+ if (native == null) {
226
+ err ??= new Error("Cannot find the native Koffi module; did you bundle it correctly?");
227
+ throw err;
228
+ }
229
+ if (native.version != PACKAGE.version)
230
+ throw new Error("Mismatched native Koffi modules");
231
+ let mod = wrap(native);
232
+ return mod;
233
+ }
234
+ function wrap(native3) {
235
+ let obj = {
236
+ ...native3,
237
+ // Deprecated functions
238
+ handle: util.deprecate(native3.opaque, "The koffi.handle() function was deprecated in Koffi 2.1, use koffi.opaque() instead", "KOFFI001"),
239
+ callback: util.deprecate(native3.proto, "The koffi.callback() function was deprecated in Koffi 2.4, use koffi.proto() instead", "KOFFI002")
240
+ };
241
+ obj.load = (...args) => {
242
+ let lib = native3.load(...args);
243
+ lib.cdecl = util.deprecate((...args2) => lib.func("__cdecl", ...args2), "The koffi.cdecl() function was deprecated in Koffi 2.7, use koffi.func(...) instead", "KOFFI003");
244
+ lib.stdcall = util.deprecate((...args2) => lib.func("__stdcall", ...args2), 'The koffi.stdcall() function was deprecated in Koffi 2.7, use koffi.func("__stdcall", ...) instead', "KOFFI004");
245
+ lib.fastcall = util.deprecate((...args2) => lib.func("__fastcall", ...args2), 'The koffi.fastcall() function was deprecated in Koffi 2.7, use koffi.func("__fastcall", ...) instead', "KOFFI005");
246
+ lib.thiscall = util.deprecate((...args2) => lib.func("__thiscall", ...args2), 'The koffi.thiscall() function was deprecated in Koffi 2.7, use koffi.func("__thiscall", ...) instead', "KOFFI006");
247
+ return lib;
248
+ };
249
+ return obj;
250
+ }
251
+ module.exports = {
252
+ detect,
253
+ init
254
+ };
255
+ }
256
+ });
257
+
258
+ // src/static.js
259
+ var require_static = __commonJS({
260
+ "src/static.js"(exports2, module2) {
261
+ function find2(pkg3) {
262
+ let native3 = null;
263
+ if (native3 == null && pkg3 == "linux-arm64") {
264
+ try {
265
+ native3 = require("../../build/koffi/linux_arm64/koffi.node");
266
+ } catch (err2) {
267
+ }
268
+ }
269
+ if (native3 == null && pkg3 == "linux-arm64") {
270
+ try {
271
+ native3 = require("../../build/koffi/musl_arm64/koffi.node");
272
+ } catch (err2) {
273
+ }
274
+ }
275
+ if (native3 == null && pkg3 == "linux-ia32") {
276
+ try {
277
+ native3 = require("../../build/koffi/linux_ia32/koffi.node");
278
+ } catch (err2) {
279
+ }
280
+ }
281
+ if (native3 == null && pkg3 == "linux-x64") {
282
+ try {
283
+ native3 = require("../../build/koffi/linux_x64/koffi.node");
284
+ } catch (err2) {
285
+ }
286
+ }
287
+ if (native3 == null && pkg3 == "linux-x64") {
288
+ try {
289
+ native3 = require("../../build/koffi/musl_x64/koffi.node");
290
+ } catch (err2) {
291
+ }
292
+ }
293
+ if (native3 == null && pkg3 == "freebsd-ia32") {
294
+ try {
295
+ native3 = require("../../build/koffi/freebsd_ia32/koffi.node");
296
+ } catch (err2) {
297
+ }
298
+ }
299
+ if (native3 == null && pkg3 == "freebsd-x64") {
300
+ try {
301
+ native3 = require("../../build/koffi/freebsd_x64/koffi.node");
302
+ } catch (err2) {
303
+ }
304
+ }
305
+ if (native3 == null && pkg3 == "freebsd-arm64") {
306
+ try {
307
+ native3 = require("../../build/koffi/freebsd_arm64/koffi.node");
308
+ } catch (err2) {
309
+ }
310
+ }
311
+ if (native3 == null && pkg3 == "openbsd-ia32") {
312
+ try {
313
+ native3 = require("../../build/koffi/openbsd_ia32/koffi.node");
314
+ } catch (err2) {
315
+ }
316
+ }
317
+ if (native3 == null && pkg3 == "openbsd-x64") {
318
+ try {
319
+ native3 = require("../../build/koffi/openbsd_x64/koffi.node");
320
+ } catch (err2) {
321
+ }
322
+ }
323
+ if (native3 == null && pkg3 == "win32-ia32") {
324
+ try {
325
+ native3 = require("../../build/koffi/win32_ia32/koffi.node");
326
+ } catch (err2) {
327
+ }
328
+ }
329
+ if (native3 == null && pkg3 == "win32-x64") {
330
+ try {
331
+ native3 = require("../../build/koffi/win32_x64/koffi.node");
332
+ } catch (err2) {
333
+ }
334
+ }
335
+ if (native3 == null && pkg3 == "darwin-x64") {
336
+ try {
337
+ native3 = require("../../build/koffi/darwin_x64/koffi.node");
338
+ } catch (err2) {
339
+ }
340
+ }
341
+ if (native3 == null && pkg3 == "darwin-arm64") {
342
+ try {
343
+ native3 = require("../../build/koffi/darwin_arm64/koffi.node");
344
+ } catch (err2) {
345
+ }
346
+ }
347
+ return native3;
348
+ }
349
+ module2.exports = { find: find2 };
350
+ }
351
+ });
352
+
353
+ // index.js
354
+ var { detect: detect2, init: init2 } = require_init();
355
+ var { find } = require_static();
356
+ var [pkg2, triplets2] = detect2();
357
+ var native2 = find(pkg2);
358
+ var mod2 = init2(__dirname + "/../..", pkg2, triplets2, native2);
359
+ module.exports = mod2;