forked from SuperIlu/DOjS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_functions.py
74 lines (61 loc) · 1.96 KB
/
extract_functions.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
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
import sys
import re
if len(sys.argv) < 3:
print("Usage:\n {} <template> <infile> <outfile>".format(sys.argv[0]))
exit(1)
fd = re.compile(r"(js_\w*)\([^;]*;")
with open(sys.argv[1], "r") as template:
with open(sys.argv[2], "r") as infile:
with open(sys.argv[3], "w") as outfile:
# create lists with includes and exports
exports = []
prototypes = []
includes = []
for l in template:
l = l.strip()
if l.startswith("#"):
includes.append(l)
elif l.startswith("extern "):
prototypes.append(l)
elif not l.startswith("//") and len(l) > 0:
exports.append(l)
# write header
outfile.write(
"""
// this file is generated, do not edit!
#include <dlfcn.h>
#include <sys/dxe.h>
#include <mujs.h>
"""
)
# write includes
outfile.write("\n")
for i in includes:
outfile.write(i)
outfile.write("\n")
# write prototypes
outfile.write("\n")
for i in prototypes:
outfile.write(i)
outfile.write("\n")
# start symbol table
outfile.write("\n")
outfile.write(
"""
DXE_EXPORT_TABLE_AUTO(symtab)
// template functions
"""
)
# write all exports
for e in exports:
outfile.write(" DXE_EXPORT({})".format(e))
outfile.write("\n")
# write js_ functions from include
outfile.write("\n // MuJS functions\n")
for l in infile:
res = fd.search(l)
if res:
outfile.write(" DXE_EXPORT({})".format(res.group(1)))
outfile.write("\n")
# finalize export table
outfile.write("DXE_EXPORT_END\n")