forked from BeamMW/beam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfix-client.py
39 lines (29 loc) · 1.14 KB
/
fix-client.py
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
#/bin/python
import sys
print("Fixing wasm client, file: ", sys.argv[1], " output: ")
t_poll = "function ___syscall_poll"
t_proxy = "if (ENVIRONMENT_IS_PTHREAD)"
t_ret = "return"
t_end = ");"
# read all input
with open(sys.argv[1], 'r') as original:
text = original.read()
# find function sys_poll
ipoll = text.index(t_poll)
#add with AB declaration
text = text[:ipoll] + "const AB = new Int32Array(new SharedArrayBuffer(4));\n" + text[ipoll:]
# find start of proxy call
ipoll = text.index(t_poll)
iret = text.index(t_proxy, ipoll)
iret = text.index(t_ret, iret)
# replace with our changes
text = text[:iret] + '{\n var ret =' + text[iret + len(t_ret):]
# find end of proxy call
iend = text.index(t_end, iret)
text = text[:iend + len(t_end)] + '\n if (ret == 0) Atomics.wait(AB, 0, 0, 50);\n return ret;\n }' + text[iend + len(t_end):]
# replace buffer instanceof SharedArrayBuffer
# to fix https://bugs.chromium.org/p/chromium/issues/detail?id=1269096
text = text.replace("buffer instanceof SharedArrayBuffer", "buffer[Symbol.toStringTag] == 'SharedArrayBuffer'")
#save fixed result
with open(sys.argv[1], 'w') as result:
result.write(text)