• <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>

            戰(zhàn)魂小筑

            討論群:309800774 知乎關(guān)注:http://zhihu.com/people/sunicdavy 開源項目:https://github.com/davyxu

               :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              257 隨筆 :: 0 文章 :: 506 評論 :: 0 Trackbacks

            以下代碼使用luabind進行l(wèi)ua的coroutine測試

               1: void ScriptManagedChannel::OnServiceInitialize()
               2: {    
               3:     try
               4:     {        
               5:         mThread = lua_newthread( GScriptScriptContext->GetVM() );
               6:  
               7:         luabind::resume_function<void>( mThread, "ScriptMain", this );
               8:  
               9:         Resume();
              10:     }
              11:     catch (std::exception& e)
              12:     {
              13:         const char* ErrorMsg = lua_tostring( GScriptScriptContext->GetVM(), -1 );            
              14:         printf("%s\n", e.what() );
              15:     }
              16:  
              17:     
              18: }
              19:  
              20: void ScriptManagedChannel::Resume( )
              21: {
              22:     luabind::resume<void>( mThread );
              23: }
              24:  
              25: void ScriptManagedChannel::StopTest( )
              26: {
              27:     lua_yield( mThread, 0 );
              28: }
              29:  
              30:  

            代碼中, mThread類型為lua_State*類型

            GScriptScriptContext->GetVM()是加載了代碼的lua_State*

            StopTest為注冊為ScriptManagedChannel類成員函數(shù)到lua中的定義

            接下來看lua端的測試代碼:

               1: function ScriptMain( Channel )
               2:  
               3:     
               4:     for i = 1, 5 do
               5:     
               6:     print("done", i)
               7:     
               8:     Channel:StopTest( )
               9:     
              10:     
              11:     
              12:     end
              13: end

            剛開始,在測試代碼時, lua中有個手誤而造成的錯誤, 導(dǎo)致C++代碼運行到第7行時彈出assert

            位于:luabind-0.9.1\luabind\detail\call_function.hpp 第264行,對應(yīng)以下代碼第13行

               1: ~proxy_function_void_caller()
               2: {
               3:     if (m_called) return;
               4:  
               5:     m_called = true;
               6:     lua_State* L = m_state;
               7:  
               8:     int top = lua_gettop(L);
               9:  
              10:     push_args_from_tuple<1>::apply(L, m_args);
              11:     if (m_fun(L, boost::tuples::length<Tuple>::value, 0))
              12:     {
              13:         assert(lua_gettop(L) == top - m_params + 1);
              14:  
              15: NO_EXCEPTIONS
              16:         throw luabind::error(L);
              17: #else
              18:         error_callback_fun e = get_error_callback();
              19:         if (e) e(L);
              20:     
              21:         assert(0 && "the lua function threw an error and exceptions are disabled."
              22:                 " If you want to handle the error you can use luabind::set_error_callback()");
              23:         std::terminate();
              24: #endif
              25:     }
              26:     // pops the return values from the function call
              27:     stack_pop pop(L, lua_gettop(L) - top + m_params);
              28: }

            11行代碼中調(diào)用的是lua_resume, 返回的是運行錯誤, 但是被13行的assert擋住了, 無法通過第16行拋出異常被外面捕獲.

            因此,嘗試注釋第13行, 再測試, 可以在lua拋出錯誤后, 在棧頂捕獲到coroutine函數(shù)resume時報出的錯誤信息.問題解決

             

            對于lua的coroutine, 網(wǎng)上資料不多, 這里有一篇比較詳細的代碼

            我比較疑惑的是, 有沒有必要將代碼在dofile或者dobuffer時, 必須傳入newthread出的state? 如果還是傳入原始的state會有什么影響?

            歡迎各位有此經(jīng)驗的討論

            posted on 2012-03-27 10:38 戰(zhàn)魂小筑 閱讀(1526) 評論(2)  編輯 收藏 引用 所屬分類: 腳本技術(shù)C++/ 編程語言

            評論

            # re: luabind使用coroutine時的一處善意提示導(dǎo)致的BUG[未登錄] 2012-03-27 15:36 陳梓瀚(vczh)
            assert和exception是不一樣的。assert是絕對不能發(fā)生的錯誤,exception只是用來替換類似HRESULT這種COM的返回值而已。發(fā)生了assert則一定要修改代碼使之不發(fā)生(對于你的情況就是修改你傳入的參數(shù)),而exception你可以處理或者不處理。

            所以這個不是lua的問題,是你的問題。  回復(fù)  更多評論
              

            # re: luabind使用coroutine時的一處善意提示導(dǎo)致的BUG 2012-03-27 17:30 戰(zhàn)魂小筑
            @陳梓瀚(vczh)
            大哥,我說了嘛,故意錯誤而導(dǎo)致的, 把assert跳過就能正確拋出代碼中錯誤信息
            如果代碼正確,壓根不會有assert  回復(fù)  更多評論
              

            超级碰碰碰碰97久久久久| 欧美黑人又粗又大久久久| 久久精品一区二区| 国内精品久久久久久中文字幕| 一本大道加勒比久久综合| 久久久久综合中文字幕| 亚洲欧美久久久久9999| 久久超碰97人人做人人爱| 99久久亚洲综合精品成人| 综合久久精品色| 久久精品国内一区二区三区| 久久最新免费视频| …久久精品99久久香蕉国产| 日韩亚洲国产综合久久久| 97久久久久人妻精品专区 | 一本色道久久88加勒比—综合| 久久精品夜色噜噜亚洲A∨ | 久久精品亚洲欧美日韩久久| 日韩精品久久久久久免费| 狠狠人妻久久久久久综合| 亚洲级αV无码毛片久久精品 | 日本欧美国产精品第一页久久| 色妞色综合久久夜夜| 色婷婷噜噜久久国产精品12p| 国产国产成人精品久久| 色综合久久久久无码专区| 香蕉99久久国产综合精品宅男自 | 亚洲国产精品无码久久久蜜芽| 国产福利电影一区二区三区久久老子无码午夜伦不 | 日韩一区二区久久久久久| 久久妇女高潮几次MBA| 久久免费大片| 久久91精品综合国产首页| 狠狠色噜噜色狠狠狠综合久久| 日本精品久久久久中文字幕| 久久久久久夜精品精品免费啦| 亚洲欧美精品一区久久中文字幕| 99久久亚洲综合精品网站| 国产精品日韩欧美久久综合| 久久久久久a亚洲欧洲aⅴ| 久久精品国产一区|