• <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中一個(gè)lua_State就對(duì)應(yīng)一個(gè)lua stack,通常情況下一個(gè)lua stack就足夠了,由此便產(chǎn)生了與C/C++不同的地方:在C/C++中每個(gè)函數(shù)調(diào)用都會(huì)有自己對(duì)應(yīng)的stack,在函數(shù)調(diào)用完成之后,便將其釋放掉,因而不需要擔(dān)心stack會(huì)無(wú)限制地生長(zhǎng)下去;而在C/C++中使用Lua,由于所有的操作都基于同一個(gè)stack,因此需要程序員自己在代碼中做stack的清理工作。
                Lua提供了一對(duì)API來(lái)幫助程序員操縱stack:lua_gettop和lua_settop。下面給出兩種清理?xiàng)5姆椒ǎ?br>    1)為每個(gè)操作Lua棧的C/C++函數(shù)添加一個(gè)棧頂位置參數(shù)。在調(diào)用該函數(shù)時(shí)將lua_gettop的結(jié)果作為參數(shù)傳遞,在函數(shù)調(diào)用結(jié)束時(shí)使用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āo)毀來(lái)自動(dòng)實(shí)現(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 );
                    }

                }
            ;
                利用此機(jī)制,改造方法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) 評(píng)論(1)  編輯 收藏 引用 所屬分類: Script Programming

            Feedback

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

            對(duì)稱操作使用RAII來(lái)清理的確是一個(gè)best practice.  回復(fù)  更多評(píng)論   

            国产精品美女久久久久久2018| 久久精品国产一区二区| 老色鬼久久亚洲AV综合| 久久久久久久尹人综合网亚洲| 国产成人久久精品二区三区| 久久国产精品二国产精品| 四虎亚洲国产成人久久精品| 亚洲AV无码一区东京热久久| 欧美777精品久久久久网| 久久热这里只有精品在线观看| 2021久久精品国产99国产精品| 久久久久亚洲AV成人网人人软件| 中文精品久久久久人妻不卡| 7国产欧美日韩综合天堂中文久久久久 | 国内精品久久久久久久coent| 性做久久久久久久久老女人| 久久精品国产亚洲77777| 久久综合色老色| 久久久久国产精品三级网| 国内精品久久久久久野外| 久久九九兔免费精品6| 国产福利电影一区二区三区久久久久成人精品综合 | 国产精品一区二区久久精品涩爱| 久久精品成人国产午夜| 久久夜色精品国产噜噜亚洲AV| 深夜久久AAAAA级毛片免费看| 国产免费久久精品丫丫| 人人狠狠综合久久亚洲婷婷| 蜜臀av性久久久久蜜臀aⅴ麻豆| 伊人久久亚洲综合影院| 亚洲精品无码久久久久AV麻豆| 精品免费久久久久国产一区| 精品久久人人做人人爽综合| 94久久国产乱子伦精品免费| 国产精品久久久久乳精品爆| 久久综合久久久| 狠狠色伊人久久精品综合网| 久久99精品国产99久久6| 日韩十八禁一区二区久久| 久久亚洲AV无码西西人体| 天天做夜夜做久久做狠狠|