Skip to content

Commit

Permalink
setup of context and run script apis
Browse files Browse the repository at this point in the history
  • Loading branch information
rogchap committed Aug 30, 2019
1 parent 9565f16 commit 089e7ce
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 51 deletions.
4 changes: 4 additions & 0 deletions .ycm_extra_conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def Settings( **kwargs ):
return {
'flags': [ '-fno-rtti', '-fpic', '-std=c++11', '-Ideps/include', '-pthread', '-lv8', '-Ldeps/darwin-x86_64' ],
}
25 changes: 25 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
@@ -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)
}
8 changes: 4 additions & 4 deletions deps/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
12 changes: 6 additions & 6 deletions isolate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
}
9 changes: 0 additions & 9 deletions testcpp.cc

This file was deleted.

89 changes: 62 additions & 27 deletions v8go.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 5 additions & 1 deletion v8go.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
25 changes: 21 additions & 4 deletions v8go_test.go
Original file line number Diff line number Diff line change
@@ -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.