Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

Commit

Permalink
Make Startupdata data manage by Go GC
Browse files Browse the repository at this point in the history
  • Loading branch information
GustavoCaso committed Jan 19, 2022
1 parent 7c3a420 commit 6752e1c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 65 deletions.
23 changes: 12 additions & 11 deletions isolate.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,19 @@ func NewIsolate(opts ...createOptions) *Isolate {
opt(params)
}

var iso *Isolate
var cOptions C.IsolateOptions

if params.startupData != nil {
iso = &Isolate{
ptr: C.NewIsolateWithCreateParams(params.startupData.ptr),
cbs: make(map[int]FunctionCallback),
createParams: params,
}
} else {
iso = &Isolate{
ptr: C.NewIsolate(),
cbs: make(map[int]FunctionCallback),
}
p := C.CString(string(params.startupData.data))
cOptions.snapshot_blob_data = p
defer C.free(unsafe.Pointer(p))
cOptions.snapshot_blob_raw_size = params.startupData.raw_size
}

iso := &Isolate{
ptr: C.NewIsolate(cOptions),
cbs: make(map[int]FunctionCallback),
createParams: params,
}
iso.null = newValueNull(iso)
iso.undefined = newValueUndefined(iso)
Expand Down
18 changes: 9 additions & 9 deletions snapshot_creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package v8go
import "C"
import (
"errors"
"unsafe"
)

type FunctionCodeHandling int
Expand All @@ -19,14 +20,9 @@ const (
)

type StartupData struct {
ptr *C.SnapshotBlob
index C.size_t
}

func (s *StartupData) Dispose() {
if s.ptr != nil {
C.SnapshotBlobDelete(s.ptr)
}
data []byte
raw_size C.int
index C.size_t
}

type SnapshotCreator struct {
Expand Down Expand Up @@ -75,7 +71,11 @@ func (s *SnapshotCreator) Create(functionCode FunctionCodeHandling) (*StartupDat
s.ctx.ptr = nil
s.iso.ptr = nil

return &StartupData{ptr: rtn, index: s.index}, nil
raw_size := rtn.raw_size
data := C.GoBytes(unsafe.Pointer(rtn.data), raw_size)
defer C.free(unsafe.Pointer(rtn.data))

return &StartupData{data: data, raw_size: raw_size, index: s.index}, nil
}

func (s *SnapshotCreator) Dispose() {
Expand Down
4 changes: 1 addition & 3 deletions snapshot_creator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ func TestCreateSnapshot(t *testing.T) {

data, err := snapshotCreator.Create(v8.FunctionCodeHandlingKlear)
fatalIf(t, err)
defer data.Dispose()

iso := v8.NewIsolate(v8.WithStartupData(data))
defer iso.Dispose()
Expand Down Expand Up @@ -60,9 +59,8 @@ func TestCreateSnapshotErrorAfterSuccessfullCreate(t *testing.T) {
err := snapshotCreator.AddContext(snapshotCreatoCtx)
fatalIf(t, err)

data, err := snapshotCreator.Create(v8.FunctionCodeHandlingKlear)
_, err = snapshotCreator.Create(v8.FunctionCodeHandlingKlear)
fatalIf(t, err)
defer data.Dispose()

err = snapshotCreator.AddContext(snapshotCreatoCtx)
if err == nil {
Expand Down
65 changes: 28 additions & 37 deletions v8go.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,34 +153,19 @@ void Init() {
return;
}

IsolatePtr NewIsolateWithCreateParams(SnapshotBlob* snapshot_blob) {
IsolatePtr NewIsolate(IsolateOptions options) {
Isolate::CreateParams params;

StartupData* startup_data =
new StartupData{snapshot_blob->data, snapshot_blob->raw_size};

params.snapshot_blob = startup_data;
params.array_buffer_allocator = default_allocator;
Isolate* iso = Isolate::New(params);
Locker locker(iso);
Isolate::Scope isolate_scope(iso);
HandleScope handle_scope(iso);

iso->SetCaptureStackTraceForUncaughtExceptions(true);

// Create a Context for internal use
m_ctx* ctx = new m_ctx;
ctx->ptr.Reset(iso, Context::New(iso));
ctx->iso = iso;
ctx->startup_data = startup_data;
iso->SetData(0, ctx);

return iso;
}
StartupData* startup_data;
if (options.snapshot_blob_data) {
startup_data =
new StartupData{options.snapshot_blob_data, options.snapshot_blob_raw_size};
params.snapshot_blob = startup_data;
} else {
startup_data = nullptr;
}

IsolatePtr NewIsolate() {
Isolate::CreateParams params;
params.array_buffer_allocator = default_allocator;
Isolate* iso = Isolate::New(params);
Locker locker(iso);
Isolate::Scope isolate_scope(iso);
Expand All @@ -192,7 +177,7 @@ IsolatePtr NewIsolate() {
m_ctx* ctx = new m_ctx;
ctx->ptr.Reset(iso, Context::New(iso));
ctx->iso = iso;
ctx->startup_data = nullptr;
ctx->startup_data = startup_data;
iso->SetData(0, ctx);

return iso;
Expand Down Expand Up @@ -324,25 +309,26 @@ size_t AddContext(SnapshotCreatorPtr snapshotCreator, ContextPtr ctx) {
;
}

SnapshotBlob* CreateBlob(SnapshotCreatorPtr snapshotCreator,
RtnSnapshotBlob CreateBlob(SnapshotCreatorPtr snapshotCreator,
ContextPtr ctx,
int function_code_handling) {

RtnSnapshotBlob rtn = {};
ContextFree(ctx);
// kKeep - keeps any compiled functions
// kClear - does not keep any compiled functions
StartupData startup_data = snapshotCreator->CreateBlob(
SnapshotCreator::FunctionCodeHandling(function_code_handling));

SnapshotBlob* sb = new SnapshotBlob;
sb->data = startup_data.data;
sb->raw_size = startup_data.raw_size;
delete snapshotCreator;
return sb;
}
int length = startup_data.raw_size;

void SnapshotBlobDelete(SnapshotBlob* ptr) {
delete[] ptr->data;
delete ptr;
char* data = (char*)malloc(length);
memcpy(data, startup_data.data, length);

rtn.data = data;
rtn.raw_size = length;
delete snapshotCreator;
return rtn;
}

/********** Exceptions & Errors **********/
Expand Down Expand Up @@ -691,8 +677,13 @@ ContextPtr NewContextFromSnapShot(IsolatePtr iso,
// side to lookup the context in the context registry. We use slot 1 as slot 0
// has special meaning for the Chrome debugger.

Local<Context> local_ctx =
Context::FromSnapshot(iso, snapshot_blob_index).ToLocalChecked();
MaybeLocal<Context> maybe_local_ctx =
Context::FromSnapshot(iso, snapshot_blob_index);

Local<Context> local_ctx;
if (!maybe_local_ctx.ToLocal(&local_ctx)) {
std::cout << "Error" << std::endl;
}
local_ctx->SetEmbedderData(1, Integer::New(iso, ref));

m_ctx* ctx = new m_ctx;
Expand Down
13 changes: 8 additions & 5 deletions v8go.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ typedef struct {
typedef struct {
const char* data;
int raw_size;
} SnapshotBlob;
} RtnSnapshotBlob;

typedef struct {
SnapshotCreatorPtr creator;
Expand All @@ -91,6 +91,11 @@ typedef struct {
int compileOption;
} CompileOptions;

typedef struct {
const char* snapshot_blob_data;
int snapshot_blob_raw_size;
} IsolateOptions;

typedef struct {
CpuProfilerPtr ptr;
IsolatePtr iso;
Expand Down Expand Up @@ -145,19 +150,17 @@ typedef struct {
} ValueBigInt;

extern void Init();
extern IsolatePtr NewIsolate();
extern IsolatePtr NewIsolateWithCreateParams(SnapshotBlob* ptr);
extern IsolatePtr NewIsolate(IsolateOptions opts);
extern void IsolatePerformMicrotaskCheckpoint(IsolatePtr ptr);
extern void IsolateDispose(IsolatePtr ptr);
extern void IsolateTerminateExecution(IsolatePtr ptr);
extern int IsolateIsExecutionTerminating(IsolatePtr ptr);
extern IsolateHStatistics IsolationGetHeapStatistics(IsolatePtr ptr);

extern void SnapshotBlobDelete(SnapshotBlob* ptr);
extern RtnSnapshotCreator NewSnapshotCreator();
extern void DeleteSnapshotCreator(SnapshotCreatorPtr snapshotCreator);
extern size_t AddContext(SnapshotCreatorPtr snapshotCreator, ContextPtr ctx);
extern SnapshotBlob* CreateBlob(SnapshotCreatorPtr snapshotCreator,
extern RtnSnapshotBlob CreateBlob(SnapshotCreatorPtr snapshotCreator,
ContextPtr ctx,
int function_code_handling);

Expand Down

0 comments on commit 6752e1c

Please sign in to comment.