• <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 閱讀(5008) 評論(1)  編輯 收藏 引用 所屬分類: Script Programming

            Feedback

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

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

            久久免费看黄a级毛片| 色综合久久久久综合体桃花网| 久久久久久亚洲精品成人| 亚洲中文字幕无码久久2020| 国产精品99久久久精品无码| 97久久国产综合精品女不卡| 色偷偷偷久久伊人大杳蕉| AAA级久久久精品无码片| 久久免费小视频| 久久久久亚洲精品无码网址 | 品成人欧美大片久久国产欧美| 国内精品伊人久久久久| 久久国产香蕉视频| 亚洲中文字幕无码久久2017| 国产日产久久高清欧美一区| 香蕉99久久国产综合精品宅男自 | 国产精品热久久无码av| 久久亚洲欧洲国产综合| 亚洲AV日韩精品久久久久| 久久久久国产一级毛片高清版| 三级韩国一区久久二区综合| 国产精品对白刺激久久久| 久久久久国产成人精品亚洲午夜| 亚洲精品乱码久久久久久久久久久久 | 久久久一本精品99久久精品66| 国产综合免费精品久久久| 一本色道久久88精品综合| 亚洲欧美精品伊人久久| 久久综合香蕉国产蜜臀AV| 久久精品国产精品亚洲艾草网美妙| 久久综合给合久久狠狠狠97色69| 四虎影视久久久免费| 93精91精品国产综合久久香蕉 | 亚洲国产成人久久精品99 | 日产精品久久久一区二区| 日本精品久久久久影院日本| 久久婷婷久久一区二区三区| 乱亲女H秽乱长久久久| 久久久久亚洲精品日久生情 | 久久久国产精品福利免费| 色欲综合久久中文字幕网|