how to return from the script (not function)
How do you exit the script?
result = _fields["Addr"]
if result == undefined {
return
}
I get the following error
Compile Error: return not allowed outside function
You can use os.exit function from the standard library https://github.com/d5/tengo/blob/master/docs/stdlib-os.md#functions
unfortunately, the scripts are user defined and we dont want to include the os library (security, etc).
I would be nice if it would allow it.
Hi @siff-duke I had same requirement before, as a workaround I used runtime errors.
Example:
var ErrUserExit = errors.New("user exit")
func Example() {
script := `
result = _fields["Addr"]
if result == undefined {
exit()
}
`
s := tengo.NewScript([]byte(script))
err := s.Add("exit", &tengo.UserFunction{
Name: "exit",
Value: func(args ...tengo.Object) (tengo.Object, error) {
return nil, ErrUserExit
},
})
if err != nil {
log.Fatal(err)
}
c, err := s.Compile()
if err != nil {
log.Fatal(err)
}
err = c.Run()
if err != nil {
if errors.Is(err, ErrUserExit) {
// normal exit
}
}
}
Maybe we should just add a new intrinsic function (e.g. exit).
my preference would be return but exit works.