PIString test for Lua
This commit is contained in:
@@ -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)
|
||||
|
||||
43
main.cpp
43
main.cpp
@@ -13,7 +13,46 @@ static const char * script
|
||||
"testString = \"LuaBridge works!\" \n"
|
||||
"number = 42 \n";
|
||||
|
||||
namespace luabridge {
|
||||
|
||||
template <>
|
||||
struct Stack <PIString>
|
||||
{
|
||||
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<std::string>();
|
||||
PIString luaString = s.cast<PIString>();
|
||||
int answer = n.cast<int>();
|
||||
piCout << StdString2PIString(luaString);
|
||||
piCout << luaString;
|
||||
piCout << "And here's our number:" << answer;
|
||||
}
|
||||
#else
|
||||
|
||||
Reference in New Issue
Block a user