split @load apart, add @run

This commit is contained in:
leaf corcoran
2014-02-01 11:59:22 -08:00
parent 8b67327708
commit f2a819b7f4
2 changed files with 41 additions and 27 deletions

View File

@@ -169,7 +169,14 @@ do
if not (success) then if not (success) then
return nil, err return nil, err
end end
return self:load(self:chunks_to_lua()) local fn
fn, err = self:load(self:chunks_to_lua())
if not (fn) then
return nil, err
end
return function(...)
return self:run(fn, ...)
end
end, end,
parse = function(self, str) parse = function(self, str)
self.str = str self.str = str
@@ -230,7 +237,9 @@ do
end end
return nil, err return nil, err
end end
return function(env, buffer) return fn
end,
run = function(self, fn, env, buffer)
if env == nil then if env == nil then
env = { } env = { }
end end
@@ -248,7 +257,6 @@ do
}) })
setfenv(fn, combined_env) setfenv(fn, combined_env)
return fn(buffer, #buffer, tostring, concat, html_escape) return fn(buffer, #buffer, tostring, concat, html_escape)
end
end, end,
chunks_to_lua = function(self) chunks_to_lua = function(self)
local buffer = { local buffer = {

View File

@@ -132,7 +132,9 @@ class Parser
compile: (str) => compile: (str) =>
success, err = @parse str success, err = @parse str
return nil, err unless success return nil, err unless success
@load @chunks_to_lua! fn, err = @load @chunks_to_lua!
return nil, err unless fn
(...) -> @run fn, ...
parse: (@str) => parse: (@str) =>
assert type(@str) == "string", "expecting string for parse" assert type(@str) == "string", "expecting string for parse"
@@ -163,6 +165,7 @@ class Parser
source_line = get_line @str, source_line_no source_line = get_line @str, source_line_no
"#{err_msg} [#{source_line_no}]: #{source_line}" "#{err_msg} [#{source_line_no}]: #{source_line}"
-- converts lua string into template function
load: (code, name="etlua") => load: (code, name="etlua") =>
code_fn = do code_fn = do
code_ref = code code_ref = code
@@ -179,7 +182,10 @@ class Parser
return nil, err return nil, err
(env={}, buffer={}) -> fn
-- takes a function from @load and executes it with correct parameters
run: (fn, env={}, buffer={}) =>
combined_env = setmetatable {}, __index: (name) => combined_env = setmetatable {}, __index: (name) =>
val = env[name] val = env[name]
val = _G[name] if val == nil val = _G[name] if val == nil