Skip to content

Commit

Permalink
return value type with stringer
Browse files Browse the repository at this point in the history
  • Loading branch information
rogchap committed Aug 31, 2019
1 parent 089e7ce commit 69d20d6
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 23 deletions.
2 changes: 1 addition & 1 deletion cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package v8go

// #include <stdlib.h>
//
// #cgo CXXFLAGS: -fno-rtti -fpic -std=c++11 -I${SRCDIR}/deps/include
// #cgo CXXFLAGS: -fno-rtti -fpic -std=c++14 -I${SRCDIR}/deps/include
// #cgo LDFLAGS: -pthread -lv8
// #cgo darwin LDFLAGS: -L${SRCDIR}/deps/darwin-x86_64
import "C"
5 changes: 3 additions & 2 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ func NewContext(iso *Isolate) *Context {
return ctx
}

func (c *Context) RunScript() {
C.RunScript(c.ptr, C.CString("source"), C.CString("origin"))
func (c *Context) RunScript(source string) (*Value, error) {
valuePtr := C.RunScript(c.ptr, C.CString(source), C.CString("origin"))
return &Value{valuePtr}, nil
}

func (c *Context) release() {
Expand Down
2 changes: 1 addition & 1 deletion deps/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
v8_enable_test_features=false
v8_extra_library_files=[]
v8_untrusted_code_mitigations=false
v8_use_snapshot=false
v8_use_snapshot=true
"""

def v8deps():
Expand Down
78 changes: 64 additions & 14 deletions v8go.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,19 @@ using namespace v8;
auto default_platform = platform::NewDefaultPlatform();
auto default_allocator = ArrayBuffer::Allocator::NewDefaultAllocator();

extern "C" {
typedef struct {
Persistent<Context> ptr;
Isolate* iso;
} m_ctx;

typedef struct {
Persistent<Value> ptr;
m_ctx* ctx_ptr;
} m_value;


extern "C"
{

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

Expand All @@ -27,7 +39,6 @@ void Init() {
IsolatePtr NewIsolate() {
Isolate::CreateParams params;
params.array_buffer_allocator = default_allocator;
Isolate::New(params);
return static_cast<IsolatePtr>(Isolate::New(params));
}

Expand All @@ -51,24 +62,62 @@ ContextPtr NewContext(IsolatePtr 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));

m_ctx* ctx = new m_ctx;
ctx->ptr.Reset(iso, Context::New(iso));
ctx->iso = iso;
return static_cast<ContextPtr>(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();
ValuePtr RunScript(ContextPtr ctx_ptr, const char* source, const char* origin) {
m_ctx* ctx = static_cast<m_ctx*>(ctx_ptr);
Isolate* iso = ctx->iso;
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);
Local<Context> local_ctx = ctx->ptr.Get(iso);
Context::Scope context_scope(local_ctx);

Local<String> src = String::NewFromUtf8(iso, source, NewStringType::kNormal).ToLocalChecked();
Local<String> ogn = String::NewFromUtf8(iso, origin, NewStringType::kNormal).ToLocalChecked();

ScriptOrigin script_origin(ogn);
Local<Script> script = Script::Compile(local_ctx, src, &script_origin).ToLocalChecked();

// TODO check to make sure script is not empty
Local<v8::Value> result = script->Run(local_ctx).ToLocalChecked();

// TODO deal with any errors/exceptions from result
m_value* val = new m_value;
val->ctx_ptr = ctx;
val->ptr.Reset(iso, Persistent<Value>(iso, result));

return static_cast<ValuePtr>(val);
}

/********** Value **********/

const char* ValueToString(ValuePtr val_ptr) {
m_value* val = static_cast<m_value*>(val_ptr);
m_ctx* ctx = val->ctx_ptr;
Isolate* iso = ctx->iso;

Locker locker(iso);
Isolate::Scope isolate_scope(iso);
HandleScope handle_scope(iso);
Context::Scope context_scope(ctx->ptr.Get(iso));

Local<Value> value = val->ptr.Get(iso);
String::Utf8Value utf8(iso, value);

char* data = static_cast<char*>(malloc(utf8.length()));
//memcpy(data, *utf8, utf8.length());
sprintf(data, "%s", *utf8);
return data;
}


Expand All @@ -81,9 +130,10 @@ const char* Version() {
}


int main(int argc, char* argv[]) {
int _main(int argc, char* argv[]) {
Init();
auto i = NewIsolate();
auto c = NewContext(i);
RunScript(c, "", "");
RunScript(c, "18 + 17", "");
return 0;
}
5 changes: 4 additions & 1 deletion v8go.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ extern "C" {

typedef void* IsolatePtr;
typedef void* ContextPtr;
typedef void* ValuePtr;

extern void Init();
extern IsolatePtr NewIsolate();
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);
extern ValuePtr RunScript(ContextPtr ctx_ptr, const char* source, const char* origin);

const char* ValueToString(ValuePtr val_ptr);

const char* Version();

Expand Down
30 changes: 26 additions & 4 deletions v8go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"rogchap.com/v8go"
)

var iso = v8go.NewIsolate()

func TestVersion(t *testing.T) {
t.Parallel()
rgx := regexp.MustCompile(`^\d+\.\d+\.\d+\.\d+-v8go$`)
Expand All @@ -16,10 +18,30 @@ func TestVersion(t *testing.T) {
}
}

func TestRunScript(t *testing.T) {
func TestRunScriptStringer(t *testing.T) {
t.Parallel()
iso := v8go.NewIsolate()
ctx := v8go.NewContext(iso)
ctx.RunScript()
t.Error("See output")
var tests = [...]struct {
name string
source string
out string
}{
{"Addition", "2 + 2", "4"},
{"Multiplication", "13 * 2", "26"},
{"String", `"string"`, "string"},
{"Object", `let obj = {}; obj`, "[object Object]"},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result, _ := ctx.RunScript(tt.source)
str := result.String()
if str != tt.out {
t.Errorf("unespected result: expected %q, got %q", tt.out, str)
}
})
}

}
12 changes: 12 additions & 0 deletions value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package v8go

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

type Value struct {
ptr C.ValuePtr
}

func (v *Value) String() string {
return C.GoString(C.ValueToString(v.ptr))
}

0 comments on commit 69d20d6

Please sign in to comment.