I have the following go-duktape code:
package main
import (
"fmt"
"gopkg.in/olebedev/go-duktape.v3"
"time"
)
func main() {
code := "function test(){log('testing');log('testing2');done();done();};"
resp := make(chan string)
ctx := duktape.New()
go doExec(code, ctx, resp)
select {
case r := <-resp:
fmt.Printf("%s
", r)
case <-time.After(5 * time.Second):
fmt.Printf("Execution timeout
")
}
kill(ctx)
close(resp)
}
func kill(ctx *duktape.Context) {
ctx.DestroyHeap()
ctx.Destroy()
}
func doExec(code string, ctx *duktape.Context, resp chan string) {
ctx.PushGlobalGoFunction("done", func(c *duktape.Context) int {
resp <- "We're done!!"
return 0
})
ctx.PushGlobalGoFunction("log", func(c *duktape.Context) int {
fmt.Println(c.SafeToString(-1))
return 0
})
err := ctx.PevalString(code + ";try{test()}catch(e){log('Error in execution');}")
if err != nil {
fmt.Printf("Error is %s
", err.Error())
resp <- "Error in exec"
}
}
My goal is to terminate the program after the FIRST done()
function call and not allow any more funtions to be executed after that. But if I run the following code, it panics because done()
is called twice and the second call tries to write on a channel that's closed. How do I make sure that it terminates after the first done()
call?
Thanks!