* update tolua++ source files

This commit is contained in:
YuLei Liao 2012-02-01 16:55:28 +08:00
parent e8e4de7077
commit 871e2b3b9c
6 changed files with 1511 additions and 1482 deletions

View File

@ -20,20 +20,20 @@
#define TOLUA_API extern #define TOLUA_API extern
#endif #endif
#define TOLUA_VERSION "tolua++-1.0.92" #define TOLUA_VERSION "tolua++-1.0.93"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#define tolua_pushcppstring(x,y) tolua_pushstring(x,y.c_str()) #define tolua_pushcppstring(x,y) tolua_pushstring(x,y.c_str())
#define tolua_iscppstring tolua_isstring #define tolua_iscppstring tolua_isstring
#define tolua_iscppstringarray tolua_isstringarray #define tolua_iscppstringarray tolua_isstringarray
#define tolua_pushfieldcppstring(L,lo,idx,s) tolua_pushfieldstring(L, lo, idx, s.c_str()) #define tolua_pushfieldcppstring(L,lo,idx,s) tolua_pushfieldstring(L, lo, idx, s.c_str())
#ifndef TEMPLATE_BIND #ifndef TEMPLATE_BIND
#define TEMPLATE_BIND(p) #define TEMPLATE_BIND(p)
#endif #endif
#define TOLUA_TEMPLATE_BIND(p) #define TOLUA_TEMPLATE_BIND(p)
@ -48,9 +48,9 @@ typedef int lua_Object;
struct tolua_Error struct tolua_Error
{ {
int index; int index;
int array; int array;
const char* type; const char* type;
}; };
typedef struct tolua_Error tolua_Error; typedef struct tolua_Error tolua_Error;
@ -69,19 +69,19 @@ TOLUA_API int tolua_isusertable (lua_State* L, int lo, const char* type, int def
TOLUA_API int tolua_isuserdata (lua_State* L, int lo, int def, tolua_Error* err); TOLUA_API int tolua_isuserdata (lua_State* L, int lo, int def, tolua_Error* err);
TOLUA_API int tolua_isusertype (lua_State* L, int lo, const char* type, int def, tolua_Error* err); TOLUA_API int tolua_isusertype (lua_State* L, int lo, const char* type, int def, tolua_Error* err);
TOLUA_API int tolua_isvaluearray TOLUA_API int tolua_isvaluearray
(lua_State* L, int lo, int dim, int def, tolua_Error* err); (lua_State* L, int lo, int dim, int def, tolua_Error* err);
TOLUA_API int tolua_isbooleanarray TOLUA_API int tolua_isbooleanarray
(lua_State* L, int lo, int dim, int def, tolua_Error* err); (lua_State* L, int lo, int dim, int def, tolua_Error* err);
TOLUA_API int tolua_isnumberarray TOLUA_API int tolua_isnumberarray
(lua_State* L, int lo, int dim, int def, tolua_Error* err); (lua_State* L, int lo, int dim, int def, tolua_Error* err);
TOLUA_API int tolua_isstringarray TOLUA_API int tolua_isstringarray
(lua_State* L, int lo, int dim, int def, tolua_Error* err); (lua_State* L, int lo, int dim, int def, tolua_Error* err);
TOLUA_API int tolua_istablearray TOLUA_API int tolua_istablearray
(lua_State* L, int lo, int dim, int def, tolua_Error* err); (lua_State* L, int lo, int dim, int def, tolua_Error* err);
TOLUA_API int tolua_isuserdataarray TOLUA_API int tolua_isuserdataarray
(lua_State* L, int lo, int dim, int def, tolua_Error* err); (lua_State* L, int lo, int dim, int def, tolua_Error* err);
TOLUA_API int tolua_isusertypearray TOLUA_API int tolua_isusertypearray
(lua_State* L, int lo, const char* type, int dim, int def, tolua_Error* err); (lua_State* L, int lo, const char* type, int dim, int def, tolua_Error* err);
TOLUA_API void tolua_open (lua_State* L); TOLUA_API void tolua_open (lua_State* L);
@ -101,7 +101,7 @@ TOLUA_API void tolua_variable (lua_State* L, const char* name, lua_CFunction get
TOLUA_API void tolua_array (lua_State* L,const char* name, lua_CFunction get, lua_CFunction set); TOLUA_API void tolua_array (lua_State* L,const char* name, lua_CFunction get, lua_CFunction set);
/* TOLUA_API void tolua_set_call_event(lua_State* L, lua_CFunction func, char* type); */ /* TOLUA_API void tolua_set_call_event(lua_State* L, lua_CFunction func, char* type); */
TOLUA_API void tolua_addbase(lua_State* L, char* name, char* base); TOLUA_API void tolua_addbase(lua_State* L, char* name, char* base);
TOLUA_API void tolua_pushvalue (lua_State* L, int lo); TOLUA_API void tolua_pushvalue (lua_State* L, int lo);
TOLUA_API void tolua_pushboolean (lua_State* L, int value); TOLUA_API void tolua_pushboolean (lua_State* L, int value);
@ -138,14 +138,14 @@ TOLUA_API int class_gc_event (lua_State* L);
#ifdef __cplusplus #ifdef __cplusplus
static inline const char* tolua_tocppstring (lua_State* L, int narg, const char* def) { static inline const char* tolua_tocppstring (lua_State* L, int narg, const char* def) {
const char* s = tolua_tostring(L, narg, def); const char* s = tolua_tostring(L, narg, def);
return s?s:""; return s?s:"";
}; };
static inline const char* tolua_tofieldcppstring (lua_State* L, int lo, int index, const char* def) { static inline const char* tolua_tofieldcppstring (lua_State* L, int lo, int index, const char* def) {
const char* s = tolua_tofieldstring(L, lo, index, def); const char* s = tolua_tofieldstring(L, lo, index, def);
return s?s:""; return s?s:"";
}; };
#else #else

View File

@ -22,112 +22,112 @@
*/ */
static void storeatubox (lua_State* L, int lo) static void storeatubox (lua_State* L, int lo)
{ {
#ifdef LUA_VERSION_NUM #ifdef LUA_VERSION_NUM
lua_getfenv(L, lo); lua_getfenv(L, lo);
if (lua_rawequal(L, -1, TOLUA_NOPEER)) { if (lua_rawequal(L, -1, TOLUA_NOPEER)) {
lua_pop(L, 1); lua_pop(L, 1);
lua_newtable(L); lua_newtable(L);
lua_pushvalue(L, -1); lua_pushvalue(L, -1);
lua_setfenv(L, lo); /* stack: k,v,table */ lua_setfenv(L, lo); /* stack: k,v,table */
}; };
lua_insert(L, -3); lua_insert(L, -3);
lua_settable(L, -3); /* on lua 5.1, we trade the "tolua_peers" lookup for a settable call */ lua_settable(L, -3); /* on lua 5.1, we trade the "tolua_peers" lookup for a settable call */
lua_pop(L, 1); lua_pop(L, 1);
#else #else
/* stack: key value (to be stored) */ /* stack: key value (to be stored) */
lua_pushstring(L,"tolua_peers"); lua_pushstring(L,"tolua_peers");
lua_rawget(L,LUA_REGISTRYINDEX); /* stack: k v ubox */ lua_rawget(L,LUA_REGISTRYINDEX); /* stack: k v ubox */
lua_pushvalue(L,lo); lua_pushvalue(L,lo);
lua_rawget(L,-2); /* stack: k v ubox ubox[u] */ lua_rawget(L,-2); /* stack: k v ubox ubox[u] */
if (!lua_istable(L,-1)) if (!lua_istable(L,-1))
{ {
lua_pop(L,1); /* stack: k v ubox */ lua_pop(L,1); /* stack: k v ubox */
lua_newtable(L); /* stack: k v ubox table */ lua_newtable(L); /* stack: k v ubox table */
lua_pushvalue(L,1); lua_pushvalue(L,1);
lua_pushvalue(L,-2); /* stack: k v ubox table u table */ lua_pushvalue(L,-2); /* stack: k v ubox table u table */
lua_rawset(L,-4); /* stack: k v ubox ubox[u]=table */ lua_rawset(L,-4); /* stack: k v ubox ubox[u]=table */
} }
lua_insert(L,-4); /* put table before k */ lua_insert(L,-4); /* put table before k */
lua_pop(L,1); /* pop ubox */ lua_pop(L,1); /* pop ubox */
lua_rawset(L,-3); /* store at table */ lua_rawset(L,-3); /* store at table */
lua_pop(L,1); /* pop ubox[u] */ lua_pop(L,1); /* pop ubox[u] */
#endif #endif
} }
/* Module index function /* Module index function
*/ */
static int module_index_event (lua_State* L) static int module_index_event (lua_State* L)
{ {
lua_pushstring(L,".get"); lua_pushstring(L,".get");
lua_rawget(L,-3); lua_rawget(L,-3);
if (lua_istable(L,-1)) if (lua_istable(L,-1))
{ {
lua_pushvalue(L,2); /* key */ lua_pushvalue(L,2); /* key */
lua_rawget(L,-2); lua_rawget(L,-2);
if (lua_iscfunction(L,-1)) if (lua_iscfunction(L,-1))
{ {
lua_call(L,0,1); lua_call(L,0,1);
return 1; return 1;
} }
else if (lua_istable(L,-1)) else if (lua_istable(L,-1))
return 1; return 1;
} }
/* call old index meta event */ /* call old index meta event */
if (lua_getmetatable(L,1)) if (lua_getmetatable(L,1))
{ {
lua_pushstring(L,"__index"); lua_pushstring(L,"__index");
lua_rawget(L,-2); lua_rawget(L,-2);
lua_pushvalue(L,1); lua_pushvalue(L,1);
lua_pushvalue(L,2); lua_pushvalue(L,2);
if (lua_isfunction(L,-1)) if (lua_isfunction(L,-1))
{ {
lua_call(L,2,1); lua_call(L,2,1);
return 1; return 1;
} }
else if (lua_istable(L,-1)) else if (lua_istable(L,-1))
{ {
lua_gettable(L,-3); lua_gettable(L,-3);
return 1; return 1;
} }
} }
lua_pushnil(L); lua_pushnil(L);
return 1; return 1;
} }
/* Module newindex function /* Module newindex function
*/ */
static int module_newindex_event (lua_State* L) static int module_newindex_event (lua_State* L)
{ {
lua_pushstring(L,".set"); lua_pushstring(L,".set");
lua_rawget(L,-4); lua_rawget(L,-4);
if (lua_istable(L,-1)) if (lua_istable(L,-1))
{ {
lua_pushvalue(L,2); /* key */ lua_pushvalue(L,2); /* key */
lua_rawget(L,-2); lua_rawget(L,-2);
if (lua_iscfunction(L,-1)) if (lua_iscfunction(L,-1))
{ {
lua_pushvalue(L,1); /* only to be compatible with non-static vars */ lua_pushvalue(L,1); /* only to be compatible with non-static vars */
lua_pushvalue(L,3); /* value */ lua_pushvalue(L,3); /* value */
lua_call(L,2,0); lua_call(L,2,0);
return 0; return 0;
} }
} }
/* call old newindex meta event */ /* call old newindex meta event */
if (lua_getmetatable(L,1) && lua_getmetatable(L,-1)) if (lua_getmetatable(L,1) && lua_getmetatable(L,-1))
{ {
lua_pushstring(L,"__newindex"); lua_pushstring(L,"__newindex");
lua_rawget(L,-2); lua_rawget(L,-2);
if (lua_isfunction(L,-1)) if (lua_isfunction(L,-1))
{ {
lua_pushvalue(L,1); lua_pushvalue(L,1);
lua_pushvalue(L,2); lua_pushvalue(L,2);
lua_pushvalue(L,3); lua_pushvalue(L,3);
lua_call(L,3,0); lua_call(L,3,0);
} }
} }
lua_settop(L,3); lua_settop(L,3);
lua_rawset(L,-3); lua_rawset(L,-3);
return 0; return 0;
} }
/* Class index function /* Class index function
@ -136,102 +136,102 @@ static int module_newindex_event (lua_State* L)
*/ */
static int class_index_event (lua_State* L) static int class_index_event (lua_State* L)
{ {
int t = lua_type(L,1); int t = lua_type(L,1);
if (t == LUA_TUSERDATA) if (t == LUA_TUSERDATA)
{ {
/* Access alternative table */ /* Access alternative table */
#ifdef LUA_VERSION_NUM /* new macro on version 5.1 */ #ifdef LUA_VERSION_NUM /* new macro on version 5.1 */
lua_getfenv(L,1); lua_getfenv(L,1);
if (!lua_rawequal(L, -1, TOLUA_NOPEER)) { if (!lua_rawequal(L, -1, TOLUA_NOPEER)) {
lua_pushvalue(L, 2); /* key */ lua_pushvalue(L, 2); /* key */
lua_gettable(L, -2); /* on lua 5.1, we trade the "tolua_peers" lookup for a gettable call */ lua_gettable(L, -2); /* on lua 5.1, we trade the "tolua_peers" lookup for a gettable call */
if (!lua_isnil(L, -1)) if (!lua_isnil(L, -1))
return 1; return 1;
}; };
#else #else
lua_pushstring(L,"tolua_peers"); lua_pushstring(L,"tolua_peers");
lua_rawget(L,LUA_REGISTRYINDEX); /* stack: obj key ubox */ lua_rawget(L,LUA_REGISTRYINDEX); /* stack: obj key ubox */
lua_pushvalue(L,1); lua_pushvalue(L,1);
lua_rawget(L,-2); /* stack: obj key ubox ubox[u] */ lua_rawget(L,-2); /* stack: obj key ubox ubox[u] */
if (lua_istable(L,-1)) if (lua_istable(L,-1))
{ {
lua_pushvalue(L,2); /* key */ lua_pushvalue(L,2); /* key */
lua_rawget(L,-2); /* stack: obj key ubox ubox[u] value */ lua_rawget(L,-2); /* stack: obj key ubox ubox[u] value */
if (!lua_isnil(L,-1)) if (!lua_isnil(L,-1))
return 1; return 1;
} }
#endif #endif
lua_settop(L,2); /* stack: obj key */ lua_settop(L,2); /* stack: obj key */
/* Try metatables */ /* Try metatables */
lua_pushvalue(L,1); /* stack: obj key obj */ lua_pushvalue(L,1); /* stack: obj key obj */
while (lua_getmetatable(L,-1)) while (lua_getmetatable(L,-1))
{ /* stack: obj key obj mt */ { /* stack: obj key obj mt */
lua_remove(L,-2); /* stack: obj key mt */ lua_remove(L,-2); /* stack: obj key mt */
if (lua_isnumber(L,2)) /* check if key is a numeric value */ if (lua_isnumber(L,2)) /* check if key is a numeric value */
{ {
/* try operator[] */ /* try operator[] */
lua_pushstring(L,".geti"); lua_pushstring(L,".geti");
lua_rawget(L,-2); /* stack: obj key mt func */ lua_rawget(L,-2); /* stack: obj key mt func */
if (lua_isfunction(L,-1)) if (lua_isfunction(L,-1))
{ {
lua_pushvalue(L,1); lua_pushvalue(L,1);
lua_pushvalue(L,2); lua_pushvalue(L,2);
lua_call(L,2,1); lua_call(L,2,1);
return 1; return 1;
} }
} }
else else
{ {
lua_pushvalue(L,2); /* stack: obj key mt key */ lua_pushvalue(L,2); /* stack: obj key mt key */
lua_rawget(L,-2); /* stack: obj key mt value */ lua_rawget(L,-2); /* stack: obj key mt value */
if (!lua_isnil(L,-1)) if (!lua_isnil(L,-1))
return 1; return 1;
else else
lua_pop(L,1); lua_pop(L,1);
/* try C/C++ variable */ /* try C/C++ variable */
lua_pushstring(L,".get"); lua_pushstring(L,".get");
lua_rawget(L,-2); /* stack: obj key mt tget */ lua_rawget(L,-2); /* stack: obj key mt tget */
if (lua_istable(L,-1)) if (lua_istable(L,-1))
{ {
lua_pushvalue(L,2); lua_pushvalue(L,2);
lua_rawget(L,-2); /* stack: obj key mt value */ lua_rawget(L,-2); /* stack: obj key mt value */
if (lua_iscfunction(L,-1)) if (lua_iscfunction(L,-1))
{ {
lua_pushvalue(L,1); lua_pushvalue(L,1);
lua_pushvalue(L,2); lua_pushvalue(L,2);
lua_call(L,2,1); lua_call(L,2,1);
return 1; return 1;
} }
else if (lua_istable(L,-1)) else if (lua_istable(L,-1))
{ {
/* deal with array: create table to be returned and cache it in ubox */ /* deal with array: create table to be returned and cache it in ubox */
void* u = *((void**)lua_touserdata(L,1)); void* u = *((void**)lua_touserdata(L,1));
lua_newtable(L); /* stack: obj key mt value table */ lua_newtable(L); /* stack: obj key mt value table */
lua_pushstring(L,".self"); lua_pushstring(L,".self");
lua_pushlightuserdata(L,u); lua_pushlightuserdata(L,u);
lua_rawset(L,-3); /* store usertype in ".self" */ lua_rawset(L,-3); /* store usertype in ".self" */
lua_insert(L,-2); /* stack: obj key mt table value */ lua_insert(L,-2); /* stack: obj key mt table value */
lua_setmetatable(L,-2); /* set stored value as metatable */ lua_setmetatable(L,-2); /* set stored value as metatable */
lua_pushvalue(L,-1); /* stack: obj key met table table */ lua_pushvalue(L,-1); /* stack: obj key met table table */
lua_pushvalue(L,2); /* stack: obj key mt table table key */ lua_pushvalue(L,2); /* stack: obj key mt table table key */
lua_insert(L,-2); /* stack: obj key mt table key table */ lua_insert(L,-2); /* stack: obj key mt table key table */
storeatubox(L,1); /* stack: obj key mt table */ storeatubox(L,1); /* stack: obj key mt table */
return 1; return 1;
} }
} }
} }
lua_settop(L,3); lua_settop(L,3);
} }
lua_pushnil(L); lua_pushnil(L);
return 1; return 1;
} }
else if (t== LUA_TTABLE) else if (t== LUA_TTABLE)
{ {
module_index_event(L); module_index_event(L);
return 1; return 1;
} }
lua_pushnil(L); lua_pushnil(L);
return 1; return 1;
} }
/* Newindex function /* Newindex function
@ -241,160 +241,160 @@ static int class_index_event (lua_State* L)
*/ */
static int class_newindex_event (lua_State* L) static int class_newindex_event (lua_State* L)
{ {
int t = lua_type(L,1); int t = lua_type(L,1);
if (t == LUA_TUSERDATA) if (t == LUA_TUSERDATA)
{ {
/* Try accessing a C/C++ variable to be set */ /* Try accessing a C/C++ variable to be set */
lua_getmetatable(L,1); lua_getmetatable(L,1);
while (lua_istable(L,-1)) /* stack: t k v mt */ while (lua_istable(L,-1)) /* stack: t k v mt */
{ {
if (lua_isnumber(L,2)) /* check if key is a numeric value */ if (lua_isnumber(L,2)) /* check if key is a numeric value */
{ {
/* try operator[] */ /* try operator[] */
lua_pushstring(L,".seti"); lua_pushstring(L,".seti");
lua_rawget(L,-2); /* stack: obj key mt func */ lua_rawget(L,-2); /* stack: obj key mt func */
if (lua_isfunction(L,-1)) if (lua_isfunction(L,-1))
{ {
lua_pushvalue(L,1); lua_pushvalue(L,1);
lua_pushvalue(L,2); lua_pushvalue(L,2);
lua_pushvalue(L,3); lua_pushvalue(L,3);
lua_call(L,3,0); lua_call(L,3,0);
return 0; return 0;
} }
} }
else else
{ {
lua_pushstring(L,".set"); lua_pushstring(L,".set");
lua_rawget(L,-2); /* stack: t k v mt tset */ lua_rawget(L,-2); /* stack: t k v mt tset */
if (lua_istable(L,-1)) if (lua_istable(L,-1))
{ {
lua_pushvalue(L,2); lua_pushvalue(L,2);
lua_rawget(L,-2); /* stack: t k v mt tset func */ lua_rawget(L,-2); /* stack: t k v mt tset func */
if (lua_iscfunction(L,-1)) if (lua_iscfunction(L,-1))
{ {
lua_pushvalue(L,1); lua_pushvalue(L,1);
lua_pushvalue(L,3); lua_pushvalue(L,3);
lua_call(L,2,0); lua_call(L,2,0);
return 0; return 0;
} }
lua_pop(L,1); /* stack: t k v mt tset */ lua_pop(L,1); /* stack: t k v mt tset */
} }
lua_pop(L,1); /* stack: t k v mt */ lua_pop(L,1); /* stack: t k v mt */
if (!lua_getmetatable(L,-1)) /* stack: t k v mt mt */ if (!lua_getmetatable(L,-1)) /* stack: t k v mt mt */
lua_pushnil(L); lua_pushnil(L);
lua_remove(L,-2); /* stack: t k v mt */ lua_remove(L,-2); /* stack: t k v mt */
} }
} }
lua_settop(L,3); /* stack: t k v */ lua_settop(L,3); /* stack: t k v */
/* then, store as a new field */ /* then, store as a new field */
storeatubox(L,1); storeatubox(L,1);
} }
else if (t== LUA_TTABLE) else if (t== LUA_TTABLE)
{ {
module_newindex_event(L); module_newindex_event(L);
} }
return 0; return 0;
} }
static int class_call_event(lua_State* L) { static int class_call_event(lua_State* L) {
if (lua_istable(L, 1)) { if (lua_istable(L, 1)) {
lua_pushstring(L, ".call"); lua_pushstring(L, ".call");
lua_rawget(L, 1); lua_rawget(L, 1);
if (lua_isfunction(L, -1)) { if (lua_isfunction(L, -1)) {
lua_insert(L, 1); lua_insert(L, 1);
lua_call(L, lua_gettop(L)-1, 1); lua_call(L, lua_gettop(L)-1, 1);
return 1; return 1;
}; };
}; };
tolua_error(L,"Attempt to call a non-callable object.",NULL); tolua_error(L,"Attempt to call a non-callable object.",NULL);
return 0; return 0;
}; };
static int do_operator (lua_State* L, const char* op) static int do_operator (lua_State* L, const char* op)
{ {
if (lua_isuserdata(L,1)) if (lua_isuserdata(L,1))
{ {
/* Try metatables */ /* Try metatables */
lua_pushvalue(L,1); /* stack: op1 op2 */ lua_pushvalue(L,1); /* stack: op1 op2 */
while (lua_getmetatable(L,-1)) while (lua_getmetatable(L,-1))
{ /* stack: op1 op2 op1 mt */ { /* stack: op1 op2 op1 mt */
lua_remove(L,-2); /* stack: op1 op2 mt */ lua_remove(L,-2); /* stack: op1 op2 mt */
lua_pushstring(L,op); /* stack: op1 op2 mt key */ lua_pushstring(L,op); /* stack: op1 op2 mt key */
lua_rawget(L,-2); /* stack: obj key mt func */ lua_rawget(L,-2); /* stack: obj key mt func */
if (lua_isfunction(L,-1)) if (lua_isfunction(L,-1))
{ {
lua_pushvalue(L,1); lua_pushvalue(L,1);
lua_pushvalue(L,2); lua_pushvalue(L,2);
lua_call(L,2,1); lua_call(L,2,1);
return 1; return 1;
} }
lua_settop(L,3); lua_settop(L,3);
} }
} }
tolua_error(L,"Attempt to perform operation on an invalid operand",NULL); tolua_error(L,"Attempt to perform operation on an invalid operand",NULL);
return 0; return 0;
} }
static int class_add_event (lua_State* L) static int class_add_event (lua_State* L)
{ {
return do_operator(L,".add"); return do_operator(L,".add");
} }
static int class_sub_event (lua_State* L) static int class_sub_event (lua_State* L)
{ {
return do_operator(L,".sub"); return do_operator(L,".sub");
} }
static int class_mul_event (lua_State* L) static int class_mul_event (lua_State* L)
{ {
return do_operator(L,".mul"); return do_operator(L,".mul");
} }
static int class_div_event (lua_State* L) static int class_div_event (lua_State* L)
{ {
return do_operator(L,".div"); return do_operator(L,".div");
} }
static int class_lt_event (lua_State* L) static int class_lt_event (lua_State* L)
{ {
return do_operator(L,".lt"); return do_operator(L,".lt");
} }
static int class_le_event (lua_State* L) static int class_le_event (lua_State* L)
{ {
return do_operator(L,".le"); return do_operator(L,".le");
} }
static int class_eq_event (lua_State* L) static int class_eq_event (lua_State* L)
{ {
/* copying code from do_operator here to return false when no operator is found */ /* copying code from do_operator here to return false when no operator is found */
if (lua_isuserdata(L,1)) if (lua_isuserdata(L,1))
{ {
/* Try metatables */ /* Try metatables */
lua_pushvalue(L,1); /* stack: op1 op2 */ lua_pushvalue(L,1); /* stack: op1 op2 */
while (lua_getmetatable(L,-1)) while (lua_getmetatable(L,-1))
{ /* stack: op1 op2 op1 mt */ { /* stack: op1 op2 op1 mt */
lua_remove(L,-2); /* stack: op1 op2 mt */ lua_remove(L,-2); /* stack: op1 op2 mt */
lua_pushstring(L,".eq"); /* stack: op1 op2 mt key */ lua_pushstring(L,".eq"); /* stack: op1 op2 mt key */
lua_rawget(L,-2); /* stack: obj key mt func */ lua_rawget(L,-2); /* stack: obj key mt func */
if (lua_isfunction(L,-1)) if (lua_isfunction(L,-1))
{ {
lua_pushvalue(L,1); lua_pushvalue(L,1);
lua_pushvalue(L,2); lua_pushvalue(L,2);
lua_call(L,2,1); lua_call(L,2,1);
return 1; return 1;
} }
lua_settop(L,3); lua_settop(L,3);
} }
} }
lua_settop(L, 3); lua_settop(L, 3);
lua_pushboolean(L, 0); lua_pushboolean(L, 0);
return 1; return 1;
} }
/* /*
@ -420,43 +420,43 @@ static int class_gc_event (lua_State* L)
*/ */
TOLUA_API int class_gc_event (lua_State* L) TOLUA_API int class_gc_event (lua_State* L)
{ {
void* u = *((void**)lua_touserdata(L,1)); void* u = *((void**)lua_touserdata(L,1));
int top; int top;
/*fprintf(stderr, "collecting: looking at %p\n", u);*/ /*fprintf(stderr, "collecting: looking at %p\n", u);*/
/* /*
lua_pushstring(L,"tolua_gc"); lua_pushstring(L,"tolua_gc");
lua_rawget(L,LUA_REGISTRYINDEX); lua_rawget(L,LUA_REGISTRYINDEX);
*/ */
lua_pushvalue(L, lua_upvalueindex(1)); lua_pushvalue(L, lua_upvalueindex(1));
lua_pushlightuserdata(L,u); lua_pushlightuserdata(L,u);
lua_rawget(L,-2); /* stack: gc umt */ lua_rawget(L,-2); /* stack: gc umt */
lua_getmetatable(L,1); /* stack: gc umt mt */ lua_getmetatable(L,1); /* stack: gc umt mt */
/*fprintf(stderr, "checking type\n");*/ /*fprintf(stderr, "checking type\n");*/
top = lua_gettop(L); top = lua_gettop(L);
if (tolua_fast_isa(L,top,top-1, lua_upvalueindex(2))) /* make sure we collect correct type */ if (tolua_fast_isa(L,top,top-1, lua_upvalueindex(2))) /* make sure we collect correct type */
{ {
/*fprintf(stderr, "Found type!\n");*/ /*fprintf(stderr, "Found type!\n");*/
/* get gc function */ /* get gc function */
lua_pushliteral(L,".collector"); lua_pushliteral(L,".collector");
lua_rawget(L,-2); /* stack: gc umt mt collector */ lua_rawget(L,-2); /* stack: gc umt mt collector */
if (lua_isfunction(L,-1)) { if (lua_isfunction(L,-1)) {
/*fprintf(stderr, "Found .collector!\n");*/ /*fprintf(stderr, "Found .collector!\n");*/
} }
else { else {
lua_pop(L,1); lua_pop(L,1);
/*fprintf(stderr, "Using default cleanup\n");*/ /*fprintf(stderr, "Using default cleanup\n");*/
lua_pushcfunction(L,tolua_default_collect); lua_pushcfunction(L,tolua_default_collect);
} }
lua_pushvalue(L,1); /* stack: gc umt mt collector u */ lua_pushvalue(L,1); /* stack: gc umt mt collector u */
lua_call(L,1,0); lua_call(L,1,0);
lua_pushlightuserdata(L,u); /* stack: gc umt mt u */ lua_pushlightuserdata(L,u); /* stack: gc umt mt u */
lua_pushnil(L); /* stack: gc umt mt u nil */ lua_pushnil(L); /* stack: gc umt mt u nil */
lua_rawset(L,-5); /* stack: gc umt mt */ lua_rawset(L,-5); /* stack: gc umt mt */
} }
lua_pop(L,3); lua_pop(L,3);
return 0; return 0;
} }
@ -465,27 +465,27 @@ TOLUA_API int class_gc_event (lua_State* L)
*/ */
TOLUA_API void tolua_moduleevents (lua_State* L) TOLUA_API void tolua_moduleevents (lua_State* L)
{ {
lua_pushstring(L,"__index"); lua_pushstring(L,"__index");
lua_pushcfunction(L,module_index_event); lua_pushcfunction(L,module_index_event);
lua_rawset(L,-3); lua_rawset(L,-3);
lua_pushstring(L,"__newindex"); lua_pushstring(L,"__newindex");
lua_pushcfunction(L,module_newindex_event); lua_pushcfunction(L,module_newindex_event);
lua_rawset(L,-3); lua_rawset(L,-3);
} }
/* Check if the object on the top has a module metatable /* Check if the object on the top has a module metatable
*/ */
TOLUA_API int tolua_ismodulemetatable (lua_State* L) TOLUA_API int tolua_ismodulemetatable (lua_State* L)
{ {
int r = 0; int r = 0;
if (lua_getmetatable(L,-1)) if (lua_getmetatable(L,-1))
{ {
lua_pushstring(L,"__index"); lua_pushstring(L,"__index");
lua_rawget(L,-2); lua_rawget(L,-2);
r = (lua_tocfunction(L,-1) == module_index_event); r = (lua_tocfunction(L,-1) == module_index_event);
lua_pop(L,2); lua_pop(L,2);
} }
return r; return r;
} }
/* Register class events /* Register class events
@ -493,44 +493,44 @@ TOLUA_API int tolua_ismodulemetatable (lua_State* L)
*/ */
TOLUA_API void tolua_classevents (lua_State* L) TOLUA_API void tolua_classevents (lua_State* L)
{ {
lua_pushstring(L,"__index"); lua_pushstring(L,"__index");
lua_pushcfunction(L,class_index_event); lua_pushcfunction(L,class_index_event);
lua_rawset(L,-3); lua_rawset(L,-3);
lua_pushstring(L,"__newindex"); lua_pushstring(L,"__newindex");
lua_pushcfunction(L,class_newindex_event); lua_pushcfunction(L,class_newindex_event);
lua_rawset(L,-3); lua_rawset(L,-3);
lua_pushstring(L,"__add"); lua_pushstring(L,"__add");
lua_pushcfunction(L,class_add_event); lua_pushcfunction(L,class_add_event);
lua_rawset(L,-3); lua_rawset(L,-3);
lua_pushstring(L,"__sub"); lua_pushstring(L,"__sub");
lua_pushcfunction(L,class_sub_event); lua_pushcfunction(L,class_sub_event);
lua_rawset(L,-3); lua_rawset(L,-3);
lua_pushstring(L,"__mul"); lua_pushstring(L,"__mul");
lua_pushcfunction(L,class_mul_event); lua_pushcfunction(L,class_mul_event);
lua_rawset(L,-3); lua_rawset(L,-3);
lua_pushstring(L,"__div"); lua_pushstring(L,"__div");
lua_pushcfunction(L,class_div_event); lua_pushcfunction(L,class_div_event);
lua_rawset(L,-3); lua_rawset(L,-3);
lua_pushstring(L,"__lt"); lua_pushstring(L,"__lt");
lua_pushcfunction(L,class_lt_event); lua_pushcfunction(L,class_lt_event);
lua_rawset(L,-3); lua_rawset(L,-3);
lua_pushstring(L,"__le"); lua_pushstring(L,"__le");
lua_pushcfunction(L,class_le_event); lua_pushcfunction(L,class_le_event);
lua_rawset(L,-3); lua_rawset(L,-3);
lua_pushstring(L,"__eq"); lua_pushstring(L,"__eq");
lua_pushcfunction(L,class_eq_event); lua_pushcfunction(L,class_eq_event);
lua_rawset(L,-3); lua_rawset(L,-3);
lua_pushstring(L,"__call"); lua_pushstring(L,"__call");
lua_pushcfunction(L,class_call_event); lua_pushcfunction(L,class_call_event);
lua_rawset(L,-3); lua_rawset(L,-3);
lua_pushstring(L,"__gc"); lua_pushstring(L,"__gc");
lua_pushstring(L, "tolua_gc_event"); lua_pushstring(L, "tolua_gc_event");
lua_rawget(L, LUA_REGISTRYINDEX); lua_rawget(L, LUA_REGISTRYINDEX);
/*lua_pushcfunction(L,class_gc_event);*/ /*lua_pushcfunction(L,class_gc_event);*/
lua_rawset(L,-3); lua_rawset(L,-3);
} }

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -19,153 +19,159 @@
TOLUA_API void tolua_pushvalue (lua_State* L, int lo) TOLUA_API void tolua_pushvalue (lua_State* L, int lo)
{ {
lua_pushvalue(L,lo); lua_pushvalue(L,lo);
} }
TOLUA_API void tolua_pushboolean (lua_State* L, int value) TOLUA_API void tolua_pushboolean (lua_State* L, int value)
{ {
lua_pushboolean(L,value); lua_pushboolean(L,value);
} }
TOLUA_API void tolua_pushnumber (lua_State* L, lua_Number value) TOLUA_API void tolua_pushnumber (lua_State* L, lua_Number value)
{ {
lua_pushnumber(L,value); lua_pushnumber(L,value);
} }
TOLUA_API void tolua_pushstring (lua_State* L, const char* value) TOLUA_API void tolua_pushstring (lua_State* L, const char* value)
{ {
if (value == NULL) if (value == NULL)
lua_pushnil(L); lua_pushnil(L);
else else
lua_pushstring(L,value); lua_pushstring(L,value);
} }
TOLUA_API void tolua_pushuserdata (lua_State* L, void* value) TOLUA_API void tolua_pushuserdata (lua_State* L, void* value)
{ {
if (value == NULL) if (value == NULL)
lua_pushnil(L); lua_pushnil(L);
else else
lua_pushlightuserdata(L,value); lua_pushlightuserdata(L,value);
} }
TOLUA_API void tolua_pushusertype (lua_State* L, void* value, const char* type) TOLUA_API void tolua_pushusertype (lua_State* L, void* value, const char* type)
{ {
if (value == NULL) if (value == NULL)
lua_pushnil(L); lua_pushnil(L);
else else
{ {
luaL_getmetatable(L, type); luaL_getmetatable(L, type); /* stack: mt */
lua_pushstring(L,"tolua_ubox"); if (lua_isnil(L, -1)) { /* NOT FOUND metatable */
lua_rawget(L,-2); /* stack: mt ubox */ lua_pop(L, 1);
if (lua_isnil(L, -1)) { return;
lua_pop(L, 1); }
lua_pushstring(L, "tolua_ubox"); lua_pushstring(L,"tolua_ubox");
lua_rawget(L, LUA_REGISTRYINDEX); lua_rawget(L,-2); /* stack: mt ubox */
}; if (lua_isnil(L, -1)) {
lua_pushlightuserdata(L,value); lua_pop(L, 1);
lua_rawget(L,-2); /* stack: mt ubox ubox[u] */ lua_pushstring(L, "tolua_ubox");
if (lua_isnil(L,-1)) lua_rawget(L, LUA_REGISTRYINDEX);
{ };
lua_pop(L,1); /* stack: mt ubox */
lua_pushlightuserdata(L,value); lua_pushlightuserdata(L,value); /* stack: mt ubox key<value> */
*(void**)lua_newuserdata(L,sizeof(void *)) = value; /* stack: mt ubox u newud */ lua_rawget(L,-2); /* stack: mt ubox ubox[value] */
lua_pushvalue(L,-1); /* stack: mt ubox u newud newud */
lua_insert(L,-4); /* stack: mt newud ubox u newud */ if (lua_isnil(L,-1))
lua_rawset(L,-3); /* stack: mt newud ubox */ {
lua_pop(L,1); /* stack: mt newud */ lua_pop(L,1); /* stack: mt ubox */
/*luaL_getmetatable(L,type);*/ lua_pushlightuserdata(L,value);
lua_pushvalue(L, -2); /* stack: mt newud mt */ *(void**)lua_newuserdata(L,sizeof(void *)) = value; /* stack: mt ubox value newud */
lua_setmetatable(L,-2); /* stack: mt newud */ lua_pushvalue(L,-1); /* stack: mt ubox value newud newud */
lua_insert(L,-4); /* stack: mt newud ubox value newud */
lua_rawset(L,-3); /* ubox[value] = newud, stack: mt newud ubox */
lua_pop(L,1); /* stack: mt newud */
/*luaL_getmetatable(L,type);*/
lua_pushvalue(L, -2); /* stack: mt newud mt */
lua_setmetatable(L,-2); /* update mt, stack: mt newud */
#ifdef LUA_VERSION_NUM #ifdef LUA_VERSION_NUM
lua_pushvalue(L, TOLUA_NOPEER); lua_pushvalue(L, TOLUA_NOPEER);
lua_setfenv(L, -2); lua_setfenv(L, -2);
#endif #endif
} }
else else
{ {
/* check the need of updating the metatable to a more specialized class */ /* check the need of updating the metatable to a more specialized class */
lua_insert(L,-2); /* stack: mt ubox[u] ubox */ lua_insert(L,-2); /* stack: mt ubox[u] ubox */
lua_pop(L,1); /* stack: mt ubox[u] */ lua_pop(L,1); /* stack: mt ubox[u] */
lua_pushstring(L,"tolua_super"); lua_pushstring(L,"tolua_super");
lua_rawget(L,LUA_REGISTRYINDEX); /* stack: mt ubox[u] super */ lua_rawget(L,LUA_REGISTRYINDEX); /* stack: mt ubox[u] super */
lua_getmetatable(L,-2); /* stack: mt ubox[u] super mt */ lua_getmetatable(L,-2); /* stack: mt ubox[u] super mt */
lua_rawget(L,-2); /* stack: mt ubox[u] super super[mt] */ lua_rawget(L,-2); /* stack: mt ubox[u] super super[mt] */
if (lua_istable(L,-1)) if (lua_istable(L,-1))
{ {
lua_pushstring(L,type); /* stack: mt ubox[u] super super[mt] type */ lua_pushstring(L,type); /* stack: mt ubox[u] super super[mt] type */
lua_rawget(L,-2); /* stack: mt ubox[u] super super[mt] flag */ lua_rawget(L,-2); /* stack: mt ubox[u] super super[mt] flag */
if (lua_toboolean(L,-1) == 1) /* if true */ if (lua_toboolean(L,-1) == 1) /* if true */
{ {
lua_pop(L,3); /* mt ubox[u]*/ lua_pop(L,3); /* mt ubox[u]*/
lua_remove(L, -2); lua_remove(L, -2);
return; return;
} }
} }
/* type represents a more specilized type */ /* type represents a more specilized type */
/*luaL_getmetatable(L,type); // stack: mt ubox[u] super super[mt] flag mt */ /*luaL_getmetatable(L,type); // stack: mt ubox[u] super super[mt] flag mt */
lua_pushvalue(L, -5); /* stack: mt ubox[u] super super[mt] flag mt */ lua_pushvalue(L, -5); /* stack: mt ubox[u] super super[mt] flag mt */
lua_setmetatable(L,-5); /* stack: mt ubox[u] super super[mt] flag */ lua_setmetatable(L,-5); /* stack: mt ubox[u] super super[mt] flag */
lua_pop(L,3); /* stack: mt ubox[u] */ lua_pop(L,3); /* stack: mt ubox[u] */
} }
lua_remove(L, -2); /* stack: ubox[u]*/ lua_remove(L, -2); /* stack: ubox[u]*/
} }
} }
TOLUA_API void tolua_pushusertype_and_takeownership (lua_State* L, void* value, const char* type) TOLUA_API void tolua_pushusertype_and_takeownership (lua_State* L, void* value, const char* type)
{ {
tolua_pushusertype(L,value,type); tolua_pushusertype(L,value,type);
tolua_register_gc(L,lua_gettop(L)); tolua_register_gc(L,lua_gettop(L));
} }
TOLUA_API void tolua_pushfieldvalue (lua_State* L, int lo, int index, int v) TOLUA_API void tolua_pushfieldvalue (lua_State* L, int lo, int index, int v)
{ {
lua_pushnumber(L,index); lua_pushnumber(L,index);
lua_pushvalue(L,v); lua_pushvalue(L,v);
lua_settable(L,lo); lua_settable(L,lo);
} }
TOLUA_API void tolua_pushfieldboolean (lua_State* L, int lo, int index, int v) TOLUA_API void tolua_pushfieldboolean (lua_State* L, int lo, int index, int v)
{ {
lua_pushnumber(L,index); lua_pushnumber(L,index);
lua_pushboolean(L,v); lua_pushboolean(L,v);
lua_settable(L,lo); lua_settable(L,lo);
} }
TOLUA_API void tolua_pushfieldnumber (lua_State* L, int lo, int index, lua_Number v) TOLUA_API void tolua_pushfieldnumber (lua_State* L, int lo, int index, lua_Number v)
{ {
lua_pushnumber(L,index); lua_pushnumber(L,index);
tolua_pushnumber(L,v); tolua_pushnumber(L,v);
lua_settable(L,lo); lua_settable(L,lo);
} }
TOLUA_API void tolua_pushfieldstring (lua_State* L, int lo, int index, const char* v) TOLUA_API void tolua_pushfieldstring (lua_State* L, int lo, int index, const char* v)
{ {
lua_pushnumber(L,index); lua_pushnumber(L,index);
tolua_pushstring(L,v); tolua_pushstring(L,v);
lua_settable(L,lo); lua_settable(L,lo);
} }
TOLUA_API void tolua_pushfielduserdata (lua_State* L, int lo, int index, void* v) TOLUA_API void tolua_pushfielduserdata (lua_State* L, int lo, int index, void* v)
{ {
lua_pushnumber(L,index); lua_pushnumber(L,index);
tolua_pushuserdata(L,v); tolua_pushuserdata(L,v);
lua_settable(L,lo); lua_settable(L,lo);
} }
TOLUA_API void tolua_pushfieldusertype (lua_State* L, int lo, int index, void* v, const char* type) TOLUA_API void tolua_pushfieldusertype (lua_State* L, int lo, int index, void* v, const char* type)
{ {
lua_pushnumber(L,index); lua_pushnumber(L,index);
tolua_pushusertype(L,v,type); tolua_pushusertype(L,v,type);
lua_settable(L,lo); lua_settable(L,lo);
} }
TOLUA_API void tolua_pushfieldusertype_and_takeownership (lua_State* L, int lo, int index, void* v, const char* type) TOLUA_API void tolua_pushfieldusertype_and_takeownership (lua_State* L, int lo, int index, void* v, const char* type)
{ {
lua_pushnumber(L,index); lua_pushnumber(L,index);
tolua_pushusertype(L,v,type); tolua_pushusertype(L,v,type);
tolua_register_gc(L,lua_gettop(L)); tolua_register_gc(L,lua_gettop(L));
lua_settable(L,lo); lua_settable(L,lo);
} }

View File

@ -19,115 +19,115 @@
TOLUA_API lua_Number tolua_tonumber (lua_State* L, int narg, lua_Number def) TOLUA_API lua_Number tolua_tonumber (lua_State* L, int narg, lua_Number def)
{ {
return lua_gettop(L)<abs(narg) ? def : lua_tonumber(L,narg); return lua_gettop(L)<abs(narg) ? def : lua_tonumber(L,narg);
} }
TOLUA_API const char* tolua_tostring (lua_State* L, int narg, const char* def) TOLUA_API const char* tolua_tostring (lua_State* L, int narg, const char* def)
{ {
return lua_gettop(L)<abs(narg) ? def : lua_tostring(L,narg); return lua_gettop(L)<abs(narg) ? def : lua_tostring(L,narg);
} }
TOLUA_API void* tolua_touserdata (lua_State* L, int narg, void* def) TOLUA_API void* tolua_touserdata (lua_State* L, int narg, void* def)
{ {
/* return lua_gettop(L)<abs(narg) ? def : lua_touserdata(L,narg); */
if (lua_gettop(L)<abs(narg)) { /* return lua_gettop(L)<abs(narg) ? def : lua_touserdata(L,narg); */
return def;
}; if (lua_gettop(L)<abs(narg)) {
return def;
if (lua_islightuserdata(L, narg)) { };
return lua_touserdata(L,narg); if (lua_islightuserdata(L, narg)) {
};
return lua_touserdata(L,narg);
return tolua_tousertype(L, narg, def); };
return tolua_tousertype(L, narg, def);
} }
extern int push_table_instance(lua_State* L, int lo); extern int push_table_instance(lua_State* L, int lo);
TOLUA_API void* tolua_tousertype (lua_State* L, int narg, void* def) TOLUA_API void* tolua_tousertype (lua_State* L, int narg, void* def)
{ {
if (lua_gettop(L)<abs(narg)) if (lua_gettop(L)<abs(narg))
return def; return def;
else else
{ {
void* u; void* u;
if (!lua_isuserdata(L, narg)) { if (!lua_isuserdata(L, narg)) {
if (!push_table_instance(L, narg)) return NULL; if (!push_table_instance(L, narg)) return NULL;
}; };
u = lua_touserdata(L,narg); u = lua_touserdata(L,narg);
return (u==NULL) ? NULL : *((void**)u); /* nil represents NULL */ return (u==NULL) ? NULL : *((void**)u); /* nil represents NULL */
} }
} }
TOLUA_API int tolua_tovalue (lua_State* L, int narg, int def) TOLUA_API int tolua_tovalue (lua_State* L, int narg, int def)
{ {
return lua_gettop(L)<abs(narg) ? def : narg; return lua_gettop(L)<abs(narg) ? def : narg;
} }
TOLUA_API int tolua_toboolean (lua_State* L, int narg, int def) TOLUA_API int tolua_toboolean (lua_State* L, int narg, int def)
{ {
return lua_gettop(L)<abs(narg) ? def : lua_toboolean(L,narg); return lua_gettop(L)<abs(narg) ? def : lua_toboolean(L,narg);
} }
TOLUA_API lua_Number tolua_tofieldnumber (lua_State* L, int lo, int index, lua_Number def) TOLUA_API lua_Number tolua_tofieldnumber (lua_State* L, int lo, int index, lua_Number def)
{ {
double v; double v;
lua_pushnumber(L,index); lua_pushnumber(L,index);
lua_gettable(L,lo); lua_gettable(L,lo);
v = lua_isnil(L,-1) ? def : lua_tonumber(L,-1); v = lua_isnil(L,-1) ? def : lua_tonumber(L,-1);
lua_pop(L,1); lua_pop(L,1);
return v; return v;
} }
TOLUA_API const char* tolua_tofieldstring TOLUA_API const char* tolua_tofieldstring
(lua_State* L, int lo, int index, const char* def) (lua_State* L, int lo, int index, const char* def)
{ {
const char* v; const char* v;
lua_pushnumber(L,index); lua_pushnumber(L,index);
lua_gettable(L,lo); lua_gettable(L,lo);
v = lua_isnil(L,-1) ? def : lua_tostring(L,-1); v = lua_isnil(L,-1) ? def : lua_tostring(L,-1);
lua_pop(L,1); lua_pop(L,1);
return v; return v;
} }
TOLUA_API void* tolua_tofielduserdata (lua_State* L, int lo, int index, void* def) TOLUA_API void* tolua_tofielduserdata (lua_State* L, int lo, int index, void* def)
{ {
void* v; void* v;
lua_pushnumber(L,index); lua_pushnumber(L,index);
lua_gettable(L,lo); lua_gettable(L,lo);
v = lua_isnil(L,-1) ? def : lua_touserdata(L,-1); v = lua_isnil(L,-1) ? def : lua_touserdata(L,-1);
lua_pop(L,1); lua_pop(L,1);
return v; return v;
} }
TOLUA_API void* tolua_tofieldusertype (lua_State* L, int lo, int index, void* def) TOLUA_API void* tolua_tofieldusertype (lua_State* L, int lo, int index, void* def)
{ {
void* v; void* v;
lua_pushnumber(L,index); lua_pushnumber(L,index);
lua_gettable(L,lo); lua_gettable(L,lo);
v = lua_isnil(L,-1) ? def : (*(void **)(lua_touserdata(L, -1))); /* lua_unboxpointer(L,-1); */ v = lua_isnil(L,-1) ? def : (*(void **)(lua_touserdata(L, -1))); /* lua_unboxpointer(L,-1); */
lua_pop(L,1); lua_pop(L,1);
return v; return v;
} }
TOLUA_API int tolua_tofieldvalue (lua_State* L, int lo, int index, int def) TOLUA_API int tolua_tofieldvalue (lua_State* L, int lo, int index, int def)
{ {
int v; int v;
lua_pushnumber(L,index); lua_pushnumber(L,index);
lua_gettable(L,lo); lua_gettable(L,lo);
v = lua_isnil(L,-1) ? def : lo; v = lua_isnil(L,-1) ? def : lo;
lua_pop(L,1); lua_pop(L,1);
return v; return v;
} }
TOLUA_API int tolua_getfieldboolean (lua_State* L, int lo, int index, int def) TOLUA_API int tolua_getfieldboolean (lua_State* L, int lo, int index, int def)
{ {
int v; int v;
lua_pushnumber(L,index); lua_pushnumber(L,index);
lua_gettable(L,lo); lua_gettable(L,lo);
v = lua_isnil(L,-1) ? 0 : lua_toboolean(L,-1); v = lua_isnil(L,-1) ? 0 : lua_toboolean(L,-1);
lua_pop(L,1); lua_pop(L,1);
return v; return v;
} }