annotate lua/luadyncall/src/larray.c @ 36:b84064293541

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