1 /* tolua: funcitons to convert to C types
2 ** Support code for Lua bindings.
3 ** Written by Waldemar Celes
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.
20 TOLUA_API lua_Number tolua_tonumber (lua_State* L, int narg, lua_Number def)
22 return lua_gettop(L)<abs(narg) ? def : lua_tonumber(L,narg);
25 TOLUA_API const char* tolua_tostring (lua_State* L, int narg, const char* def)
27 return lua_gettop(L)<abs(narg) ? def : lua_tostring(L,narg);
30 TOLUA_API void* tolua_touserdata (lua_State* L, int narg, void* def)
33 /* return lua_gettop(L)<abs(narg) ? def : lua_touserdata(L,narg); */
35 if (lua_gettop(L)<abs(narg)) {
39 if (lua_islightuserdata(L, narg)) {
41 return lua_touserdata(L,narg);
44 return tolua_tousertype(L, narg, def);
47 extern int push_table_instance(lua_State* L, int lo);
49 TOLUA_API void* tolua_tousertype (lua_State* L, int narg, void* def)
51 if (lua_gettop(L)<abs(narg))
56 if (!lua_isuserdata(L, narg)) {
57 if (!push_table_instance(L, narg)) return NULL;
59 u = lua_touserdata(L,narg);
60 return (u==NULL) ? NULL : *((void**)u); /* nil represents NULL */
64 TOLUA_API int tolua_tovalue (lua_State* L, int narg, int def)
66 return lua_gettop(L)<abs(narg) ? def : narg;
69 TOLUA_API int tolua_toboolean (lua_State* L, int narg, int def)
71 return lua_gettop(L)<abs(narg) ? def : lua_toboolean(L,narg);
74 TOLUA_API lua_Number tolua_tofieldnumber (lua_State* L, int lo, int index, lua_Number def)
77 lua_pushnumber(L,index);
79 v = lua_isnil(L,-1) ? def : lua_tonumber(L,-1);
84 TOLUA_API const char* tolua_tofieldstring
85 (lua_State* L, int lo, int index, const char* def)
88 lua_pushnumber(L,index);
90 v = lua_isnil(L,-1) ? def : lua_tostring(L,-1);
95 TOLUA_API void* tolua_tofielduserdata (lua_State* L, int lo, int index, void* def)
98 lua_pushnumber(L,index);
100 v = lua_isnil(L,-1) ? def : lua_touserdata(L,-1);
105 TOLUA_API void* tolua_tofieldusertype (lua_State* L, int lo, int index, void* def)
108 lua_pushnumber(L,index);
110 v = lua_isnil(L,-1) ? def : (*(void **)(lua_touserdata(L, -1))); /* lua_unboxpointer(L,-1); */
115 TOLUA_API int tolua_tofieldvalue (lua_State* L, int lo, int index, int def)
118 lua_pushnumber(L,index);
120 v = lua_isnil(L,-1) ? def : lo;
125 TOLUA_API int tolua_getfieldboolean (lua_State* L, int lo, int index, int def)
128 lua_pushnumber(L,index);
130 v = lua_isnil(L,-1) ? 0 : lua_toboolean(L,-1);