Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use return tuple so that we can provide error messages
- Loading branch information
Showing
7 changed files
with
125 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,55 @@ | ||
package v8go | ||
|
||
// #import <stdlib.h> | ||
// #import "v8go.h" | ||
import "C" | ||
import "runtime" | ||
import ( | ||
"errors" | ||
"runtime" | ||
"unsafe" | ||
) | ||
|
||
type Context struct { | ||
ptr C.ContextPtr | ||
} | ||
|
||
func NewContext(iso *Isolate) *Context { | ||
ctx := &Context{C.NewContext(iso.ptr)} | ||
runtime.SetFinalizer(ctx, (*Context).release) | ||
runtime.SetFinalizer(ctx, (*Context).finalizer) | ||
return ctx | ||
} | ||
|
||
func (c *Context) RunScript(source string) (*Value, error) { | ||
valuePtr := C.RunScript(c.ptr, C.CString(source), C.CString("origin")) | ||
return &Value{valuePtr}, nil | ||
// RunScript executes the source JavaScript; origin or filename provides a | ||
// reference for the script and used in the exception stack trace. | ||
func (c *Context) RunScript(source string, origin string) (*Value, error) { | ||
cSource := C.CString(source) | ||
cOrigin := C.CString(origin) | ||
defer C.free(unsafe.Pointer(cSource)) | ||
defer C.free(unsafe.Pointer(cOrigin)) | ||
|
||
rtn := C.RunScript(c.ptr, cSource, cOrigin) | ||
return getValue(rtn), getError(rtn) | ||
} | ||
|
||
func (c *Context) release() { | ||
//TODO dispose of object in C++A | ||
func (c *Context) finalizer() { | ||
C.ContextDispose(c.ptr) | ||
c.ptr = nil | ||
runtime.SetFinalizer(c, nil) | ||
} | ||
|
||
func getValue(rtn C.RtnValue) *Value { | ||
if rtn.value == nil { | ||
return nil | ||
} | ||
v := &Value{rtn.value} | ||
runtime.SetFinalizer(v, (*Value).finalizer) | ||
return v | ||
} | ||
|
||
func getError(rtn C.RtnValue) error { | ||
if rtn.error == nil { | ||
return nil | ||
} | ||
defer C.free(unsafe.Pointer(rtn.error)) | ||
return errors.New(C.GoString(rtn.error)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package v8go_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"rogchap.com/v8go" | ||
) | ||
|
||
func TestRunScriptStringer(t *testing.T) { | ||
t.Parallel() | ||
var iso = v8go.NewIsolate() | ||
ctx := v8go.NewContext(iso) | ||
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, "test.js") | ||
str := result.String() | ||
if str != tt.out { | ||
t.Errorf("unespected result: expected %q, got %q", tt.out, str) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters