File tree 6 files changed +83
-4
lines changed
6 files changed +83
-4
lines changed Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ std::string LuaCoroutine::RunScript(std::string script)
14
14
lua_pop (state.get (), 1 );
15
15
int status = luaL_loadstring (thread, script.c_str ());
16
16
status = lua_resume (thread, NULL , 0 );
17
+
17
18
if (status != LUA_OK && status != LUA_YIELD)
18
19
{
19
20
return LuaGetLastError (thread);
@@ -26,7 +27,8 @@ std::string LuaCoroutine::Resume()
26
27
PushToStack (state.get ());
27
28
lua_State* thread = lua_tothread (state.get (), -1 );
28
29
lua_pop (state.get (), 1 );
29
- int status = lua_resume (thread, NULL , 0 );
30
+ int status = lua_resume (thread, NULL , lua_gettop (thread));
31
+
30
32
if (status != LUA_OK && status != LUA_YIELD)
31
33
{
32
34
return LuaGetLastError (thread);
Original file line number Diff line number Diff line change @@ -65,8 +65,7 @@ class Lua
65
65
static int lua_yieldingFunction (lua_State* state)
66
66
{
67
67
int numVals = LuaFunction<SIG>::staticFunction (state);
68
- lua_yield (state, numVals);
69
- return numVals;
68
+ return lua_yield (state, numVals);
70
69
};
71
70
72
71
template <typename SIG>
Original file line number Diff line number Diff line change @@ -30,9 +30,11 @@ TESTS = crashtest \
30
30
testgettypeofvalueat \
31
31
testinvalidscript \
32
32
testregistry \
33
+ testreturnfromyieldingfunction \
33
34
demonstration1 \
34
35
demonstration2 \
35
- demonstration3
36
+ demonstration3 \
37
+ demonstration4
36
38
37
39
check_PROGRAMS = $(TESTS )
38
40
@@ -123,6 +125,8 @@ testinvalidscript_LDADD = ../LuaCppInterface/libluacppinterface.a ../lua/src/lib
123
125
testregistry_SOURCES = testregistry.cpp lua
124
126
testregistry_LDADD = ../LuaCppInterface/libluacppinterface.a ../lua/src/liblua.a
125
127
128
+ testreturnfromyieldingfunction_SOURCES = testreturnfromyieldingfunction.cpp lua
129
+ testreturnfromyieldingfunction_LDADD = ../LuaCppInterface/libluacppinterface.a ../lua/src/liblua.a
126
130
127
131
128
132
demonstration1_SOURCES = demonstration1.cpp lua
@@ -134,6 +138,9 @@ demonstration2_LDADD = ../LuaCppInterface/libluacppinterface.a ../lua/src/liblua
134
138
demonstration3_SOURCES = demonstration3.cpp lua
135
139
demonstration3_LDADD = ../LuaCppInterface/libluacppinterface.a ../lua/src/liblua.a
136
140
141
+ demonstration4_SOURCES = demonstration4.cpp lua
142
+ demonstration4_LDADD = ../LuaCppInterface/libluacppinterface.a ../lua/src/liblua.a
143
+
137
144
138
145
BUILT_SOURCES = ../lua/src/liblua.a
139
146
Original file line number Diff line number Diff line change
1
+ width = 640
2
+ height = 480
3
+ windowTitle = " Lua Rocks"
Original file line number Diff line number Diff line change
1
+ #include < fstream>
2
+ #include < streambuf>
3
+ #include < string>
4
+ #include < iostream>
5
+ #include < luacppinterface.h>
6
+
7
+ int main ()
8
+ {
9
+ std::ifstream file (" config.lua" );
10
+ std::string script ((std::istreambuf_iterator<char >(file)), std::istreambuf_iterator<char >());
11
+ Lua lua;
12
+ lua.RunScript (script);
13
+ int width = lua.GetGlobalEnvironment ().Get <int >(" width" ); // get the width
14
+ int height = lua.GetGlobalEnvironment ().Get <int >(" height" ); // get the height
15
+ std::string windowTitle = lua.GetGlobalEnvironment ().Get <std::string>(" windowTitle" );
16
+
17
+ return width != 640 || height != 480 || windowTitle.compare (" Lua Rocks" );
18
+ }
Original file line number Diff line number Diff line change
1
+ #include < memory>
2
+ #include < iostream>
3
+ #include < luacppinterface.h>
4
+ #include < sstream>
5
+
6
+ int main ()
7
+ {
8
+ Lua luaInstance;
9
+
10
+ auto globalTable = luaInstance.GetGlobalEnvironment ();
11
+ std::stringstream ss;
12
+
13
+ auto myOwnPrint = luaInstance.CreateYieldingFunction <void (std::string)>
14
+ (
15
+ [&](std::string str)
16
+ {
17
+ ss << str;
18
+ }
19
+ );
20
+
21
+ auto lengthOf = luaInstance.CreateYieldingFunction <int (std::string)>
22
+ (
23
+ [](std::string str) -> int
24
+ {
25
+ return str.size ();
26
+ }
27
+ );
28
+
29
+ globalTable.Set (" myOwnPrint" , myOwnPrint);
30
+ globalTable.Set (" lengthOf" , lengthOf);
31
+
32
+ luaInstance.LoadStandardLibraries ();
33
+
34
+ auto cr = luaInstance.CreateCoroutine ();
35
+
36
+ auto err = cr.RunScript (
37
+ " x = lengthOf('haha')\n "
38
+ " myOwnPrint ('size:' .. x)\n "
39
+ );
40
+
41
+ while (cr.CanResume ())
42
+ {
43
+ ss << " ;yield;" ;
44
+ err = cr.Resume ();
45
+ }
46
+
47
+ auto resstr = ss.str ();
48
+
49
+ return resstr.compare (" ;yield;size:4;yield;" );
50
+ }
You can’t perform that action at this time.
0 commit comments