annotate lua/test/luadc.c @ 54:918dab7a6606

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