lua5.3与C++交互学习(二)


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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值