diff --git a/context.go b/context.go index 0b7e7959..620ad996 100644 --- a/context.go +++ b/context.go @@ -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 { diff --git a/isolate.go b/isolate.go index 81034eac..550bf7e8 100644 --- a/isolate.go +++ b/isolate.go @@ -27,7 +27,7 @@ type Isolate struct { null *Value undefined *Value - createParams CreateParams + createParams *CreateParams } // HeapStatistics represents V8 isolate heap statistics @@ -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 } @@ -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 diff --git a/snapshot_creator_test.go b/snapshot_creator_test.go index 163f609a..ad86c50f 100644 --- a/snapshot_creator_test.go +++ b/snapshot_creator_test.go @@ -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)