-
Notifications
You must be signed in to change notification settings - Fork 5
/
mkwrapfuns.py
executable file
·77 lines (59 loc) · 1.8 KB
/
mkwrapfuns.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
75
76
77
#!/usr/bin/env python
import os
import sys
def gen(out, arity):
typenames = ', '.join('typename Arg%d' % (i+1) for i in range(arity))
types = ', '.join('Arg%d' % (i+1) for i in range(arity))
callsig = ', '.join('Arg%d a%d' % (i+1, i+1) for i in range(arity))
args = ', '.join('a%d' % (i+1) for i in range(arity))
out.write("""template <typename Rv, %(typenames)s>
Rv invoke(struct ftype *ft, %(callsig)s) {
assert(ft);
assert(ft->num_args == %(arity)d);
void *forig = ft->orig ? ft->orig : dlsym(RTLD_NEXT, ft->name);
assert(forig);
Rv (*x)(%(callsig)s) = function_cast<Rv (*)(%(callsig)s)>(forig);
return x(%(args)s);
}
template <typename Rv, %(typenames)s>
Rv wrapper(const char *name, %(callsig)s) {
static struct ftype *func;
if (func == NULL) {
func = findFunc(name);
}
LuaInvocation li;
if (!li.ready) {
return invoke<Rv, %(types)s>(func, %(args)s);
}
if (func->has_before) {
before_call<%(types)s>(name, %(args)s);
}
Rv rv;
if (func->has_around) {
around_call(func, %(args)s, rv);
} else {
rv = invoke<Rv, %(types)s>(func, %(args)s);
}
if (func->has_after) {
after_call<Rv, %(types)s>(name, rv, %(args)s);
}
return rv;
}
""" % {'typenames': typenames, 'types': types, 'callsig': callsig,
'args': args, 'arity': arity})
if __name__ == '__main__':
out = file('gen_wrapperfuns.hh.tmp', 'w')
out.write("""#ifndef GEN_WRAPPERFUNS_HH
#define GEN_WRAPPERFUNS_HH 1
#include "labreatypes.h"
#include "scripting.hh"
namespace labrea {
struct ftype *findFunc(const char *name);
""")
for i in range(1, 6):
gen(out, i)
out.write("""}
#endif /* GEN_WRAPPERFUNS_HH */
""")
out.close()
os.rename('gen_wrapperfuns.hh.tmp', 'gen_wrapperfuns.hh')