Lua: Dynamic function call with arguments

Using Lua, I am trying to dynamically call a function with parameters. I want to send a string that will be parsed in such a way that:

  • 1st argument is an instance of the Handle class
  • 2nd is a function to be called
  • All that remains is arguments

"modules" is a table of type { string=<instance of a class> }
split () - a simple parser that returns a table with indexed rows.

function Dynamic(msg)
    local args = split(msg, " ")
    module = args[1]
    table.remove(args, 1)
    if module then
        module = modules[module]
        command = args[1]
        table.remove(args, 1)
        if command then
            if not args then
                module[command]()
            else
                module[command](unpack(args))  -- Reference 1
            end
        else
            -- Function doesnt exist
        end
    else
        -- Module doesnt exist
    end
end

When I try to do this using the "ignore remove bob" link "1", it tries to call "remove" in the instance associated with the "ignore" in the modules and gives the argument "bob" contained in the table (with one value )

, , remove . "Reference 1"

module[command]("bob")

.

, "bob":

function TIF_Ignore:remove(name)
    print(name)  -- Reference 2
    TIF_Ignore:rawremove(name)
    TIF_Ignore:rawremovetmp(name)
    print(title.. name.. " is not being ignored.")
end

"Reference 2" , , . " remove bob", "unpack (args)" "bob" "Reference 1", "name" "remove" - .

+3
2

function TIF_Ignore:remove(name) function TIF_Ignore.remove(self, name). , . , "bob" self name.

, remove " " : function TIF_Ignore.remove(name). rawremove rawremovetmp , , . ( ) , module args, .

+3

, :, , , . , , self, ., , :

function Dynamic(msg)
    local args   = split(msg, " ")
    local module = table.remove(args, 1)
    if module and modules[module] then
        module = modules[module]
        local command = table.remove(args, 1)
        if command then
            local command = module[command]
            command(module, unpack(args))
        else
            -- Function doesnt exist
        end
    else
        -- Module doesnt exist
    end
end

:

  • local.
  • args .
  • modules[module] .
  • table.remove , , .
+3

Source: https://habr.com/ru/post/1736859/


All Articles