Skip to content

Commit

Permalink
v8.Value becomes manually releaseable (#361)
Browse files Browse the repository at this point in the history
* values are manually releaseable

Co-authored-by: Kyle Maxwell <kyle.maxwell@reddit.com>
  • Loading branch information
fizx and Kyle Maxwell committed Jan 19, 2023
1 parent 1f00b50 commit 7d843f1
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 20 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added
- Added support for Value.release() and FunctionCallbackInfo.release(). This is useful when using v8go in a long-running context.

### Fixed
- Use string length to ensure null character-containing strings in Go/JS are not terminated early.
- Object.Set with an empty key string is now supported
Expand Down
6 changes: 6 additions & 0 deletions context.go
Expand Up @@ -81,6 +81,12 @@ func (c *Context) Isolate() *Isolate {
return c.iso
}

func (c *Context) RetainedValueCount() int {
ctxMutex.Lock()
defer ctxMutex.Unlock()
return int(C.ContextRetainedValueCount(c.ptr))
}

// RunScript executes the source JavaScript; origin (a.k.a. filename) provides a
// reference for the script and used in the stack trace if there is an error.
// error will be of type `JSError` if not nil.
Expand Down
4 changes: 2 additions & 2 deletions context_test.go
Expand Up @@ -104,8 +104,8 @@ func TestMemoryLeak(t *testing.T) {

for i := 0; i < 6000; i++ {
ctx := v8.NewContext(iso)
obj := ctx.Global()
_ = obj.String()
_ = ctx.Global()
// _ = obj.String()
_, _ = ctx.RunScript("2", "")
ctx.Close()
}
Expand Down
7 changes: 7 additions & 0 deletions function_template.go
Expand Up @@ -37,6 +37,13 @@ func (i *FunctionCallbackInfo) Args() []*Value {
return i.args
}

func (i *FunctionCallbackInfo) Release() {
for _, arg := range i.args {
arg.Release()
}
i.this.Release()
}

// FunctionTemplate is used to create functions at runtime.
// There can only be one function created from a FunctionTemplate in a context.
// The lifetime of the created function is equal to the lifetime of the context.
Expand Down
39 changes: 39 additions & 0 deletions function_template_test.go
Expand Up @@ -48,6 +48,45 @@ func TestFunctionTemplate_panic_on_nil_callback(t *testing.T) {
defer iso.Dispose()
v8.NewFunctionTemplate(iso, nil)
}
func TestFunctionTemplate_generates_values(t *testing.T) {
t.Parallel()

iso := v8.NewIsolate()
defer iso.Dispose()
global := v8.NewObjectTemplate(iso)
printfn := v8.NewFunctionTemplate(iso, func(info *v8.FunctionCallbackInfo) *v8.Value {
fmt.Printf("%+v\n", info.Args())
return nil
})
global.Set("print", printfn, v8.ReadOnly)
ctx := v8.NewContext(iso, global)
defer ctx.Close()
ctx.RunScript("print('foo', 'bar', 0, 1)", "")
if ctx.RetainedValueCount() != 6 {
t.Errorf("expected 6 retained values, got: %d", ctx.RetainedValueCount())
}
}

func TestFunctionTemplate_releases_values(t *testing.T) {
t.Parallel()

iso := v8.NewIsolate()
defer iso.Dispose()
global := v8.NewObjectTemplate(iso)
printfn := v8.NewFunctionTemplate(iso, func(info *v8.FunctionCallbackInfo) *v8.Value {
defer info.Release()
fmt.Printf("%+v\n", info.Args())
return nil
})
global.Set("print", printfn, v8.ReadOnly)
ctx := v8.NewContext(iso, global)
defer ctx.Close()
ctx.RunScript("print('foo', 'bar', 0, 1)", "")
// there is a constant factor associated with the global.
if ctx.RetainedValueCount() != 1 {
t.Errorf("expected 1 retained values, got: %d", ctx.RetainedValueCount())
}
}

func TestFunctionTemplateGetFunction(t *testing.T) {
t.Parallel()
Expand Down

0 comments on commit 7d843f1

Please sign in to comment.