annotate lua/test/main.c @ 26:3745790db233

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