的笔记

随时随地编辑

lua-c笔记

参考文档

lua所有的一切尽在一本小册:Lua 5.0 Reference Manual 
   pdf: http://www.lua.org/ftp/refman-5.0.pdf.
   html:http://www.lua.org/manual/5.1/

LuaPlus Callback Dispatcher 1.00   
   http://wwhiz.com/LuaPlus/LuaPlusCallDispatcher.html

LuaPlus for Lua 5.01 Distribution   
   http://wwhiz.com/LuaPlus/LuaPlus.html

Lua:Tutorials:Scripting with LuaPlus and Cpp   
   http://www.gpwiki.org/index.php/Lua:Tutorials:Scripting_with_LuaPlus_and_Cpp
lua维基
   http://zh.wikipedia.org/wiki/Lua 
   http://en.wikipedia.org/wiki/Lua_(programming_language)

C++中使用Lua脚本 和lua中调用c的方法
   http://www.cnweblog.com/fly2700/archive/2010/02/09/282920.html

Calling C++ Functions From Lua
http://gamedevgeek.com/tutorials/calling-c-functions-from-lua/


设置lua环境

extern "C"
{
    #include 
"lua.h"
    #include 
"lualib.h"
    #include 
"lauxlib.h"
}


load script

//--test.lua
//function add(x,y)
//return x+y
//end

//print("1+2" , add(1,2))

int _tmain(int argc, _TCHAR* argv[])
{
    lua_State
* L = lua_open();    
    luaopen_base(L);    
    
/* load the script */
    luaL_dofile(L, 
"test.lua");    // 读取Lua源文件到内存编译
    lua_close( L);
    
return 0;
}

 

call lua

//--hellow.lua
//function add(x,y)
//print("x+y=" , x+y );
//return
//end
//
//--print(dddd)
//add(1 , 2)

double fun(lua_State* L ,  double x, double y )
{
    
double ret;
    lua_getglobal( L, 
"add");      
    lua_pushnumber( L,x);         
    lua_pushnumber( L,y);         
    lua_call( L, 
21);          
    ret 
= lua_tonumber( L, -1);    
    lua_pop( L, 
1);              
    
return ret;
}

int _tmain(int argc, _TCHAR* argv[])
{
    
int error;
    
// 创建Lua接口指针
    lua_State* L = lua_open();    
    
// 加载Lua基本库
    luaopen_base(L);    
    
// 加载Lua通用扩展库
    luaL_openlibs(L);     

    
/* load the script */
    error 
= luaL_dofile(L, "test.lua");

    srand( time(
0) );
    
while(1)
    
{
        
int alpha = rand();
        
int beta = rand();
        
int ret = fun(L , alpha , beta );
        printf(
"%d " , ret);
    }


    lua_close( L);
    
return 0;
}

 

lua call c

//--luacallc.lua
//avg = average(20 , 30 , 4)
//print("lua got average value:" , avg)

//被lua调用的方法
static int average(lua_State * L)
{
    
/* get number of arguments */
    
int n = lua_gettop(L);
    
int sum=0;
    
/* loop through each argument */
    
for (int i = 1; i <= n; i++)
    
{
        
/* total the arguments */
        sum 
+= lua_tonumber(L, i);
    }


    lua_pushnumber(L, sum 
/ n);
    
/* return the number of results */
    printf(
"c average called. [ok]\n");
    
return 1;
}


int _tmain(int argc, _TCHAR* argv[])
{
    
int error;
    
// 创建Lua接口指针
    lua_State* L = lua_open();    
    
// 加载Lua基本库
    luaopen_base(L);    
    
// 加载Lua通用扩展库
    luaL_openlibs(L);     

    lua_register(L, 
"average", average);
    
/* load the script */
    error 
= luaL_dofile(L, "luacallc.lua");    // 读取Lua源文件到内存编译

    lua_close( L);
    
return 0;
}

 

lua call C++ object(lua way)

//--luacallcplusfun.lua
//
//o = obj();
//o:set( 50 );
//o:get();
//print("lua got average value:" , avg)

class obj
{
public:
    obj() : val( 
0 ) {}

    
void   setdouble v )
    

        val 
= v;
    }

    
double getvoid )  
    

        
return val;
    }


private:
    
double val;
}
;

class lua_bind
{
public:
    
static void reg( lua_State* L )
    
{
        lua_newtable( L );
        
int tbl = lua_gettop( L );

        luaL_newmetatable( L, 
"obj" );
        
int meta_tbl = lua_gettop( L );

        lua_pushliteral( L, 
"__metatable" );
        lua_pushvalue( L, tbl );
        lua_settable( L, meta_tbl );

        lua_pushliteral( L, 
"__index" );
        lua_pushvalue( L, tbl );
        lua_settable( L, meta_tbl );

        lua_pushliteral( L, 
"__gc" );
        lua_pushcfunction( L, gc );
        lua_settable( L, meta_tbl );

        lua_pop( L, 
1 );
        luaI_openlib( L, 
0, functions, 0 );

        lua_pop( L, 
1 );
        lua_register( L, class_name, build );
    }


    
static int build( lua_State* lua )
    
{
        obj
* p = new obj();
        
*void** )( lua_newuserdata( lua, sizeofvoid* ) ) ) = p;

        luaL_getmetatable( lua, class_name );
        lua_setmetatable( lua, 
-2 );

        cout 
<< "build object, val is 0" << endl;
        
return 1;
    }


    
static int gc( lua_State* lua )
    
{
        obj
* p = ( obj* )( *void** )( lua_touserdata( lua, 1 ) ) );
        delete p;

        cout 
<< "object gc" << endl;
        
return 0;
    }


    
static int set_val( lua_State* lua )
    
{
        obj
* p = ptr( lua, 1 );
        
double val = luaL_checknumber( lua, 2 );

        p
->set( val );

        cout 
<< "set value to " << val << endl;
        
return 0;
    }


    
static int get_val( lua_State* lua )
    
{
        obj
* p = ptr( lua, 1 );
        
double val = p->get();

        lua_pushnumber( lua, val );

        cout 
<< "get value is " << val << endl;
        
return 1;
    }


    
static obj* ptr( lua_State* lua, int narg )
    
{
        luaL_checktype( lua, narg, LUA_TUSERDATA );
        
void* ud = luaL_checkudata( lua, narg, class_name );

        
if( ud )
            
return *( obj** )ud;

        luaL_typerror( lua, narg, class_name );
        
return 0;
    }


    
static const char     class_name[];
    
static const luaL_reg functions[];
}
;

const char lua_bind::class_name[] = "obj";

const luaL_reg lua_bind::functions[] =
{
    
"set", lua_bind::set_val },
    
"get", lua_bind::get_val },
    
{     0,                 0 }
}
;

int _tmain(int argc, _TCHAR* argv[])
{
    lua_State
* L = lua_open();    

    lua_bind::reg( L );

    luaL_dofile( L, 
"luacallcplusobj.lua" );

    lua_close( L);

    
return 0;
}

 

lua call C++ object(luaplus way)

 

posted on 2011-07-01 19:55 的笔记 阅读(1238) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理