#include "pip.h" #ifdef PIP_LUA extern "C" { # include "lua.h" # include "lauxlib.h" # include "lualib.h" } #include #include "pistring_std.h" static const char * script = "-- script.lua \n" "testString = \"LuaBridge works!\" \n" "number = 42 \n"; namespace luabridge { template <> struct Stack { static void push (lua_State* L, PIString const& str) { lua_pushstring(L, str.dataAscii()); } static PIString get (lua_State* L, int index) { size_t len; if (lua_type (L, index) == LUA_TSTRING) { const char* str = lua_tolstring(L, index, &len); return PIString(str, len); } // Lua reference manual: // If the value is a number, then lua_tolstring also changes the actual value in the stack to a string. // (This change confuses lua_next when lua_tolstring is applied to keys during a table traversal.) lua_pushvalue (L, index); const char* str = lua_tolstring(L, -1, &len); PIString string (str, len); lua_pop (L, 1); // Pop the temporary string return string; } static bool isInstance (lua_State* L, int index) { return lua_type (L, index) == LUA_TSTRING; } }; } using namespace luabridge; int main() { lua_State* L = luaL_newstate(); luaL_dostring(L, script); luaL_openlibs(L); lua_pcall(L, 0, 0, 0); LuaRef s = getGlobal(L, "testString"); LuaRef n = getGlobal(L, "number"); PIString luaString = s.cast(); int answer = n.cast(); piCout << luaString; piCout << "And here's our number:" << answer; } #else int main() { return 0; } #endif