71 lines
1.7 KiB
C++
71 lines
1.7 KiB
C++
|
|
#include "ag.h"
|
|
#include <iostream>
|
|
#include <lua.hpp>
|
|
using namespace std;
|
|
|
|
namespace ag
|
|
{
|
|
void register_functions(lua_State * L)
|
|
{
|
|
static const luaL_Reg functions[] = {
|
|
{ "print", ag_print },
|
|
{ NULL, NULL }
|
|
};
|
|
luaL_register(L, "ag", functions);
|
|
}
|
|
|
|
static int ag_print_val(lua_State * L, int index)
|
|
{
|
|
int type = lua_type(L, index);
|
|
switch (type)
|
|
{
|
|
case LUA_TNUMBER:
|
|
case LUA_TSTRING:
|
|
cout << lua_tostring(L, index);
|
|
break;
|
|
case LUA_TTABLE:
|
|
cout << '[';
|
|
|
|
/* traverse the table and print the keys/values */
|
|
lua_checkstack(L, 3);
|
|
lua_pushnil(L);
|
|
for (bool first = true; lua_next(L, index) != 0; first = false)
|
|
{
|
|
if (!first)
|
|
cout << ", ";
|
|
ag_print_val(L, -2);
|
|
cout << " => ";
|
|
ag_print_val(L, -1);
|
|
lua_pop(L, 1);
|
|
}
|
|
|
|
cout << ']';
|
|
break;
|
|
case LUA_TFUNCTION:
|
|
cout << "<function>";
|
|
break;
|
|
case LUA_TUSERDATA:
|
|
cout << "<userdata>";
|
|
break;
|
|
case LUA_TTHREAD:
|
|
cout << "<thread>";
|
|
break;
|
|
case LUA_TLIGHTUSERDATA:
|
|
cout << "<lightuserdata>";
|
|
break;
|
|
}
|
|
}
|
|
|
|
int ag_print(lua_State * L)
|
|
{
|
|
int argc = lua_gettop(L);
|
|
|
|
for ( int n = 1; n <= argc; n++ )
|
|
{
|
|
ag_print_val(L, n);
|
|
}
|
|
return 0;
|
|
}
|
|
}
|