Home > front end >  Lua rewrite function
Lua rewrite function

Time:05-28

Hey is it possible to rewrite a function (add code inside it) I really want to execute my code in the function that the user is executing. Without an other argument

Like this:

function exampel(fnct)
     fnct( function()  
        print('code thad i want to add') 
     end)
end

exampel( function() -- function user is executing
    print('ok')
end)

CodePudding user response:

You can run code before and after the function runs:

function wrap(f,b,a)
    return function (...) b(...) f(...) a(...) end
end

function before()
    print"before"
end

function after()
    print"after"
end

function user()
    print"user code"
end

wrap(user,before,after)()

CodePudding user response:

here is a global function load(lua5.4),you can use it to dynamic execute a lua code.

local str = [[
    local a = 3
    local b = 4
    return a b
]]

print(load(str)())--7
  • Related