summaryrefslogtreecommitdiffhomepage
path: root/modules/luci-base-ucode/luasrc/ucodebridge/luci/ucodebridge.lua
diff options
context:
space:
mode:
Diffstat (limited to 'modules/luci-base-ucode/luasrc/ucodebridge/luci/ucodebridge.lua')
-rw-r--r--modules/luci-base-ucode/luasrc/ucodebridge/luci/ucodebridge.lua52
1 files changed, 52 insertions, 0 deletions
diff --git a/modules/luci-base-ucode/luasrc/ucodebridge/luci/ucodebridge.lua b/modules/luci-base-ucode/luasrc/ucodebridge/luci/ucodebridge.lua
new file mode 100644
index 0000000000..fa4943dc99
--- /dev/null
+++ b/modules/luci-base-ucode/luasrc/ucodebridge/luci/ucodebridge.lua
@@ -0,0 +1,52 @@
+-- Copyright 2022 Jo-Philipp Wich <jo@mein.io>
+-- Licensed to the public under the Apache License 2.0.
+
+local coroutine, assert, error, type, require = coroutine, assert, error, type, require
+local tmpl = require "luci.template"
+local util = require "luci.util"
+local http = require "luci.http"
+
+
+--- LuCI ucode bridge library.
+module "luci.ucodebridge"
+
+local function run(fn, ...)
+ local co = coroutine.create(fn)
+ local ok, ret
+
+ while coroutine.status(co) ~= "dead" do
+ ok, ret = coroutine.resume(co, ...)
+
+ if not ok then
+ error(ret)
+ end
+ end
+
+ return ret
+end
+
+function compile(path)
+ run(function(path)
+ return tmpl.Template(path)
+ end, path)
+end
+
+function render(path, scope)
+ run(tmpl.render, path, scope)
+end
+
+function call(modname, method, ...)
+ return run(function(module, method, ...)
+ local mod = require(modname)
+ local func = mod[method]
+
+ assert(func ~= nil,
+ 'Cannot resolve function "' .. method .. '". Is it misspelled or local?')
+
+ assert(type(func) == "function",
+ 'The symbol "' .. method .. '" does not refer to a function but data ' ..
+ 'of type "' .. type(func) .. '".')
+
+ return func(...)
+ end, modname, method, ...)
+end