您的位置:首页 > 编程语言 > Lua

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协程