Skip to content

Commit

Permalink
add module and base of create isolate
Browse files Browse the repository at this point in the history
  • Loading branch information
rogchap committed Aug 29, 2019
1 parent 5da33db commit 9565f16
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 5 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module rogchap.com/v8go

go 1.12
30 changes: 30 additions & 0 deletions isolate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package v8go

// #include "v8go.h"
import "C"

import (
"runtime"
"sync"
)

var v8once sync.Once

type Isolate struct {
ptr C.IsolatePtr
}

func (i *Isolate) release() {
C.IsolateRelease(i.ptr)
i.ptr = nil
runtime.SetFinalizer(i, nil)
}

func NewIsolate() *Isolate {
v8once.Do(func() {
C.Init()
})
iso := &Isolate{C.NewIsolate()}
runtime.SetFinalizer(iso, (*Isolate).release)
return iso
}
9 changes: 9 additions & 0 deletions testcpp.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

#include "v8go.h"

int main(int argc, char* argv[]) {

NewIsolate();
Version();
return 0;
}
46 changes: 43 additions & 3 deletions v8go.cc
Original file line number Diff line number Diff line change
@@ -1,13 +1,53 @@
#include "v8go.h"

#include "v8.h"
#include "_cgo_export.h"
#include "libplatform/libplatform.h"

#include <cstdlib>
#include <cstring>
#include <string>
#include <sstream>
#include <stdio.h>

using namespace v8;

auto allocator = ArrayBuffer::Allocator::NewDefaultAllocator();

extern "C" {

const char* version() {
return V8::GetVersion();
void Init() {
std::unique_ptr<Platform> plt = platform::NewDefaultPlatform();
V8::InitializePlatform(plt.get());
V8::Initialize();
return;
}

IsolatePtr NewIsolate() {
// Isolate::CreateParams params;
// params.array_buffer_allocator = allocator;
// Isolate::New(params);
// return static_cast<IsolatePtr>(Isolate::New(params));
v8::Isolate::CreateParams create_params;
create_params.array_buffer_allocator =
v8::ArrayBuffer::Allocator::NewDefaultAllocator();
v8::Isolate* isolate = v8::Isolate::New(create_params);
isolate->Dispose();
v8::V8::Dispose();
v8::V8::ShutdownPlatform();
delete create_params.array_buffer_allocator;
return nullptr;
}

void IsolateRelease(IsolatePtr ptr) {
if (ptr == nullptr) {
return;
}
Isolate* iso = static_cast<Isolate*>(ptr);
iso->Dispose();
}

const char* Version() {
return V8::GetVersion();
}

}
Expand Down
2 changes: 1 addition & 1 deletion v8go.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ package v8go
import "C"

func Version() string {
return C.GoString(C.version())
return C.GoString(C.Version())
}
9 changes: 8 additions & 1 deletion v8go.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@
extern "C" {
#endif

const char* version();
typedef void* IsolatePtr;
typedef void* ContextPtr;

extern void Init();
extern IsolatePtr NewIsolate();
extern void IsolateRelease(IsolatePtr ptr);

const char* Version();

#ifdef __cplusplus
} // extern "C"
Expand Down

0 comments on commit 9565f16

Please sign in to comment.