From f1f3524e54f82e7bb992fa909a1d59a4cd58d9f3 Mon Sep 17 00:00:00 2001 From: Gustavo Caso Date: Tue, 18 Jan 2022 19:00:37 +0100 Subject: [PATCH] Work on the new API design. Make it working with passing custom context to CreateBlob --- snapshot_creator.go | 31 +++++---- snapshot_creator_test.go | 131 ++++++++++++++++++++------------------- v8go.cc | 64 +++++-------------- v8go.h | 14 ++--- 4 files changed, 106 insertions(+), 134 deletions(-) diff --git a/snapshot_creator.go b/snapshot_creator.go index 9708dd05f..66dba74b4 100644 --- a/snapshot_creator.go +++ b/snapshot_creator.go @@ -9,7 +9,6 @@ package v8go import "C" import ( "errors" - "unsafe" ) type FunctionCodeHandling int @@ -32,6 +31,8 @@ func (s *StartupData) Dispose() { type SnapshotCreator struct { ptr C.SnapshotCreatorPtr + iso *Isolate + ctx *Context index C.size_t } @@ -40,26 +41,21 @@ func NewSnapshotCreator() *SnapshotCreator { C.Init() }) + rtn := C.NewSnapshotCreator() + return &SnapshotCreator{ - ptr: C.NewSnapshotCreator(), + ptr: rtn.creator, + iso: &Isolate{ptr: rtn.iso}, } } -func (s *SnapshotCreator) AddContext(source, origin string) error { - cSource := C.CString(source) - cOrigin := C.CString(origin) - defer C.free(unsafe.Pointer(cSource)) - defer C.free(unsafe.Pointer(cOrigin)) - - rtn := C.AddContext(s.ptr, cSource, cOrigin) - - if rtn.error.msg != nil { - return newJSError(rtn.error) - } - - s.index = rtn.index +func (s *SnapshotCreator) GetIsolate() *Isolate { + return s.iso +} - return nil +func (s *SnapshotCreator) AddContext(ctx *Context) { + s.index = C.AddContext(s.ptr, ctx.ptr) + s.ctx = ctx } func (s *SnapshotCreator) Create(functionCode FunctionCodeHandling) (*StartupData, error) { @@ -67,9 +63,10 @@ func (s *SnapshotCreator) Create(functionCode FunctionCodeHandling) (*StartupDat return nil, errors.New("v8go: Cannot use snapshot creator after creating the blob") } - rtn := C.CreateBlob(s.ptr, C.int(functionCode)) + rtn := C.CreateBlob(s.ptr, s.ctx.ptr, C.int(functionCode)) s.ptr = nil + s.ctx.ptr = nil return &StartupData{ptr: rtn, index: s.index}, nil } diff --git a/snapshot_creator_test.go b/snapshot_creator_test.go index 23ff29259..2def112e7 100644 --- a/snapshot_creator_test.go +++ b/snapshot_creator_test.go @@ -12,76 +12,20 @@ import ( func TestCreateSnapshot(t *testing.T) { snapshotCreator := v8.NewSnapshotCreator() - err := snapshotCreator.AddContext("function run() { return 1 };", "script.js") - fatalIf(t, err) - - data, err := snapshotCreator.Create(v8.FunctionCodeHandlingKlear) - fatalIf(t, err) - defer data.Dispose() - - iso := v8.NewIsolate(v8.WithStartupData(data)) - defer iso.Dispose() - - ctx := v8.NewContext(iso) - defer ctx.Close() - - runVal, err := ctx.Global().Get("run") - if err != nil { - panic(err) - } - - fn, err := runVal.AsFunction() - if err != nil { - panic(err) - } - val, err := fn.Call(v8.Undefined(iso)) - if err != nil { - panic(err) - } - if val.String() != "1" { - t.Fatal("invalid val") - } -} - -func TestCreateSnapshotErrorAfterSuccessfullCreate(t *testing.T) { - snapshotCreator := v8.NewSnapshotCreator() + snapshotCreatorIso := snapshotCreator.GetIsolate() + snapshotCreatoCtx := v8.NewContext(snapshotCreatorIso) + defer snapshotCreatoCtx.Close() - err := snapshotCreator.AddContext("function run() { return 1 };", "script.js") - fatalIf(t, err) + snapshotCreatoCtx.RunScript(`const add = (a, b) => a + b`, "add.js") + snapshotCreatoCtx.RunScript(`function run() { return add(3, 4); }`, "main.js") + snapshotCreator.AddContext(snapshotCreatoCtx) data, err := snapshotCreator.Create(v8.FunctionCodeHandlingKlear) fatalIf(t, err) defer data.Dispose() - _, err = snapshotCreator.Create(v8.FunctionCodeHandlingKlear) - if err == nil { - t.Error("Creating snapshot should have fail") - } -} - -func TestAddContextFail(t *testing.T) { - snapshotCreator := v8.NewSnapshotCreator() - defer snapshotCreator.Dispose() - - err := snapshotCreator.AddContext("feuihyvfeuyfeu", "script.js") - if err == nil { - t.Error("add context should have fail") - } -} - -func TestCreateSnapshotFailAndReuse(t *testing.T) { - snapshotCreator := v8.NewSnapshotCreator() - err := snapshotCreator.AddContext("feuihyvfeuyfeu", "script.js") - if err == nil { - t.Error("add context should have fail") - } - err = snapshotCreator.AddContext("function run() { return 1 };", "script.js") - data, err := snapshotCreator.Create(v8.FunctionCodeHandlingKlear) - fatalIf(t, err) - iso := v8.NewIsolate(v8.WithStartupData(data)) defer iso.Dispose() - defer data.Dispose() ctx := v8.NewContext(iso) defer ctx.Close() @@ -99,7 +43,68 @@ func TestCreateSnapshotFailAndReuse(t *testing.T) { if err != nil { panic(err) } - if val.String() != "1" { + if val.String() != "7" { t.Fatal("invalid val") } } + +// func TestCreateSnapshotErrorAfterSuccessfullCreate(t *testing.T) { +// snapshotCreator := v8.NewSnapshotCreator() + +// err := snapshotCreator.AddContext("function run() { return 1 };", "script.js") +// fatalIf(t, err) + +// data, err := snapshotCreator.Create(v8.FunctionCodeHandlingKlear) +// fatalIf(t, err) +// defer data.Dispose() + +// _, err = snapshotCreator.Create(v8.FunctionCodeHandlingKlear) +// if err == nil { +// t.Error("Creating snapshot should have fail") +// } +// } + +// func TestAddContextFail(t *testing.T) { +// snapshotCreator := v8.NewSnapshotCreator() +// defer snapshotCreator.Dispose() + +// err := snapshotCreator.AddContext("feuihyvfeuyfeu", "script.js") +// if err == nil { +// t.Error("add context should have fail") +// } +// } + +// func TestCreateSnapshotFailAndReuse(t *testing.T) { +// snapshotCreator := v8.NewSnapshotCreator() +// err := snapshotCreator.AddContext("feuihyvfeuyfeu", "script.js") +// if err == nil { +// t.Error("add context should have fail") +// } +// err = snapshotCreator.AddContext("function run() { return 1 };", "script.js") +// data, err := snapshotCreator.Create(v8.FunctionCodeHandlingKlear) +// fatalIf(t, err) + +// iso := v8.NewIsolate(v8.WithStartupData(data)) +// defer iso.Dispose() +// defer data.Dispose() + +// ctx := v8.NewContext(iso) +// defer ctx.Close() + +// runVal, err := ctx.Global().Get("run") +// if err != nil { +// panic(err) +// } + +// fn, err := runVal.AsFunction() +// if err != nil { +// panic(err) +// } +// val, err := fn.Call(v8.Undefined(iso)) +// if err != nil { +// panic(err) +// } +// if val.String() != "1" { +// t.Fatal("invalid val") +// } +// } diff --git a/v8go.cc b/v8go.cc index b13468f1b..55106b088 100644 --- a/v8go.cc +++ b/v8go.cc @@ -293,7 +293,8 @@ RtnUnboundScript IsolateCompileUnboundScript(IsolatePtr iso, /********** SnapshotCreator **********/ -SnapshotCreatorPtr NewSnapshotCreator() { +RtnSnapshotCreator NewSnapshotCreator() { + RtnSnapshotCreator rtn = {}; SnapshotCreator* creator = new SnapshotCreator; Isolate* iso = creator->GetIsolate(); { @@ -302,62 +303,31 @@ SnapshotCreatorPtr NewSnapshotCreator() { creator->SetDefaultContext(ctx); } - return creator; + rtn.creator = creator; + rtn.iso = iso; + + return rtn; } void DeleteSnapshotCreator(SnapshotCreatorPtr snapshotCreator) { delete snapshotCreator; } -RtnSnapshotAddContext AddContext(SnapshotCreatorPtr snapshotCreator, - const char* source, - const char* origin) { - RtnSnapshotAddContext rtn = {}; - Isolate* iso = snapshotCreator->GetIsolate(); - size_t index; - - { - bool fail = false; - HandleScope handle_scope(iso); - Local ctx = Context::New(iso); - TryCatch try_catch(iso); - Context::Scope context_scope(ctx); - - MaybeLocal maybeSrc = - String::NewFromUtf8(iso, source, NewStringType::kNormal); - MaybeLocal maybeOgn = - String::NewFromUtf8(iso, origin, NewStringType::kNormal); - Local src, ogn; - if (maybeSrc.ToLocal(&src) && maybeOgn.ToLocal(&ogn)) { - String::Utf8Value value(iso, src); - ScriptOrigin script_origin(ogn); - Local