### Background #1102 changed how `Interpreter.Use` interprets export paths such that the last path component is stripped and used as the package name. This resulted in #1139 - attempting to Use an export with only one path component, such as `foo`, would result in the import path being `.`. ### Breaking API Change This PR changes the signature of `Interpreter.Use` from `Use(Exports)` to `Use(Exports) error`. ### Fix for #1139 With this PR, if Use is called with an incomplete export path, such as `foo`, Use will return an error.
50 lines
842 B
Go
50 lines
842 B
Go
package interp_test
|
|
|
|
import (
|
|
"log"
|
|
|
|
"github.com/traefik/yaegi/interp"
|
|
"github.com/traefik/yaegi/stdlib"
|
|
)
|
|
|
|
func ExampleInterpreter_self() {
|
|
i := interp.New(interp.Options{})
|
|
|
|
if err := i.Use(stdlib.Symbols); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
if err := i.Use(interp.Symbols); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
_, err := i.Eval(`import (
|
|
"fmt"
|
|
"log"
|
|
|
|
// Import interp to gain access to Self.
|
|
"github.com/traefik/yaegi/interp"
|
|
)`)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
_, err = i.Eval(`
|
|
// Evaluate code directly.
|
|
fmt.Println("Hello Yaegi from Go")
|
|
|
|
// Evaluate code indirectly via the Self access point.
|
|
_, err := interp.Self.Eval("fmt.Println(\"Hello Yaegi from Yaegi\")")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
`)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// Output:
|
|
//
|
|
// Hello Yaegi from Go
|
|
// Hello Yaegi from Yaegi
|
|
}
|