yaegi icon indicating copy to clipboard operation
yaegi copied to clipboard

Got 'slice bounds out of range' when parsing multiple scrpit file in different sequences.

Open Andrew-M-C opened this issue 2 years ago • 0 comments

The following program sample.go triggers an unexpected result

package main

import (
	"fmt"

	"github.com/traefik/yaegi/interp"
	"github.com/traefik/yaegi/stdlib"
)

const definition = `
package desc

const desc = "Hello, world"
`

const function = `
package desc

func Desc() string {
	return desc
}
`

func parse(codes ...string) (fu func() string) {
	y := interp.New(interp.Options{})
	y.Use(stdlib.Symbols)

	for _, code := range codes {
		y.Eval(code)
		y.Eval(code)
	}

	v, _ := y.Eval("desc.Desc")
	fu = v.Interface().(func() string)
	return fu
}

func main() {
	fmt.Println("Case 01:", parse(definition, function)())
	fmt.Println("Case 02:", parse(function, definition)())
}

Expected result

$ go run ./sample.go
Case 01: Hello, world
Case 02: Hello, world

Got

$ go run ./sample.go
Case 01: Hello, world
panic: runtime error: slice bounds out of range [1:0]

goroutine 1 [running]:
github.com/traefik/yaegi/interp.genFunctionWrapper.func2.1({0x20a3d40, 0x0, 0x0?})
	/Users/xxxx/go/pkg/mod/github.com/traefik/[email protected]/interp/run.go:984 +0x96d
main.main()
	/Users/xxxx/some-path/main.go:40 +0x130
exit status 2

Yaegi Version

v0.13.0

Additional Notes

I already met slice bounds out of range error several times, but I cannot replecate it by other simple ways. This is the simplest case.

Andrew-M-C avatar Apr 08 '22 10:04 Andrew-M-C