跨C-call边界的Lua让步在以下场景中具有优势和应用场景: 性能优化:通过将一些性能敏感的代码用C语言实现,并在Lua脚本中调用,可以提高整体的执行效率。 系统编程:C语言具有强大的系统编程能力,可以直接调用系统API来实现一些底层功能,如文件操作、网络通信等。 扩展性:通过跨C-call边界的Lua让步,可以方便地扩展Lua的功...
1. void lua_call(lua_State *L, int nargs, int nresults);:这个函数首先将Lua函数压入栈中,然后将参数依次压入栈,最后调用函数。调用函数后,参数和函数都会从栈中弹出,调用返回后,结果会被推入栈中。如果nresults不等于LUA_MULTRET,返回值个数则根据nresults来确定。2. int lua_pcall(l...
int sum; /* 通过名字得到Lua函数 */ lua_getglobal(L, func_name); /* 第一个参数 */ lua_pushnumber(L, x); /* 第二个参数 */ lua_pushnumber(L, y); /* 调用函数,告知有两个参数,一个返回值 */ lua_call(L, 2, 1); /* 得到结果 */ sum = (int)lua_tointeger(L, -1); lua_...
void (lua_replace) (lua_State *L, int idx) 把栈顶的值替换掉idx处的值 int (lua_checkstack) (lua_State *L, int sz) 检查栈的大小是否已经增长到最大值,如果没有则将栈的大小增长sz 返回0表示栈溢出,返回1表示成功 void (lua_xmove) (lua_State *from, lua_State *to, int n) 从LavVM的...
// callLuaScript.c : Defines the entry point for the console application. // #include "stdio.h" #include <lua.h> #include <lualib.h> #include <lauxlib.h> int fun(int n); int main(void) { int result = 0; result = fun(6); ...
多个lua_calls退出并生成"C堆栈溢出"是指在Lua脚本中多次调用lua_call函数,并在每次调用后退出,最终导致C堆栈溢出的错误。 在Lua中,lua_call函数用于调用Lua函数。当我们在C代码中使用lua_call函数调用Lua函数时,会将函数参数压入栈中,然后执行函数,并将返回值压入栈中。如果我们在C代码中多次调用lua_call函数,...
//c call lua lua_getglobal(L, "l_ff"); lua_pushnumber(L, 2); lua_pushnumber(L, 3); if (lua_pcall(L, 2, 1, 0) != 0) { printf("fail to call func: %s\n", lua_tostring(L, -1)); return 0; } int res = lua_tonumber(L, -1); ...
luaadd(intx,inty) { intsum; /*the function name*/ lua_getglobal(L,"add"); /*the first argument*/ lua_pushnumber(L, x); /*the second argument*/ lua_pushnumber(L, y); /*call the function with 2 arguments, return 1 result.*/ ...
通过lua_call和lua_pcall实现,先把函数压栈,这里的函数是在lua中的function,由于上面C函数可以关联到lua的某个table中,所以,理论上也可以是C函数,然后把返回结果再压栈。具体参数含义见API说明。 The following example shows how the host program can do the equivalent to this Lua code: a = f("how", ...
这是lua 官方的设定,lua 调用 c 函数或者其他什么函数,都是被编译成 OP_CALL 指令,而 OP_CALL 并不会设一个标志位导致后面有 yield 的时候报错;而 c 调用 lua 是用 lua_call 这个 api,它会设置一个标志位,后面 yield 时判断到标志位就报错: "attempt to yield across a C-call boundary"。