lua post hooking a function

↘锁芯ラ 提交于 2019-12-11 16:22:47

问题


I found one topic that's about post hooking, but I don't think it's the same thing as I want to accomplish. topic i found

What I need is the following:

local someBoolean = false
function doSomething() -- this is the function used in __index in a proxy table
  someBoolean = true
  return aFunction -- this is function that we can NOT alter
end

I need to ble able to run the code "someBoolean = false" after the return... (yes, I know that's not supposed to happen :p) considering aFunction may contain other functions itself, I want someBoolean to be true for the entire scope of aFunction, but after that, it HAS to be turned back to false

I'm sorry if I didn't manage to explain it well enough. Copy pasting the relevant code of my actual project would be way too large, and I don't want to waste your time. I've been stuck on this for a while now, and I just can't seem to figure it out...

(edit: I can't just put someBoolean = false after the function, because the function is actually an __index function on a proxy table)

edit: relevent piece of code. I hope it's a bit clear

local function objectProxyDelegate(t, key)
  if not done then  -- done = true when our object is fully initialised
    return cls[key]   -- cls is the class, newinst is the new instance (duh...)
  end
  print("trying to delegate " .. key)
  if accessTable.public[key] then
    print(key .. " passed the test")
    objectScope = true
    if accessTable.static[key] then -- static function. return the static one
      return cls[key]   -- we need to somehow set objectScope back to false after this, otherwise we'll keep overriding protected/private functions
    else
      return newinst[key]
    end
  elseif objectScope then
    print("overridden protected/private")
    return cls[key]
  end
  if accessTable.private[key] then
    error ("This function is not visible. (private)", 2)
  elseif accessTable.protected[key] then
    error ("This function is not visible to an instance. (protected)", 2)
  else
    error ("The function " .. key .. " doesn't exiist in " .. newinst:getType(), 2)                      -- den deze...
  end
end

回答1:


Since you need to return a function (rather than evaluate a function) you can create a proxy for the aFunction and return that instead. Here's how it could work (with a bunch of code taken from the solution by Nicol Bolas):

local someBoolean = false

function doSomething(...) -- this is the function used in __index in a proxy table
  someBoolean = true

  -- Return a proxy function instead of aFunction
  return function(...)
    local rets = { aFunction(...) }
    someBoolean = false
    return table.unpack(rets)
  end
end



回答2:


You cannot execute code after a return statement. The correct answer is to call the function, catch the return values, set the variable, and then return the return values. For example:

local someBoolean = false
function doSomething(...) -- this is the function used in __index in a proxy table
  someBoolean = true
  local rets = { aFunction(...) } -- this is function that we can NOT alter
  someBoolean = false
  return table.unpack(rets)
end


来源:https://stackoverflow.com/questions/12083085/lua-post-hooking-a-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!