-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests, fix yielding function not returning correctly
- Loading branch information
Showing
6 changed files
with
83 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
width = 640 | ||
height = 480 | ||
windowTitle = "Lua Rocks" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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;"); | ||
} |