comparison lua/test/main.c @ 0:0cfcc391201f

initial from svn dyncall-1745
author Daniel Adler
date Thu, 19 Mar 2015 22:26:28 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:0cfcc391201f
1 #include <lua.h>
2 #include <assert.h>
3 #include <dynload.h>
4 #include <dyncall.h>
5 #include <dyncall_signature.h>
6
7 int lua_dcload(lua_State* L)
8 {
9 void* handle;
10 if (lua_gettop(L) != 1) return luaL_error(L,"wrong number of arguments");
11 handle = dlLoadLibrary( lua_tostring(L,1) );
12 if (!handle) return luaL_error(L,"library not found");
13 lua_pushlightuserdata(L, handle);
14 return 1;
15 }
16
17 int lua_dcfind(lua_State* L)
18 {
19 void* h;
20 const char* s;
21 void* f;
22 if (lua_gettop(L) != 2) return luaL_error(L,"wrong number of arguments");
23 h = lua_touserdata(L, 1);
24 s = lua_tostring(L, 2);
25 f = dlFindSymbol(h, s);
26 if (!f) return luaL_error(L,"symbol not found");
27 lua_pushlightuserdata(L, f);
28 return 1;
29 }
30
31 DCCallVM* g_pCallVM;
32
33 int lua_dccall(lua_State* L)
34 {
35 void* f;
36 const char* s;
37 if (lua_gettop(L) < 2) return luaL_error(L,"missing arguments");
38
39 if ( lua_iscfunction(L,1) )
40 f = (void*) lua_tocfunction(L,1);
41 else if (lua_islightuserdata(L,1) )
42 f = lua_touserdata(L, 1);
43 else
44 return luaL_error(L,"argument #1 mismatch: expected userdata");
45
46 s = lua_tostring(L,2);
47
48 // dcMode( g_pCallVM, DC_CALL_C_X86_WIN32_STD );
49 dcReset( g_pCallVM );
50
51 char ch;
52 int p = 3;
53 while ( (ch = *s++) != DC_SIGCHAR_ENDARG)
54 {
55 switch(ch)
56 {
57 case DC_SIGCHAR_BOOL:
58 dcArgBool(g_pCallVM, (DCbool) lua_toboolean(L, p) );
59 break;
60 case DC_SIGCHAR_CHAR:
61 dcArgChar(g_pCallVM, (DCchar) lua_tonumber(L, p) );
62 break;
63 case DC_SIGCHAR_SHORT:
64 dcArgShort(g_pCallVM, (DCshort) lua_tonumber(L, p) );
65 break;
66 case DC_SIGCHAR_INT:
67 dcArgInt(g_pCallVM, (DCint) lua_tonumber(L, p) );
68 break;
69 case DC_SIGCHAR_LONG:
70 dcArgLong(g_pCallVM, (DClong) lua_tonumber(L, p) );
71 break;
72 case DC_SIGCHAR_LONGLONG:
73 dcArgLongLong(g_pCallVM, (DClonglong) lua_tonumber(L, p) );
74 break;
75 case DC_SIGCHAR_FLOAT:
76 dcArgFloat(g_pCallVM, (DCfloat) lua_tonumber(L, p) );
77 break;
78 case DC_SIGCHAR_DOUBLE:
79 dcArgDouble(g_pCallVM, (DCdouble) lua_tonumber(L, p) );
80 break;
81 case DC_SIGCHAR_POINTER:
82 dcArgPointer(g_pCallVM, (DCpointer) lua_topointer(L, p) );
83 break;
84 case DC_SIGCHAR_STRING:
85 dcArgPointer(g_pCallVM, (DCpointer) lua_tostring(L, p) );
86 break;
87 }
88 ++p;
89 }
90
91 switch(*s)
92 {
93 case DC_SIGCHAR_VOID:
94 dcCallVoid(g_pCallVM, f);
95 return 0;
96 case DC_SIGCHAR_BOOL:
97 lua_pushboolean( L, (int) dcCallBool(g_pCallVM, f) );
98 break;
99 case DC_SIGCHAR_CHAR:
100 lua_pushnumber( L, (lua_Number) ( dcCallChar(g_pCallVM,f) ) );
101 break;
102 case DC_SIGCHAR_SHORT:
103 lua_pushnumber( L, (lua_Number)( dcCallShort(g_pCallVM, f) ) );
104 break;
105 case DC_SIGCHAR_INT:
106 lua_pushnumber( L, (lua_Number)( dcCallInt(g_pCallVM, f) ) );
107 break;
108 case DC_SIGCHAR_LONG:
109 lua_pushnumber( L, (lua_Number)( dcCallLong(g_pCallVM, f) ) );
110 break;
111 case DC_SIGCHAR_LONGLONG:
112 lua_pushnumber( L, (lua_Number)( dcCallLongLong(g_pCallVM, f) ) );
113 break;
114 case DC_SIGCHAR_FLOAT:
115 lua_pushnumber( L, (lua_Number) dcCallFloat(g_pCallVM, f) );
116 break;
117 case DC_SIGCHAR_DOUBLE:
118 lua_pushnumber( L, (lua_Number) dcCallDouble(g_pCallVM, f) );
119 break;
120 case DC_SIGCHAR_STRING:
121 lua_pushstring( L, (const char*) dcCallPointer(g_pCallVM, f) );
122 break;
123 case DC_SIGCHAR_POINTER:
124 lua_pushlightuserdata( L, dcCallPointer(g_pCallVM, f) );
125 break;
126 }
127 return 1;
128 }
129
130 int main(int argc, char* argv[])
131 {
132 lua_State* L;
133 lua_Debug d;
134 int status;
135 const char* scriptFilename = argv[1];
136
137 L = lua_open();
138 assert(L);
139 luaL_openlibs(L);
140 luaopen_base(L);
141
142 g_pCallVM = dcNewCallVM(256);
143
144 lua_register(L, "dcLoad", lua_dcload);
145 lua_register(L, "dcFind", lua_dcfind);
146 lua_register(L, "dcCall", lua_dccall);
147
148 status = luaL_loadfile(L, scriptFilename);
149
150 if (status == 0)
151 {
152 status = lua_pcall(L, 0, LUA_MULTRET, 0);
153 }
154 if (status)
155 {
156 printf("error: %s\n", lua_tostring(L,-1));
157 lua_pop(L, 1);
158 }
159
160 dcFree(g_pCallVM);
161
162 lua_close(L);
163 }