From 0c23f179a46f919adf075b46f4cb8cbff3fc8ebb Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Mon, 28 Oct 2024 16:58:18 +0100 Subject: Some internal function name changes + slight lane:__index optimization --- src/cancel.cpp | 4 ++-- src/cancel.h | 2 +- src/lane.cpp | 48 +++++++++++++++++++++++++----------------------- 3 files changed, 28 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/cancel.cpp b/src/cancel.cpp index 46ed347..ebb0c67 100644 --- a/src/cancel.cpp +++ b/src/cancel.cpp @@ -61,7 +61,7 @@ THE SOFTWARE. // ################################################################################################# //--- -// = thread_cancel( lane_ud [,timeout_secs=0.0] [,wake_lindas_bool=false] ) +// = lane_cancel( lane_ud [,timeout_secs=0.0] [,wake_lindas_bool=false] ) // // The originator thread asking us specifically to cancel the other thread. // @@ -139,7 +139,7 @@ LUAG_FUNC(cancel_test) // ################################################################################################# // bool[,reason] = lane_h:cancel( [cancel_op, hookcount] [, timeout] [, wake_lane]) -LUAG_FUNC(thread_cancel) +LUAG_FUNC(lane_cancel) { Lane* const _lane{ ToLane(L_, StackIndex{ 1 }) }; CancelOp const _op{ WhichCancelOp(L_, StackIndex{ 2 }) }; // this removes the cancel_op string from the stack diff --git a/src/cancel.h b/src/cancel.h index 6dbea99..24fd724 100644 --- a/src/cancel.h +++ b/src/cancel.h @@ -57,4 +57,4 @@ static constexpr UniqueKey kCancelError{ 0x0630345FEF912746ull, "lanes.cancel_er // ################################################################################################# LUAG_FUNC(cancel_test); -LUAG_FUNC(thread_cancel); +LUAG_FUNC(lane_cancel); diff --git a/src/lane.cpp b/src/lane.cpp index 6e6130c..aaddf85 100644 --- a/src/lane.cpp +++ b/src/lane.cpp @@ -45,7 +45,7 @@ static constexpr UniqueKey kCachedTostring{ 0xAB5EA23BCEA0C35Cull }; // ################################################################################################# // lane:get_threadname() -static LUAG_FUNC(get_threadname) +static LUAG_FUNC(lane_get_threadname) { Lane* const _lane{ ToLane(L_, StackIndex{ 1 }) }; luaL_argcheck(L_, lua_gettop(L_) == 1, 2, "too many arguments"); @@ -111,7 +111,7 @@ static LUAG_FUNC(lane_threadname) // error: returns nil + error value [+ stack table] // cancelled: returns nil // -static LUAG_FUNC(thread_join) +static LUAG_FUNC(lane_join) { Lane* const _lane{ ToLane(L_, StackIndex{ 1 }) }; @@ -197,7 +197,7 @@ static LUAG_FUNC(thread_join) // ################################################################################################# -LUAG_FUNC(thread_resume) +LUAG_FUNC(lane_resume) { static constexpr StackIndex kIdxSelf{ 1 }; Lane* const _lane{ ToLane(L_, kIdxSelf) }; @@ -252,7 +252,7 @@ LUAG_FUNC(thread_resume) // key is numeric, wait until the thread returns and populate the environment with the return values // If the return values signal an error, propagate it // Else If key is found in the environment, return it -static int thread_index_number(lua_State* L_) +static int lane_index_number(lua_State* L_) { static constexpr StackIndex kIdxSelf{ 1 }; @@ -282,7 +282,7 @@ static int thread_index_number(lua_State* L_) // If key is "status" return the thread status // If key is found in the environment, return it // Else raise an error -static int thread_index_string(lua_State* L_) +static int lane_index_string(lua_State* L_) { static constexpr StackIndex kIdxSelf{ 1 }; static constexpr StackIndex kIdxKey{ 2 }; @@ -292,6 +292,16 @@ static int thread_index_string(lua_State* L_) std::string_view const _keystr{ luaG_tostring(L_, kIdxKey) }; lua_settop(L_, 2); // keep only our original arguments on the stack + + // look in metatable first + lua_getmetatable(L_, kIdxSelf); // L_: lane "key" mt + lua_replace(L_, -3); // L_: mt "key" + lua_rawget(L_, -2); // L_: mt value + if (luaG_type(L_, kIdxTop) != LuaType::NIL) { // found something? + return 1; // done + } + + lua_pop(L_, 2); // L_: if (_keystr == "status") { _lane->pushStatusString(L_); // L_: lane "key" "" return 1; @@ -300,21 +310,13 @@ static int thread_index_string(lua_State* L_) std::ignore = _lane->pushErrorTraceLevel(L_); // L_: lane "key" "" return 1; } - // return self.metatable[key] - lua_getmetatable(L_, kIdxSelf); // L_: lane "key" mt - lua_replace(L_, -3); // L_: mt "key" - lua_rawget(L_, -2); // L_: mt value - // only "cancel" and "join" are registered as functions, any other string will raise an error - if (!lua_iscfunction(L_, -1)) { - raise_luaL_error(L_, "can't index a lane with '%s'", _keystr.data()); - } - return 1; + raise_luaL_error(L_, "unknown field '%s'", _keystr.data()); } // ################################################################################################# // lane:__index(key,usr) -> value -static LUAG_FUNC(thread_index) +static LUAG_FUNC(lane_index) { static constexpr StackIndex kIdxSelf{ 1 }; static constexpr StackIndex kKey{ 2 }; @@ -323,10 +325,10 @@ static LUAG_FUNC(thread_index) switch (luaG_type(L_, kKey)) { case LuaType::NUMBER: - return thread_index_number(L_); // stack modification is undefined, returned value is at the top + return lane_index_number(L_); // stack modification is undefined, returned value is at the top case LuaType::STRING: - return thread_index_string(L_); // stack modification is undefined, returned value is at the top + return lane_index_string(L_); // stack modification is undefined, returned value is at the top default: // unknown key lua_getmetatable(L_, kIdxSelf); // L_: mt @@ -802,7 +804,7 @@ static LUAG_FUNC(lane_close) // no error if the lane body doesn't return a non-nil first value luaG_pushstring(L_, "close"); // L_: lane "close" - lua_pushcclosure(L_, LG_thread_join, 1); // L_: lane join() + lua_pushcclosure(L_, LG_lane_join, 1); // L_: lane join() lua_insert(L_, 1); // L_: join() lane lua_call(L_, 1, LUA_MULTRET); // L_: join() results return lua_gettop(L_); @@ -1007,11 +1009,11 @@ namespace { { "__close", LG_lane_close }, #endif // LUA_VERSION_NUM >= 504 { "__gc", LG_lane_gc }, - { "__index", LG_thread_index }, - { "cancel", LG_thread_cancel }, - { "get_threadname", LG_get_threadname }, - { "join", LG_thread_join }, - { "resume", LG_thread_resume }, + { "__index", LG_lane_index }, + { "cancel", LG_lane_cancel }, + { "get_threadname", LG_lane_get_threadname }, + { "join", LG_lane_join }, + { "resume", LG_lane_resume }, { nullptr, nullptr } }; } // namespace local -- cgit v1.2.3-55-g6feb