lua5.3与C++交互学习(二)
2016-04-05 15:38
387 查看
C++中对lua协程和返回值的处理
lua语言中, 在coroutine的resume和yield之间可以相互传递参数, 最近研究了下lua_resume和lua_yield 两个函数, 同样利用这两个函数可以通过栈在C++与lua之间很方便的交换数据;这里给出一个简单的示例;1.lua中写一个协程函数
function CorTest() print("coroutine begin") local re = coroutine.yield(100, "hello c++") // 往栈中传参数以便C++能获取 print("lua coroutine continue: " .. re) // 从c中lua_resume传回来的参数是者的re Stop("call c yield!") // 调用c的挂起函数 print("coroutine continue after yield") print("coroutine end") end
这里尝试了lua的yield和C++的lua_yield:
2. 在C++中写一个挂起函数注册到lua中使用
static int Stop(lua_State* L) { // 会传一个参数进来 const char* str = lua_tostring(L,-1); print("C Stop: %s\n", str); //将这个值再次压入栈中 lua_pushstring(L,str); lua_yield(L,1); assert(0); // 协程恢复后是不会回到这边的, 而是直接回到lua中Stop()后一句, 原因是C++栈已经释放 return 0; }
3.这里直接在C中创建协程:
// 参数corName是在lua中写的协程函数 lua_State* CreateCoroutine(lua_State* gL, const char* corName) { lua_checkstack(L,10); // 10个是随便写的一个数字, 不影响 lua_State* lt = lua_newthread(gL); lua_getglobal(lt, corName); // 协程函数入栈 return lt; }
4.这里在main函数中进行主要处理:
int main() { // 省略前面的lua初始化代码 /**....**/ lua_register(L,"Wait",Wait); lua_State* lt = CreateCoroutine(L,"CorTest"); int re = lua_resume(lt, L, 0); // 这次传0个参数(因为CorTest没有参数) if(re != LUA_YIELD) printf("ERROR"); // lua挂起后, 穿回来100, 和hello C++两个值, 可以lua_gettop(lt)看个数 int rint = luaL_checknumber(lt, -2); const char* str = lua_tostring(lt, -1); printf("stack value: %d, %s\n", rint, str); // ⇒ 100, hello C++ // 这次恢复协程时传入参数, 参数压栈, lua中就是对应返回值local re lua_pushstring(lt, " from c++"); re = lua_resume(lt, L, 1); // 挂起后是在Stop()函数中传入了一个"call c yield!" printf("%s\n",lua_tostring(lt,-1)); re = lua_resume(lt, L, 0); // 这里无法传值回去lua了,因为Stop的栈已经被释放 ,而是直接运行lua中Stop的后一句 }
输出结果如下:
coroutine begin 100, hello C++ coroutine contine , from C++ C Stop: call yield coroutine continue after C yield, from c++ lua end
相关文章推荐
- 详解Lua中的表的概念及其相关操作方法
- Lua编程示例(二):面向对象、metatable对表进行扩展
- 把Lua编译进nginx步骤方法
- Lua脚本自动生成APK包
- Lua中的元表(metatable)、元方法(metamethod)详解
- Lua中的metatable介绍
- Lua中ipair和pair的区别
- Lua中的函数精讲笔记
- 浅谈Lua的面向对象特性
- 详解Lua中的变量相关知识点
- Lua脚本语言入门笔记
- Lua脚本调用外部脚本
- 详解Lua中的if语句的使用方法
- Lua中调用函数使用点号和冒号的区别
- Lua中的闭合函数、非全局函数与函数的尾调用详解
- Lua中强大的元方法__index详解
- Lua中调用C++函数示例
- Lua面向对象之类和继承浅析
- Lua性能优化技巧(一):前言
- Lua中获取table长度问题探讨