From aa56a8713105d303226e40d3e5078ec16cda888e Mon Sep 17 00:00:00 2001 From: Caspar van Leeuwen Date: Wed, 6 Mar 2024 13:57:53 +0100 Subject: [PATCH 1/5] Provide two implementations of a hook.register-like function that give the ability to append. Also, make usre the hook.apply works when the hook is an array --- src/Hook.lua | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/src/Hook.lua b/src/Hook.lua index 5765a12cb..4b92da339 100644 --- a/src/Hook.lua +++ b/src/Hook.lua @@ -84,11 +84,36 @@ local validT = -- @param func The function to store with it. function M.register(name, func) if (validT[name] ~= nil) then - validT[name] = func + -- If func and validT[name] are both tables, append func to the current table. + -- else, overwrite it. This also provides backwards compatibility. + if type(func) == "table" and type(validT[name]) == "table" then + for i=1,#func do + validT[name][#validT[name]+1] = func[i] + end + else + validT[name] = func + end else LmodWarning{msg="w_Unknown_Hook",name = tostring(name)} end +end +function M.register_alt(name, func, append) + if (validT[name] ~= nil) then + -- set default for append to be backwards compatible + append = append or false + if append then + if type(validT[name]) == "table" then + validT[name][#validT[name]+1] = func + else + validT[name] = {validT[name], func} + end + else + validT[name] = func + end + else + LmodWarning{msg="w_Unknown_Hook",name = tostring(name)} + end end -------------------------------------------------------------------------- @@ -97,7 +122,27 @@ end -- @return the results of the hook if it exists. function M.apply(name, ...) if (validT[name]) then - return validT[name](...) + if type(validT[name]) == "table" then + LmodMessage("Running apply hook "..name.." with table input of length "..#validT[name]) + local returnT = {} + for i=1,#validT[name] do + local return_val = validT[name][i](...) + if return_val ~= nil then + table.insert(returnT, return_val) + end + end + -- not entirely sure what the most sensible thing is to return here + -- I would like it to be an array of return values, but arrays cant hold nil as a value... + -- The result is that the returned table might have fewer elements than the original hooks + -- The alternative would be to just return nil in this case + -- That does not allow handling of any return values, but sensible handling of + -- hook values is hard anyway, since hooks are arbitrary and one doesn't know the meaning + -- of the return values anyway. + return returnT + else + LmodMessage("Running apply hook "..name.." with non-table input") + return validT[name](...) + end end end From ce625d1a1fef9508cff50bfd326c6ab408d77eb4 Mon Sep 17 00:00:00 2001 From: Caspar van Leeuwen Date: Wed, 6 Mar 2024 14:22:55 +0100 Subject: [PATCH 2/5] Make the internal type for string hooks a table, always. This simplifies the logic. --- src/Hook.lua | 64 ++++++++++++++++++++++++++-------------------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/src/Hook.lua b/src/Hook.lua index 4b92da339..795d02efd 100644 --- a/src/Hook.lua +++ b/src/Hook.lua @@ -84,14 +84,15 @@ local validT = -- @param func The function to store with it. function M.register(name, func) if (validT[name] ~= nil) then - -- If func and validT[name] are both tables, append func to the current table. - -- else, overwrite it. This also provides backwards compatibility. - if type(func) == "table" and type(validT[name]) == "table" then + -- If func is a table and validT[name] was previously set, append func to the current table. + if type(func) == "table" and (validT[name]) then for i=1,#func do - validT[name][#validT[name]+1] = func[i] + validT[name][#validT[name]+1] = func[i] end - else + elseif type(func) == "table" then validT[name] = func + else + validT[name] = {func} end else LmodWarning{msg="w_Unknown_Hook",name = tostring(name)} @@ -102,14 +103,12 @@ function M.register_alt(name, func, append) if (validT[name] ~= nil) then -- set default for append to be backwards compatible append = append or false - if append then - if type(validT[name]) == "table" then - validT[name][#validT[name]+1] = func - else - validT[name] = {validT[name], func} - end + -- if append and validT[name] was set before, append. Otherwise, overwrite. + if append and validT[name] then + -- if validT[name] was set before (i.e. isn't false), append + validT[name][#validT[name]+1] = func else - validT[name] = func + validT[name] = {func} end else LmodWarning{msg="w_Unknown_Hook",name = tostring(name)} @@ -122,27 +121,28 @@ end -- @return the results of the hook if it exists. function M.apply(name, ...) if (validT[name]) then - if type(validT[name]) == "table" then - LmodMessage("Running apply hook "..name.." with table input of length "..#validT[name]) - local returnT = {} - for i=1,#validT[name] do - local return_val = validT[name][i](...) - if return_val ~= nil then - table.insert(returnT, return_val) - end - end - -- not entirely sure what the most sensible thing is to return here - -- I would like it to be an array of return values, but arrays cant hold nil as a value... - -- The result is that the returned table might have fewer elements than the original hooks - -- The alternative would be to just return nil in this case - -- That does not allow handling of any return values, but sensible handling of - -- hook values is hard anyway, since hooks are arbitrary and one doesn't know the meaning - -- of the return values anyway. - return returnT - else - LmodMessage("Running apply hook "..name.." with non-table input") - return validT[name](...) + LmodMessage("Running apply hook "..name.." with table input of length "..#validT[name]) + -- not entirely sure what the most sensible thing is to return here. + -- I would like it to be an array of return values, but arrays cant hold nil as a value... + -- The result is that the returned table might have fewer elements than the original hooks + -- That's... awkward, but could be done like this: + -- + -- local returnT = {} + -- for i=1,#validT[name] do + -- local return_val = validT[name][i](...) + -- if return_val ~= nil then + -- table.insert(returnT, return_val) + -- end + -- end + -- return returnT + -- + -- I prefer the alternative: just dont return anything. + -- That does not allow handling of any return values, but sensible handling of + -- arbitrary hook functions is hard anyway (we don't know the meaning of any return type) + for i=1,#validT[name] do + validT[name][i](...) end + return end end From 5d27dc3e136932bc2e3d62c72021a675b2ea4cdf Mon Sep 17 00:00:00 2001 From: Caspar van Leeuwen Date: Wed, 6 Mar 2024 14:25:18 +0100 Subject: [PATCH 3/5] Code cleanup, reduce this branch to one register function only --- src/Hook.lua | 37 +------------------------------------ 1 file changed, 1 insertion(+), 36 deletions(-) diff --git a/src/Hook.lua b/src/Hook.lua index 795d02efd..c114ce2c8 100644 --- a/src/Hook.lua +++ b/src/Hook.lua @@ -82,24 +82,7 @@ local validT = -- Checks for a valid hook name and stores it if valid. -- @param name The name of the hook. -- @param func The function to store with it. -function M.register(name, func) - if (validT[name] ~= nil) then - -- If func is a table and validT[name] was previously set, append func to the current table. - if type(func) == "table" and (validT[name]) then - for i=1,#func do - validT[name][#validT[name]+1] = func[i] - end - elseif type(func) == "table" then - validT[name] = func - else - validT[name] = {func} - end - else - LmodWarning{msg="w_Unknown_Hook",name = tostring(name)} - end -end - -function M.register_alt(name, func, append) +function M.register(name, func, append) if (validT[name] ~= nil) then -- set default for append to be backwards compatible append = append or false @@ -121,24 +104,6 @@ end -- @return the results of the hook if it exists. function M.apply(name, ...) if (validT[name]) then - LmodMessage("Running apply hook "..name.." with table input of length "..#validT[name]) - -- not entirely sure what the most sensible thing is to return here. - -- I would like it to be an array of return values, but arrays cant hold nil as a value... - -- The result is that the returned table might have fewer elements than the original hooks - -- That's... awkward, but could be done like this: - -- - -- local returnT = {} - -- for i=1,#validT[name] do - -- local return_val = validT[name][i](...) - -- if return_val ~= nil then - -- table.insert(returnT, return_val) - -- end - -- end - -- return returnT - -- - -- I prefer the alternative: just dont return anything. - -- That does not allow handling of any return values, but sensible handling of - -- arbitrary hook functions is hard anyway (we don't know the meaning of any return type) for i=1,#validT[name] do validT[name][i](...) end From 6c2db393234194879bbd4016bad7c07f07bda294 Mon Sep 17 00:00:00 2001 From: Caspar van Leeuwen Date: Wed, 6 Mar 2024 14:28:01 +0100 Subject: [PATCH 4/5] Remove empty return --- src/Hook.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Hook.lua b/src/Hook.lua index c114ce2c8..68b52c20f 100644 --- a/src/Hook.lua +++ b/src/Hook.lua @@ -107,7 +107,6 @@ function M.apply(name, ...) for i=1,#validT[name] do validT[name][i](...) end - return end end From d89e9745eb649564266c94e37fe5c3bb8822cc68 Mon Sep 17 00:00:00 2001 From: Caspar van Leeuwen Date: Wed, 6 Mar 2024 14:41:03 +0100 Subject: [PATCH 5/5] Cleanup, remove duplicate comment --- src/Hook.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Hook.lua b/src/Hook.lua index 68b52c20f..cdb24d6f4 100644 --- a/src/Hook.lua +++ b/src/Hook.lua @@ -88,7 +88,6 @@ function M.register(name, func, append) append = append or false -- if append and validT[name] was set before, append. Otherwise, overwrite. if append and validT[name] then - -- if validT[name] was set before (i.e. isn't false), append validT[name][#validT[name]+1] = func else validT[name] = {func}