src/tinyl2c.cpp
//expects:
//-2: user data object
//-1: metatable to use
user data object
1: metatable to use
user data object 1 : metatable to use
int l2c_printobjectwithmetatable(lua_State* L)
{
	int object_idx = lua_absindex(L,-2);
	int metatable_idx = lua_absindex(L,-1);
	lua_getglobal(L, "tostring");
	int tostring_idx = lua_gettop(L);
	lua_getfield(L, metatable_idx, "_l2c_inherits");
	if (!lua_isnil(L, -1))
	{
		lua_pushvalue(L,object_idx);
		lua_pushvalue(L,-2);
		l2c_printobjectwithmetatable(L);
		lua_pop(L,2);
	}
	lua_pop(L,1);
	lua_getfield(L, metatable_idx, "_l2c_getters");
	int getters_idx = lua_absindex(L,-1);
	lua_pushnil(L);
	while (lua_next(L, getters_idx) != 0)
	{
		const char* name = lua_tostring(L,-2);
		lua_pushvalue(L,object_idx);
		lua_call(L,1,1); 
		lua_pushvalue(L,tostring_idx);
		lua_pushvalue(L,-2);
		lua_call(L,1,1); 
		const char* val = lua_tostring(L,-1);
		l2c_printf("%s=%s\n",name,val);
		lua_pop(L,2);
	}
	lua_pop(L,2);
	return 0;
}
int l2c_printobjectwithmetatable ( lua_State * L ) { int object_idx = lua_absindex ( L , -2 ) ; int metatable_idx = lua_absindex ( L , -1 ) ; lua_getglobal ( L , " " ) ; int tostring_idx = lua_gettop ( L ) ; lua_getfield ( L , metatable_idx , " " ) ; if ( ! lua_isnil ( L , -1 ) ) { lua_pushvalue ( L , object_idx ) ; lua_pushvalue ( L , -2 ) ; l2c_printobjectwithmetatable ( L ) ; lua_pop ( L , 2 ) ; } lua_pop ( L , 1 ) ; lua_getfield ( L , metatable_idx , " " ) ; int getters_idx = lua_absindex ( L , -1 ) ; lua_pushnil ( L ) ; while ( lua_next ( L , getters_idx ) != 0 ) { const char * name = lua_tostring ( L , -2 ) ; lua_pushvalue ( L , object_idx ) ; lua_call ( L , 1 , 1 ) ; lua_pushvalue ( L , tostring_idx ) ; lua_pushvalue ( L , -2 ) ; lua_call ( L , 1 , 1 ) ; const char * val = lua_tostring ( L , -1 ) ; l2c_printf ( " \n " , name , val ) ; lua_pop ( L , 2 ) ; } lua_pop ( L , 2 ) ; return 0 ; }
expects:
2: user data object
1: metatable to use
expects : 2 : user data object 1 : metatable to use
//call getter //call tostring