live.lua 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. local live = {}
  2. function live.require(libName)
  3. local filename = libName .. ".lua"
  4. local lib = require(libName)
  5. local liveLib = {}
  6. for name, fn in pairs(lib) do
  7. if type(fn) == "function" then
  8. -- the actual function that does the work
  9. liveLib["__" .. name] = fn
  10. -- a proxy function that updates the code before letting the real function continue
  11. liveLib[name] = function(...)
  12. -- copy the old upvals
  13. local nups = debug.getinfo(liveLib["__" .. name]).nups
  14. local upvals = {}
  15. for i=1,nups,1 do
  16. local _, uv = debug.getupvalue(liveLib["__" .. name], i)
  17. -- we special case out function upvals because we want to update them with the latest from the file too
  18. if type(uv) == "function" then
  19. upvals[i] = nil
  20. else
  21. upvals[i] = uv
  22. end
  23. end
  24. -- update the function
  25. local newLib = loadfile(filename)()
  26. liveLib["__" .. name] = newLib[name]
  27. -- and give it the old upvals
  28. for i=1,nups,1 do
  29. if upvals[i] then
  30. debug.setupvalue(liveLib["__" .. name], i, upvals[i])
  31. end
  32. end
  33. -- and finally just call our updated function
  34. return liveLib["__" .. name](...)
  35. end
  36. else
  37. -- incase someone has some goofy stuff in thier module or is doing OOP
  38. liveLib[name] = fn
  39. end
  40. end
  41. return liveLib
  42. end
  43. return live