• <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基本庫(kù)
                luaopen_base(L);    
                
            // 加載Lua通用擴(kuò)展庫(kù)
                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基本庫(kù)
                luaopen_base(L);    
                
            // 加載Lua通用擴(kuò)展庫(kù)
                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 的筆記 閱讀(1276) 評(píng)論(0)  編輯 收藏 引用


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


            国产精品美女久久久久AV福利| 伊人精品久久久久7777| 久久久国产亚洲精品| 精品久久久无码人妻中文字幕豆芽| 无码人妻少妇久久中文字幕| 久久涩综合| 欧美无乱码久久久免费午夜一区二区三区中文字幕| 国产精品福利一区二区久久| 久久精品国产男包| 久久天天躁夜夜躁狠狠躁2022 | 国产精品成人久久久久三级午夜电影 | 国产精品久久午夜夜伦鲁鲁| 国产亚洲色婷婷久久99精品| 丰满少妇人妻久久久久久| 久久99精品久久久久久| 久久精品亚洲男人的天堂| 亚洲精品高清国产一久久| 久久国产乱子伦精品免费午夜| 久久亚洲高清综合| 久久久久久久久久久久久久| 久久精品无码一区二区无码| 88久久精品无码一区二区毛片 | 日本精品久久久中文字幕| 久久九九免费高清视频| 久久久这里只有精品加勒比| 久久综合亚洲欧美成人| 久久综合九色综合精品| 精品久久久久成人码免费动漫| 久久久精品2019免费观看| 国内精品免费久久影院| 久久99精品国产自在现线小黄鸭| 久久久久这里只有精品| 亚洲国产精品久久久天堂| 国产韩国精品一区二区三区久久| 久久亚洲欧洲国产综合| 久久久久亚洲精品天堂| 欧美性大战久久久久久| 久久精品黄AA片一区二区三区| 欧美精品一区二区久久| 99久久国产热无码精品免费| 免费精品久久久久久中文字幕|