加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Safe Lua调用C注册函数

发布时间:2020-12-14 21:47:04 所属栏目:大数据 来源:网络整理
导读:嘿大家!我有一个嵌入Lua的C应用程序作为脚本.非程序员编辑Lua脚本,然后C app调用Lua脚本,Lua脚本也调用C注册函数. 我使用Luaplus完成上述工作.我的问题是:当脚本编辑器出错(如拼写错误参数)时,C应用程序崩溃了!我该怎么做才能防止这种情况发生?谢谢 解决
嘿大家!我有一个嵌入Lua的C应用程序作为脚本.非程序员编辑Lua脚本,然后C app调用Lua脚本,Lua脚本也调用C注册函数.

我使用Luaplus完成上述工作.我的问题是:当脚本编辑器出错(如拼写错误参数)时,C应用程序崩溃了!我该怎么做才能防止这种情况发生?谢谢

解决方法

看看lua_cpcall和lua_pcall.它们都允许在c中保护lua的函数.如果它们返回非负数,则调用失败,并且lua堆栈仅包含错误字符串.在cpcalls的情况下,堆栈是未经修改的.对于pcall,您需要查看lua_pushcclosure以安全地调用cfunction.

你要做的是:用你想要的所有lua_ *调用创建一个c函数,比如loadfile和dofile.您可以使用lua_cpcall或lua_pushcclosure amd lua_pcall调用此函数.这允许您检测t中是否发生错误
他的功能你传递给了cpcall.

例子:

function hello() {
  string hello_ = "Hello Lua!";
  struct C {
    static int call(lua_State* L) {
      C *p = static_cast<C*>(lua_touserdata(L,-1));
      lua_pushstring(L,p->str.c_str() );
      lua_getglobal(L,"print"); 
      lua_call(L,1,0); //ok
      lua_pushstring(L,"notprint"); 
      lua_call(L,0); //error -> longjmps
      return 0; //Number of values on stack to 'return' to lua
    }
    const string& str;
  } p = { hello_ };
  //protected call of C::call() above
  //with &p as 1st/only element on Lua stack
  //any errors encountered will trigger a longjmp out of lua and
  //return a non-0 error code and a string on the stack
  //A return of 0 indicates success and the stack is unmodified
  //to invoke LUA functions safely use the lua_pcall function
  int res = lua_cpcall(L,&C::call,&p);
  if( res ) {
    string err = lua_tostring(L,-1);
    lua_pop(L,1);
    //Error hanlder here
  }
  //load a .lua file
  if( (res=luaL_loadfile(L,"myLuaFile.lua")) ) {
    string err = lua_tostring(L,1);
    //res is one of
    //LUA_ERRSYNTAX - Lua syntax error
    //LUA_ERRMEM    - Out of memory error
    //LUE_ERRFILE   - File not found/accessible error
  }
  //execute it
  if( (res=lua_pcall(L,0)) ) {
    string err = lua_tostring(L,1);
    // res is one of
    // LUA_ERRRUN: a runtime error.
    // LUA_ERRMEM: memory allocation error.
    // LUA_ERRERR: error while running the error handler function (NULL in this case).
  }
  // try to call [a_int,b_str] = Foo(1,2,"3")
  lua_getglobal(L,"Foo");
  if( lua_isfunction(L,lua_gettop(L)) ) { //Foo exists
    lua_pushnumber(L,1);
    lua_pushnumber(L,2);
    lua_pushstring(L,"3");
    lua_pushvalue(L,-4); //copy of foo()

    if( (res = lua_pcall(L,3,0/*default error func*/)) ) {
      string err = lua_tostring(L,-1);
      lua_pop(L,1);
      //error: see above
    }
    int a_int = (int)lua_tointeger(L,-2);
    string b_str = lua_tostring(L,2+1); //2 returns,+ extra copy of Foo()
  }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读