-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.ts
136 lines (115 loc) · 3.29 KB
/
index.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
* TODO:
* - Use proper platform checks (CPU and OS)
* - Use strict null checks?
* - Use platform-agnostic data structure sizes (size_t et al.)
* - Register for notification when a new module is added
* - Add demangled symbol look-up
* - Add parsing of function names
* - inout params
*/
import { getApi, Api, getPrivateAPI } from "./lib/api";
import {
Class,
Struct,
Enum,
Protocol,
ProtocolComposition,
EnumValue,
ObjectInstance,
StructValue,
Type,
} from "./lib/types";
import {
makeSwiftNativeFunction,
NativeSwiftType,
} from "./lib/callingconvention";
import { Registry, SwiftModule } from "./lib/registry";
import { SwiftInterceptor } from "./lib/interceptor";
import { getSymbolicator } from "./lib/symbols";
type ConvenientSwiftType = Type | Protocol | ProtocolComposition | NativeFunctionReturnType | NativeFunctionArgumentType;
class Runtime {
#api: Api = null;
#initializatioError: Error = null;
constructor() {
try {
this.tryInitialize();
} catch (e) {
/* empty */
}
}
get available(): boolean {
try {
return this.tryInitialize();
} catch (e) {
return false;
}
}
get api(): Api {
return this.#api;
}
get modules(): Record<string, SwiftModule> {
return Registry.shared().modules;
}
get classes(): Record<string, Class> {
return Registry.shared().classes;
}
get structs(): Record<string, Struct> {
return Registry.shared().structs;
}
get enums(): Record<string, Enum> {
return Registry.shared().enums;
}
get protocols(): Record<string, Protocol> {
return Registry.shared().protocols;
}
readonly Object = ObjectInstance;
readonly Struct = StructValue;
readonly Enum = EnumValue;
readonly ProtocolComposition = ProtocolComposition;
readonly Interceptor = SwiftInterceptor;
NativeFunction(
address: NativePointer,
retType: ConvenientSwiftType,
argTypes: ConvenientSwiftType[],
context?: NativePointer,
throws?: boolean
) {
function getNativeType(type: ConvenientSwiftType): NativeSwiftType {
if (type instanceof Type) {
return type.$metadata;
} else if (type instanceof Protocol) {
return new ProtocolComposition(type);
}
return type;
}
const nativeRetType = getNativeType(retType);
const nativeArgType = argTypes.map((ty) => getNativeType(ty));
return makeSwiftNativeFunction(
address,
nativeRetType,
nativeArgType,
context,
throws
);
}
private tryInitialize(): boolean {
if (this.#api !== null) {
return true;
}
if (this.#initializatioError !== null) {
throw this.#initializatioError;
}
try {
this.#api = getApi();
/* Call these for their side effects, i.e. throwing on failure */
getPrivateAPI()
getSymbolicator();
} catch (e) {
this.#initializatioError = e;
throw e;
}
return this.#api !== null;
}
}
export = new Runtime();