From 916c239b290f327a9f76d2f801808b6d5362be98 Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 26 Dec 2023 00:18:17 +0800 Subject: [PATCH] Refactor Copy function and add Context function --- v8go.cc | 29 ++++++++++++++++++++++++----- v8go.h | 1 + yao.go | 20 +++++++++++++++++++- yao_test.go | 13 +++++++++++++ 4 files changed, 57 insertions(+), 6 deletions(-) diff --git a/v8go.cc b/v8go.cc index e3822cd1..f6a80711 100644 --- a/v8go.cc +++ b/v8go.cc @@ -127,6 +127,7 @@ m_unboundScript* tracked_unbound_script(m_ctx* ctx, m_unboundScript* us) { extern "C" { /********** Yao App Enine **********/ + static IsolatePtr globalIsolate = nullptr; void YaoDispose() { @@ -136,7 +137,7 @@ void YaoDispose() { } } -extern IsolatePtr YaoNewIsolate() { +IsolatePtr YaoNewIsolate() { Isolate::CreateParams params; params.array_buffer_allocator = default_allocator; @@ -156,7 +157,7 @@ extern IsolatePtr YaoNewIsolate() { } -extern IsolatePtr YaoNewIsolateFromGlobal() { +IsolatePtr YaoNewIsolateFromGlobal() { if (globalIsolate == nullptr) { return nullptr; } @@ -165,16 +166,34 @@ extern IsolatePtr YaoNewIsolateFromGlobal() { return ptr; } +ContextPtr YaoIsolateContext( IsolatePtr iso ) { + if (iso == nullptr) { + return nullptr; + } + m_ctx* ctx = static_cast(iso->GetData(0)); + return ctx; +} extern IsolatePtr YaoCopyIsolate( IsolatePtr iso ) { Isolate::CreateParams params; - params.array_buffer_allocator = default_allocator; - return iso->New(params); + params.array_buffer_allocator = ArrayBuffer::Allocator::NewDefaultAllocator(); + IsolatePtr new_iso = iso->New(params); + + Locker locker(new_iso); + Isolate::Scope isolate_scope(new_iso); + HandleScope handle_scope(new_iso); + + // Create a Context for internal use + m_ctx* ctx = new m_ctx; + ctx->ptr.Reset(new_iso, Context::New(new_iso)); + ctx->iso = new_iso; + new_iso->SetData(0, ctx); + return new_iso; } // Should call in the main thread only -extern void YaoIsolateAsGlobal( IsolatePtr iso ) { +void YaoIsolateAsGlobal( IsolatePtr iso ) { if (globalIsolate != nullptr) { globalIsolate->Dispose(); globalIsolate = nullptr; diff --git a/v8go.h b/v8go.h index 803411fa..3d9d03ee 100644 --- a/v8go.h +++ b/v8go.h @@ -144,6 +144,7 @@ typedef struct { extern void YaoDispose(); extern IsolatePtr YaoNewIsolate(); +extern ContextPtr YaoIsolateContext( IsolatePtr iso ); extern IsolatePtr YaoCopyIsolate( IsolatePtr iso ); extern IsolatePtr YaoNewIsolateFromGlobal(); extern void YaoIsolateAsGlobal( IsolatePtr iso ); diff --git a/yao.go b/yao.go index 3966c263..3020fbf0 100644 --- a/yao.go +++ b/yao.go @@ -49,7 +49,7 @@ func YaoDispose() { // Copy copies the current isolate. func (iso *Isolate) Copy() (*Isolate, error) { - if iso == nil || iso.ptr == nil { + if iso.ptr == nil { return nil, fmt.Errorf("invalid isolate") } new := &Isolate{ @@ -59,6 +59,24 @@ func (iso *Isolate) Copy() (*Isolate, error) { return new, nil } +// Context returns the current context for this isolate. +// DO NOT CALL CLOSE, IT WILL CAUSE PANIC +// THE CONTEXT WILL BE DISPOSED AUTOMATICALLY +func (iso *Isolate) Context() (*Context, error) { + ptr := C.YaoIsolateContext(iso.ptr) + if ptr == nil { + return nil, fmt.Errorf("no current context") + } + + ctxSeq++ + ref := ctxSeq + return &Context{ + ref: ref, + ptr: ptr, + iso: iso, + }, nil +} + // AsGlobal makes the isolate into a global object. func (iso *Isolate) AsGlobal() { C.YaoIsolateAsGlobal(iso.ptr) diff --git a/yao_test.go b/yao_test.go index b0fe5b9a..74e0987a 100644 --- a/yao_test.go +++ b/yao_test.go @@ -41,6 +41,19 @@ func TestYaoCopyIsolate(t *testing.T) { defer new.Dispose() } +func TestYaoIsolateContext(t *testing.T) { + v8.YaoInit(1024) + defer v8.YaoDispose() + + iso := v8.YaoNewIsolate() + defer iso.Dispose() + + _, err := iso.Context() + if err != nil { + t.Error(err) + } +} + func TestYaoIsolateAsGlobal(t *testing.T) { v8.YaoInit(1024)