forked from denosaurs/deno_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.ts
More file actions
57 lines (52 loc) · 1.63 KB
/
util.ts
File metadata and controls
57 lines (52 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
export const encoder = new TextEncoder();
export const decoder = new TextDecoder();
const libDlDef = {
dlopen: {
parameters: ["buffer", "i32"],
result: "pointer",
},
} as const;
/**
* On Unix based systems, we need to supply dlopen with RTLD_GLOBAL
* but Deno.dlopen does not support passing that flag. So we'll open
* libc and use its dlopen to open with RTLD_LAZY | RTLD_GLOBAL to
* allow subsequently loaded shared libraries to be able to use symbols
* from Python C API.
*/
export function postSetup(lib: string) {
let libdl: Deno.DynamicLibrary<typeof libDlDef>;
if (Deno.build.os === "linux") {
const libc = Deno.dlopen(`libc.so.6`, {
gnu_get_libc_version: { parameters: [], result: "pointer" },
});
const ptrView = new Deno.UnsafePointerView(
libc.symbols.gnu_get_libc_version()!,
);
const glibcVersion = parseFloat(ptrView.getCString());
libdl = Deno.dlopen(
// starting with glibc 2.34, libdl is merged into libc
glibcVersion >= 2.34 ? `libc.so.6` : `libdl.so.2`,
libDlDef,
);
} else if (Deno.build.os === "darwin") {
libdl = Deno.dlopen(`libc.dylib`, libDlDef);
} else {
return;
}
libdl.symbols.dlopen(cstr(lib), 0x00001 | 0x00100);
}
/**
* Encodes a C string.
*/
export function cstr(str: string): Uint8Array<ArrayBuffer> {
const buf = new Uint8Array(str.length + 1);
encoder.encodeInto(str, buf);
return buf;
}
/**
* Regular Expression used to test if a string is a `proper_slice`.
*
* Based on https://docs.python.org/3/reference/expressions.html#slicings
*/
export const SliceItemRegExp =
/^\s*(-?\d+)?\s*:\s*(-?\d+)?\s*(:\s*(-?\d+)?\s*)?$/;