Heavy memory/performance optimizations #3
This commit is contained in:
parent
45319b232b
commit
37b5ef40df
1 changed files with 6 additions and 22 deletions
|
@ -34,7 +34,7 @@ local coroutine = require "coroutine"
|
||||||
|
|
||||||
local tostring, pairs, loadstring = tostring, pairs, loadstring
|
local tostring, pairs, loadstring = tostring, pairs, loadstring
|
||||||
local setmetatable, loadfile = setmetatable, loadfile
|
local setmetatable, loadfile = setmetatable, loadfile
|
||||||
local getfenv, setfenv = getfenv, setfenv
|
local getfenv, setfenv, rawget = getfenv, setfenv, rawget
|
||||||
local assert, type, error = assert, type, error
|
local assert, type, error = assert, type, error
|
||||||
|
|
||||||
--- LuCI template library.
|
--- LuCI template library.
|
||||||
|
@ -55,10 +55,6 @@ compiler_mode = config.template.compiler_mode or "memory"
|
||||||
-- Define the namespace for template modules
|
-- Define the namespace for template modules
|
||||||
context = util.threadlocal()
|
context = util.threadlocal()
|
||||||
|
|
||||||
viewns = {
|
|
||||||
include = function(name) Template(name):render(getfenv(2)) end,
|
|
||||||
}
|
|
||||||
|
|
||||||
--- Manually compile a given template into an executable Lua function
|
--- Manually compile a given template into an executable Lua function
|
||||||
-- @param template LuCI template
|
-- @param template LuCI template
|
||||||
-- @return Lua template function
|
-- @return Lua template function
|
||||||
|
@ -160,13 +156,7 @@ function Template.__init__(self, name)
|
||||||
self.name = name
|
self.name = name
|
||||||
|
|
||||||
-- Create a new namespace for this template
|
-- Create a new namespace for this template
|
||||||
self.viewns = {sink=self.sink}
|
self.viewns = context.viewns
|
||||||
|
|
||||||
-- Copy over from general namespace
|
|
||||||
util.update(self.viewns, viewns)
|
|
||||||
if context.viewns then
|
|
||||||
util.update(self.viewns, context.viewns)
|
|
||||||
end
|
|
||||||
|
|
||||||
-- If we have a cached template, skip compiling and loading
|
-- If we have a cached template, skip compiling and loading
|
||||||
if self.template then
|
if self.template then
|
||||||
|
@ -235,21 +225,15 @@ end
|
||||||
function Template.render(self, scope)
|
function Template.render(self, scope)
|
||||||
scope = scope or getfenv(2)
|
scope = scope or getfenv(2)
|
||||||
|
|
||||||
-- Save old environment
|
|
||||||
local oldfenv = getfenv(self.template)
|
|
||||||
|
|
||||||
-- Put our predefined objects in the scope of the template
|
-- Put our predefined objects in the scope of the template
|
||||||
util.resfenv(self.template)
|
setfenv(self.template, setmetatable({}, {__index =
|
||||||
util.updfenv(self.template, scope)
|
function(tbl, key)
|
||||||
util.updfenv(self.template, self.viewns)
|
return rawget(tbl, key) or self.viewns[key] or scope[key]
|
||||||
|
end}))
|
||||||
|
|
||||||
-- Now finally render the thing
|
-- Now finally render the thing
|
||||||
local stat, err = util.copcall(self.template)
|
local stat, err = util.copcall(self.template)
|
||||||
if not stat then
|
if not stat then
|
||||||
setfenv(self.template, oldfenv)
|
|
||||||
error("Error in template %s: %s" % {self.name, err})
|
error("Error in template %s: %s" % {self.name, err})
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Reset environment
|
|
||||||
setfenv(self.template, oldfenv)
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue