Skip to content
Permalink
Browse files

setup of context and run script apis

  • Loading branch information...
rogchap committed Aug 30, 2019
1 parent 9565f16 commit 089e7ce940f2ccbe12df7d017c8a624e72730c4d
Showing with 127 additions and 51 deletions.
  1. +4 −0 .ycm_extra_conf.py
  2. +25 −0 context.go
  3. +4 −4 deps/build.py
  4. +6 −6 isolate.go
  5. +0 −9 testcpp.cc
  6. +62 −27 v8go.cc
  7. +5 −1 v8go.h
  8. +21 −4 v8go_test.go
@@ -0,0 +1,4 @@
def Settings( **kwargs ):
return {
'flags': [ '-fno-rtti', '-fpic', '-std=c++11', '-Ideps/include', '-pthread', '-lv8', '-Ldeps/darwin-x86_64' ],
}
@@ -0,0 +1,25 @@
package v8go

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

type Context struct {
ptr C.ContextPtr
}

func NewContext(iso *Isolate) *Context {
ctx := &Context{C.NewContext(iso.ptr)}
runtime.SetFinalizer(ctx, (*Context).release)
return ctx
}

func (c *Context) RunScript() {
C.RunScript(c.ptr, C.CString("source"), C.CString("origin"))
}

func (c *Context) release() {
//TODO dispose of object in C++A
c.ptr = nil
runtime.SetFinalizer(c, nil)
}
@@ -40,21 +40,21 @@
linux_use_bundled_binutils=false
use_custom_libcxx=false
use_sysroot=false
is_debug=false
symbol_level=0
is_debug=true
symbol_level=1
strip_debug_info=true
is_component_build=false
v8_monolithic=true
v8_static_library=true
v8_use_external_startup_data=false
treat_warnings_as_errors=false
v8_embedder_string="-v8go"
strip_debug_info=true
v8_enable_gdbjit=false
v8_enable_i18n_support=false
v8_enable_test_features=false
v8_extra_library_files=[]
v8_untrusted_code_mitigations=false
v8_use_snapshot=true
v8_use_snapshot=false
"""

def v8deps():
@@ -14,12 +14,6 @@ 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()
@@ -28,3 +22,9 @@ func NewIsolate() *Isolate {
runtime.SetFinalizer(iso, (*Isolate).release)
return iso
}

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

This file was deleted.

89 v8go.cc
@@ -11,44 +11,79 @@

using namespace v8;

auto allocator = ArrayBuffer::Allocator::NewDefaultAllocator();
auto default_platform = platform::NewDefaultPlatform();
auto default_allocator = ArrayBuffer::Allocator::NewDefaultAllocator();

extern "C" {

/********** Isolate **********/

void Init() {
std::unique_ptr<Platform> plt = platform::NewDefaultPlatform();
V8::InitializePlatform(plt.get());
V8::Initialize();
return;
V8::InitializePlatform(default_platform.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();
Isolate::CreateParams params;
params.array_buffer_allocator = default_allocator;
Isolate::New(params);
return static_cast<IsolatePtr>(Isolate::New(params));
}

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

void TerminateExecution(IsolatePtr ptr) {
Isolate* iso = static_cast<Isolate*>(ptr);
iso->TerminateExecution();
}

/********** Context **********/

ContextPtr NewContext(IsolatePtr ptr) {
Isolate* iso = static_cast<Isolate*>(ptr);
Locker locker(iso);
Isolate::Scope isolate_scope(iso);
HandleScope handle_scope(iso);
iso->SetCaptureStackTraceForUncaughtExceptions(true);
Local<Context> ctx = Context::New(iso);
return static_cast<ContextPtr>(std::move(&ctx));
}

void RunScript(ContextPtr ctx_ptr, const char* source, const char* origin) {
Local<Context> ctx = *(static_cast<Local<Context>*>(ctx_ptr));
Isolate* iso = ctx->GetIsolate();
Locker locker(iso);
Isolate::Scope isolate_scope(iso);
HandleScope handle_scope(iso);
TryCatch try_catch(iso);

Local<Script> script = Script::Compile(ctx, String::NewFromUtf8(iso, "1 + 1").ToLocalChecked()).ToLocalChecked();
v8::Local<v8::Value> result = script->Run(ctx).ToLocalChecked();
// Convert the result to an UTF8 string and print it.
v8::String::Utf8Value utf8(iso, result);
printf("%s\n", *utf8);
}


/********** Version **********/

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

}


int main(int argc, char* argv[]) {
Init();
auto i = NewIsolate();
auto c = NewContext(i);
RunScript(c, "", "");
}
6 v8go.h
@@ -9,7 +9,11 @@ typedef void* ContextPtr;

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

extern ContextPtr NewContext(IsolatePtr prt);
extern void RunScript(ContextPtr ctx_ptr, const char* source, const char* origin);

const char* Version();

@@ -1,8 +1,25 @@
package v8go
package v8go_test

import "testing"
import (
"regexp"
"testing"

"rogchap.com/v8go"
)

func TestVersion(t *testing.T) {
print(Version())
t.Fatal("error")
t.Parallel()
rgx := regexp.MustCompile(`^\d+\.\d+\.\d+\.\d+-v8go$`)
v := v8go.Version()
if !rgx.MatchString(v) {
t.Errorf("version string is in the incorrect format: %s", v)
}
}

func TestRunScript(t *testing.T) {
t.Parallel()
iso := v8go.NewIsolate()
ctx := v8go.NewContext(iso)
ctx.RunScript()
t.Error("See output")
}

0 comments on commit 089e7ce

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