• <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>

            的筆記

            隨時隨地編輯

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

            国产色综合久久无码有码| 一本色道久久88加勒比—综合| 久久精品不卡| 久久人人爽人人爽人人片AV东京热 | 亚洲AV无码久久精品蜜桃| 国产精品美女久久久m| 久久精品国产亚洲Aⅴ香蕉| 久久综合偷偷噜噜噜色| 久久久久女人精品毛片| 欧美午夜A∨大片久久| 狠狠久久亚洲欧美专区| 日本五月天婷久久网站| 国产亚州精品女人久久久久久 | 亚洲精品无码久久久久sm| 久久本道久久综合伊人| 99久久免费国产精精品| 久久午夜夜伦鲁鲁片免费无码影视| 久久er国产精品免费观看2| 99精品国产免费久久久久久下载| 亚洲成色999久久网站| 亚洲AV无码久久寂寞少妇| 久久性生大片免费观看性| 久久电影网一区| 久久99国产乱子伦精品免费| 少妇熟女久久综合网色欲| 亚洲国产高清精品线久久 | 欧美亚洲国产精品久久蜜芽| 国内精品人妻无码久久久影院| 99精品国产99久久久久久97| 亚洲精品乱码久久久久久不卡| 久久久久女教师免费一区| 久久精品亚洲欧美日韩久久| 伊人久久大香线蕉精品| 久久综合九色综合精品| 99re这里只有精品热久久| 狠狠色丁香婷婷久久综合不卡| 精品久久久久久久久中文字幕| 国产精品一区二区久久国产| 久久国产精品99久久久久久老狼| 久久精品国产福利国产秒| 99热成人精品免费久久|