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

            Heath's Blog

            There is no end, it is just the beginning! - A Game Developer's Notes

            C/C++中Lua棧的清理

                Lua中一個lua_State就對應一個lua stack,通常情況下一個lua stack就足夠了,由此便產生了與C/C++不同的地方:在C/C++中每個函數調用都會有自己對應的stack,在函數調用完成之后,便將其釋放掉,因而不需要擔心stack會無限制地生長下去;而在C/C++中使用Lua,由于所有的操作都基于同一個stack,因此需要程序員自己在代碼中做stack的清理工作。
                Lua提供了一對API來幫助程序員操縱stack:lua_gettop和lua_settop。下面給出兩種清理棧的方法:
                1)為每個操作Lua棧的C/C++函數添加一個棧頂位置參數。在調用該函數時將lua_gettop的結果作為參數傳遞,在函數調用結束時使用lua_settop以傳入的棧頂位置作為參數恢復調用前的棧頂位置。CEGUI中的script module便采用此方法,下面摘錄相關代碼:
            /*************************************************************************
                Execute script code string
            ************************************************************************
            */

            void LuaScriptModule::executeString(const String& str)
            {
                
            int top = lua_gettop(d_state);

                executeString_impl(str, initErrorHandlerFunc(), top);
                cleanupErrorHandlerFunc();
            }


            void LuaScriptModule::executeString_impl(const String& str, const int err_idx,
                
            const int top)
            {
                
            // load code into lua and call it
                int error = luaL_loadbuffer(d_state, str.c_str(), str.length(), str.c_str()) ||
                            lua_pcall(d_state, 
            00, err_idx);

                
            // handle errors
                if (error)
                
            {
                    String errMsg 
            = lua_tostring(d_state,-1);
                    lua_settop(d_state,top);
                    
            throw ScriptException("Unable to execute Lua script string: '" +
                                          str 
            + "'\n\n" + errMsg + "\n");
                }


                lua_settop(d_state,top);
            }

                2)利用局部變量的創建與銷毀來自動實現stack清理。下面為局部變量的類型
                struct LuaStackAutoPopup
                
            {
                    lua_State
            *    mBackupLS;;
                    
            int            mPos;
                    LuaStackAutoPopup( lua_State
            * vpSL )
                    
            {
                        mPos    
            = lua_gettop( vpSL );
                        mBackupLS    
            = vpSL;
                    }


                    
            void Popup( )
                    
            {
                        lua_settop( mBackupLS,mPos );
                        mBackupLS    
            = NULL;
                    }


                    
            ~LuaStackAutoPopup( )
                    
            {
                        
            if( mBackupLS )
                            lua_settop( mBackupLS,mPos );
                    }

                }
            ;
                利用此機制,改造方法1中的executeString:
            /*************************************************************************
                Execute script code string
            ************************************************************************
            */

            void LuaScriptModule::executeString(const String& str)
            {
                LuaStackAutoPopup stack(d_state);

                executeString_impl(str, initErrorHandlerFunc());
                cleanupErrorHandlerFunc();
            }


            void LuaScriptModule::executeString_impl(const String& str, const int err_idx)
            {
                
            // load code into lua and call it
                int error = luaL_loadbuffer(d_state, str.c_str(), str.length(), str.c_str()) ||
                            lua_pcall(d_state, 
            00, err_idx);

                
            // handle errors
                if (error)
                
            {
                    String errMsg 
            = lua_tostring(d_state,-1);
                    
            throw ScriptException("Unable to execute Lua script string: '" +
                                          str 
            + "'\n\n" + errMsg + "\n");
                }

            }

            posted on 2009-04-25 15:24 Heath 閱讀(4988) 評論(1)  編輯 收藏 引用 所屬分類: Script Programming

            Feedback

            # re: C/C++中Lua棧的清理[未登錄] 2011-12-24 18:52 patz

            對稱操作使用RAII來清理的確是一個best practice.  回復  更多評論   

            精品久久久久久无码人妻热 | 久久久受www免费人成| 久久男人AV资源网站| 久久综合色老色| 99久久人妻无码精品系列| 国产亚州精品女人久久久久久 | 精品免费久久久久国产一区 | 久久男人Av资源网站无码软件 | 日日狠狠久久偷偷色综合免费 | 久久国产色AV免费观看| 久久精品国产亚洲一区二区三区| 久久精品中文字幕大胸| 久久青草国产手机看片福利盒子| 性欧美大战久久久久久久| 97久久久久人妻精品专区| 国产成人综合久久精品红| 久久婷婷久久一区二区三区| 中文精品久久久久人妻不卡| 久久精品成人免费观看97| 久久久久人妻一区精品色| 区亚洲欧美一级久久精品亚洲精品成人网久久久久 | 久久久久婷婷| 色成年激情久久综合| 日本强好片久久久久久AAA| 一日本道伊人久久综合影| 国产精品成人99久久久久91gav | 国产激情久久久久影院老熟女| 久久久婷婷五月亚洲97号色| 热99RE久久精品这里都是精品免费| 久久久久一本毛久久久| 久久99国产精品99久久| 久久91精品国产91久久户| 色综合久久无码五十路人妻| 伊人久久精品无码二区麻豆| av色综合久久天堂av色综合在| 日日狠狠久久偷偷色综合0| 日本精品久久久久影院日本| 久久男人中文字幕资源站| 伊人伊成久久人综合网777| 色老头网站久久网| 久久99精品国产麻豆宅宅|