diff --git a/context.go b/context.go index d0bd3f2..0566055 100644 --- a/context.go +++ b/context.go @@ -20,6 +20,7 @@ type Context struct { globals *Value proxy *Value asyncProxy *Value + handles map[int64]any // add by Star } // Runtime returns the runtime of the context. @@ -41,6 +42,14 @@ func (ctx *Context) Close() { ctx.globals.Free() } + // add by Star + for _, hv := range ctx.handles { + if h, ok := hv.(cgo.Handle); ok { + h.Delete() + } + } + ctx.handles = nil + C.JS_FreeContext(ctx.ref) } @@ -162,8 +171,16 @@ func (ctx *Context) Function(fn func(ctx *Context, this Value, args []Value) Val } } - fnHandler := ctx.Int64(int64(cgo.NewHandle(fn))) - ctxHandler := ctx.Int64(int64(cgo.NewHandle(ctx))) + // edit by Star + //fnHandler := ctx.Int64(int64(cgo.NewHandle(fn))) + //ctxHandler := ctx.Int64(int64(cgo.NewHandle(ctx))) + fnH := cgo.NewHandle(fn) + ctxH := cgo.NewHandle(ctx) + ctx.handles[int64(fnH)] = fnH + ctx.handles[int64(ctxH)] = ctxH + fnHandler := ctx.Int64(int64(fnH)) + ctxHandler := ctx.Int64(int64(ctxH)) + args := []C.JSValue{ctx.proxy.ref, fnHandler.ref, ctxHandler.ref} val, err := ctx.Eval(`(proxy, fnHandler, ctx) => function() { return proxy.call(this, fnHandler, ctx, ...arguments); }`) diff --git a/runtime.go b/runtime.go index 6ac7dcd..f6edb6b 100644 --- a/runtime.go +++ b/runtime.go @@ -177,5 +177,7 @@ func (r Runtime) NewContext() *Context { C.JS_FreeValue(ctx_ref, init_run) // C.js_std_loop(ctx_ref) - return &Context{ref: ctx_ref, runtime: &r} + // edit by Star + //return &Context{ref: ctx_ref, runtime: &r} + return &Context{ref: ctx_ref, runtime: &r, handles: map[int64]any{}} }