Skip to content

Commit 70c3a77

Browse files
committed
Enhance Value methods with improved naming and add ReleaseObject for custom cleanup before releasing values
1 parent 32345e2 commit 70c3a77

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

value.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,12 +391,12 @@ func (v *Value) IsRegExp() bool {
391391
return C.ValueIsRegExp(v.ptr) != 0
392392
}
393393

394-
// IsAsyncFunc returns true if this value is an async function.
394+
// IsAsyncFunction returns true if this value is an async function.
395395
func (v *Value) IsAsyncFunction() bool {
396396
return C.ValueIsAsyncFunction(v.ptr) != 0
397397
}
398398

399-
// Is IsGeneratorFunc returns true if this value is a Generator function.
399+
// IsGeneratorFunction returns true if this value is a Generator function.
400400
func (v *Value) IsGeneratorFunction() bool {
401401
return C.ValueIsGeneratorFunction(v.ptr) != 0
402402
}
@@ -535,13 +535,42 @@ func (v *Value) IsProxy() bool {
535535
// Release this value. Using the value after calling this function will result in undefined behavior.
536536
func (v *Value) Release() {
537537

538+
// Yao: Invoke the __release() callback if present on the object.
539+
// This allows JavaScript objects to perform custom cleanup (e.g., unregistering from Go maps)
540+
// before the underlying V8 value is released.
541+
v.ReleaseObject()
542+
538543
// Yao: Before releasing the value, we need to release the external object
539544
// if it is an external object.
540545
v.ReleaseExternal()
541546

542547
C.ValueRelease(v.ptr)
543548
}
544549

550+
// ReleaseObject release the object if it is an object. (Yao Application Engine)
551+
func (v *Value) ReleaseObject() {
552+
if v.IsObject() {
553+
obj, err := v.AsObject()
554+
if err != nil {
555+
return
556+
}
557+
558+
// Yao: Get the release function from the object
559+
release, err := obj.Get("__release")
560+
if err != nil {
561+
return
562+
}
563+
564+
if release.IsFunction() {
565+
fn, err := release.AsFunction()
566+
if err != nil {
567+
return
568+
}
569+
fn.Call(v)
570+
}
571+
}
572+
}
573+
545574
// IsWasmModuleObject returns true if this value is a `WasmModuleObject`.
546575
func (v *Value) IsWasmModuleObject() bool {
547576
// TODO(rogchap): requires test case
@@ -564,13 +593,15 @@ func (v *Value) AsObject() (*Object, error) {
564593
return &Object{v}, nil
565594
}
566595

596+
// AsPromise will cast the value to the Promise type. If the value is not a Promise
567597
func (v *Value) AsPromise() (*Promise, error) {
568598
if !v.IsPromise() {
569599
return nil, errors.New("v8go: value is not a Promise")
570600
}
571601
return &Promise{&Object{v}}, nil
572602
}
573603

604+
// AsFunction will cast the value to the Function type. If the value is not a Function
574605
func (v *Value) AsFunction() (*Function, error) {
575606
if !v.IsFunction() {
576607
return nil, errors.New("v8go: value is not a Function")
@@ -587,6 +618,7 @@ func (v *Value) MarshalJSON() ([]byte, error) {
587618
return []byte(jsonStr), nil
588619
}
589620

621+
// SharedArrayBufferGetContents will get the contents of the SharedArrayBuffer.
590622
func (v *Value) SharedArrayBufferGetContents() ([]byte, func(), error) {
591623
if !v.IsSharedArrayBuffer() {
592624
return nil, nil, errors.New("v8go: value is not a SharedArrayBuffer")

0 commit comments

Comments
 (0)