zoukankan      html  css  js  c++  java
  • C中调用Lua函数

    我们先来看一个简单的例子:

    lua_State* L = NULL;

    // 内部调用lua函数 double f(double x, double y) { double z; lua_getglobal(L, "f"); // 获取lua函数f lua_pushnumber(L, x); // 压入参数x和y lua_pushnumber(L, y); if(lua_pcall(L, 2, 1, 0) != 0) error(L, "error running function 'f': %s", lua_tostring(L, -1)); if(!lua_isnumber(L, -1)) error(L, "function 'f' must return a number"); z = lua_tonumber(L, -1); lua_pop(L, 1); return z; } int main(void) { L = lua_open(); luaL_openlibs(L); if(luaL_loadfile(L, "c:\luatest\functest.lua") || lua_pcall(L, 0, 0, 0)) error(L, "cannot run configuration file: %s", lua_tostring(L, -1)); printf("%f ", f(1.0, 2.0)); return 0; }

    functest.lua:

    f = function(a, b)
        return a + b
    end

      这其中最关键的是调用函数的使用,在C中调用Lua函数的API主要由以下几个:

    (1)void lua_call (lua_State *L, int nargs, int nresults);
      函数调用,nargs表示参数的个数,nresults表示返回值的个数
      首先将lua函数压栈,然后将参数依次压栈,最后调用函数即可
      函数调用时,参数和函数都会pop出栈,调用返回后,结果会push进栈
      nresults==LUA_MULTRET,所有的返回值都会push进栈
      nresults!=LUA_MULTRET,返回值个数根据nresults来调整
      Lua语句:

    a = f("how", t.x, 14)

      在C中的实现:

    lua_getglobal(L, "f");        // 函数入栈
    lua_pushstring(L, "how");      // 参数1入栈
    lua_getglobal(L, "t");       // 表t入栈
    lua_getfield(L, -1, "x");       // 参数2入栈
    lua_remove(L, -2);         // 跳t出栈
    lua_pushinteger(L, 14);     // 参数3入栈
    lua_call(L, 3, 1);            // 调用函数,参数和函数都会出栈
    lua_setglobal(L, "a");         // 给a赋值,栈顶出栈

      上述代码执行完毕后,堆栈状态恢复原样。

    (2)int lua_pcall (lua_State *L, int nargs, int nresults, int msgh);
      函数调用,在安全模式下,并且可以添加错误处理函数。
      如果调用期间发生error,lua_pcall会捕获之,然后push stack一个错误信息(会先将函数和参数pop出栈),并且返回一个error code(非0的一个值)。
      发生error时,如果指定了错误处理函数,会在error message入栈前调用错误处理函数,具体由msgh参数来决定:
      (1)msgh==0,不指定错误处理函数,入栈信息不变;
      (2)msgh!=0,msgh表示错误处理函数的堆栈index,错误处理函数会以error message为参数,并将返回的新的error message入栈。主要用来给error message添加  更多的debug信息,比如堆栈跟踪,因为这些信息在pcall调用完之后是收集不到的。
      函数返回代码:
      LUA_OK(0):调用成功
      LUA_ERRRUN:runtime error
      LUA_ERRMEM:内存分配错误,这种情况下不会调用错误处理函数
      LUA_ERRERR:调用错误处理函数时出错,当然,不会再进一步调用错误处理函数
      LUA_ERRGCMM:调用metamethod.__gc时报错,由gc引起,和函数本身没关系

    (3)int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, int ctx, lua_CFunction k);
      函数调用,在安全模式下,并且允许函数yield。

  • 相关阅读:
    java.net.BindException: Cannot assign requested address: bind
    Caused by: com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'name': was expecting ('true', 'false' or 'null')
    springMVC上传错误StandardMultipartHttpServletRequest
    常用软件
    虚拟机修改静态ip
    linux下脚本做成服务
    Unable to resolve persistence unit root URL
    MyBatis对不同数据库的主键生成策略
    MyBatis定义复合主键
    MongoDB安装,启动,注册为windows系统服务
  • 原文地址:https://www.cnblogs.com/sifenkesi/p/3873720.html
Copyright © 2011-2022 走看看