From 8ae6793d4d3c7c362f8b97165b730517c71cee74 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 14 Nov 2021 17:34:06 +0900 Subject: [PATCH] internal/shader: Check the number of arguments --- internal/shader/expr.go | 9 +++++++++ .../shader/testdata/call_multiple.expected.vs | 2 +- internal/shader/testdata/call_multiple.go | 2 +- shader_test.go | 15 +++++++++++++++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/internal/shader/expr.go b/internal/shader/expr.go index 869726a9d..5e090ee51 100644 --- a/internal/shader/expr.go +++ b/internal/shader/expr.go @@ -291,6 +291,15 @@ func (cs *compileState) parseExpr(block *block, expr ast.Expr, markLocalVariable f := cs.funcs[callee.Index] + if len(f.ir.InParams) < len(args) { + cs.addError(e.Pos(), fmt.Sprintf("too many arguments in call to %s", e.Fun)) + return nil, nil, nil, false + } + if len(f.ir.InParams) > len(args) { + cs.addError(e.Pos(), fmt.Sprintf("not enough arguments in call to %s", e.Fun)) + return nil, nil, nil, false + } + for i, p := range f.ir.InParams { if args[i].Type == shaderir.NumberExpr && p.Main == shaderir.Int { if !cs.forceToInt(e, &args[i]) { diff --git a/internal/shader/testdata/call_multiple.expected.vs b/internal/shader/testdata/call_multiple.expected.vs index cec73210d..6026d888c 100644 --- a/internal/shader/testdata/call_multiple.expected.vs +++ b/internal/shader/testdata/call_multiple.expected.vs @@ -6,7 +6,7 @@ void F0(in vec2 l0, out vec2 l1) { float l3 = float(0); float l4 = float(0); float l5 = float(0); - F1((l0).x, (l0).y, l2, l3); + F1((l0).x, l2, l3); l4 = l2; l5 = l3; l1 = vec2(l4, l5); diff --git a/internal/shader/testdata/call_multiple.go b/internal/shader/testdata/call_multiple.go index 9e33b3f20..d97c33c47 100644 --- a/internal/shader/testdata/call_multiple.go +++ b/internal/shader/testdata/call_multiple.go @@ -1,7 +1,7 @@ package main func Foo(x vec2) vec2 { - var xx, yx float = Bar(x.x, x.y) + var xx, yx float = Bar(x.x) return vec2(xx, yx) } diff --git a/shader_test.go b/shader_test.go index 643eb64c2..5b2735722 100644 --- a/shader_test.go +++ b/shader_test.go @@ -1180,3 +1180,18 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 { }) } } + +func TestShaderUnmatchedArgs(t *testing.T) { + if _, err := ebiten.NewShader([]byte(`package main + +func Foo() { +} + +func Fragment(position vec4, texCoord vec2, color vec4) vec4 { + Foo(1) + return position +} +`)); err == nil { + t.Errorf("error must be non-nil but was nil") + } +}