From 73f6fae6266a05f6f48cda51ee39f6687c58bce9 Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 13 Nov 2025 14:57:52 +0800 Subject: [PATCH] Refactor object registration in Context for improved lifecycle management - Updated the objectRegister method to generate a new ID internally, simplifying the API by removing the need for an external ID parameter. - Adjusted the NewObject method to utilize the new object registration mechanism, ensuring proper ID assignment and resource cleanup during instance creation. --- agent/context/jsapi.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/agent/context/jsapi.go b/agent/context/jsapi.go index e0ca4025..a9799f53 100644 --- a/agent/context/jsapi.go +++ b/agent/context/jsapi.go @@ -20,10 +20,8 @@ func (ctx *Context) NewObject(v8ctx *v8go.Context) (*v8go.Value, error) { jsObject := v8go.NewObjectTemplate(v8ctx.Isolate()) - id := uuid.NewString() - ctx.objectRegister(id) - // Set the id and release function + id := ctx.objectRegister() jsObject.Set("__id", id) jsObject.Set("__release", ctx.objectRelease(v8ctx.Isolate(), id)) @@ -32,15 +30,19 @@ func (ctx *Context) NewObject(v8ctx *v8go.Context) (*v8go.Value, error) { jsObject.Set("Sid", ctx.Sid) instance, err := jsObject.NewInstance(v8ctx) if err != nil { + ctx.objectRelease(v8ctx.Isolate(), id) return nil, err } + return instance.Value, nil } -func (ctx *Context) objectRegister(id string) { +func (ctx *Context) objectRegister() string { + id := uuid.NewString() objectsMutex.Lock() defer objectsMutex.Unlock() objects[id] = ctx + return id } func (ctx *Context) objectRelease(iso *v8go.Isolate, id string) *v8go.FunctionTemplate {