comparison lua/test/luadc.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 <lauxlib.h>
3 #include <dyncall.h>
4 #include <dyncall_signature.h>
5 #include <dynload.h>
6
7 #define LUA_DCLIBNAME "dc"
8
9 DCCallVM* g_pCallVM;
10
11 int luaDC_load(lua_State* L)
12 {
13 void* handle;
14 if (lua_gettop(L) != 1) return luaL_error(L,"wrong number of arguments");
15 handle = dlLoadLibrary( lua_tostring(L,1) );
16 if (!handle) return luaL_error(L,"library not found");
17 lua_pushlightuserdata(L, handle);
18 return 1;
19 }
20
21 int luaDC_find(lua_State* L)
22 {
23 void* h;
24 const char* s;
25 void* f;
26 if (lua_gettop(L) != 2) return luaL_error(L,"wrong number of arguments");
27 h = lua_touserdata(L, 1);
28 s = lua_tostring(L, 2);
29 f = dlFindSymbol(h, s);
30 if (!f) return luaL_error(L,"symbol not found");
31 lua_pushlightuserdata(L, f);
32 return 1;
33 }
34
35 int luaDC_call(lua_State* L)
36 {
37 void* f;
38 const char* s;
39 if (lua_gettop(L) < 2) return luaL_error(L,"missing arguments");
40
41 if ( lua_iscfunction(L,1) )
42 f = (void*) lua_tocfunction(L,1);
43 else if (lua_islightuserdata(L,1) )
44 f = lua_touserdata(L, 1);
45 else
46 return luaL_error(L,"argument #1 mismatch: expected userdata");
47
48 s = lua_tostring(L,2);
49
50 dcMode( g_pCallVM, DC_CALL_C_DEFAULT );
51 dcReset( g_pCallVM );
52
53 char ch;
54 int p = 3;
55 while ( (ch = *s++) != DC_SIGCHAR_ENDARG)
56 {
57 switch(ch)
58 {
59 case DC_SIGCHAR_BOOL:
60 dcArgBool(g_pCallVM, (DCbool) lua_toboolean(L, p) );
61 break;
62 case DC_SIGCHAR_CHAR:
63 dcArgChar(g_pCallVM, (DCchar) lua_tonumber(L, p) );
64 break;
65 case DC_SIGCHAR_SHORT:
66 dcArgShort(g_pCallVM, (DCshort) lua_tonumber(L, p) );
67 break;
68 case DC_SIGCHAR_INT:
69 dcArgInt(g_pCallVM, (DCint) lua_tonumber(L, p) );
70 break;
71 case DC_SIGCHAR_LONG:
72 dcArgLong(g_pCallVM, (DClong) lua_tonumber(L, p) );
73 break;
74 case DC_SIGCHAR_LONGLONG:
75 dcArgLongLong(g_pCallVM, (DClonglong) lua_tonumber(L, p) );
76 break;
77 case DC_SIGCHAR_FLOAT:
78 dcArgFloat(g_pCallVM, (DCfloat) lua_tonumber(L, p) );
79 break;
80 case DC_SIGCHAR_DOUBLE:
81 dcArgDouble(g_pCallVM, (DCdouble) lua_tonumber(L, p) );
82 break;
83 case DC_SIGCHAR_POINTER:
84 dcArgPointer(g_pCallVM, (DCpointer) lua_topointer(L, p) );
85 break;
86 case DC_SIGCHAR_STRING:
87 dcArgPointer(g_pCallVM, (DCpointer) lua_tostring(L, p) );
88 break;
89 }
90 ++p;
91 }
92
93 switch(*s)
94 {
95 case DC_SIGCHAR_VOID:
96 dcCallVoid(g_pCallVM, f);
97 return 0;
98 case DC_SIGCHAR_BOOL:
99 lua_pushboolean( L, (int) dcCallBool(g_pCallVM, f) );
100 break;
101 case DC_SIGCHAR_CHAR:
102 lua_pushnumber( L, (lua_Number) ( dcCallChar(g_pCallVM,f) ) );
103 break;
104 case DC_SIGCHAR_SHORT:
105 lua_pushnumber( L, (lua_Number)( dcCallShort(g_pCallVM, f) ) );
106 break;
107 case DC_SIGCHAR_INT:
108 lua_pushnumber( L, (lua_Number)( dcCallInt(g_pCallVM, f) ) );
109 break;
110 case DC_SIGCHAR_LONG:
111 lua_pushnumber( L, (lua_Number)( dcCallLong(g_pCallVM, f) ) );
112 break;
113 case DC_SIGCHAR_LONGLONG:
114 lua_pushnumber( L, (lua_Number)( dcCallLongLong(g_pCallVM, f) ) );
115 break;
116 case DC_SIGCHAR_FLOAT:
117 lua_pushnumber( L, (lua_Number) dcCallFloat(g_pCallVM, f) );
118 break;
119 case DC_SIGCHAR_DOUBLE:
120 lua_pushnumber( L, (lua_Number) dcCallDouble(g_pCallVM, f) );
121 break;
122 case DC_SIGCHAR_STRING:
123 lua_pushstring( L, (const char*) dcCallPointer(g_pCallVM, f) );
124 break;
125 case DC_SIGCHAR_POINTER:
126 lua_pushlightuserdata( L, dcCallPointer(g_pCallVM, f) );
127 break;
128 }
129 return 1;
130 }
131
132 static const luaL_Reg dclib[] =
133 {
134 {"load", luaDC_load},
135 {"find", luaDC_find},
136 {"call", luaDC_call},
137 {NULL,NULL}
138 };
139
140 LUA_API int luadc_open (lua_State* L)
141 {
142 g_pCallVM = dcNewCallVM(256);
143 luaL_register(L, LUA_DCLIBNAME, dclib);
144 return 1;
145 }
146