Skip to content

Commit

Permalink
Simplistic argument checking
Browse files Browse the repository at this point in the history
To be expanded when optional arguments arrive.
  • Loading branch information
sbarzowski committed Sep 26, 2017
1 parent 850575c commit 9ad91ea
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions testdata/bad_function_call.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
RUNTIME ERROR: function expected 1 argument(s), but got 0
1 change: 1 addition & 0 deletions testdata/bad_function_call.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(function(x) x)()
1 change: 1 addition & 0 deletions testdata/bad_function_call2.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
RUNTIME ERROR: function expected 1 argument(s), but got 2
1 change: 1 addition & 0 deletions testdata/bad_function_call2.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(function(x) x)(1, 2)
1 change: 1 addition & 0 deletions testdata/bad_function_call_and_error.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
RUNTIME ERROR: function expected 1 argument(s), but got 2
1 change: 1 addition & 0 deletions testdata/bad_function_call_and_error.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(function(x) x)(error "x", error "y")
5 changes: 4 additions & 1 deletion thunks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
10 changes: 10 additions & 0 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down

0 comments on commit 9ad91ea

Please sign in to comment.