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

Commit

Permalink
Work on the new API design. Make it working with passing custom conte…
Browse files Browse the repository at this point in the history
…xt to CreateBlob
  • Loading branch information
GustavoCaso committed Jan 19, 2022
1 parent d611464 commit 7c3a420
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 114 deletions.
30 changes: 17 additions & 13 deletions snapshot_creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package v8go
import "C"
import (
"errors"
"unsafe"
)

type FunctionCodeHandling int
Expand All @@ -32,6 +31,8 @@ func (s *StartupData) Dispose() {

type SnapshotCreator struct {
ptr C.SnapshotCreatorPtr
iso *Isolate
ctx *Context
index C.size_t
}

Expand All @@ -40,24 +41,25 @@ 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)
func (s *SnapshotCreator) GetIsolate() *Isolate {
return s.iso
}

if rtn.error.msg != nil {
return newJSError(rtn.error)
func (s *SnapshotCreator) AddContext(ctx *Context) error {
if s.ptr == nil {
return errors.New("v8go: Cannot add context to snapshot creator after creating the blob")
}

s.index = rtn.index
s.index = C.AddContext(s.ptr, ctx.ptr)
s.ctx = ctx

return nil
}
Expand All @@ -67,9 +69,11 @@ 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
s.iso.ptr = nil

return &StartupData{ptr: rtn, index: s.index}, nil
}
Expand Down
65 changes: 18 additions & 47 deletions snapshot_creator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ import (

func TestCreateSnapshot(t *testing.T) {
snapshotCreator := v8.NewSnapshotCreator()
err := snapshotCreator.AddContext("function run() { return 1 };", "script.js")
snapshotCreatorIso := snapshotCreator.GetIsolate()
snapshotCreatoCtx := v8.NewContext(snapshotCreatorIso)
defer snapshotCreatoCtx.Close()

snapshotCreatoCtx.RunScript(`const add = (a, b) => a + b`, "add.js")
snapshotCreatoCtx.RunScript(`function run() { return add(3, 4); }`, "main.js")
err := snapshotCreator.AddContext(snapshotCreatoCtx)
fatalIf(t, err)

data, err := snapshotCreator.Create(v8.FunctionCodeHandlingKlear)
Expand All @@ -38,68 +44,33 @@ func TestCreateSnapshot(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()
snapshotCreatorIso := snapshotCreator.GetIsolate()
snapshotCreatoCtx := v8.NewContext(snapshotCreatorIso)
defer snapshotCreatoCtx.Close()

err := snapshotCreator.AddContext("function run() { return 1 };", "script.js")
snapshotCreatoCtx.RunScript(`const add = (a, b) => a + b`, "add.js")
snapshotCreatoCtx.RunScript(`function run() { return add(3, 4); }`, "main.js")
err := snapshotCreator.AddContext(snapshotCreatoCtx)
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")
err = snapshotCreator.AddContext(snapshotCreatoCtx)
if err == nil {
t.Error("add context should have fail")
t.Error("Adding context should have fail")
}
}

func TestCreateSnapshotFailAndReuse(t *testing.T) {
snapshotCreator := v8.NewSnapshotCreator()
err := snapshotCreator.AddContext("feuihyvfeuyfeu", "script.js")
_, err = snapshotCreator.Create(v8.FunctionCodeHandlingKlear)
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")
t.Error("Creating snapshot should have fail")
}
}
64 changes: 17 additions & 47 deletions v8go.cc
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,8 @@ RtnUnboundScript IsolateCompileUnboundScript(IsolatePtr iso,

/********** SnapshotCreator **********/

SnapshotCreatorPtr NewSnapshotCreator() {
RtnSnapshotCreator NewSnapshotCreator() {
RtnSnapshotCreator rtn = {};
SnapshotCreator* creator = new SnapshotCreator;
Isolate* iso = creator->GetIsolate();
{
Expand All @@ -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<Context> ctx = Context::New(iso);
TryCatch try_catch(iso);
Context::Scope context_scope(ctx);

MaybeLocal<String> maybeSrc =
String::NewFromUtf8(iso, source, NewStringType::kNormal);
MaybeLocal<String> maybeOgn =
String::NewFromUtf8(iso, origin, NewStringType::kNormal);
Local<String> src, ogn;
if (maybeSrc.ToLocal(&src) && maybeOgn.ToLocal(&ogn)) {
String::Utf8Value value(iso, src);
ScriptOrigin script_origin(ogn);
Local<Script> script;
if (!Script::Compile(ctx, src, &script_origin).ToLocal(&script)) {
fail = true;
}

Local<Value> result;
if (!script->Run(ctx).ToLocal(&result)) {
fail = true;
}
} else {
fail = true;
}

if (fail) {
rtn.error = ExceptionError(try_catch, iso, ctx);
return rtn;
}

index = snapshotCreator->AddContext(ctx);
}
rtn.index = index;
return rtn;
size_t AddContext(SnapshotCreatorPtr snapshotCreator, ContextPtr ctx) {
Isolate* iso = ctx->iso;
Locker locker(iso);
Isolate::Scope isolate_scope(iso);
HandleScope handle_scope(iso);
Local<Context> local_ctx = ctx->ptr.Get(iso);
Context::Scope context_scope(local_ctx);
return snapshotCreator->AddContext(local_ctx);
;
}

SnapshotBlob* CreateBlob(SnapshotCreatorPtr snapshotCreator,
ContextPtr ctx,
int function_code_handling) {
// CreateBlob cannot be called within a HandleScope
ContextFree(ctx);
// kKeep - keeps any compiled functions
// kClear - does not keep any compiled functions
StartupData startup_data = snapshotCreator->CreateBlob(
Expand Down
13 changes: 6 additions & 7 deletions v8go.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ typedef struct {
} SnapshotBlob;

typedef struct {
size_t index;
RtnError error;
} RtnSnapshotAddContext;
SnapshotCreatorPtr creator;
IsolatePtr iso;
} RtnSnapshotCreator;

typedef struct {
ScriptCompilerCachedData cachedData;
Expand Down Expand Up @@ -154,12 +154,11 @@ extern int IsolateIsExecutionTerminating(IsolatePtr ptr);
extern IsolateHStatistics IsolationGetHeapStatistics(IsolatePtr ptr);

extern void SnapshotBlobDelete(SnapshotBlob* ptr);
extern SnapshotCreatorPtr NewSnapshotCreator();
extern RtnSnapshotCreator NewSnapshotCreator();
extern void DeleteSnapshotCreator(SnapshotCreatorPtr snapshotCreator);
extern RtnSnapshotAddContext AddContext(SnapshotCreatorPtr snapshotCreator,
const char* source,
const char* origin);
extern size_t AddContext(SnapshotCreatorPtr snapshotCreator, ContextPtr ctx);
extern SnapshotBlob* CreateBlob(SnapshotCreatorPtr snapshotCreator,
ContextPtr ctx,
int function_code_handling);

extern ValuePtr IsolateThrowException(IsolatePtr iso, ValuePtr value);
Expand Down

0 comments on commit 7c3a420

Please sign in to comment.