-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.ts
91 lines (82 loc) · 1.98 KB
/
util.ts
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
export function toPointer(
v: Deno.PointerValue | Uint8Array | null,
): Deno.PointerValue {
if (v === null) {
return null;
} else if (
typeof v === "object" && Object.getPrototypeOf(v) !== null &&
v instanceof Uint8Array
) {
return Deno.UnsafePointer.of(v);
} else {
return v as Deno.PointerValue;
}
}
export function pstrToFfi(str: string | Uint8Array | null): Uint8Array | null {
if (str === null) {
return null;
}
if (str instanceof Uint8Array) {
return str;
}
return new TextEncoder().encode(str + "\0");
}
export function pstrFromFfi(ptr: Deno.PointerValue): string | null {
if (ptr === null) {
return null;
}
return Deno.UnsafePointerView.getCString(ptr!);
}
export function pwstrToFfi(
str: string | Uint8Array | Uint16Array | null,
): Uint8Array | null {
if (str === null) {
return null;
}
if (str instanceof Uint8Array) {
return str;
}
if (str instanceof Uint16Array) {
return new Uint8Array(str.buffer);
}
return new Uint8Array(
new Uint16Array(new TextEncoder().encode(str + "\0")).buffer,
);
}
export function pwstrFromFfi(ptr: Deno.PointerValue): string | null {
if (ptr === null) {
return null;
}
let res = "";
const view = new Deno.UnsafePointerView(ptr);
for (let i = 0;; i += 2) {
const code = view.getUint16(i);
if (code === 0) {
break;
}
res += String.fromCharCode(code);
}
return res;
}
export function boolToFfi(b: boolean | number): number {
return b ? 1 : 0;
}
export function boolFromFfi(b: number): boolean {
return b !== 0;
}
export function unwrap(hr: Deno.PointerValue): Deno.PointerValue {
if (hr !== null) {
throw new Error(
`HRESULT: 0x${
Deno.UnsafePointer.value(hr).toString(16).padStart(8, "0")
}`,
);
}
return hr;
}
export function charToFfi(c: string | number): number {
return typeof c === "number" ? c : c.charCodeAt(0);
}
export function charFromFfi(c: number): string {
return String.fromCharCode(c);
}