00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "tolua++.h"
00015
00016 #include <string.h>
00017 #include <stdlib.h>
00018
00019 TOLUA_API double tolua_tonumber (lua_State* L, int narg, double def)
00020 {
00021 return lua_gettop(L)<abs(narg) ? def : lua_tonumber(L,narg);
00022 }
00023
00024 TOLUA_API const char* tolua_tostring (lua_State* L, int narg, const char* def)
00025 {
00026 return lua_gettop(L)<abs(narg) ? def : lua_tostring(L,narg);
00027 }
00028
00029 TOLUA_API void* tolua_touserdata (lua_State* L, int narg, void* def)
00030 {
00031
00032
00033
00034 if (lua_gettop(L)<abs(narg)) {
00035 return def;
00036 };
00037
00038 if (lua_islightuserdata(L, narg)) {
00039
00040 return lua_touserdata(L,narg);
00041 };
00042
00043 return tolua_tousertype(L, narg, def);
00044 }
00045
00046 extern int push_table_instance(lua_State* L, int lo);
00047
00048 TOLUA_API void* tolua_tousertype (lua_State* L, int narg, void* def)
00049 {
00050 if (lua_gettop(L)<abs(narg))
00051 return def;
00052 else
00053 {
00054 void* u;
00055 if (!lua_isuserdata(L, narg)) {
00056 if (!push_table_instance(L, narg)) return NULL;
00057 };
00058 u = lua_touserdata(L,narg);
00059 return (u==NULL) ? NULL : *((void**)u);
00060 }
00061 }
00062
00063 TOLUA_API int tolua_tovalue (lua_State* L, int narg, int def)
00064 {
00065 return lua_gettop(L)<abs(narg) ? def : narg;
00066 }
00067
00068 TOLUA_API int tolua_toboolean (lua_State* L, int narg, int def)
00069 {
00070 return lua_gettop(L)<abs(narg) ? def : lua_toboolean(L,narg);
00071 }
00072
00073 TOLUA_API double tolua_tofieldnumber (lua_State* L, int lo, int index, double def)
00074 {
00075 double v;
00076 lua_pushnumber(L,index);
00077 lua_gettable(L,lo);
00078 v = lua_isnil(L,-1) ? def : lua_tonumber(L,-1);
00079 lua_pop(L,1);
00080 return v;
00081 }
00082
00083 TOLUA_API const char* tolua_tofieldstring
00084 (lua_State* L, int lo, int index, const char* def)
00085 {
00086 const char* v;
00087 lua_pushnumber(L,index);
00088 lua_gettable(L,lo);
00089 v = lua_isnil(L,-1) ? def : lua_tostring(L,-1);
00090 lua_pop(L,1);
00091 return v;
00092 }
00093
00094 TOLUA_API void* tolua_tofielduserdata (lua_State* L, int lo, int index, void* def)
00095 {
00096 void* v;
00097 lua_pushnumber(L,index);
00098 lua_gettable(L,lo);
00099 v = lua_isnil(L,-1) ? def : lua_touserdata(L,-1);
00100 lua_pop(L,1);
00101 return v;
00102 }
00103
00104 TOLUA_API void* tolua_tofieldusertype (lua_State* L, int lo, int index, void* def)
00105 {
00106 void* v;
00107 lua_pushnumber(L,index);
00108 lua_gettable(L,lo);
00109 v = lua_isnil(L,-1) ? def : (*(void **)(lua_touserdata(L, -1)));
00110 lua_pop(L,1);
00111 return v;
00112 }
00113
00114 TOLUA_API int tolua_tofieldvalue (lua_State* L, int lo, int index, int def)
00115 {
00116 int v;
00117 lua_pushnumber(L,index);
00118 lua_gettable(L,lo);
00119 v = lua_isnil(L,-1) ? def : lo;
00120 lua_pop(L,1);
00121 return v;
00122 }
00123
00124 TOLUA_API int tolua_getfieldboolean (lua_State* L, int lo, int index, int def)
00125 {
00126 int v;
00127 lua_pushnumber(L,index);
00128 lua_gettable(L,lo);
00129 v = lua_isnil(L,-1) ? 0 : lua_toboolean(L,-1);
00130 lua_pop(L,1);
00131 return v;
00132 }