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

Commit

Permalink
Wrap snapshot creator + snapshot creation otherwise the destructor is
Browse files Browse the repository at this point in the history
called and we lose the creator on return

Latest error:
```
==== C stack trace ===============================

    0   v8go.test                           0x000000000429d2b3 v8::base::debug::StackTrace::StackTrace() + 19
    1   v8go.test                           0x00000000042a55bb v8::platform::(anonymous namespace)::PrintStackTrace() + 59
    2   v8go.test                           0x00000000042904a1 V8_Fatal(char const*, int, char const*, ...) + 337
    3   v8go.test                           0x0000000004d4539a v8::internal::ReadOnlySerializer::SerializeReadOnlyRoots() + 282
    4   v8go.test                           0x0000000004d62fed v8::internal::Snapshot::Create(v8::internal::Isolate*, std::__1::vector<v8::internal::Context, std::__1::allocator<v8::internal::Context> >*, std::__1::vector<v8::SerializeInternalFieldsCallback, std::__1::allocator<v8::SerializeInternalFieldsCallback> > const&, v8::internal::CombinationAssertScope<v8::internal::PerThreadAssertScopeDebugOnly<(v8::internal::PerThreadAssertType)0, false>, v8::internal::PerThreadAssertScopeDebugOnly<(v8::internal::PerThreadAssertType)1, false> > const&, v8::base::Flags<v8::internal::Snapshot::SerializerFlag, int>) + 253
    5   v8go.test                           0x00000000042b7b23 v8::SnapshotCreator::CreateBlob(v8::SnapshotCreator::FunctionCodeHandling) + 3427
    6   v8go.test                           0x0000000004279c56 CreateSnapshot + 278
    7   v8go.test                           0x0000000004277cd1 _cgo_6c7faf9e9e5b_Cfunc_CreateSnapshot + 33
    8   v8go.test                           0x000000000406a850 runtime.asmcgocall.abi0 + 112
```
  • Loading branch information
Genevieve L'Esperance authored and GustavoCaso committed Jan 12, 2022
1 parent 7d48015 commit 718b639
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 17 deletions.
36 changes: 29 additions & 7 deletions snapshot_creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,48 @@ package v8go
// #include <stdlib.h>
// #include "v8go.h"
import "C"
import "unsafe"

func CreateSnapshot(source, origin string) *StartupData {
cSource := C.CString(source)
cOrigin := C.CString(origin)
defer C.free(unsafe.Pointer(cSource))
defer C.free(unsafe.Pointer(cOrigin))

ptr := C.CreateSnapshot(cSource, cOrigin)
return &StartupData{ptr: ptr}
}

type SnapshotCreator struct {
ptr C.SnapshotCreatorPtr
iso *Isolate
}

func NewSnapshotCreator(iso *Isolate) *SnapshotCreator {
ptr := C.NewSnapshotCreator()
func NewSnapshotCreator() *SnapshotCreator {
v8once.Do(func() {
C.Init()
})

wrap := C.NewSnapshotCreator()

iso := &Isolate{
ptr: wrap.iso,
cbs: make(map[int]FunctionCallback),
}
iso.null = newValueNull(iso)
iso.undefined = newValueUndefined(iso)

return &SnapshotCreator{
ptr: ptr,
ptr: wrap.ptr,
iso: iso,
}
}

// TODO: Delete snapshot creator will delete associated iso too

// func (s *SnapshotCreator) GetIsolate() *Isolate {
// isoptr := C.SnapshotCreatorGetIsolate(s.ptr)
// return s.iso
// }
func (s *SnapshotCreator) GetIsolate() *Isolate {
return s.iso
}

type FunctionCodeHandling string

Expand Down
21 changes: 18 additions & 3 deletions snapshot_creator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,31 @@ import (
v8 "rogchap.com/v8go"
)

func TestCreateSnapshot(t *testing.T) {
// TODO: This is needed to run C.Init()
iso := v8.NewIsolate()
defer iso.Dispose()

data := v8.CreateSnapshot("function run() { return 1 };", "script.js")

iso2 := v8.NewIsolateWithCreateParams(v8.CreateParams{SnapshotBlob: data})
ctx2 := v8.NewContext(iso2)
val, err := ctx2.RunScript("run()", "script.js")
fatalIf(t, err)
if val.String() != "1" {
t.Fatal("invalid val")
}
}

func TestSnapshotCreator(t *testing.T) {
t.Parallel()

iso1 := v8.NewIsolate()
snapshotCreator := v8.NewSnapshotCreator()
iso1 := snapshotCreator.GetIsolate()
ctx1 := v8.NewContext(iso1)
_, err := ctx1.RunScript("function run() { return 1 };", "script.js")
fatalIf(t, err)

snapshotCreator := v8.NewSnapshotCreator(iso1)

data := snapshotCreator.CreateBlob(v8.FunctionCodeHandlingKeep)

iso2 := v8.NewIsolateWithCreateParams(v8.CreateParams{SnapshotBlob: data})
Expand Down
45 changes: 39 additions & 6 deletions v8go.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,48 @@ extern "C" {

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

SnapshotCreator* NewSnapshotCreator() {
v8::SnapshotCreator snapshot_creator;
SnapshotCreator* sc = new SnapshotCreator;
sc = &snapshot_creator;
StartupData* CreateSnapshot(const char* source, const char* origin) {
SnapshotCreator creator;
Isolate* iso = creator.GetIsolate();

HandleScope handle_scope(iso);
Local<Context> ctx = Context::New(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)) {
// TODO
}

ScriptOrigin script_origin(ogn);
Local<Script> script;
if (!Script::Compile(ctx, src, &script_origin).ToLocal(&script)) {
// TODO
}
script->Run(ctx);

creator.SetDefaultContext(ctx);

StartupData blob = creator.CreateBlob(v8::SnapshotCreator::FunctionCodeHandling::kKeep);
StartupData* sd = new StartupData;
sd = &blob;
return sd;
}

SnapshotCreatorWrap* NewSnapshotCreator() {
SnapshotCreator snapshot_creator;
SnapshotCreatorWrap* sc = new SnapshotCreatorWrap;
sc->ptr = &snapshot_creator;
sc->iso = snapshot_creator.GetIsolate();
return sc;
}

StartupData* SnapshotCreatorCreateBlob(SnapshotCreator* sc) {
v8::StartupData blob = sc->CreateBlob(v8::SnapshotCreator::FunctionCodeHandling::kClear);
StartupData* SnapshotCreatorCreateBlob(SnapshotCreatorPtr sc) {
StartupData blob = sc->CreateBlob(v8::SnapshotCreator::FunctionCodeHandling::kClear);
StartupData* sd = new StartupData;
sd = &blob;
return sd;
Expand Down
8 changes: 7 additions & 1 deletion v8go.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ typedef struct {
StartupDataPtr startup_data;
} IsolateCreateParams;

typedef struct {
SnapshotCreatorPtr ptr;
IsolatePtr iso;
} SnapshotCreatorWrap;

extern void Init();
extern IsolatePtr NewIsolate();
extern IsolatePtr NewIsolateWithCreateParams(StartupDataPtr ptr);
Expand All @@ -151,7 +156,8 @@ extern void IsolateTerminateExecution(IsolatePtr ptr);
extern int IsolateIsExecutionTerminating(IsolatePtr ptr);
extern IsolateHStatistics IsolationGetHeapStatistics(IsolatePtr ptr);

extern SnapshotCreatorPtr NewSnapshotCreator();
extern StartupDataPtr CreateSnapshot(const char* source, const char* origin);
extern SnapshotCreatorWrap* NewSnapshotCreator();
extern StartupDataPtr SnapshotCreatorCreateBlob(SnapshotCreatorPtr sc);

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

0 comments on commit 718b639

Please sign in to comment.