-
Notifications
You must be signed in to change notification settings - Fork 5
/
mkcallfuns.py
executable file
·107 lines (94 loc) · 3.33 KB
/
mkcallfuns.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
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
#!/usr/bin/env python
import os
import sys
def gen_calls(out, arity):
typenames = ', '.join(['typename Arg%d' % (i+1) for i in range(arity)])
callsig = ', '.join(['Arg%d a%d' % (i+1, i+1) for i in range(arity)])
out.write("""
template <%(typenames)s>
void before_call(const char *call, %(callsig)s) {
char fname[32];
snprintf(fname, sizeof(fname)-1, "before_%%s", call);
LuaStateHolder lsh(getLuaState());
assert(lsh.state);
lua_settop(lsh.state, 0);
lua_getfield(lsh.state, LUA_GLOBALSINDEX, fname);
if (!lua_isnil(lsh.state, -1)) {
""" % {'typenames': typenames,
'callsig': callsig})
for i in range(arity):
out.write(" add_arg(lsh.state, a%d);\n" % (i+1))
out.write(""" if (lua_pcall(lsh.state, %d, 0, 0)) {
std::cerr << "Error invoking " << fname << ": "
<< lua_tostring(lsh.state, -1) << std::endl;
}
}
}
""" % arity)
out.write("""
template <typename Rv, %(typenames)s>
void around_call(struct ftype *f, %(callsig)s, Rv &out) {
char fname[32];
snprintf(fname, sizeof(fname)-1, "around_%%s", f->name);
LuaStateHolder lsh(getLuaState());
assert(lsh.state);
lua_settop(lsh.state, 0);
lua_getfield(lsh.state, LUA_GLOBALSINDEX, fname);
add_arg(lsh.state, f->pos);
""" % {'typenames': typenames, 'callsig': callsig})
for i in range(arity):
out.write(" add_arg(lsh.state, a%d);\n" % (i+1))
out.write(""" if (lua_pcall(lsh.state, %d, 1, 0)) {
std::cerr << "Error invoking " << fname << ": "
<< lua_tostring(lsh.state, -1) << std::endl;
}
assert(lua_gettop(lsh.state) > 0);
assert(lua_isnumber(lsh.state, 1));
out = (Rv)lua_tointeger(lsh.state, 1);
}
""" % (arity + 1))
out.write("""
template <typename Rv, %(typenames)s>
void after_call(const char *call, Rv rv, %(callsig)s) {
char fname[32];
snprintf(fname, sizeof(fname)-1, "after_%%s", call);
LuaStateHolder lsh(getLuaState());
assert(lsh.state);
lua_settop(lsh.state, 0);
lua_getfield(lsh.state, LUA_GLOBALSINDEX, fname);
if (!lua_isnil(lsh.state, -1)) {
add_arg(lsh.state, rv);
""" % {'typenames': typenames, 'callsig': callsig})
for i in range(arity):
out.write(" add_arg(lsh.state, a%d);\n" % (i+1))
out.write(""" if (lua_pcall(lsh.state, %d, 0, 0)) {
std::cerr << "Error invoking " << fname << ": "
<< lua_tostring(lsh.state, -1) << std::endl;
}
}
}
""" % (arity+1))
if __name__ == '__main__':
out = file("gen_scripting.hh.tmp", "w")
out.write("""#ifndef GEN_SCRIPTING_HH
#define GEN_SCRIPTING_HH 1
#include "labreatypes.h"
#include "script_state.hh"
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
namespace labrea {
void add_arg(lua_State *state, const int val);
void add_arg(lua_State *state, const unsigned int val);
void add_arg(lua_State *state, const long val);
void add_arg(lua_State *state, const long unsigned val);
void add_arg(lua_State *state, const long long val);
void add_arg(lua_State *state, const unsigned long long val);
void add_arg(lua_State *state, const void* val);
""")
for i in range(1, 6):
gen_calls(out, i)
out.write("""}
#endif /* GEN_SCRIPTING_HH */\n""")
out.close()
os.rename("gen_scripting.hh.tmp", 'gen_scripting.hh')