• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            的筆記

            隨時(shí)隨地編輯

            lua-c筆記

            參考文檔

            lua所有的一切盡在一本小冊(cè):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中調(diào)用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/


            設(shè)置lua環(huán)境

            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源文件到內(nèi)存編譯
                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;
                
            // 創(chuàng)建Lua接口指針
                lua_State* L = lua_open();    
                
            // 加載Lua基本庫
                luaopen_base(L);    
                
            // 加載Lua通用擴(kuò)展庫
                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調(diào)用的方法
            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;
                
            // 創(chuàng)建Lua接口指針
                lua_State* L = lua_open();    
                
            // 加載Lua基本庫
                luaopen_base(L);    
                
            // 加載Lua通用擴(kuò)展庫
                luaL_openlibs(L);     

                lua_register(L, 
            "average", average);
                
            /* load the script */
                error 
            = luaL_dofile(L, "luacallc.lua");    // 讀取Lua源文件到內(nèi)存編譯

                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 的筆記 閱讀(1275) 評(píng)論(0)  編輯 收藏 引用


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            久久这里只有精品首页| 97r久久精品国产99国产精| 丰满少妇人妻久久久久久4| 一本久久精品一区二区| 亚洲国产成人久久综合一区77 | 久久久无码精品亚洲日韩蜜臀浪潮| 久久久久这里只有精品| 模特私拍国产精品久久| 国产精品gz久久久| 人妻精品久久无码专区精东影业| 久久无码AV中文出轨人妻| 天天久久狠狠色综合| 久久精品一区二区三区不卡| 91精品国产乱码久久久久久| 久久久久亚洲av无码专区导航| 久久久久亚洲精品无码网址| 国内精品久久人妻互换| 久久综合丁香激情久久| 999久久久无码国产精品| 久久夜色精品国产噜噜麻豆| 亚洲AV无码久久精品狠狠爱浪潮| 久久国产亚洲精品| 久久久久亚洲AV无码专区桃色 | 国产高清国内精品福利99久久| 久久99国产精品久久99果冻传媒| 亚洲va久久久噜噜噜久久| 婷婷国产天堂久久综合五月| 国产精品xxxx国产喷水亚洲国产精品无码久久一区 | 亚洲AV无码成人网站久久精品大| 久久精品无码一区二区app| 欧美激情精品久久久久久久九九九| 无码国内精品久久人妻蜜桃| 91精品国产91热久久久久福利 | 一级做a爰片久久毛片毛片| 亚洲精品无码久久久久| 久久无码人妻一区二区三区| 精品国产一区二区三区久久| 久久精品成人影院| 97久久超碰国产精品2021| 久久久久无码精品国产app| 久久精品中文闷骚内射|