node-ctypes 2.2.0 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +71 -13
- package/build/Release/ctypes-darwin-arm64.node +0 -0
- package/build/Release/ctypes-darwin-x64.node +0 -0
- package/build/Release/ctypes-linux-arm64-musl.node +0 -0
- package/build/Release/ctypes-linux-arm64.node +0 -0
- package/build/Release/ctypes-linux-x64-musl.node +0 -0
- package/build/Release/ctypes-linux-x64.node +0 -0
- package/build/Release/ctypes-win32-arm64.node +0 -0
- package/build/Release/ctypes-win32-x64.node +0 -0
- package/lib/core/Library.js +86 -76
- package/lib/core/callback.js +10 -17
- package/lib/core/conversions.js +172 -4
- package/lib/core/funcptr.js +63 -46
- package/lib/core/lazy-loader.js +10 -4
- package/lib/core/types.js +196 -98
- package/lib/index.d.ts +163 -35
- package/lib/index.js +36 -26
- package/lib/memory/buffer.js +45 -16
- package/lib/memory/operations.js +55 -43
- package/lib/memory/pointer.js +135 -26
- package/lib/platform/constants.js +0 -1
- package/lib/platform/errors.js +48 -4
- package/lib/platform/find_library.js +78 -19
- package/lib/structures/Structure.js +258 -178
- package/lib/structures/Union.js +10 -21
- package/lib/structures/helpers/array.js +71 -4
- package/lib/structures/helpers/bitfield.js +66 -150
- package/lib/structures/helpers/common.js +115 -15
- package/lib/structures/helpers/layout.js +131 -0
- package/lib/structures/helpers/struct.js +142 -400
- package/lib/structures/helpers/union.js +130 -141
- package/lib/structures/helpers/wrap.js +3 -15
- package/lib/types/primitives.js +130 -101
- package/package.json +2 -1
|
@@ -18,7 +18,8 @@
|
|
|
18
18
|
*/
|
|
19
19
|
|
|
20
20
|
import { execSync } from "node:child_process";
|
|
21
|
-
import { existsSync } from "node:fs";
|
|
21
|
+
import { existsSync, readdirSync, statSync } from "node:fs";
|
|
22
|
+
import os from "node:os";
|
|
22
23
|
import path from "node:path";
|
|
23
24
|
|
|
24
25
|
// Windows has no canonical mapping for generic library names (CPython returns
|
|
@@ -43,37 +44,94 @@ function findLibraryWindows(name) {
|
|
|
43
44
|
return `${name}.dll`;
|
|
44
45
|
}
|
|
45
46
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
47
|
+
// Search order of ctypes.macholib.dyld.dyld_find: DYLD_* paths, then the
|
|
48
|
+
// fallback directories of dyld, with Homebrew first as in Homebrew's Python
|
|
49
|
+
// (Apple's /usr/lib/libssl.dylib and libcrypto.dylib abort the process).
|
|
50
|
+
function findLibraryDarwin(name, inSharedCache) {
|
|
51
|
+
const home = os.homedir();
|
|
52
|
+
const envDirs = (variable) => (process.env[variable] ?? "").split(":").filter(Boolean);
|
|
53
|
+
const libDirs = [...envDirs("DYLD_LIBRARY_PATH"), ...envDirs("DYLD_FALLBACK_LIBRARY_PATH"), "/opt/homebrew/lib", path.join(home, "lib"), "/usr/local/lib", "/lib", "/usr/lib"];
|
|
54
|
+
const frameworkDirs = [
|
|
55
|
+
...envDirs("DYLD_FRAMEWORK_PATH"),
|
|
56
|
+
...envDirs("DYLD_FALLBACK_FRAMEWORK_PATH"),
|
|
57
|
+
"/opt/homebrew/Frameworks",
|
|
58
|
+
path.join(home, "Library/Frameworks"),
|
|
59
|
+
"/Library/Frameworks",
|
|
60
|
+
"/Network/Library/Frameworks",
|
|
61
|
+
"/System/Library/Frameworks",
|
|
62
|
+
];
|
|
63
|
+
const candidates = [
|
|
64
|
+
...libDirs.map((dir) => path.join(dir, `lib${name}.dylib`)),
|
|
65
|
+
...libDirs.map((dir) => path.join(dir, `${name}.dylib`)),
|
|
66
|
+
...frameworkDirs.map((dir) => path.join(dir, `${name}.framework`, name)),
|
|
67
|
+
`/opt/homebrew/opt/${name}/lib/lib${name}.dylib`,
|
|
68
|
+
];
|
|
69
|
+
for (const candidate of candidates) {
|
|
70
|
+
if (isFile(candidate) || inSharedCache(candidate)) return candidate;
|
|
50
71
|
}
|
|
51
72
|
// Fallback: bare soname, let dlopen do the work
|
|
52
73
|
return `lib${name}.dylib`;
|
|
53
74
|
}
|
|
54
75
|
|
|
76
|
+
function isFile(file) {
|
|
77
|
+
try {
|
|
78
|
+
return statSync(file).isFile();
|
|
79
|
+
} catch {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// ABI di `ldconfig -p` per l'architettura del processo, come il mach_map di
|
|
85
|
+
// ctypes.util: su sistemi multiarch evita di scegliere la libreria a 32 bit.
|
|
86
|
+
function ldconfigAbi() {
|
|
87
|
+
switch (os.arch()) {
|
|
88
|
+
case "x64":
|
|
89
|
+
return "libc6,x86-64";
|
|
90
|
+
case "ppc64":
|
|
91
|
+
case "s390x":
|
|
92
|
+
return "libc6,64bit";
|
|
93
|
+
default:
|
|
94
|
+
return "libc6";
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Confronto di versioni numeriche di soname ("so.1.10" > "so.1.9").
|
|
99
|
+
function compareVersions(a, b) {
|
|
100
|
+
const pa = a.split(".").map(Number);
|
|
101
|
+
const pb = b.split(".").map(Number);
|
|
102
|
+
for (let i = 0; i < Math.max(pa.length, pb.length); i++) {
|
|
103
|
+
const d = (pa[i] ?? -1) - (pb[i] ?? -1);
|
|
104
|
+
if (d !== 0) return d;
|
|
105
|
+
}
|
|
106
|
+
return 0;
|
|
107
|
+
}
|
|
108
|
+
|
|
55
109
|
function findLibraryLinux(name) {
|
|
56
|
-
// 1)
|
|
110
|
+
// 1) ldconfig cache, as ctypes.util._findSoname_ldconfig: the soname (e.g.
|
|
111
|
+
// "libc.so.6") of the first entry with the ABI of this process.
|
|
57
112
|
try {
|
|
58
|
-
const out = execSync("/sbin/ldconfig -p", { encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] });
|
|
59
|
-
const re = new RegExp(
|
|
113
|
+
const out = execSync("/sbin/ldconfig -p", { encoding: "utf8", stdio: ["ignore", "pipe", "ignore"], env: { ...process.env, LC_ALL: "C", LANG: "C" } });
|
|
114
|
+
const re = new RegExp(`\\s+(lib${escapeRegex(name)}\\.[^\\s]+)\\s+\\(${escapeRegex(ldconfigAbi())}`);
|
|
60
115
|
const m = out.match(re);
|
|
61
116
|
if (m) return m[1];
|
|
62
117
|
} catch {}
|
|
63
118
|
|
|
64
|
-
// 2) Walk common directories.
|
|
119
|
+
// 2) Walk common directories: highest versioned soname, then the bare name.
|
|
65
120
|
const dirs = ["/usr/lib", "/usr/lib64", "/usr/local/lib", "/lib", "/lib64", "/lib/x86_64-linux-gnu", "/usr/lib/x86_64-linux-gnu", "/usr/lib/aarch64-linux-gnu"];
|
|
121
|
+
const prefix = `lib${name}.so.`;
|
|
66
122
|
for (const dir of dirs) {
|
|
67
|
-
|
|
68
|
-
if (existsSync(bare)) return bare;
|
|
69
|
-
// Also try versioned sonames (libc.so.6, libssl.so.3, ...)
|
|
123
|
+
let versions = [];
|
|
70
124
|
try {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
if (entry.startsWith(`lib${name}.so.`)) return path.join(dir, entry);
|
|
75
|
-
}
|
|
125
|
+
versions = readdirSync(dir)
|
|
126
|
+
.filter((entry) => entry.startsWith(prefix) && /^\d+(\.\d+)*$/.test(entry.slice(prefix.length)))
|
|
127
|
+
.map((entry) => entry.slice(prefix.length));
|
|
76
128
|
} catch {}
|
|
129
|
+
if (versions.length > 0) {
|
|
130
|
+
versions.sort(compareVersions);
|
|
131
|
+
return path.join(dir, prefix + versions[versions.length - 1]);
|
|
132
|
+
}
|
|
133
|
+
const bare = path.join(dir, `lib${name}.so`);
|
|
134
|
+
if (existsSync(bare)) return bare;
|
|
77
135
|
}
|
|
78
136
|
|
|
79
137
|
// 3) Fallback: bare soname. dlopen will still try LD_LIBRARY_PATH.
|
|
@@ -88,6 +146,7 @@ function escapeRegex(s) {
|
|
|
88
146
|
* Resolve a library name to a platform-specific filename/path.
|
|
89
147
|
*
|
|
90
148
|
* @param {string} name - Bare library name (e.g. "c", "ssl", "user32").
|
|
149
|
+
* @param {Object} [native] - Native module (dyld shared cache lookup on macOS)
|
|
91
150
|
* @returns {string|null} Resolved filename/path, or null if the input is invalid.
|
|
92
151
|
*
|
|
93
152
|
* @example
|
|
@@ -95,13 +154,13 @@ function escapeRegex(s) {
|
|
|
95
154
|
* find_library("ssl") // "libssl.so.3" / "ssl.dll"
|
|
96
155
|
* find_library("user32") // "user32.dll" (Windows)
|
|
97
156
|
*/
|
|
98
|
-
export function find_library(name) {
|
|
157
|
+
export function find_library(name, native) {
|
|
99
158
|
if (!name || typeof name !== "string") return null;
|
|
100
159
|
switch (process.platform) {
|
|
101
160
|
case "win32":
|
|
102
161
|
return findLibraryWindows(name);
|
|
103
162
|
case "darwin":
|
|
104
|
-
return findLibraryDarwin(name);
|
|
163
|
+
return findLibraryDarwin(name, (file) => native?.dyldSharedCacheContains?.(file) === true);
|
|
105
164
|
default:
|
|
106
165
|
return findLibraryLinux(name);
|
|
107
166
|
}
|