diff options
author | Benoit Germain <benoit.germain@ubisoft.com> | 2024-10-28 16:58:18 +0100 |
---|---|---|
committer | Benoit Germain <benoit.germain@ubisoft.com> | 2024-10-28 16:58:18 +0100 |
commit | 0c23f179a46f919adf075b46f4cb8cbff3fc8ebb (patch) | |
tree | 58e6c51a0de6389475601bc542b939a84811465d | |
parent | c860f557a7ba72e6a39ea5db36e293de802adea5 (diff) | |
download | lanes-0c23f179a46f919adf075b46f4cb8cbff3fc8ebb.tar.gz lanes-0c23f179a46f919adf075b46f4cb8cbff3fc8ebb.tar.bz2 lanes-0c23f179a46f919adf075b46f4cb8cbff3fc8ebb.zip |
Some internal function name changes + slight lane:__index optimization
-rw-r--r-- | src/cancel.cpp | 4 | ||||
-rw-r--r-- | src/cancel.h | 2 | ||||
-rw-r--r-- | src/lane.cpp | 48 |
3 files changed, 28 insertions, 26 deletions
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. | |||
61 | // ################################################################################################# | 61 | // ################################################################################################# |
62 | 62 | ||
63 | //--- | 63 | //--- |
64 | // = thread_cancel( lane_ud [,timeout_secs=0.0] [,wake_lindas_bool=false] ) | 64 | // = lane_cancel( lane_ud [,timeout_secs=0.0] [,wake_lindas_bool=false] ) |
65 | // | 65 | // |
66 | // The originator thread asking us specifically to cancel the other thread. | 66 | // The originator thread asking us specifically to cancel the other thread. |
67 | // | 67 | // |
@@ -139,7 +139,7 @@ LUAG_FUNC(cancel_test) | |||
139 | // ################################################################################################# | 139 | // ################################################################################################# |
140 | 140 | ||
141 | // bool[,reason] = lane_h:cancel( [cancel_op, hookcount] [, timeout] [, wake_lane]) | 141 | // bool[,reason] = lane_h:cancel( [cancel_op, hookcount] [, timeout] [, wake_lane]) |
142 | LUAG_FUNC(thread_cancel) | 142 | LUAG_FUNC(lane_cancel) |
143 | { | 143 | { |
144 | Lane* const _lane{ ToLane(L_, StackIndex{ 1 }) }; | 144 | Lane* const _lane{ ToLane(L_, StackIndex{ 1 }) }; |
145 | CancelOp const _op{ WhichCancelOp(L_, StackIndex{ 2 }) }; // this removes the cancel_op string from the stack | 145 | 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 | |||
57 | // ################################################################################################# | 57 | // ################################################################################################# |
58 | 58 | ||
59 | LUAG_FUNC(cancel_test); | 59 | LUAG_FUNC(cancel_test); |
60 | LUAG_FUNC(thread_cancel); | 60 | 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 }; | |||
45 | // ################################################################################################# | 45 | // ################################################################################################# |
46 | 46 | ||
47 | // lane:get_threadname() | 47 | // lane:get_threadname() |
48 | static LUAG_FUNC(get_threadname) | 48 | static LUAG_FUNC(lane_get_threadname) |
49 | { | 49 | { |
50 | Lane* const _lane{ ToLane(L_, StackIndex{ 1 }) }; | 50 | Lane* const _lane{ ToLane(L_, StackIndex{ 1 }) }; |
51 | luaL_argcheck(L_, lua_gettop(L_) == 1, 2, "too many arguments"); | 51 | luaL_argcheck(L_, lua_gettop(L_) == 1, 2, "too many arguments"); |
@@ -111,7 +111,7 @@ static LUAG_FUNC(lane_threadname) | |||
111 | // error: returns nil + error value [+ stack table] | 111 | // error: returns nil + error value [+ stack table] |
112 | // cancelled: returns nil | 112 | // cancelled: returns nil |
113 | // | 113 | // |
114 | static LUAG_FUNC(thread_join) | 114 | static LUAG_FUNC(lane_join) |
115 | { | 115 | { |
116 | Lane* const _lane{ ToLane(L_, StackIndex{ 1 }) }; | 116 | Lane* const _lane{ ToLane(L_, StackIndex{ 1 }) }; |
117 | 117 | ||
@@ -197,7 +197,7 @@ static LUAG_FUNC(thread_join) | |||
197 | 197 | ||
198 | // ################################################################################################# | 198 | // ################################################################################################# |
199 | 199 | ||
200 | LUAG_FUNC(thread_resume) | 200 | LUAG_FUNC(lane_resume) |
201 | { | 201 | { |
202 | static constexpr StackIndex kIdxSelf{ 1 }; | 202 | static constexpr StackIndex kIdxSelf{ 1 }; |
203 | Lane* const _lane{ ToLane(L_, kIdxSelf) }; | 203 | Lane* const _lane{ ToLane(L_, kIdxSelf) }; |
@@ -252,7 +252,7 @@ LUAG_FUNC(thread_resume) | |||
252 | // key is numeric, wait until the thread returns and populate the environment with the return values | 252 | // key is numeric, wait until the thread returns and populate the environment with the return values |
253 | // If the return values signal an error, propagate it | 253 | // If the return values signal an error, propagate it |
254 | // Else If key is found in the environment, return it | 254 | // Else If key is found in the environment, return it |
255 | static int thread_index_number(lua_State* L_) | 255 | static int lane_index_number(lua_State* L_) |
256 | { | 256 | { |
257 | static constexpr StackIndex kIdxSelf{ 1 }; | 257 | static constexpr StackIndex kIdxSelf{ 1 }; |
258 | 258 | ||
@@ -282,7 +282,7 @@ static int thread_index_number(lua_State* L_) | |||
282 | // If key is "status" return the thread status | 282 | // If key is "status" return the thread status |
283 | // If key is found in the environment, return it | 283 | // If key is found in the environment, return it |
284 | // Else raise an error | 284 | // Else raise an error |
285 | static int thread_index_string(lua_State* L_) | 285 | static int lane_index_string(lua_State* L_) |
286 | { | 286 | { |
287 | static constexpr StackIndex kIdxSelf{ 1 }; | 287 | static constexpr StackIndex kIdxSelf{ 1 }; |
288 | static constexpr StackIndex kIdxKey{ 2 }; | 288 | static constexpr StackIndex kIdxKey{ 2 }; |
@@ -292,6 +292,16 @@ static int thread_index_string(lua_State* L_) | |||
292 | 292 | ||
293 | std::string_view const _keystr{ luaG_tostring(L_, kIdxKey) }; | 293 | std::string_view const _keystr{ luaG_tostring(L_, kIdxKey) }; |
294 | lua_settop(L_, 2); // keep only our original arguments on the stack | 294 | lua_settop(L_, 2); // keep only our original arguments on the stack |
295 | |||
296 | // look in metatable first | ||
297 | lua_getmetatable(L_, kIdxSelf); // L_: lane "key" mt | ||
298 | lua_replace(L_, -3); // L_: mt "key" | ||
299 | lua_rawget(L_, -2); // L_: mt value | ||
300 | if (luaG_type(L_, kIdxTop) != LuaType::NIL) { // found something? | ||
301 | return 1; // done | ||
302 | } | ||
303 | |||
304 | lua_pop(L_, 2); // L_: | ||
295 | if (_keystr == "status") { | 305 | if (_keystr == "status") { |
296 | _lane->pushStatusString(L_); // L_: lane "key" "<status>" | 306 | _lane->pushStatusString(L_); // L_: lane "key" "<status>" |
297 | return 1; | 307 | return 1; |
@@ -300,21 +310,13 @@ static int thread_index_string(lua_State* L_) | |||
300 | std::ignore = _lane->pushErrorTraceLevel(L_); // L_: lane "key" "<level>" | 310 | std::ignore = _lane->pushErrorTraceLevel(L_); // L_: lane "key" "<level>" |
301 | return 1; | 311 | return 1; |
302 | } | 312 | } |
303 | // return self.metatable[key] | 313 | raise_luaL_error(L_, "unknown field '%s'", _keystr.data()); |
304 | lua_getmetatable(L_, kIdxSelf); // L_: lane "key" mt | ||
305 | lua_replace(L_, -3); // L_: mt "key" | ||
306 | lua_rawget(L_, -2); // L_: mt value | ||
307 | // only "cancel" and "join" are registered as functions, any other string will raise an error | ||
308 | if (!lua_iscfunction(L_, -1)) { | ||
309 | raise_luaL_error(L_, "can't index a lane with '%s'", _keystr.data()); | ||
310 | } | ||
311 | return 1; | ||
312 | } | 314 | } |
313 | 315 | ||
314 | // ################################################################################################# | 316 | // ################################################################################################# |
315 | 317 | ||
316 | // lane:__index(key,usr) -> value | 318 | // lane:__index(key,usr) -> value |
317 | static LUAG_FUNC(thread_index) | 319 | static LUAG_FUNC(lane_index) |
318 | { | 320 | { |
319 | static constexpr StackIndex kIdxSelf{ 1 }; | 321 | static constexpr StackIndex kIdxSelf{ 1 }; |
320 | static constexpr StackIndex kKey{ 2 }; | 322 | static constexpr StackIndex kKey{ 2 }; |
@@ -323,10 +325,10 @@ static LUAG_FUNC(thread_index) | |||
323 | 325 | ||
324 | switch (luaG_type(L_, kKey)) { | 326 | switch (luaG_type(L_, kKey)) { |
325 | case LuaType::NUMBER: | 327 | case LuaType::NUMBER: |
326 | return thread_index_number(L_); // stack modification is undefined, returned value is at the top | 328 | return lane_index_number(L_); // stack modification is undefined, returned value is at the top |
327 | 329 | ||
328 | case LuaType::STRING: | 330 | case LuaType::STRING: |
329 | return thread_index_string(L_); // stack modification is undefined, returned value is at the top | 331 | return lane_index_string(L_); // stack modification is undefined, returned value is at the top |
330 | 332 | ||
331 | default: // unknown key | 333 | default: // unknown key |
332 | lua_getmetatable(L_, kIdxSelf); // L_: mt | 334 | lua_getmetatable(L_, kIdxSelf); // L_: mt |
@@ -802,7 +804,7 @@ static LUAG_FUNC(lane_close) | |||
802 | 804 | ||
803 | // no error if the lane body doesn't return a non-nil first value | 805 | // no error if the lane body doesn't return a non-nil first value |
804 | luaG_pushstring(L_, "close"); // L_: lane "close" | 806 | luaG_pushstring(L_, "close"); // L_: lane "close" |
805 | lua_pushcclosure(L_, LG_thread_join, 1); // L_: lane join() | 807 | lua_pushcclosure(L_, LG_lane_join, 1); // L_: lane join() |
806 | lua_insert(L_, 1); // L_: join() lane | 808 | lua_insert(L_, 1); // L_: join() lane |
807 | lua_call(L_, 1, LUA_MULTRET); // L_: join() results | 809 | lua_call(L_, 1, LUA_MULTRET); // L_: join() results |
808 | return lua_gettop(L_); | 810 | return lua_gettop(L_); |
@@ -1007,11 +1009,11 @@ namespace { | |||
1007 | { "__close", LG_lane_close }, | 1009 | { "__close", LG_lane_close }, |
1008 | #endif // LUA_VERSION_NUM >= 504 | 1010 | #endif // LUA_VERSION_NUM >= 504 |
1009 | { "__gc", LG_lane_gc }, | 1011 | { "__gc", LG_lane_gc }, |
1010 | { "__index", LG_thread_index }, | 1012 | { "__index", LG_lane_index }, |
1011 | { "cancel", LG_thread_cancel }, | 1013 | { "cancel", LG_lane_cancel }, |
1012 | { "get_threadname", LG_get_threadname }, | 1014 | { "get_threadname", LG_lane_get_threadname }, |
1013 | { "join", LG_thread_join }, | 1015 | { "join", LG_lane_join }, |
1014 | { "resume", LG_thread_resume }, | 1016 | { "resume", LG_lane_resume }, |
1015 | { nullptr, nullptr } | 1017 | { nullptr, nullptr } |
1016 | }; | 1018 | }; |
1017 | } // namespace local | 1019 | } // namespace local |