summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Maw <richard.maw@codethink.co.uk>2015-11-26 17:02:19 +0000
committerRichard Maw <richard.maw@codethink.co.uk>2015-11-27 17:46:53 +0000
commit58a3969d885519c426a7c2aebe7f964e93c31cb8 (patch)
treeed46b767ce61b8173cf023850be90fd477120e4e
parent8474c27d9d3db37616bfd1c95158d03f9efcd2af (diff)
downloadlace-58a3969d885519c426a7c2aebe7f964e93c31cb8.tar.gz
lace.builtin: Pass the rule into builtin functions
This added the rule parameter after the exec_context parameter, by extending the arguments list, rather than changing the engine to pass the rule back into its callback, so that user-defined rules aren't broken by getting an unexpected parameter. Unfortunately this required renumbering all the unit tests' indexes.
-rw-r--r--lib/lace/builtin.lua34
-rw-r--r--test/test-lace.builtin.lua40
-rw-r--r--test/test-lace.compiler.lua18
3 files changed, 50 insertions, 42 deletions
diff --git a/lib/lace/builtin.lua b/lib/lace/builtin.lua
index 8ff2d75..cd16bbf 100644
--- a/lib/lace/builtin.lua
+++ b/lib/lace/builtin.lua
@@ -97,7 +97,7 @@ local function get_set_last_result(newv)
return ret
end
-local function _do_return(exec_context, result, reason, cond)
+local function _do_return(exec_context, rule, result, reason, cond)
local pass, msg = run_conditions(exec_context, cond)
if pass == nil then
-- Pass errors
@@ -139,10 +139,11 @@ local function _return(compcontext, result, reason, ...)
end
last_result = result
- return {
+ local rule = {
fn = _do_return,
- args = { result, reason, cond }
}
+ rule.args = { rule, result, reason, cond }
+ return rule
end
builtin.allow = _return
@@ -204,8 +205,8 @@ end
--[ Control types ]--------------------------------------------------
-local function _do_any_all_of(...)
- local pass, msg = run_conditions(...)
+local function _do_any_all_of(exec_context, rule, cond, anyof)
+ local pass, msg = run_conditions(exec_context, cond, anyof)
if pass == nil then
-- Offset error location by anyof/allof word
err.offset(msg, 1)
@@ -222,10 +223,11 @@ local function _compile_any_all_of(compcontext, mtype, first, second, ...)
return err.error("Expected at least two names, only got one", {1, 2})
end
- return {
+ local rule = {
fn = _do_any_all_of,
- args = { { first, second, ...}, mtype == "anyof" }
}
+ rule.args = { rule, { first, second, ...}, mtype == "anyof" }
+ return rule
end
local builtin_control_fn = {
@@ -244,6 +246,10 @@ local function _controlfn(ctx, name)
return cfn
end
+local function _do_define(exec_context, rule, name, defn)
+ return engine.define(exec_context, name, defn)
+end
+
--- Compile a definition command
--
-- Definitions are a core behaviour of Lace. This builtin allows the ruleset
@@ -289,17 +295,18 @@ function builtin.define(compcontext, define, name, controltype, ...)
end
-- Successfully created a control table, return a rule for it
- return {
- fn = engine.define,
- args = { name, ctrltab }
+ local rule = {
+ fn = _do_define,
}
+ rule.args = { rule, name, ctrltab }
+ return rule
end
builtin.def = builtin.define
--[ Inclusion of rulesets ]-------------------------------------------
-local function _do_include(exec_context, ruleset, conds)
+local function _do_include(exec_context, rule, ruleset, conds)
local pass, msg = run_conditions(exec_context, conds)
if pass == nil then
-- Propagate errors
@@ -374,10 +381,11 @@ function builtin.include(comp_context, cmd, file, ...)
end
-- Okay, we parsed, so build the runtime
- return {
+ local rule = {
fn = _do_include,
- args = { ruleset, conds }
}
+ rule.args = { rule, ruleset, conds }
+ return rule
end
return {
diff --git a/test/test-lace.builtin.lua b/test/test-lace.builtin.lua
index 927acb1..84499a3 100644
--- a/test/test-lace.builtin.lua
+++ b/test/test-lace.builtin.lua
@@ -55,10 +55,10 @@ function suite.compile_builtin_allow_deny_novariables()
assert(type(cmdtab) == "table", "Result should be a table")
assert(type(cmdtab.fn) == "function", "Result should contain a function")
assert(type(cmdtab.args) == "table", "Result table should contain an args table")
- assert(cmdtab.args[1] == "allow", "Result args table should contain the given result 'allow'")
- assert(cmdtab.args[2] == "because", "Result args table should contain te given reason 'because'")
- assert(type(cmdtab.args[3]) == "table", "The third argument should be a table")
- assert(#cmdtab.args[3] == 0, "There should be no conditions")
+ assert(cmdtab.args[2] == "allow", "Result args table should contain the given result 'allow'")
+ assert(cmdtab.args[3] == "because", "Result args table should contain te given reason 'because'")
+ assert(type(cmdtab.args[4]) == "table", "The third argument should be a table")
+ assert(#cmdtab.args[4] == 0, "There should be no conditions")
end
function suite.run_builtin_allow_deny_novariables()
@@ -536,17 +536,17 @@ function suite.compile_anyof_two_args()
assert(type(cmdtab) == "table", "Successful compilations should return tables")
assert(type(cmdtab.fn) == "function", "With functions")
assert(type(cmdtab.args) == "table", "And arguments")
- assert(#cmdtab.args == 2, "There should be two args")
- assert(type(cmdtab.args[1]) == "string", "The first should be a table")
- assert(type(cmdtab.args[2]) == "table", "The second should be a bool")
- local ctrltab = cmdtab.args[2]
+ assert(#cmdtab.args == 3, "There should be two args")
+ assert(type(cmdtab.args[2]) == "string", "The first should be a table")
+ assert(type(cmdtab.args[3]) == "table", "The second should be a bool")
+ local ctrltab = cmdtab.args[3]
assert(type(ctrltab) == "table", "Successfully compiled control functions should return tables")
assert(type(ctrltab.fn) == "function", "With functions")
assert(type(ctrltab.args) == "table", "And arguments")
- assert(#ctrltab.args == 2, "There should be two args")
- assert(type(ctrltab.args[1]) == "table", "The first should be a table")
- assert(type(ctrltab.args[2]) == "boolean", "The second should be a bool")
- assert(ctrltab.args[2] == true, "The anyof indicator should be true")
+ assert(#ctrltab.args == 3, "There should be two args")
+ assert(type(ctrltab.args[2]) == "table", "The first should be a table")
+ assert(type(ctrltab.args[3]) == "boolean", "The second should be a bool")
+ assert(ctrltab.args[3] == true, "The anyof indicator should be true")
end
function suite.compile_allof_two_args()
@@ -555,17 +555,17 @@ function suite.compile_allof_two_args()
assert(type(cmdtab) == "table", "Successful compilations should return tables")
assert(type(cmdtab.fn) == "function", "With functions")
assert(type(cmdtab.args) == "table", "And arguments")
- assert(#cmdtab.args == 2, "There should be two args")
- assert(type(cmdtab.args[1]) == "string", "The first should be a table")
- assert(type(cmdtab.args[2]) == "table", "The second should be a bool")
- local ctrltab = cmdtab.args[2]
+ assert(#cmdtab.args == 3, "There should be two args")
+ assert(type(cmdtab.args[2]) == "string", "The first should be a table")
+ assert(type(cmdtab.args[3]) == "table", "The second should be a bool")
+ local ctrltab = cmdtab.args[3]
assert(type(ctrltab) == "table", "Successfully compiled control functions should return tables")
assert(type(ctrltab.fn) == "function", "With functions")
assert(type(ctrltab.args) == "table", "And arguments")
- assert(#ctrltab.args == 2, "There should be two args")
- assert(type(ctrltab.args[1]) == "table", "The first should be a table")
- assert(type(ctrltab.args[2]) == "boolean", "The second should be a bool")
- assert(ctrltab.args[2] == false, "The anyof indicator should be false")
+ assert(#ctrltab.args == 3, "There should be two args")
+ assert(type(ctrltab.args[2]) == "table", "The first should be a table")
+ assert(type(ctrltab.args[3]) == "boolean", "The second should be a bool")
+ assert(ctrltab.args[3] == false, "The anyof indicator should be false")
end
function suite.run_anyof_two_args()
diff --git a/test/test-lace.compiler.lua b/test/test-lace.compiler.lua
index 5c2f5c2..9eb7e99 100644
--- a/test/test-lace.compiler.lua
+++ b/test/test-lace.compiler.lua
@@ -83,9 +83,9 @@ function suite.no_unconditional_action()
-- rule 2 should be an unconditional allow with 'Default behaviour' as the reason,
-- let's check
local r2a = result.rules[2].args
- assert(r2a[1] == "allow", "Rule 2 should be an allow")
- assert(r2a[2] == "Default behaviour", "Rule 2's reason should be 'Default behaviour'")
- assert(#r2a[3] == 0, "Rule 2 should have no conditions")
+ assert(r2a[2] == "allow", "Rule 2 should be an allow")
+ assert(r2a[3] == "Default behaviour", "Rule 2's reason should be 'Default behaviour'")
+ assert(#r2a[4] == 0, "Rule 2 should have no conditions")
end
function suite.no_unconditional_action_default_deny()
@@ -99,9 +99,9 @@ function suite.no_unconditional_action_default_deny()
-- rule 3 should be an unconditional deny with 'Default behaviour' as the reason,
-- let's check
local r3a = result.rules[3].args
- assert(r3a[1] == "deny", "Rule 3 should be a deny, despite last rule behind a deny")
- assert(r3a[2] == "Default behaviour", "Rule 3's reason should be 'Default behaviour'")
- assert(#r3a[3] == 0, "Rule 2 should have no conditions")
+ assert(r3a[2] == "deny", "Rule 3 should be a deny, despite last rule behind a deny")
+ assert(r3a[3] == "Default behaviour", "Rule 3's reason should be 'Default behaviour'")
+ assert(#r3a[4] == 0, "Rule 3 should have no conditions")
end
function suite.is_unconditional_action_default_deny()
@@ -115,9 +115,9 @@ function suite.is_unconditional_action_default_deny()
-- rule 2 should be an unconditional allow with 'stuff' as the reason
-- let's check
local r2a = result.rules[2].args
- assert(r2a[1] == "allow", "Rule 2 should be an allow, despite default being deny")
- assert(r2a[2] == "stuff", "Rule 2's reason should be 'stuff'")
- assert(#r2a[3] == 0, "Rule 2 should have no conditions")
+ assert(r2a[2] == "allow", "Rule 2 should be an allow, despite default being deny")
+ assert(r2a[3] == "stuff", "Rule 2's reason should be 'stuff'")
+ assert(#r2a[4] == 0, "Rule 2 should have no conditions")
end
-- Now we set up a more useful context and use that going forward: