Debian Codegraph @

3rdparty/toluapp/src/lib/tolua_to.c

raw
1 /* tolua: funcitons to convert to C types
2 ** Support code for Lua bindings.
3 ** Written by Waldemar Celes
4 ** TeCGraf/PUC-Rio
5 ** Apr 2003
6 ** $Id: $
7 */
8
9 /* This code is free software; you can redistribute it and/or modify it.
10 ** The software provided hereunder is on an "as is" basis, and
11 ** the author has no obligation to provide maintenance, support, updates,
12 ** enhancements, or modifications.
13 */
14
15 #include "tolua++.h"
16
17 #include <string.h>
18 #include <stdlib.h>
19
20 TOLUA_API lua_Number tolua_tonumber(lua_State *L, int narg, lua_Number def)
21 {
22 return lua_gettop(L) < abs(narg) ? def : lua_tonumber(L, narg);
23 }
24
25 TOLUA_API const char *tolua_tostring(lua_State *L, int narg, const char *def)
26 {
27 return lua_gettop(L) < abs(narg) ? def : lua_tostring(L, narg);
28 }
29
30 TOLUA_API void *tolua_touserdata(lua_State *L, int narg, void *def)
31 {
32
33 /* return lua_gettop(L)<abs(narg) ? def : lua_touserdata(L,narg); */
34
35 if (lua_gettop(L) < abs(narg))
36 {
37 return def;
38 };
39
40 if (lua_islightuserdata(L, narg))
41 {
42
43 return lua_touserdata(L, narg);
44 };
45
46 return tolua_tousertype(L, narg, def);
47 }
48
49 extern int push_table_instance(lua_State *L, int lo);
50
51 TOLUA_API void *tolua_tousertype(lua_State *L, int narg, void *def)
52 {
53 if (lua_gettop(L) < abs(narg))
54 return def;
55 else
56 {
57 void *u;
58 if (!lua_isuserdata(L, narg))
59 {
60 if (!push_table_instance(L, narg))
61 return NULL;
62 };
63 u = lua_touserdata(L, narg);
64 return (u == NULL) ? NULL : *((void **)u); /* nil represents NULL */
65 }
66 }
67
68 TOLUA_API int tolua_tovalue(lua_State *L, int narg, int def)
69 {
70 return lua_gettop(L) < abs(narg) ? def : narg;
71 }
72
73 TOLUA_API int tolua_toboolean(lua_State *L, int narg, int def)
74 {
75 return lua_gettop(L) < abs(narg) ? def : lua_toboolean(L, narg);
76 }
77
78 TOLUA_API lua_Number tolua_tofieldnumber(lua_State *L, int lo, int index, lua_Number def)
79 {
80 double v;
81 lua_pushnumber(L, index);
82 lua_gettable(L, lo);
83 v = lua_isnil(L, -1) ? def : lua_tonumber(L, -1);
84 lua_pop(L, 1);
85 return v;
86 }
87
88 TOLUA_API const char *tolua_tofieldstring(lua_State *L, int lo, int index, const char *def)
89 {
90 const char *v;
91 lua_pushnumber(L, index);
92 lua_gettable(L, lo);
93 v = lua_isnil(L, -1) ? def : lua_tostring(L, -1);
94 lua_pop(L, 1);
95 return v;
96 }
97
98 TOLUA_API void *tolua_tofielduserdata(lua_State *L, int lo, int index, void *def)
99 {
100 void *v;
101 lua_pushnumber(L, index);
102 lua_gettable(L, lo);
103 v = lua_isnil(L, -1) ? def : lua_touserdata(L, -1);
104 lua_pop(L, 1);
105 return v;
106 }
107
108 TOLUA_API void *tolua_tofieldusertype(lua_State *L, int lo, int index, void *def)
109 {
110 void *v;
111 lua_pushnumber(L, index);
112 lua_gettable(L, lo);
113 v = lua_isnil(L, -1) ? def : (*(void **)(lua_touserdata(L, -1))); /* lua_unboxpointer(L,-1); */
114 lua_pop(L, 1);
115 return v;
116 }
117
118 TOLUA_API int tolua_tofieldvalue(lua_State *L, int lo, int index, int def)
119 {
120 int v;
121 lua_pushnumber(L, index);
122 lua_gettable(L, lo);
123 v = lua_isnil(L, -1) ? def : lo;
124 lua_pop(L, 1);
125 return v;
126 }
127
128 TOLUA_API int tolua_getfieldboolean(lua_State *L, int lo, int index, int def)
129 {
130 int v;
131 (void)def;
132 lua_pushnumber(L, index);
133 lua_gettable(L, lo);
134 v = lua_isnil(L, -1) ? 0 : lua_toboolean(L, -1);
135 lua_pop(L, 1);
136 return v;
137 }