From 544fe257004ac35ae4c57dab77f8b9f18a73db2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stanis=C5=82aw=20Barzowski?= Date: Tue, 26 Sep 2017 17:55:14 -0400 Subject: [PATCH] Simplistic argument checking To be expanded when optional arguments arrive. --- testdata/bad_function_call.golden | 1 + testdata/bad_function_call.jsonnet | 1 + testdata/bad_function_call2.golden | 1 + testdata/bad_function_call2.jsonnet | 1 + testdata/bad_function_call_and_error.golden | 1 + testdata/bad_function_call_and_error.jsonnet | 1 + thunks.go | 5 ++++- value.go | 10 ++++++++++ 8 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 testdata/bad_function_call.golden create mode 100644 testdata/bad_function_call.jsonnet create mode 100644 testdata/bad_function_call2.golden create mode 100644 testdata/bad_function_call2.jsonnet create mode 100644 testdata/bad_function_call_and_error.golden create mode 100644 testdata/bad_function_call_and_error.jsonnet diff --git a/testdata/bad_function_call.golden b/testdata/bad_function_call.golden new file mode 100644 index 0000000..7ec1af3 --- /dev/null +++ b/testdata/bad_function_call.golden @@ -0,0 +1 @@ +RUNTIME ERROR: function expected 1 argument(s), but got 0 diff --git a/testdata/bad_function_call.jsonnet b/testdata/bad_function_call.jsonnet new file mode 100644 index 0000000..e56145a --- /dev/null +++ b/testdata/bad_function_call.jsonnet @@ -0,0 +1 @@ +(function(x) x)() diff --git a/testdata/bad_function_call2.golden b/testdata/bad_function_call2.golden new file mode 100644 index 0000000..83f8a45 --- /dev/null +++ b/testdata/bad_function_call2.golden @@ -0,0 +1 @@ +RUNTIME ERROR: function expected 1 argument(s), but got 2 diff --git a/testdata/bad_function_call2.jsonnet b/testdata/bad_function_call2.jsonnet new file mode 100644 index 0000000..73668d7 --- /dev/null +++ b/testdata/bad_function_call2.jsonnet @@ -0,0 +1 @@ +(function(x) x)(1, 2) diff --git a/testdata/bad_function_call_and_error.golden b/testdata/bad_function_call_and_error.golden new file mode 100644 index 0000000..83f8a45 --- /dev/null +++ b/testdata/bad_function_call_and_error.golden @@ -0,0 +1 @@ +RUNTIME ERROR: function expected 1 argument(s), but got 2 diff --git a/testdata/bad_function_call_and_error.jsonnet b/testdata/bad_function_call_and_error.jsonnet new file mode 100644 index 0000000..1605d30 --- /dev/null +++ b/testdata/bad_function_call_and_error.jsonnet @@ -0,0 +1 @@ +(function(x) x)(error "x", error "y") diff --git a/thunks.go b/thunks.go index 759f4e6..b62b183 100644 --- a/thunks.go +++ b/thunks.go @@ -80,7 +80,10 @@ func makeCallThunk(ec evalCallable, args callArguments) potentialValue { func (th *callThunk) getValue(i *interpreter, trace *TraceElement) (value, error) { evaluator := makeEvaluator(i, trace) - // TODO(sbarzowski): actually this trace is kinda useless inside... + err := checkArguments(evaluator, th.args, th.function.Parameters()) + if err != nil { + return nil, err + } return th.function.EvalCall(th.args, evaluator) } diff --git a/value.go b/value.go index f514f49..2a2a7ec 100644 --- a/value.go +++ b/value.go @@ -244,6 +244,16 @@ func (f *valueFunction) parameters() ast.Identifiers { return f.ec.Parameters() } +func checkArguments(e *evaluator, args callArguments, params ast.Identifiers) error { + // TODO(sbarzowski) this will get much more complicated with named params + numPassed := len(args.positional) + numExpected := len(params) + if numPassed != numExpected { + return e.Error(fmt.Sprintf("function expected %v argument(s), but got %v", numExpected, numPassed)) + } + return nil +} + func (f *valueFunction) typename() string { return "function" }