-
Notifications
You must be signed in to change notification settings - Fork 0
/
lcall.c
186 lines (162 loc) · 4 KB
/
lcall.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
#include "lcall.h"
#include <stdio.h>
#include <string.h>
#include "lcallapi.h"
// load a library and return the handler
static int lcall_load (lua_State *L){
hlibrary hlib;
const char * lib_name = luaL_checkstring(L, 1);
printf("library name: %s\n", lib_name);
hlib = lib_load(lib_name);
printf("hlibrary @: %p\n", hlib);
lua_pushlightuserdata(L, hlib);
return 1;
}
// get a function pointer
static int lcall_get (lua_State *L){
hlibrary hlib;
hprocedure hproc;
const char * proc_name;
hlib = (hlibrary)lua_topointer(L, 1);
if(!lua_islightuserdata(L, 1) || hlib == NULL){
luaL_error(L, "invalid hlibrary");
return 0;
}
proc_name = luaL_checkstring(L, 2);
if(proc_name == NULL){
luaL_error(L, "invalid procedure name");
return 0;
}
hproc = lib_get(hlib, proc_name);
printf("procedure at: %p\n", hproc);
lua_pushlightuserdata(L, hproc);
return 1;
}
char* c_stdcall = "__stdcall";
// do call by using `__stdcall` call convention.
static int _call_stdcall(lua_State *L){
int i, n;
hreturn hret;
long l;
const void *pproc;
const char *pstring; // for lua lua_String
int num; // for lua_Number, lua_Bool
const void* p; // for lightuserdata
n = lua_gettop(L);
stack_save(l);
for(i = n; i >= 2; i--){
switch(lua_type(L, i)){
case LUA_TNIL:
arg_set(n, i, 0);
break;
case LUA_TNUMBER:
num = (int)luaL_checknumber(L, i);
arg_set(n, i, num);
break;
case LUA_TSTRING:
pstring = luaL_checkstring(L, i);
arg_set(n, i, pstring);
break;
case LUA_TLIGHTUSERDATA:
p = lua_topointer(L, i);
arg_set(n, i, p);
break;
case LUA_TBOOLEAN:
num = lua_toboolean(L, i);
arg_set(n, i, num);
break;
}
}
pproc = lua_topointer(L, 1);
proc_call(pproc);
ret_get(hret);
stack_rewind(l);
lua_pushlightuserdata(L, hret);
return 1;
}
char* c_cdecl = "__cdecl";
// do call by using `__cdecl` call convention.
static int _call_cdecl(lua_State *L){
// because __stdcall store and recover stack, so __cdecl equals __stdcall
return _call_stdcall(L);
}
// dispatch to `call_*`
int _dispatch_call(lua_State *L, const char *proto){
if(strcmp("__stdcall", proto) == 0){
return _call_stdcall(L);
}else if(strcmp("__cdecl", proto) == 0){
return _call_cdecl(L);
}
return 0;
}
// check arguments and set arguments for `call_*`
static int lcall_call(lua_State *L){
int n, i;
const char * proto;
// check proto
proto = luaL_checkstring(L, 1);
if(proto == NULL){
luaL_error(L, ("need call convention like \"__stdcall\", \"__cdecl \" and so on.\n"));
return 0;
}
// check function pointer
if(!lua_islightuserdata(L, 2)){
luaL_error(L, "invalid hprocedure");
return 0;
}
// has unsupported type
n = lua_gettop(L);
for(i = 2; i <= n; i++){
switch(lua_type(L, i)){
case LUA_TTABLE:
case LUA_TFUNCTION:
case LUA_TUSERDATA:
case LUA_TTHREAD:
luaL_error(L, "lcall does not support this kind of type temporaryly");
return 0;
}
}
lua_remove(L, 1);
return _dispatch_call(L, proto);
return 0;
}
static int lcall_calls(lua_State *L){
const char *proto, *lib_name, *proc_name;
hlibrary hlib;
hprocedure hproc;
proto = luaL_checkstring(L, 1);
lib_name = luaL_checkstring(L, 2);
proc_name = luaL_checkstring(L, 3);
hlib = lib_load(lib_name);
hproc = lib_get(hlib, proc_name);
lua_remove(L, 1);
lua_remove(L, 1);
lua_remove(L, 1);
lua_pushlightuserdata(L, hproc);
lua_insert(L, 1);
return _dispatch_call(L, proto);
}
//////////////////////////////////////////////////////////////////////////
static const luaL_Reg mathlib[] ={
{"load", lcall_load},
{"get", lcall_get},
{"call", lcall_call},
{"calls", lcall_calls},
{NULL, NULL},
};
LUAMOD_API int luaopen_lcall (lua_State *L){
luaL_newlib(L, mathlib);
return 1;
}
static const luaL_Reg lcalllibs[] = {
{"lcall", luaopen_lcall},
{NULL, NULL}
};
LUALIB_API void lcall_open(lua_State* L){
const luaL_Reg *lib;
/* "require" functions from 'lcalllibs' and set results to global table */
for (lib = lcalllibs; lib->func; lib++) {
luaL_requiref(L, lib->name, lib->func, 1);
lua_pop(L, 1); /* remove lib */
}
}