2
0

ucodebridge.lua 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. -- Copyright 2022 Jo-Philipp Wich <jo@mein.io>
  2. -- Licensed to the public under the Apache License 2.0.
  3. local coroutine, assert, error, type, require = coroutine, assert, error, type, require
  4. local tmpl = require "luci.template"
  5. local util = require "luci.util"
  6. local http = require "luci.http"
  7. local sys = require "luci.sys"
  8. local ltn12 = require "luci.ltn12"
  9. --- LuCI ucode bridge library.
  10. module "luci.ucodebridge"
  11. local function run(fn, ...)
  12. local co = coroutine.create(fn)
  13. local ok, ret
  14. while coroutine.status(co) ~= "dead" do
  15. ok, ret = coroutine.resume(co, ...)
  16. if not ok then
  17. error(ret)
  18. end
  19. end
  20. return ret
  21. end
  22. function compile(path)
  23. run(function(path)
  24. return tmpl.Template(path)
  25. end, path)
  26. end
  27. function render(path, scope)
  28. run(tmpl.render, path, scope)
  29. end
  30. function call(modname, method, ...)
  31. return run(function(module, method, ...)
  32. local mod = require(modname)
  33. local func = mod[method]
  34. assert(func ~= nil,
  35. 'Cannot resolve function "' .. method .. '". Is it misspelled or local?')
  36. assert(type(func) == "function",
  37. 'The symbol "' .. method .. '" does not refer to a function but data ' ..
  38. 'of type "' .. type(func) .. '".')
  39. return func(...)
  40. end, modname, method, ...)
  41. end