Skip to content

Commit

Permalink
add tests, fix yielding function not returning correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
davidsiaw committed Mar 7, 2015
1 parent e1c4aeb commit e88e63f
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 4 deletions.
4 changes: 3 additions & 1 deletion LuaCppInterface/luacoroutine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ std::string LuaCoroutine::RunScript(std::string script)
lua_pop(state.get(), 1);
int status = luaL_loadstring(thread, script.c_str());
status = lua_resume(thread, NULL, 0);

if (status != LUA_OK && status != LUA_YIELD)
{
return LuaGetLastError(thread);
Expand All @@ -26,7 +27,8 @@ std::string LuaCoroutine::Resume()
PushToStack(state.get());
lua_State* thread = lua_tothread(state.get(), -1);
lua_pop(state.get(), 1);
int status = lua_resume(thread, NULL, 0);
int status = lua_resume(thread, NULL, lua_gettop(thread));

if (status != LUA_OK && status != LUA_YIELD)
{
return LuaGetLastError(thread);
Expand Down
3 changes: 1 addition & 2 deletions LuaCppInterface/luacppinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ class Lua
static int lua_yieldingFunction(lua_State* state)
{
int numVals = LuaFunction<SIG>::staticFunction(state);
lua_yield(state, numVals);
return numVals;
return lua_yield(state, numVals);
};

template<typename SIG>
Expand Down
9 changes: 8 additions & 1 deletion tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ TESTS = crashtest \
testgettypeofvalueat \
testinvalidscript \
testregistry \
testreturnfromyieldingfunction \
demonstration1 \
demonstration2 \
demonstration3
demonstration3 \
demonstration4

check_PROGRAMS = $(TESTS)

Expand Down Expand Up @@ -123,6 +125,8 @@ testinvalidscript_LDADD = ../LuaCppInterface/libluacppinterface.a ../lua/src/lib
testregistry_SOURCES = testregistry.cpp lua
testregistry_LDADD = ../LuaCppInterface/libluacppinterface.a ../lua/src/liblua.a

testreturnfromyieldingfunction_SOURCES = testreturnfromyieldingfunction.cpp lua
testreturnfromyieldingfunction_LDADD = ../LuaCppInterface/libluacppinterface.a ../lua/src/liblua.a


demonstration1_SOURCES = demonstration1.cpp lua
Expand All @@ -134,6 +138,9 @@ demonstration2_LDADD = ../LuaCppInterface/libluacppinterface.a ../lua/src/liblua
demonstration3_SOURCES = demonstration3.cpp lua
demonstration3_LDADD = ../LuaCppInterface/libluacppinterface.a ../lua/src/liblua.a

demonstration4_SOURCES = demonstration4.cpp lua
demonstration4_LDADD = ../LuaCppInterface/libluacppinterface.a ../lua/src/liblua.a


BUILT_SOURCES = ../lua/src/liblua.a

Expand Down
3 changes: 3 additions & 0 deletions tests/config.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
width = 640
height = 480
windowTitle = "Lua Rocks"
18 changes: 18 additions & 0 deletions tests/demonstration4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <fstream>
#include <streambuf>
#include <string>
#include <iostream>
#include <luacppinterface.h>

int main()
{
std::ifstream file("config.lua");
std::string script((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
Lua lua;
lua.RunScript(script);
int width = lua.GetGlobalEnvironment().Get<int>("width"); // get the width
int height = lua.GetGlobalEnvironment().Get<int>("height"); // get the height
std::string windowTitle = lua.GetGlobalEnvironment().Get<std::string>("windowTitle");

return width != 640 || height != 480 || windowTitle.compare("Lua Rocks");
}
50 changes: 50 additions & 0 deletions tests/testreturnfromyieldingfunction.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <memory>
#include <iostream>
#include <luacppinterface.h>
#include <sstream>

int main()
{
Lua luaInstance;

auto globalTable = luaInstance.GetGlobalEnvironment();
std::stringstream ss;

auto myOwnPrint = luaInstance.CreateYieldingFunction<void(std::string)>
(
[&](std::string str)
{
ss << str;
}
);

auto lengthOf = luaInstance.CreateYieldingFunction<int(std::string)>
(
[](std::string str) -> int
{
return str.size();
}
);

globalTable.Set("myOwnPrint", myOwnPrint);
globalTable.Set("lengthOf", lengthOf);

luaInstance.LoadStandardLibraries();

auto cr = luaInstance.CreateCoroutine();

auto err = cr.RunScript(
"x = lengthOf('haha')\n"
"myOwnPrint ('size:' .. x)\n"
);

while (cr.CanResume())
{
ss << ";yield;";
err = cr.Resume();
}

auto resstr = ss.str();

return resstr.compare(";yield;size:4;yield;");
}

0 comments on commit e88e63f

Please sign in to comment.