tengo icon indicating copy to clipboard operation
tengo copied to clipboard

Call compiled function from go return empty

Open moisespsena opened this issue 5 years ago • 2 comments

Hello! When call compiled function from go return empty.

package main

import (
	"context"
	"fmt"

	"github.com/d5/tengo"
)

func main() {
	// create a new Script instance
	script := tengo.NewScript([]byte(`
res := wait_go_hello_message(func(message) {
	return "Go Hello Message:" + message
})
`))
	script.Add("wait_go_hello_message", func(args ...tengo.Object) (ret tengo.Object, err error) {
		return args[0].(*tengo.CompiledFunction).Call(&tengo.String{Value: "hello!"})
	})
	// run the script
	compiled, err := script.RunContext(context.Background())
	if err != nil {
		panic(err)
	}

	res := compiled.Get("res")

	// EXPECTED: Go Hello Message: hello!
	fmt.Printf("%T: %q", res, res) // OUTPUT: *tengo.Variable: ""
}

moisespsena avatar Sep 07 '20 22:09 moisespsena

@moisespsena , Your usecase mixes builtin and compiled functions in a way that Tengo does not support. You can't Call into a compiled function directly. Look at #275 This is a duplicate issue.

DawDavis avatar Oct 31 '20 07:10 DawDavis

@DawDavis , I have implemented a branch "vm_context" based on tengox with the definition of objects based on maps and reflection. I did it to be able to use it in my personal projects.

See to call tengo function from go.

What do you think?

moisespsena avatar Nov 17 '20 22:11 moisespsena