Tuned dispatcher behaviour
[project/luci.git] / libs / web / luasrc / dispatcher.lua
index 296c762..8e8e190 100644 (file)
@@ -112,11 +112,14 @@ function httpdispatch(request)
        end
        
        luci.http.close()
+
+       --context._disable_memtrace()
 end
 
 --- Dispatches a LuCI virtual path.
 -- @param request      Virtual path
 function dispatch(request)
+       --context._disable_memtrace = require "luci.debug".trap_memtrace()
        local ctx = context
        ctx.path = request
        
@@ -160,9 +163,10 @@ function dispatch(request)
        -- Init template engine
        if not track.notemplate then
                local tpl = require("luci.template")
-               local viewns = {}
+               local viewns = setmetatable({}, {__index=_G})
                tpl.context.viewns = viewns
                viewns.write       = luci.http.write
+               viewns.include     = function(name) tpl.Template(name):render(getfenv(2)) end
                viewns.translate   = function(...) return require("luci.i18n").translate(...) end
                viewns.striptags   = util.striptags
                viewns.controller  = luci.http.getenv("SCRIPT_NAME")
@@ -171,6 +175,7 @@ function dispatch(request)
                viewns.REQUEST_URI = (luci.http.getenv("SCRIPT_NAME") or "") .. (luci.http.getenv("PATH_INFO") or "")
        end
        
+       track.dependent = (track.dependent ~= false)
        assert(not track.dependent or not track.auto, "Access Violation")
        
        if track.sysauth then
@@ -218,7 +223,15 @@ function dispatch(request)
                context.dispatched = c
                
                util.copcall(function()
-                       util.updfenv(c.target, require(c.module))
+                       local oldenv = getfenv(c.target)
+                       local module = require(c.module)
+                       local env = setmetatable({}, {__index=
+                               
+                       function(tbl, key)
+                               return rawget(tbl, key) or module[key] or oldenv[key] 
+                       end})
+
+                       setfenv(c.target, env)
                end)
                
                c.target(unpack(args))
@@ -315,12 +328,7 @@ function createtree()
        -- Load default translation
        require "luci.i18n".loadc("default")
        
-       local scope = setmetatable({}, {__index = _G})
-       for k,v in pairs(luci.dispatcher) do
-               if type(v) == "function" then
-                       scope[k] = v
-               end
-       end
+       local scope = setmetatable({}, {__index = luci.dispatcher})
 
        for k, v in pairs(index) do
                scope._NAME = k
@@ -344,17 +352,8 @@ function assign(path, clone, title, order)
        
        obj.title = title
        obj.order = order
-       
-       local c = context.tree
-       for k, v in ipairs(clone) do
-               if not c.nodes[v] then
-                       c.nodes[v] = {nodes={}}
-               end
 
-               c = c.nodes[v]
-       end
-       
-       setmetatable(obj, {__index = c})
+       setmetatable(obj, {__index = _create_node(clone)})
        
        return obj
 end
@@ -380,7 +379,7 @@ end
 -- @param      ...             Virtual path
 -- @return                     Dispatching tree node
 function node(...)
-       local c = _create_node(arg)
+       local c = _create_node({...})
 
        c.module = getfenv(2)._NAME
        c.path = arg