• <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三级久久精品| 久久久久久av无码免费看大片| 欧美无乱码久久久免费午夜一区二区三区中文字幕 | 久久99精品久久久久久野外| 青青热久久国产久精品 | 久久综合久久自在自线精品自| 久久99精品久久久久久久久久| 精品久久久久久无码中文野结衣| 欧美一区二区久久精品| 久久国产乱子伦免费精品| 国产成人99久久亚洲综合精品| 欧美成人免费观看久久| 久久91精品国产91久久麻豆| 日日狠狠久久偷偷色综合免费| 色婷婷综合久久久中文字幕| 国产精品综合久久第一页 | 要久久爱在线免费观看| 久久国产精品-久久精品| 伊人久久精品影院| 夜夜亚洲天天久久| 亚洲精品蜜桃久久久久久| 久久精品国产99久久久香蕉| 久久精品麻豆日日躁夜夜躁| 久久亚洲欧洲国产综合| 国产亚洲欧美精品久久久| 久久天天躁夜夜躁狠狠躁2022| 久久青青草原精品影院| 久久久久免费精品国产| 久久久综合香蕉尹人综合网| 狠狠色丁香久久综合五月| 亚洲午夜久久久影院| 亚洲欧美成人久久综合中文网 | 欧美久久综合九色综合| 久久精品国产99国产精品澳门| 丁香色欲久久久久久综合网| 色欲综合久久躁天天躁| 久久国产视频网| 国产一区二区三精品久久久无广告| 99久久人妻无码精品系列蜜桃| 狠狠色丁香久久婷婷综合|