Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ void ContextFree(ContextPtr ctx) {
delete ctx;
}

int ContextIsEmpty(ContextPtr ctx) {
// ContextFree deletes ContextPtr, so we have to handle the case
// when the context was already closed, gracefully, by go side
if (ctx == nullptr) {
return 1;
}

return ctx->ptr.IsEmpty();
}

m_value* tracked_value(m_ctx* ctx, m_value* val) {
// (rogchap) we track values against a context so that when the context is
// closed (either manually or GC'd by Go) we can also release all the
Expand Down
4 changes: 4 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ func (c *Context) Close() {
c.ptr = nil
}

func (c *Context) IsEmpty() bool {
return C.ContextIsEmpty(c.ptr) != 0
}

func (c *Context) register() {
ctxMutex.Lock()
r := ctxRegistry[c.ref]
Expand Down
1 change: 1 addition & 0 deletions context.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ extern ContextPtr NewContext(IsolatePtr iso_ptr,
extern int ContextRetainedValueCount(ContextPtr ctx);
extern ValuePtr ContextGlobal(ContextPtr ctx_ptr);
extern void ContextFree(ContextPtr ctx);
extern int ContextIsEmpty(ContextPtr ctx);
extern RtnValue RunScript(ContextPtr ctx_ptr,
const char* source,
const char* origin);
Expand Down
21 changes: 21 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,27 @@ func TestRegistryFromJSON(t *testing.T) {
}
}

func TestContextIsEmpty(t *testing.T) {
iso := v8.NewIsolate()
defer iso.Dispose()

t.Run("Closed", func(t *testing.T) {
ctx := v8.NewContext(iso)
ctx.Close()

if !ctx.IsEmpty() {
t.Error("expected context to be empty. ctx.IsEmpty() == false")
}
})

t.Run("Active", func(t *testing.T) {
ctx := v8.NewContext(iso)
if ctx.IsEmpty() {
t.Error("expected context to be active. ctx.IsEmpty() == true")
}
})
}

func BenchmarkContext(b *testing.B) {
b.ReportAllocs()
iso := v8.NewIsolate()
Expand Down