From 37b5ef40df48bb7d4023e66f09a68c1c0e229b8d Mon Sep 17 00:00:00 2001 From: Steven Barth Date: Fri, 5 Sep 2008 18:35:03 +0000 Subject: [PATCH] Heavy memory/performance optimizations #3 --- libs/web/luasrc/template.lua | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/libs/web/luasrc/template.lua b/libs/web/luasrc/template.lua index 91977798af..4aa9b0933a 100644 --- a/libs/web/luasrc/template.lua +++ b/libs/web/luasrc/template.lua @@ -34,7 +34,7 @@ local coroutine = require "coroutine" local tostring, pairs, loadstring = tostring, pairs, loadstring local setmetatable, loadfile = setmetatable, loadfile -local getfenv, setfenv = getfenv, setfenv +local getfenv, setfenv, rawget = getfenv, setfenv, rawget local assert, type, error = assert, type, error --- LuCI template library. @@ -55,10 +55,6 @@ compiler_mode = config.template.compiler_mode or "memory" -- Define the namespace for template modules context = util.threadlocal() -viewns = { - include = function(name) Template(name):render(getfenv(2)) end, -} - --- Manually compile a given template into an executable Lua function -- @param template LuCI template -- @return Lua template function @@ -160,13 +156,7 @@ function Template.__init__(self, name) self.name = name -- Create a new namespace for this template - self.viewns = {sink=self.sink} - - -- Copy over from general namespace - util.update(self.viewns, viewns) - if context.viewns then - util.update(self.viewns, context.viewns) - end + self.viewns = context.viewns -- If we have a cached template, skip compiling and loading if self.template then @@ -235,21 +225,15 @@ end function Template.render(self, scope) scope = scope or getfenv(2) - -- Save old environment - local oldfenv = getfenv(self.template) - -- Put our predefined objects in the scope of the template - util.resfenv(self.template) - util.updfenv(self.template, scope) - util.updfenv(self.template, self.viewns) + setfenv(self.template, setmetatable({}, {__index = + function(tbl, key) + return rawget(tbl, key) or self.viewns[key] or scope[key] + end})) -- Now finally render the thing local stat, err = util.copcall(self.template) if not stat then - setfenv(self.template, oldfenv) error("Error in template %s: %s" % {self.name, err}) end - - -- Reset environment - setfenv(self.template, oldfenv) end -- 2.30.2