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

Commit

Permalink
Allow to pass a list of options to NewIsolate function
Browse files Browse the repository at this point in the history
  • Loading branch information
GustavoCaso authored and Genevieve L'Esperance committed Jan 5, 2022
1 parent f52226e commit 6bd432f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
5 changes: 3 additions & 2 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ func NewContext(opt ...ContextOption) *Context {
ctxMutex.Unlock()

var ctx *Context
if opts.iso.createParams.StartupData.ptr != nil {
createParams := opts.iso.createParams
if createParams != nil && createParams.StartupData != nil {
ctx = &Context{
ref: ref,
ptr: C.NewContextFromSnapShot(opts.iso.ptr, opts.iso.createParams.StartupData.ptr.index, opts.gTmpl.ptr, C.int(ref)),
ptr: C.NewContextFromSnapShot(opts.iso.ptr, createParams.StartupData.ptr.index, opts.gTmpl.ptr, C.int(ref)),
iso: opts.iso,
}
} else {
Expand Down
43 changes: 24 additions & 19 deletions isolate.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Isolate struct {

null *Value
undefined *Value
createParams CreateParams
createParams *CreateParams
}

// HeapStatistics represents V8 isolate heap statistics
Expand All @@ -45,6 +45,14 @@ type HeapStatistics struct {
NumberOfDetachedContexts uint64
}

type createOptions func(*CreateParams)

func WithStartupData(startupData *StartupData) createOptions {
return func(params *CreateParams) {
params.StartupData = startupData
}
}

type CreateParams struct {
StartupData *StartupData
}
Expand All @@ -56,34 +64,31 @@ type CreateParams struct {
// by calling iso.Dispose().
// An *Isolate can be used as a v8go.ContextOption to create a new
// Context, rather than creating a new default Isolate.
func NewIsolate() *Isolate {
func NewIsolate(opts ...createOptions) *Isolate {
v8once.Do(func() {
C.Init()
})
iso := &Isolate{
ptr: C.NewIsolate(),
cbs: make(map[int]FunctionCallback),
params := &CreateParams{}
for _, opt := range opts {
opt(params)
}
iso.null = newValueNull(iso)
iso.undefined = newValueUndefined(iso)
return iso
}

func NewIsolateWithCreateParams(params CreateParams) *Isolate {
v8once.Do(func() {
C.Init()
})
if params.StartupData.ptr != nil {
iso := &Isolate{
var iso *Isolate
if params.StartupData != nil {
iso = &Isolate{
ptr: C.NewIsolateWithCreateParams(params.StartupData.ptr),
cbs: make(map[int]FunctionCallback),
createParams: params,
}
iso.null = newValueNull(iso)
iso.undefined = newValueUndefined(iso)
return iso
} else {
iso = &Isolate{
ptr: C.NewIsolate(),
cbs: make(map[int]FunctionCallback),
}
}
return nil
iso.null = newValueNull(iso)
iso.undefined = newValueUndefined(iso)
return iso
}

// TerminateExecution terminates forcefully the current thread
Expand Down
2 changes: 1 addition & 1 deletion snapshot_creator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func TestCreateSnapshot(t *testing.T) {
data := v8.CreateSnapshot("function run() { return 1 };", "script.js", v8.FunctionCodeHandlingKlear)

iso := v8.NewIsolateWithCreateParams(v8.CreateParams{StartupData: data})
iso := v8.NewIsolate(v8.WithStartupData(data))
defer iso.Dispose()
defer data.Dispose()
ctx := v8.NewContext(iso)
Expand Down

0 comments on commit 6bd432f

Please sign in to comment.