traefik / yaegi

Yaegi is Another Elegant Go Interpreter
https://pkg.go.dev/github.com/traefik/yaegi
Apache License 2.0
6.78k stars 341 forks source link

It panics when append functions into a slice of that type of function #1592

Open PatriotBo opened 10 months ago

PatriotBo commented 10 months ago

The following program sample.go triggers an unexpected result

package main

import (
    "fmt"

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

var funcs = `
package main

import "fmt"

func AppendSlice() {
    type funcType func()

    var fs []funcType
    fs = append(fs, a)

    for _, f := range fs {
        f()
    }
}

func a(){
    fmt.Println("execute func a")
}`

func main(){
    engine := interp.New(interp.Options{})
    // 引入标准库
    if err := engine.Use(stdlib.Symbols); err != nil {
        panic(err)
    }

    _, err := engine.Eval(funcs)
    if err != nil {
        fmt.Printf("evel funcs failed:%v \n", err)
        return
    }

    v, err := engine.Eval("main.AppendSlice")
    if err != nil {
        fmt.Printf("evel main.AppendSlice failed:%v \n", err)
        return
    }

    f, ok := v.Interface().(func())
    if !ok {
        fmt.Println("func not type of metadataudf.UserDefineFunc ")
        return
    }
    f()
}

Expected result

go run main.go

Got

7:2: panic
panic: reflect.Set: value of type *interp.node is not assignable to type func() [recovered]
        panic: reflect.Set: value of type *interp.node is not assignable to type func()

Yaegi Version

v0.15.1

Additional Notes

No response