diff options
Diffstat (limited to 'src/lane.cpp')
-rw-r--r-- | src/lane.cpp | 84 |
1 files changed, 46 insertions, 38 deletions
diff --git a/src/lane.cpp b/src/lane.cpp index 7650d6b..f220bce 100644 --- a/src/lane.cpp +++ b/src/lane.cpp | |||
@@ -354,32 +354,38 @@ static LUAG_FUNC(thread_index) | |||
354 | // ################################################################################################# | 354 | // ################################################################################################# |
355 | 355 | ||
356 | #if USE_DEBUG_SPEW() | 356 | #if USE_DEBUG_SPEW() |
357 | // can't use direct LUA_x errcode indexing because the sequence is not the same between Lua 5.1 and 5.2 :-( | 357 | namespace { |
358 | // LUA_ERRERR doesn't have the same value | 358 | // can't use direct LUA_x errcode indexing because the sequence is not the same between Lua 5.1 and 5.2 :-( |
359 | struct errcode_name | 359 | // LUA_ERRERR doesn't have the same value |
360 | { | 360 | struct errcode_name |
361 | LuaError code; | 361 | { |
362 | char const* name; | 362 | LuaError code; |
363 | }; | 363 | std::string_view const name; |
364 | 364 | }; | |
365 | static struct errcode_name s_errcodes[] = { | 365 | |
366 | { LuaError::OK, "LUA_OK" }, | 366 | namespace local { |
367 | { LuaError::YIELD, "LUA_YIELD" }, | 367 | |
368 | { LuaError::ERRRUN, "LUA_ERRRUN" }, | 368 | static struct errcode_name sErrCodes[] = { |
369 | { LuaError::ERRSYNTAX, "LUA_ERRSYNTAX" }, | 369 | { LuaError::OK, "LUA_OK" }, |
370 | { LuaError::ERRMEM, "LUA_ERRMEM" }, | 370 | { LuaError::YIELD, "LUA_YIELD" }, |
371 | { LuaError::ERRGCMM, "LUA_ERRGCMM" }, | 371 | { LuaError::ERRRUN, "LUA_ERRRUN" }, |
372 | { LuaError::ERRERR, "LUA_ERRERR" }, | 372 | { LuaError::ERRSYNTAX, "LUA_ERRSYNTAX" }, |
373 | }; | 373 | { LuaError::ERRMEM, "LUA_ERRMEM" }, |
374 | static char const* get_errcode_name(LuaError _code) | 374 | { LuaError::ERRGCMM, "LUA_ERRGCMM" }, |
375 | { | 375 | { LuaError::ERRERR, "LUA_ERRERR" }, |
376 | for (errcode_name const& _entry : s_errcodes) { | 376 | }; |
377 | if (_entry.code == _code) { | 377 | } // namespace local |
378 | return _entry.name; | 378 | |
379 | static std::string_view GetErrcodeName(LuaError _code) noexcept | ||
380 | { | ||
381 | for (errcode_name const& _entry : local::sErrCodes) { | ||
382 | if (_entry.code == _code) { | ||
383 | return _entry.name; | ||
384 | } | ||
379 | } | 385 | } |
386 | return "<nullptr>"; | ||
380 | } | 387 | } |
381 | return "<nullptr>"; | 388 | } // namespace |
382 | } | ||
383 | #endif // USE_DEBUG_SPEW() | 389 | #endif // USE_DEBUG_SPEW() |
384 | 390 | ||
385 | // ################################################################################################# | 391 | // ################################################################################################# |
@@ -545,7 +551,7 @@ static void push_stack_trace(lua_State* L_, Lane::ErrorTraceLevel errorTraceLeve | |||
545 | LUA_ASSERT(L_, lua_isfunction(L_, -1)); | 551 | LUA_ASSERT(L_, lua_isfunction(L_, -1)); |
546 | if (lua_rc_ != LuaError::OK) { // we have an error message and an optional stack trace at the bottom of the stack | 552 | if (lua_rc_ != LuaError::OK) { // we have an error message and an optional stack trace at the bottom of the stack |
547 | LUA_ASSERT(L_, _finalizers_index == 2 || _finalizers_index == 3); | 553 | LUA_ASSERT(L_, _finalizers_index == 2 || _finalizers_index == 3); |
548 | // char const* err_msg = lua_tostring(L_, 1); | 554 | //std::string_view const _err_msg{ lua_tostringview(L_, 1) }; |
549 | lua_pushvalue(L_, 1); // L_: ... finalizers lane_error finalizer err_msg | 555 | lua_pushvalue(L_, 1); // L_: ... finalizers lane_error finalizer err_msg |
550 | // note we don't always have a stack trace for example when kCancelError, or when we got an error that doesn't call our handler, such as LUA_ERRMEM | 556 | // note we don't always have a stack trace for example when kCancelError, or when we got an error that doesn't call our handler, such as LUA_ERRMEM |
551 | if (_finalizers_index == 3) { | 557 | if (_finalizers_index == 3) { |
@@ -677,11 +683,11 @@ static void lane_main(Lane* lane_) | |||
677 | // in case of error and if it exists, fetch stack trace from registry and push it | 683 | // in case of error and if it exists, fetch stack trace from registry and push it |
678 | push_stack_trace(_L, lane_->errorTraceLevel, _rc, 1); // L: retvals|error [trace] | 684 | push_stack_trace(_L, lane_->errorTraceLevel, _rc, 1); // L: retvals|error [trace] |
679 | 685 | ||
680 | DEBUGSPEW_CODE(DebugSpew(_U) << "Lane " << _L << " body: " << get_errcode_name(_rc) << " (" << (kCancelError.equals(_L, 1) ? "cancelled" : lua_typename(_L, lua_type(_L, 1))) << ")" << std::endl); | 686 | DEBUGSPEW_CODE(DebugSpew(_U) << "Lane " << _L << " body: " << GetErrcodeName(_rc) << " (" << (kCancelError.equals(_L, 1) ? "cancelled" : lua_typename(_L, lua_type(_L, 1))) << ")" << std::endl); |
681 | // Call finalizers, if the script has set them up. | 687 | // Call finalizers, if the script has set them up. |
682 | // | 688 | // |
683 | LuaError const _rc2{ run_finalizers(_L, lane_->errorTraceLevel, _rc) }; | 689 | LuaError const _rc2{ run_finalizers(_L, lane_->errorTraceLevel, _rc) }; |
684 | DEBUGSPEW_CODE(DebugSpew(_U) << "Lane " << _L << " finalizer: " << get_errcode_name(_rc2) << std::endl); | 690 | DEBUGSPEW_CODE(DebugSpew(_U) << "Lane " << _L << " finalizer: " << GetErrcodeName(_rc2) << std::endl); |
685 | if (_rc2 != LuaError::OK) { // Error within a finalizer! | 691 | if (_rc2 != LuaError::OK) { // Error within a finalizer! |
686 | // the finalizer generated an error, and left its own error message [and stack trace] on the stack | 692 | // the finalizer generated an error, and left its own error message [and stack trace] on the stack |
687 | _rc = _rc2; // we're overruling the earlier script error or normal return | 693 | _rc = _rc2; // we're overruling the earlier script error or normal return |
@@ -840,23 +846,25 @@ void Lane::changeDebugName(int nameIdx_) | |||
840 | 846 | ||
841 | // ################################################################################################# | 847 | // ################################################################################################# |
842 | 848 | ||
843 | namespace global { | 849 | namespace { |
844 | static struct luaL_Reg const sLaneFunctions[] = { | 850 | namespace local { |
845 | { "__gc", lane_gc }, | 851 | static struct luaL_Reg const sLaneFunctions[] = { |
846 | { "__index", LG_thread_index }, | 852 | { "__gc", lane_gc }, |
847 | { "cancel", LG_thread_cancel }, | 853 | { "__index", LG_thread_index }, |
848 | { "get_debug_threadname", LG_get_debug_threadname }, | 854 | { "cancel", LG_thread_cancel }, |
849 | { "join", LG_thread_join }, | 855 | { "get_debug_threadname", LG_get_debug_threadname }, |
850 | { nullptr, nullptr } | 856 | { "join", LG_thread_join }, |
851 | }; | 857 | { nullptr, nullptr } |
852 | } // namespace global | 858 | }; |
859 | } // namespace local | ||
860 | } // namespace | ||
853 | 861 | ||
854 | // contains keys: { __gc, __index, cached_error, cached_tostring, cancel, join, get_debug_threadname } | 862 | // contains keys: { __gc, __index, cached_error, cached_tostring, cancel, join, get_debug_threadname } |
855 | void Lane::PushMetatable(lua_State* L_) | 863 | void Lane::PushMetatable(lua_State* L_) |
856 | { | 864 | { |
857 | STACK_CHECK_START_REL(L_, 0); | 865 | STACK_CHECK_START_REL(L_, 0); |
858 | if (luaL_newmetatable(L_, kLaneMetatableName)) { // L_: mt | 866 | if (luaL_newmetatable(L_, kLaneMetatableName)) { // L_: mt |
859 | luaG_registerlibfuncs(L_, global::sLaneFunctions); | 867 | luaG_registerlibfuncs(L_, local::sLaneFunctions); |
860 | // cache error() and tostring() | 868 | // cache error() and tostring() |
861 | kCachedError.pushKey(L_); // L_: mt kCachedError | 869 | kCachedError.pushKey(L_); // L_: mt kCachedError |
862 | lua_getglobal(L_, "error"); // L_: mt kCachedError error() | 870 | lua_getglobal(L_, "error"); // L_: mt kCachedError error() |