comparison lua/luadyncall/src/larray.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 "dyntype.h"
6
7 int newudata(lua_State* L)
8 {
9 size_t size = luaL_checkint(L, 1);
10 void * pointer = lua_newuserdata(L, size);
11 return 1;
12 }
13
14 int newudata2(lua_State* L)
15 {
16 size_t size = luaL_checkint(L, 1);
17 void * pointer = lua_newuserdata(L, size);
18 lua_pushvalue(L,-2);
19 lua_setmetatable(L,-2);
20 return 1;
21 }
22
23
24 int peek(lua_State* L)
25 {
26 char* pointer = (char*) lua_topointer(L, 1);
27 int offset = luaL_checkint(L, 2);
28 const char* typeinfo = luaL_checkstring(L, 3);
29 pointer += offset;
30 switch( *typeinfo )
31 {
32 case DC_SIGCHAR_BOOL: lua_pushboolean(L, (int) * ( (DCbool*) pointer ) ); break;
33 case DC_SIGCHAR_CHAR: lua_pushnumber(L, (lua_Number) * ( (DCchar*) pointer ) ); break;
34 case DC_SIGCHAR_UCHAR: lua_pushnumber(L, (lua_Number) * ( (DCuchar*) pointer ) ); break;
35 case DC_SIGCHAR_SHORT: lua_pushnumber(L, (lua_Number) * ( (DCshort*) pointer ) ); break;
36 case DC_SIGCHAR_USHORT: lua_pushnumber(L, (lua_Number) * ( (DCushort*) pointer ) ); break;
37 case DC_SIGCHAR_INT: lua_pushnumber(L, (lua_Number) * ( (DCint*) pointer ) ); break;
38 case DC_SIGCHAR_UINT: lua_pushnumber(L, (lua_Number) * ( (DCuint*) pointer ) ); break;
39 case DC_SIGCHAR_LONG: lua_pushnumber(L, (lua_Number) * ( (DClong*) pointer ) ); break;
40 case DC_SIGCHAR_ULONG: lua_pushnumber(L, (lua_Number) * ( (DCulong*) pointer ) ); break;
41 case DC_SIGCHAR_LONGLONG: lua_pushnumber(L, (lua_Number) * ( (DClonglong*) pointer ) ); break;
42 case DC_SIGCHAR_ULONGLONG: lua_pushnumber(L, (lua_Number) * ( (DCulonglong*) pointer ) ); break;
43 case DC_SIGCHAR_FLOAT: lua_pushnumber(L, (lua_Number) * ( (DCfloat*) pointer ) ); break;
44 case DC_SIGCHAR_DOUBLE: lua_pushnumber(L, (lua_Number) * ( (DCdouble*) pointer ) ); break;
45 case DC_SIGCHAR_POINTER: lua_pushlightuserdata(L, * ( (DCpointer*) pointer ) ); break;
46 default: luaL_error(L, "invalid type signature: %s\n", typeinfo); break;
47 }
48 return 1;
49 }
50
51 int poke(lua_State* L)
52 {
53 char* pointer = (char*) lua_topointer(L, 1);
54 int offset = luaL_checkint(L, 2);
55 const char* typeinfo = luaL_checkstring(L, 3);
56 pointer += offset;
57 switch( *typeinfo )
58 {
59 case DC_SIGCHAR_BOOL: * ( (DCbool*) pointer ) = lua_toboolean(L, 4); break;
60 case DC_SIGCHAR_CHAR: * ( (DCchar*) pointer ) = (DCchar) luaL_checkint(L, 4); break;
61 case DC_SIGCHAR_UCHAR: * ( (DCuchar*) pointer ) = (DCuchar) luaL_checkint(L, 4); break;
62 case DC_SIGCHAR_SHORT: * ( (DCshort*) pointer ) = (DCshort) luaL_checkint(L, 4); break;
63 case DC_SIGCHAR_USHORT: * ( (DCushort*) pointer ) = (DCushort) luaL_checkint(L, 4); break;
64 case DC_SIGCHAR_INT: * ( (DCint*) pointer ) = (DCint) luaL_checkint(L, 4); break;
65 case DC_SIGCHAR_UINT: * ( (DCuint*) pointer ) = (DCuint) luaL_checknumber(L, 4); break;
66 case DC_SIGCHAR_LONG: * ( (DClong*) pointer ) = (DClong) luaL_checknumber(L, 4); break;
67 case DC_SIGCHAR_ULONG: * ( (DCulong*) pointer ) = (DCulong) luaL_checknumber(L, 4); break;
68 case DC_SIGCHAR_LONGLONG: * ( (DClonglong*) pointer ) = (DClonglong) luaL_checknumber(L,4); break;
69 case DC_SIGCHAR_ULONGLONG: * ( (DCulonglong*) pointer ) = (DCulonglong) luaL_checknumber(L,4); break;
70 case DC_SIGCHAR_FLOAT: * ( (DCfloat*) pointer ) = (DCfloat) luaL_checknumber(L,4); break;
71 case DC_SIGCHAR_DOUBLE: * ( (DCdouble*) pointer ) = (DCdouble) luaL_checknumber(L,4); break;
72 case DC_SIGCHAR_POINTER:
73 {
74 switch(lua_type(L,4)) {
75 case LUA_TUSERDATA:
76 case LUA_TLIGHTUSERDATA:
77 * ( (DCpointer*) pointer ) = (DCpointer) lua_topointer(L,4); break;
78 case LUA_TSTRING:
79 * ( (DCpointer*) pointer ) = (DCpointer) lua_tostring(L,4); break;
80 default:
81 luaL_error(L, "invalid argument for signature : %s\n", typeinfo); break;
82 }
83 break;
84 }
85 default: luaL_error(L, "invalid type signature: %s\n", typeinfo); break;
86 }
87 return 0;
88 }
89
90 int lua_sizeof(lua_State *L)
91 {
92 const char* typeinfo = luaL_checkstring(L, 1);
93 lua_pushinteger(L, dtSize(typeinfo) );
94 return 1;
95 }
96
97 int copy(lua_State *L)
98 {
99 char* dstptr = (char*) lua_topointer(L, 1);
100 int dstoffset = luaL_checkint(L, 2);
101 const char* srcptr = lua_topointer(L, 3);
102 int srcoffset = luaL_checkint(L, 4);
103 int copysize = luaL_checkint(L, 5);
104 dstptr += dstoffset;
105 srcptr += srcoffset;
106 int i;
107 for(i=0;i<copysize;++i)
108 {
109 *dstptr++ = *srcptr++;
110 }
111 return 0;
112 }
113
114 static const struct luaL_Reg luareg_larray[] =
115 {
116 {"newudata" ,newudata},
117 {"newudata2", newudata2},
118 {"peek" ,peek},
119 {"poke" ,poke},
120 {"sizeof" ,lua_sizeof},
121 {"copy" ,copy},
122 {NULL ,NULL}
123 };
124
125 LUA_API int luaopen_larray(lua_State* L)
126 {
127 luaL_register(L, "larray", luareg_larray);
128 return 0;
129 }