• <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就對應(yīng)一個lua stack,通常情況下一個lua stack就足夠了,由此便產(chǎn)生了與C/C++不同的地方:在C/C++中每個函數(shù)調(diào)用都會有自己對應(yīng)的stack,在函數(shù)調(diào)用完成之后,便將其釋放掉,因而不需要擔心stack會無限制地生長下去;而在C/C++中使用Lua,由于所有的操作都基于同一個stack,因此需要程序員自己在代碼中做stack的清理工作。
                Lua提供了一對API來幫助程序員操縱stack:lua_gettop和lua_settop。下面給出兩種清理棧的方法:
                1)為每個操作Lua棧的C/C++函數(shù)添加一個棧頂位置參數(shù)。在調(diào)用該函數(shù)時將lua_gettop的結(jié)果作為參數(shù)傳遞,在函數(shù)調(diào)用結(jié)束時使用lua_settop以傳入的棧頂位置作為參數(shù)恢復(fù)調(diào)用前的棧頂位置。CEGUI中的script module便采用此方法,下面摘錄相關(guān)代碼:
            /*************************************************************************
                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)利用局部變量的創(chuàng)建與銷毀來自動實現(xiàn)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 閱讀(5027) 評論(1)  編輯 收藏 引用 所屬分類: Script Programming

            Feedback

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

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

            国产精品久久久久久久久免费 | 国产精品久久久久久一区二区三区| 国产aⅴ激情无码久久| 久久超碰97人人做人人爱| 草草久久久无码国产专区| 中文字幕久久精品无码| 久久久国产精品网站| 亚洲色大成网站www久久九| 国产成人精品久久亚洲| 无码专区久久综合久中文字幕 | 久久久久久久精品成人热色戒| 久久96国产精品久久久| 日韩AV毛片精品久久久| 国产精品日韩欧美久久综合| 国产69精品久久久久久人妻精品| 国内精品久久久久久久影视麻豆| 久久精品99久久香蕉国产色戒| 综合久久久久久中文字幕亚洲国产国产综合一区首 | 伊人久久大香线蕉亚洲五月天| 久久996热精品xxxx| a高清免费毛片久久| 97久久超碰国产精品2021| 97久久国产露脸精品国产| 国产精品久久久久a影院| 久久久久国产一区二区三区| 99久久精品费精品国产 | 久久人人爽人人爽人人片AV东京热 | 97超级碰碰碰久久久久| 精品人妻久久久久久888| 亚洲va中文字幕无码久久| 国产精品久久久久a影院| 99精品国产免费久久久久久下载| 久久久久亚洲AV成人网人人软件 | 国产午夜精品理论片久久影视| 色欲综合久久躁天天躁蜜桃| 久久综合久久美利坚合众国| 久久国产AVJUST麻豆| 久久婷婷五月综合97色 | 青青草国产精品久久| 国产精品久久久99| 一本久道久久综合狠狠躁AV|