如何在GoDuktape中停止执行js函数
我有以下go duktape代码:如何在GoDuktape中停止执行js函数,go,duktape,Go,Duktape,我有以下go duktape代码: 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()
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\n", r)
case <-time.After(5 * time.Second):
fmt.Printf("Execution timeout\n")
}
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\n", err.Error())
resp <- "Error in exec"
}
}
主程序包
进口(
“fmt”
“gopkg.in/olebedev/go duktape.v3”
“时间”
)
func main(){
代码:=“函数测试(){log('testing');log('testing2');完成();完成();};”
resp:=制造(成串)
ctx:=duktape.New()
go doExec(代码、ctx、resp)
挑选{
案例r:=在关闭频道之前,你能在done()
内部调用kill()
吗?@svsd我试过了,但是我得到了segfaults。如果我在done()
内部调用kill()
,它会破坏duktape堆和上下文,而PevalString()
仍在执行,因为它仍必须执行第二个done()
调用