-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrampolines_to_cs.py
More file actions
executable file
·89 lines (73 loc) · 2.34 KB
/
trampolines_to_cs.py
File metadata and controls
executable file
·89 lines (73 loc) · 2.34 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
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
#!/usr/bin/env python3
import re
re_pointer = re.compile(r"\*\s*(?:const|mut)\s+(?P<type>.+)")
re_param = re.compile(r"\s*(?P<name>[#\w]+):\s*(?P<type>.+)\s*")
TYPE_MAP = {
"bool": "byte",
"c_char": "byte",
"c_void": "void",
"u1c": "byte",
"u4c": "uint",
"ByondCallback": "delegate* unmanaged[Cdecl]<void*, CByondValue>"
}
def convertType(t: str | None) -> str:
if t is None:
return "void"
if mapped := TYPE_MAP.get(t):
return mapped
if match := re_pointer.match(t):
return convertType(match.group("type")) + "*"
return t
def sanitizeCsName(t: str) -> str:
if t == "r#type":
return "type"
return t
file = open("trampoline/src/lib.rs", "r").read()
print("//")
print("// Function stubs")
print("//")
functions = list(re.finditer(r"^\s+fn\s+(?P<name>\w+)\((?P<params>.*)\)\s*(?:->\s*(?P<ret>.+))?;$", file, re.MULTILINE))
for match in functions:
name = match.group("name")
return_type = convertType(match.group("ret"))
params = match.group("params") or ""
if params:
param_types = []
for param in params.split(","):
# print(param)
match = re_param.match(param)
param_name = sanitizeCsName(match.group("name"))
param_type = convertType(match.group("type"))
param_types.append(f"{param_type} {param_name}")
arg_types = ", ".join(param_types)
else:
arg_types = ""
print(f"""
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])]
private static {return_type} {name}({arg_types}) {{
throw new NotImplementedException();
}}
""")
print("//")
print("// Trampoline definition")
print("//")
for match in functions:
name = match.group("name")
return_type = convertType(match.group("ret"))
params = match.group("params") or ""
if params:
param_types = []
for param in params.split(","):
# print(param)
match = re_param.match(param)
param_types.append(convertType(match.group("type")))
arg_types = ", ".join(param_types) + ", "
else:
arg_types = ""
print(f" public delegate* unmanaged[Cdecl]<{arg_types}{return_type}> {name};")
print("//")
print("// Trampoline fill")
print("//")
for match in functions:
name = match.group("name")
print(" " * 12 + f"{name} = &{name},")