Skip to content

Commit

Permalink
[repl] [bugfix] "var a string" returns *interface{}
Browse files Browse the repository at this point in the history
This is odd. Yaegi returns last evaluated expression to the user, which is a pointer to an empty interface. But it supposed to be a string.

This commit fixes the issue (hopefully).
  • Loading branch information
elgopher committed Aug 14, 2023
1 parent 9462bf9 commit af6d78c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
5 changes: 1 addition & 4 deletions devtools/internal/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,7 @@ func (i Instance) runGoCode(source string) (r EvalResult, e error) {
// if source is a statement or an expression. If source is a statement and previously source was an expression
// then Yaegi returns the same result again. That's why following code overrides last computed result to the value
// used as a discriminator for no result.
_, err := i.yaegi.Eval("interpreter.noResult{}")
if err != nil {
return GoCodeExecuted, fmt.Errorf("yaegi Eval failed: %w", err)
}
source = "interpreter.noResult{}; " + source

res, err := i.yaegi.Eval(source)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions devtools/internal/interpreter/interpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ func TestEval(t *testing.T) {
code: "pi.Spr(0,0,0)",
expectedResult: interpreter.GoCodeExecuted,
},
"var declaration": {
code: "var a string",
expectedOutput: "string: \n",
expectedResult: interpreter.GoCodeExecuted,
},
"expression returning a struct": {
code: "struct{}{}",
expectedOutput: "struct {}: {}\n",
Expand Down

0 comments on commit af6d78c

Please sign in to comment.