Giter Site home page Giter Site logo

hook.lua's Introduction

hook.lua

Module for hooking and intercepting function calls.

Hooks can be added to any function. If multiple hooks are added to a single function, then each hook is executed in the order they have been added. If any hook returns anything other than nil, the execution of further hooks and the actual (intercepted) function is stopped. The returned values of the hook is returned to the original caller of the function.

Developed and tested using Lua 5.1 and Lua 5.2.

API References

The following references are based on hook.lua being included using local hook = require("hook").

hook.add(func, hook)

Adds a hook to a function, the hook is a function which will be called prior to the actual (intercepted) function.

If a function has multiple hooks added, each hook will be executed in the order they have been added.

If any hook returns anything other than nil, the execution of further hooks and the actual (intercepted) function is stopped. The returned values of the hook is returned to the original caller of the function.

hook.call(func, ...)

Executes each hooked function, until a hook returns anything other than nil. Then the returned values of the hook is returned to the original caller of hook.call().

The varargs would be the hooked function's parameters. As when calling the hooked function, the arguments is what will be passed through the varargs.

hook.remove(func, hook)

Remove a hook from a function.

hook.clear(func)

Removes all hooks from a function.

hook.count(func)

Counts the amount of hooks a function has (Note that the same hook can be added multiple times).

hook.gethooks(func)

Returns an array (indexed table), containing all the hooks.

Example

local hook = require("hook")


local function test(msg)
	print("test(\"" .. tostring(msg) .. "\")")
end


local function testHook(msg)
	print("testHook(\"" .. tostring(msg) .. "\")")
	
	if msg == "stop" then
		return true
	end
end

local function testHook2(msg)
	print("testHook2(\"" .. tostring(msg) .. "\")")
end


test = hook.add(test, testHook)
test = hook.add(test, testHook2)


print(hook.count(test))
-- Prints:
-- 2


for i, hook in ipairs(hook.gethooks(test)) do
	print("Hook #" .. i .. ": " .. tostring(hook))
end
-- Prints:
-- Hook #1: function: ........
-- Hook #2: function: ........


test("Hello World")
-- Prints:
-- testHook("Hello World")
-- testHook2("Hello World")
-- test("Hello World")


test("stop")
-- Prints:
-- testHook("stop")


hook.call(test, "Hooks Only")
-- Prints:
-- testHook("Hooks Only")
-- testHook2("Hooks Only")


-- Best for performance:
test = hook.remove(test, testHook)

-- Also works:
hook.remove(test, testHook)


test("Hello Again")
-- Prints:
-- testHook2("Hello Again")
-- test("Hello Again")


-- Best for performance:
test = hook.clear(test)

-- Also works:
hook.clear(test)

License

This module is shared under the MIT license, and is therefore free to use, share, distribute and modify. See LICENSE for more details.

hook.lua's People

Contributors

vallentin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.