Lua 与C/C++ 交互系列: Lua调用C/C++函数(2).

来源:互联网 发布:卞正伟淘宝店 编辑:程序博客网 时间:2024/05/16 05:59


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.
当C函数在任何时候被调用,都可以在C函数内部获取到这些值。然后参数n指定 c closure的数量。 n最大值为255个
为了关联c closure和C函数,首先把要关联的C closure压入到虚拟栈上面。


在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");}

在Lua Code中调用C函数,并打印结果值:

print("sample_3.lua")local a=sum(1,2,3,4,5)print("a:"..a)

3、下面例子,是在上面例子的基础上,增加了闭包。

//在Lua Code中注册的函数int  sum_closure(lua_State *L){   //到该函数被调用时,可以通过lua_upvalueindex()函数获取到该函数的closureint 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;}//提供upvaluesvoid 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");}

在Lua Code中调用C函数

local c =sum_closure(1,2,3,4,5)print("c="..c)

int main(int argc, char **argv){ //创建lua_Statelua_State *L = lua_open();  /* create state *///注册标准库luaL_openlibs (L);//注册sum 函数到全局变量中 _G["sum"]register_sum(L);//注册sum_closure(),包括2个upvalueregister_sum_closure(L);//执行sample_1.lualuaL_dofile (L, "sample_3.lua");lua_close(L);return 1;}


0 0