internal/shader: Check the number of arguments

This commit is contained in:
Hajime Hoshi 2021-11-14 17:34:06 +09:00
parent 9be4ce928b
commit 8ae6793d4d
4 changed files with 26 additions and 2 deletions

View File

@ -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]) {

View File

@ -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);

View File

@ -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)
}

View File

@ -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")
}
}