mirror of
https://github.com/rakshasa/rtorrent.git
synced 2026-10-04 21:29:21 +00:00
feat(lua): Extend insert_lua_method's func_name's arguments
Allows short form with number of arguments and long flexible form with table:
```lua
rtorrent.insert_lua_method('d.watch_handler', 'watch_handler', { '$argument.0=' })
rtorrent.insert_lua_method('d.watch_handler', 'watch_handler', { 0 })
rtorrent.insert_lua_method('d.watch_handler', 'watch_handler', { [1] = 0 })
rtorrent.insert_lua_method('d.watch_handler', 'watch_handler', 1)
rtorrent.insert_lua_method('d.watch_handler', 'watch_handler', 0)
```
Also, there are way to pass no arguments at all:
```lua
rtorrent.insert_lua_method('d.watch_handler', 'watch_handler')
```
Type checking added.
This commit is contained in:
+30
-6
@@ -2,6 +2,7 @@
|
|||||||
-- return it for loading.
|
-- return it for loading.
|
||||||
local args = {...}
|
local args = {...}
|
||||||
local rtorrent = args[1]
|
local rtorrent = args[1]
|
||||||
|
local math = require('math')
|
||||||
|
|
||||||
-- Autocall
|
-- Autocall
|
||||||
-- Passes an empty first argument implicitly, as "".
|
-- Passes an empty first argument implicitly, as "".
|
||||||
@@ -62,13 +63,36 @@ end
|
|||||||
-- Allow insert global lua-finction `func_name` at rtorrent slot `name`
|
-- Allow insert global lua-finction `func_name` at rtorrent slot `name`
|
||||||
-- @param name string rtorrent's slot
|
-- @param name string rtorrent's slot
|
||||||
-- @param func_name string name of global lua-function
|
-- @param func_name string name of global lua-function
|
||||||
-- @param ... string list of rtorrent's arguments like `(d.name)` to be
|
-- @param ... list of rtorrent's arguments like `(d.name)` to be
|
||||||
-- passed to lua-function when it be called by rtorrent
|
-- passed to lua-function when it be called by rtorrent
|
||||||
-- NOTE: first one is always target, may be empty string
|
-- 1) non-negative integer - number of first arguments
|
||||||
rtorrent["insert_lua_method"] = function (name, func_name, ...)
|
-- 2) table (array) of strings - concrete rtorrent strings,
|
||||||
|
-- passed in specified order
|
||||||
|
-- 3) table (array) of non-negative integer - indexes, converted
|
||||||
|
-- to $argument.0, $argument.1 ..., passed in specified order
|
||||||
|
-- NOTE: first argement of func_name is always target,
|
||||||
|
-- may be empty string
|
||||||
|
rtorrent["insert_lua_method"] = function (name, func_name, method_args)
|
||||||
local args = {}
|
local args = {}
|
||||||
for i, v in ipairs({...}) do
|
|
||||||
args[i] = v or ("$argument." .. i-1 .. "=")
|
if method_args == nil then method_args = 0 end
|
||||||
|
|
||||||
|
if math.type(method_args) == 'integer' and method_args >= 0 then
|
||||||
|
for i = 1, method_args do
|
||||||
|
args[i] = "$argument." .. i-1 .. "="
|
||||||
|
end
|
||||||
|
elseif type(method_args) == 'table' then
|
||||||
|
for i, v in ipairs(method_args) do
|
||||||
|
if type(v) == 'string' then
|
||||||
|
args[i] = v
|
||||||
|
elseif math.type(v) == 'integer' and v >= 0 then
|
||||||
|
args[i] = "$argument." .. v .. "="
|
||||||
|
else
|
||||||
|
error("Incorrect arguments")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
error("Incorrect arguments")
|
||||||
end
|
end
|
||||||
|
|
||||||
local arg_str = table.concat(args, ",")
|
local arg_str = table.concat(args, ",")
|
||||||
|
|||||||
Reference in New Issue
Block a user