Skip to content

Commit

Permalink
Refactor Copy function and add Context function
Browse files Browse the repository at this point in the history
  • Loading branch information
trheyi committed Dec 25, 2023
1 parent c0d27a6 commit 916c239
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
29 changes: 24 additions & 5 deletions v8go.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -136,7 +137,7 @@ void YaoDispose() {
}
}

extern IsolatePtr YaoNewIsolate() {
IsolatePtr YaoNewIsolate() {
Isolate::CreateParams params;
params.array_buffer_allocator = default_allocator;

Expand All @@ -156,7 +157,7 @@ extern IsolatePtr YaoNewIsolate() {
}


extern IsolatePtr YaoNewIsolateFromGlobal() {
IsolatePtr YaoNewIsolateFromGlobal() {
if (globalIsolate == nullptr) {
return nullptr;
}
Expand All @@ -165,16 +166,34 @@ extern IsolatePtr YaoNewIsolateFromGlobal() {
return ptr;
}

ContextPtr YaoIsolateContext( IsolatePtr iso ) {
if (iso == nullptr) {
return nullptr;
}
m_ctx* ctx = static_cast<m_ctx*>(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;
Expand Down
1 change: 1 addition & 0 deletions v8go.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
20 changes: 19 additions & 1 deletion yao.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions yao_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 916c239

Please sign in to comment.