diff --git a/CMakeLists.txt b/CMakeLists.txt index a9b643c6..e66837a8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -617,7 +617,7 @@ if (NOT CROSSTOOLS) if(MINGW) set(LUA_INCLUDE_DIR ${MINGW_INCLUDE}) endif() - find_package(Lua REQUIRED) + find_package(Lua QUIET) if (LUA_FOUND) message(STATUS "Building PIP with Lua support") add_definitions(-DPIP_LUA) diff --git a/main.cpp b/main.cpp index c8f8b312..518d390a 100644 --- a/main.cpp +++ b/main.cpp @@ -13,7 +13,46 @@ static const char * script "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); @@ -21,9 +60,9 @@ int main() { lua_pcall(L, 0, 0, 0); LuaRef s = getGlobal(L, "testString"); LuaRef n = getGlobal(L, "number"); - std::string luaString = s.cast(); + PIString luaString = s.cast(); int answer = n.cast(); - piCout << StdString2PIString(luaString); + piCout << luaString; piCout << "And here's our number:" << answer; } #else