forked from xenith-studios/golua
/
golua.c
233 lines (197 loc) · 5.44 KB
/
golua.c
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include <stdint.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#include "_cgo_export.h"
//metatables to register:
// GoLua.GoInterface
// GoLua.GoFunction
//
static const char GoStateRegistryKey = 'k'; //golua registry key
static const char PanicFIDRegistryKey = 'k';
unsigned int* clua_checkgofunction(lua_State* L, int index)
{
unsigned int* fid = (unsigned int*)luaL_checkudata(L,index,"GoLua.GoFunction");
luaL_argcheck(L, fid != NULL, index, "'GoFunction' expected");
return fid;
}
GoInterface* clua_getgostate(lua_State* L)
{
//get gostate from registry entry
lua_pushlightuserdata(L,(void*)&GoStateRegistryKey);
lua_gettable(L, LUA_REGISTRYINDEX);
GoInterface* gip = lua_touserdata(L,-1);
lua_pop(L,1);
return gip;
}
//wrapper for callgofunction
int callback_function(lua_State* L)
{
unsigned int *fid = clua_checkgofunction(L,1);
GoInterface* gi = clua_getgostate(L);
//remove the go function from the stack (to present same behavior as lua_CFunctions)
lua_remove(L,1);
return golua_callgofunction(*gi,*fid);
}
//wrapper for gchook
int gchook_wrapper(lua_State* L)
{
unsigned int* fid = clua_checkgofunction(L,-1); //TODO: this will error
GoInterface* gi = clua_getgostate(L);
if(fid != NULL)
return golua_gchook(*gi,*fid);
printf("GCHook failed\n");
//TODO: try udata or whatever, after impl
return 0;
}
unsigned int clua_togofunction(lua_State* L, int index)
{
return *(clua_checkgofunction(L,index));
}
void clua_pushgofunction(lua_State* L, unsigned int fid)
{
unsigned int* fidptr = (unsigned int*)lua_newuserdata(L, sizeof(unsigned int));
*fidptr = fid;
luaL_getmetatable(L, "GoLua.GoFunction");
lua_setmetatable(L, -2);
}
void clua_pushlightinteger(lua_State* L, int n)
{
lua_pushlightuserdata(L, (void*)(GoUintptr)n);
}
uintptr_t clua_tolightinteger(lua_State *L, unsigned int index)
{
return (uintptr_t)lua_touserdata(L, index);
}
void clua_setgostate(lua_State* L, GoInterface gi)
{
lua_pushlightuserdata(L,(void*)&GoStateRegistryKey);
GoInterface* gip = (GoInterface*)lua_newuserdata(L,sizeof(GoInterface));
//copy interface value to userdata
gip->v = gi.v;
gip->t = gi.t;
//set into registry table
lua_settable(L,LUA_REGISTRYINDEX);
}
void clua_pushgointerface(lua_State* L, GoInterface gi)
{
GoInterface* iptr = (GoInterface*)lua_newuserdata(L, sizeof(GoInterface));
iptr->v = gi.v;
iptr->t = gi.t;
luaL_getmetatable(L, "GoLua.GoInterface");
lua_setmetatable(L,-2);
}
void clua_initstate(lua_State* L)
{
/* create the GoLua.GoFunction metatable */
luaL_newmetatable(L,"GoLua.GoFunction");
//pushkey
lua_pushliteral(L,"__call");
//push value
lua_pushcfunction(L,&callback_function);
//t[__call] = &callback_function
lua_settable(L,-3);
//push key
lua_pushliteral(L,"__gc");
//pushvalue
lua_pushcfunction(L,&gchook_wrapper);
lua_settable(L,-3);
lua_pop(L,1);
}
int callback_panicf(lua_State* L)
{
lua_pushlightuserdata(L,(void*)&PanicFIDRegistryKey);
lua_gettable(L,LUA_REGISTRYINDEX);
unsigned int fid = lua_tointeger(L,-1);
lua_pop(L,1);
GoInterface* gi = clua_getgostate(L);
return golua_callpanicfunction(*gi,fid);
}
//TODO: currently setting garbage when panicf set to null
GoInterface clua_atpanic(lua_State* L, unsigned int panicf_id)
{
//get old panicfid
unsigned int old_id;
lua_pushlightuserdata(L, (void*)&PanicFIDRegistryKey);
lua_gettable(L,LUA_REGISTRYINDEX);
if(lua_isnil(L,-1) == 0)
old_id = lua_tointeger(L,-1);
lua_pop(L,1);
//set registry key for function id of go panic function
lua_pushlightuserdata(L,(void*)&PanicFIDRegistryKey);
//push id value
lua_pushinteger(L,panicf_id);
//set into registry table
lua_settable(L,LUA_REGISTRYINDEX);
//now set the panic function
lua_CFunction pf = lua_atpanic(L,&callback_panicf);
//make a GoInterface with a wrapped C panicf or the original go panicf
if(pf == &callback_panicf)
{
return golua_idtointerface(old_id);
}
else
{
//TODO: technically UB, function ptr -> non function ptr
return golua_cfunctiontointerface((GoUintptr *)pf);
}
}
int clua_callluacfunc(lua_State* L, lua_CFunction f)
{
return f(L);
}
void* allocwrapper(void* ud, void *ptr, size_t osize, size_t nsize)
{
return (void*)golua_callallocf((GoUintptr)ud,(GoUintptr)ptr,osize,nsize);
}
lua_State* clua_newstate(void* goallocf)
{
return lua_newstate(&allocwrapper,goallocf);
}
void clua_setallocf(lua_State* L, void* goallocf)
{
lua_setallocf(L,&allocwrapper,goallocf);
}
void clua_openbase(lua_State* L){
lua_pushcfunction(L,&luaopen_base);
lua_pushstring(L,"");
lua_call(L, 1, 0);
}
void clua_openio(lua_State* L){
lua_pushcfunction(L,&luaopen_io);
lua_pushstring(L,"io");
lua_call(L, 1, 0);
}
void clua_openmath(lua_State* L){
lua_pushcfunction(L,&luaopen_math);
lua_pushstring(L,"math");
lua_call(L, 1, 0);
}
void clua_openpackage(lua_State* L){
lua_pushcfunction(L,&luaopen_package);
lua_pushstring(L,"package");
lua_call(L, 1, 0);
}
void clua_openstring(lua_State* L){
lua_pushcfunction(L,&luaopen_string);
lua_pushstring(L,"string");
lua_call(L, 1, 0);
}
void clua_opentable(lua_State* L){
lua_pushcfunction(L,&luaopen_table);
lua_pushstring(L,"table");
lua_call(L, 1, 0);
}
void clua_openos(lua_State* L){
lua_pushcfunction(L,&luaopen_os);
lua_pushstring(L,"os");
lua_call(L, 1, 0);
}
void clua_hook_function(lua_State *L, lua_Debug *ar) {
lua_checkstack(L, 2);
lua_pushstring(L, "Lua execution quantum exceeded");
lua_error(L);
}
void clua_setexecutionlimit(lua_State* L, int n) {
lua_sethook(L, &clua_hook_function, LUA_MASKCOUNT, n);
}