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

            Feedback

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

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

            日本欧美国产精品第一页久久| 99精品久久精品一区二区| 久久精品aⅴ无码中文字字幕不卡| 精品久久久无码21p发布| 久久久久亚洲AV无码观看| 久久人人添人人爽添人人片牛牛| 色诱久久久久综合网ywww| 日本免费一区二区久久人人澡| 久久国产成人精品国产成人亚洲| 久久中文字幕精品| 久久99国产精品久久久| 久久久午夜精品福利内容| 国产人久久人人人人爽| 久久精品国产亚洲7777| 蜜臀av性久久久久蜜臀aⅴ麻豆| 2021国产成人精品久久| 午夜人妻久久久久久久久| 久久性生大片免费观看性| 九九久久自然熟的香蕉图片| 久久五月精品中文字幕| 99国产欧美久久久精品蜜芽| 久久综合久久综合亚洲| 18岁日韩内射颜射午夜久久成人| 一本色道久久99一综合| 一本大道久久香蕉成人网| 99久久精品无码一区二区毛片| 蜜臀av性久久久久蜜臀aⅴ麻豆| 中文字幕久久亚洲一区| 国产激情久久久久影院老熟女| 欧美黑人又粗又大久久久| 久久精品视频一| 亚洲国产高清精品线久久| 久久久久成人精品无码| 久久er国产精品免费观看8| 亚洲国产精品人久久| 久久精品国产福利国产秒| 久久精品黄AA片一区二区三区| 久久久无码精品亚洲日韩蜜臀浪潮| 欧美无乱码久久久免费午夜一区二区三区中文字幕 | 久久久久高潮毛片免费全部播放 | 久久久久国产一区二区|