Skip to content
Permalink
Browse files

add module and base of create isolate

  • Loading branch information...
rogchap committed Aug 29, 2019
1 parent 5da33db commit 9565f1625fd3c01d934dea649ae7d4afd565e3c6
Showing with 94 additions and 5 deletions.
  1. +3 −0 go.mod
  2. +30 −0 isolate.go
  3. +9 −0 testcpp.cc
  4. +43 −3 v8go.cc
  5. +1 −1 v8go.go
  6. +8 −1 v8go.h
3 go.mod
@@ -0,0 +1,3 @@
module rogchap.com/v8go

go 1.12
@@ -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
}
@@ -0,0 +1,9 @@

#include "v8go.h"

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

NewIsolate();
Version();
return 0;
}
46 v8go.cc
@@ -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();
}

}
@@ -7,5 +7,5 @@ package v8go
import "C"

func Version() string {
return C.GoString(C.version())
return C.GoString(C.Version())
}
9 v8go.h
@@ -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"

0 comments on commit 9565f16

Please sign in to comment.
You can’t perform that action at this time.