标签:
1、本文将使用void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n); 来讲解在Lua Code中注册C函数,其他注册方式将在下一篇文章中讲解。
When a C function is created, it is possible to associate some values with it, thus creating a C closure ;
these values are then accessible to the function whenever it is called.
To associate values with a C function, first these values should be pushed onto the stack (when there are multiple values, the first value is pushed first).
Then lua_pushcclosure is called to create and push the C function onto the stack, with the argument n telling how many values should be associated with the function.
lua_pushcclosure also pops these values from the stack.
The maximum value for n is 255.
void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);
Pushes a new C closure onto the stack.
当C函数创建时,可以通过c closure 关联一些值,这些值被称作upvalues.
在Lua 5.1 Reference Manual 3.4 - C Closures 有详细说明。
2、第一个例子为在Lua Code中注册函数到全局变量中。
//在Lua Code中注册的函数
int sum(lua_State *L)
{
int top =lua_gettop(L);
int sum =0;
for(size_t t=top;t>0;t--)
{
int lnum =lua_tonumber(L,t);
sum+=lnum;
}
lua_pushnumber(L,sum);
return 1;
}
//通过lua_pushcclosure()方法来在Lua Code中注册C函数
//lua_pushcclosure()函数是Lua C API提供注册C函数最基础的。其他注册方式都是在该函数上面拓展的。
//typedef int (*lua_CFunction) (lua_State *L); 为注册C函数的标准形式
// lua_pushcclosure() 中参数n为提供闭包upvalues值的数量
void register_sum(lua_State *L)
{ lua_settop(L,0);
int n =0;
lua_pushcclosure (L, sum, n);
lua_setglobal(L,"sum");
}print("sample_3.lua")
local a=sum(1,2,3,4,5)
print("a:"..a)//在Lua Code中注册的函数
int sum_closure(lua_State *L)
{ //到该函数被调用时,可以通过lua_upvalueindex()函数获取到该函数的closure
int c1 =(int)lua_tonumber(L, lua_upvalueindex(1));
int c2 =(int)lua_tonumber(L, lua_upvalueindex(2));
int top =lua_gettop(L);
int sum =c1+c2;
for(size_t t=top;t>0;t--)
{
int lnum =lua_tonumber(L,t);
sum+=lnum;
}
//把结果值压入虚拟栈返回
lua_pushnumber(L,sum);
return 1;
}
//提供upvalues
void register_sum_closure(lua_State *L)
{ lua_settop(L,0);
//与sum_closure()函数管理的closure管理的值
lua_pushnumber(L,100);
lua_pushnumber(L,2000);
int n =2; //指定与sum_closure()函数管理的upvalue数量
lua_pushcclosure (L, sum_closure, n);
lua_setglobal(L,"sum_closure");
}local c =sum_closure(1,2,3,4,5)
print("c="..c)int main(int argc, char **argv)
{
//创建lua_State
lua_State *L = lua_open(); /* create state */
//注册标准库
luaL_openlibs (L);
//注册sum 函数到全局变量中 _G["sum"]
register_sum(L);
//注册sum_closure(),包括2个upvalue
register_sum_closure(L);
//执行sample_1.lua
luaL_dofile (L, "sample_3.lua");
lua_close(L);
return 1;
}Lua 与C/C++ 交互系列: Lua调用C/C++函数(2).
标签:
原文地址:http://blog.csdn.net/sunning9001/article/details/46573895