-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathpinvoke.py
More file actions
164 lines (138 loc) · 5.96 KB
/
Copy pathpinvoke.py
File metadata and controls
164 lines (138 loc) · 5.96 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python3
"""The live P/Invoke surface, read out of Unity's generated `tests/*.csproj`.
Shared by `check-exports.py` (does the library export it?) and `check-signatures.py`
(does the declaration match the header?). Both need the same two things, and the C#
preprocessor evaluator is too much to keep in two places:
* the **real** define set and source list, so a declaration inside a dead `#if`
is not counted as a requirement -- `JSB_ATOM_Operators` is live or not depending
on `JSB_NO_BIGNUM`, and guessing wrong moves the answer either way;
* `EntryPoint` resolution, because a member named `JS_DupValue` can bind the
symbol `JSB_DupValue`. Three do, and an audit by member name misses all three.
"""
import glob
import io
import os
import re
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
REPO = os.path.normpath(os.path.join(HERE, "..", ".."))
TESTS = os.path.join(REPO, "tests")
DIRECTIVE = re.compile(r"^\s*#\s*(if|elif|else|endif)\b(.*)$")
TOK = re.compile(r"\(|\)|\|\||&&|!|[A-Za-z_][A-Za-z_0-9]*")
def evaluate(expr, defines):
"""Evaluate a C# preprocessor condition. Unknown symbols are undefined."""
toks = TOK.findall(expr)
pos = [0]
def peek():
return toks[pos[0]] if pos[0] < len(toks) else None
def primary():
t = toks[pos[0]]
pos[0] += 1
if t == "(":
v = or_expr()
if peek() == ")":
pos[0] += 1
return v
if t == "!":
return not primary()
if t in ("true", "false"):
return t == "true"
return t in defines
def and_expr():
v = primary()
while peek() == "&&":
pos[0] += 1
v = primary() and v
return v
def or_expr():
v = and_expr()
while peek() == "||":
pos[0] += 1
v = and_expr() or v
return v
return or_expr()
def live_source(path, defines):
"""The file with preprocessor-excluded lines blanked out."""
text = io.open(path, encoding="utf-8-sig", newline="").read()
stack, out = [], []
for raw in text.replace("\r\n", "\n").split("\n"):
m = DIRECTIVE.match(raw)
active = all(f[0] for f in stack)
if m:
kind, rest = m.group(1), m.group(2).split("//")[0]
if kind == "if":
val = active and evaluate(rest, defines)
stack.append([val, val, active])
elif kind == "elif" and stack:
f = stack[-1]
val = f[2] and not f[1] and evaluate(rest, defines)
f[0], f[1] = val, f[1] or val
elif kind == "else" and stack:
f = stack[-1]
f[0], f[1] = f[2] and not f[1], True
elif kind == "endif" and stack:
stack.pop()
out.append("")
continue
out.append(raw if active else "")
return "\n".join(out)
def have_projects():
"""Whether the live surface can be derived *here*.
Their existence is not enough. Unity writes `<Compile Include>` as an absolute path for
every file outside the project -- which is all of the quickjs C# -- so a checkout mounted
somewhere else resolves none of them. That used to yield a live set missing most of the
surface and a drift report naming every function in it, which reads like a real regression
and is not one; WSL hits it every time, reading the same tree through /mnt.
"""
if not glob.glob(os.path.join(TESTS, "*.csproj")):
return False
return any(sources for _, _, sources in unity_projects())
def read_surface(name):
"""The committed surface, for a checkout with no Unity-generated .csproj."""
path = os.path.join(HERE, name)
if not os.path.exists(path):
sys.exit("no %s, and no generated .csproj to derive it from" % name)
names = []
for line in io.open(path, encoding="utf-8"):
line = line.strip()
if line and not line.startswith("#"):
names.append(line)
if not names:
sys.exit("%s is empty" % path)
return names
def write_surface(name, names, what):
path = os.path.join(HERE, name)
with io.open(path, "w", encoding="utf-8", newline="\n") as fp:
fp.write("# %s\n" % what)
fp.write("# Generated -- do not edit by hand. Regenerate with the --write flag on\n")
fp.write("# the check that owns this file, from a checkout Unity has opened.\n")
for n in sorted(names):
fp.write("%s\n" % n)
print("wrote %s (%d names)" % (path, len(names)))
def surface_drift(name, live):
"""The committed file against the live derivation, as (missing, extra)."""
committed = set(read_surface(name))
return sorted(set(live) - committed), sorted(committed - set(live))
def unity_projects():
"""(project, defines, source paths) per generated csproj that compiles quickjs C#."""
projects = sorted(glob.glob(os.path.join(TESTS, "*.csproj")))
if not projects:
sys.exit("no .csproj in %s -- open tests/ in the Unity Editor once to generate them" % TESTS)
for proj in projects:
base = os.path.basename(proj)[:-7]
if not (base.startswith("jsb.") or base.startswith("ReactUnity")):
continue
text = io.open(proj, encoding="utf-8-sig").read()
m = re.search(r"<DefineConstants>(.*?)</DefineConstants>", text, re.S)
defines = {x.strip() for x in (m.group(1) if m else "").split(";") if x.strip()}
sources = []
for rel in re.findall(r'<Compile Include="([^"]+)"', text):
full = os.path.normpath(os.path.join(TESTS, rel.replace("\\", "/")))
if os.path.exists(full) and "quickjs" in full:
sources.append(full)
if sources:
yield proj, defines, sources
def entry_point(dllimport_args, member):
"""The symbol a declaration actually binds -- `EntryPoint` wins over the name."""
ep = re.search(r'EntryPoint\s*=\s*"([^"]+)"', dllimport_args)
return ep.group(1) if ep else member