forked from TrueCloudLab/neoneo-go
compiler: support variadic function definitions
Pack variadic arguments into a slice. This currently doen't work with byte slices though.
This commit is contained in:
parent
c57d4cfff2
commit
a88ac44147
2 changed files with 40 additions and 6 deletions
|
@ -758,6 +758,14 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
|
||||||
}
|
}
|
||||||
// Do not swap for builtin functions.
|
// Do not swap for builtin functions.
|
||||||
if !isBuiltin {
|
if !isBuiltin {
|
||||||
|
if typ, ok := c.typeOf(n.Fun).(*types.Signature); ok && typ.Variadic() {
|
||||||
|
// pack variadic args into an array
|
||||||
|
varSize := len(n.Args) - typ.Params().Len() + 1
|
||||||
|
c.emitReverse(varSize)
|
||||||
|
emit.Int(c.prog.BinWriter, int64(varSize))
|
||||||
|
emit.Opcode(c.prog.BinWriter, opcode.PACK)
|
||||||
|
numArgs -= varSize - 1
|
||||||
|
}
|
||||||
c.emitReverse(numArgs)
|
c.emitReverse(numArgs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1116,12 +1124,6 @@ func (c *codegen) convertSyscall(expr *ast.CallExpr, api, name string) {
|
||||||
c.prog.Err = fmt.Errorf("unknown VM syscall api: %s", name)
|
c.prog.Err = fmt.Errorf("unknown VM syscall api: %s", name)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
switch name {
|
|
||||||
case "Notify":
|
|
||||||
numArgs := len(expr.Args)
|
|
||||||
emit.Int(c.prog.BinWriter, int64(numArgs))
|
|
||||||
emit.Opcode(c.prog.BinWriter, opcode.PACK)
|
|
||||||
}
|
|
||||||
emit.Syscall(c.prog.BinWriter, api)
|
emit.Syscall(c.prog.BinWriter, api)
|
||||||
switch name {
|
switch name {
|
||||||
case "GetTransaction", "GetBlock":
|
case "GetTransaction", "GetBlock":
|
||||||
|
|
|
@ -184,3 +184,35 @@ func TestLocalsCount(t *testing.T) {
|
||||||
}`
|
}`
|
||||||
eval(t, src, big.NewInt(7))
|
eval(t, src, big.NewInt(7))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestVariadic(t *testing.T) {
|
||||||
|
src := `package foo
|
||||||
|
func someFunc(a int, b ...int) int {
|
||||||
|
sum := a
|
||||||
|
for i := range b {
|
||||||
|
sum = sum - b[i]
|
||||||
|
}
|
||||||
|
return sum
|
||||||
|
}
|
||||||
|
func Main() int {
|
||||||
|
return someFunc(10, 1, 2, 3)
|
||||||
|
}`
|
||||||
|
eval(t, src, big.NewInt(4))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestVariadicMethod(t *testing.T) {
|
||||||
|
src := `package foo
|
||||||
|
type myInt int
|
||||||
|
func (x myInt) someFunc(a int, b ...int) int {
|
||||||
|
sum := int(x) + a
|
||||||
|
for i := range b {
|
||||||
|
sum = sum - b[i]
|
||||||
|
}
|
||||||
|
return sum
|
||||||
|
}
|
||||||
|
func Main() int {
|
||||||
|
x := myInt(38)
|
||||||
|
return x.someFunc(10, 1, 2, 3)
|
||||||
|
}`
|
||||||
|
eval(t, src, big.NewInt(42))
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue