Skip to content

Commit

Permalink
Recommend importing as v8 instead of the default of v8go (rogchap#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanahsmith authored Sep 19, 2021
1 parent 9ecb72f commit f550375
Show file tree
Hide file tree
Showing 12 changed files with 312 additions and 312 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
## Usage

```go
import "rogchap.com/v8go"
import v8 "rogchap.com/v8go"
```

### Running a script

```go
ctx := v8go.NewContext() // creates a new V8 context with a new Isolate aka VM
ctx := v8.NewContext() // creates a new V8 context with a new Isolate aka VM
ctx.RunScript("const add = (a, b) => a + b", "math.js") // executes a script on the global context
ctx.RunScript("const result = add(3, 4)", "main.js") // any functions previously added to the context can be called
val, _ := ctx.RunScript("result", "value.js") // return a value in JavaScript back to Go
Expand All @@ -30,11 +30,11 @@ fmt.Printf("addition result: %s", val)
### One VM, many contexts

```go
iso := v8go.NewIsolate() // creates a new JavaScript VM
ctx1 := v8go.NewContext(iso) // new context within the VM
iso := v8.NewIsolate() // creates a new JavaScript VM
ctx1 := v8.NewContext(iso) // new context within the VM
ctx1.RunScript("const multiply = (a, b) => a * b", "math.js")

ctx2 := v8go.NewContext(iso) // another context on the same VM
ctx2 := v8.NewContext(iso) // another context on the same VM
if _, err := ctx2.RunScript("multiply(3, 4)", "main.js"); err != nil {
// this will error as multiply is not defined in this context
}
Expand All @@ -43,22 +43,22 @@ if _, err := ctx2.RunScript("multiply(3, 4)", "main.js"); err != nil {
### JavaScript function with Go callback

```go
iso := v8go.NewIsolate() // create a new VM
iso := v8.NewIsolate() // create a new VM
// a template that represents a JS function
printfn := v8go.NewFunctionTemplate(iso, func(info *v8go.FunctionCallbackInfo) *v8go.Value {
printfn := v8.NewFunctionTemplate(iso, func(info *v8.FunctionCallbackInfo) *v8.Value {
fmt.Printf("%v", info.Args()) // when the JS function is called this Go callback will execute
return nil // you can return a value back to the JS caller if required
})
global := v8go.NewObjectTemplate(iso) // a template that represents a JS Object
global := v8.NewObjectTemplate(iso) // a template that represents a JS Object
global.Set("print", printfn) // sets the "print" property of the Object to our function
ctx := v8go.NewContext(iso, global) // new Context with the global Object set to our object template
ctx := v8.NewContext(iso, global) // new Context with the global Object set to our object template
ctx.RunScript("print('foo')", "print.js") // will execute the Go callback with a single argunent 'foo'
```

### Update a JavaScript object from Go

```go
ctx := v8go.NewContext() // new context with a default VM
ctx := v8.NewContext() // new context with a default VM
obj := ctx.Global() // get the global object from the context
obj.Set("version", "v1.0.0") // set the property "version" on the object
val, _ := ctx.RunScript("version", "version.js") // global object will have the property set within the JS VM
Expand All @@ -74,7 +74,7 @@ if obj.Has("version") { // check if a property exists on the object
```go
val, err := ctx.RunScript(src, filename)
if err != nil {
e := err.(*v8go.JSError) // JavaScript errors will be returned as the JSError struct
e := err.(*v8.JSError) // JavaScript errors will be returned as the JSError struct
fmt.Println(e.Message) // the message of the exception thrown
fmt.Println(e.Location) // the filename, line number and the column where the error occured
fmt.Println(e.StackTrace) // the full stack trace of the error, if available
Expand All @@ -87,7 +87,7 @@ if err != nil {
### Terminate long running scripts

```go
vals := make(chan *v8go.Value, 1)
vals := make(chan *v8.Value, 1)
errs := make(chan error, 1)

go func() {
Expand Down
38 changes: 19 additions & 19 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (
"fmt"
"testing"

"rogchap.com/v8go"
v8 "rogchap.com/v8go"
)

func TestContextExec(t *testing.T) {
t.Parallel()
ctx := v8go.NewContext(nil)
ctx := v8.NewContext(nil)
defer ctx.Isolate().Dispose()
defer ctx.Close()

Expand All @@ -31,7 +31,7 @@ func TestContextExec(t *testing.T) {
}

iso := ctx.Isolate()
ctx2 := v8go.NewContext(iso)
ctx2 := v8.NewContext(iso)
_, err = ctx2.RunScript(`add`, "ctx2.js")
if err == nil {
t.Error("error expected but was <nil>")
Expand All @@ -51,7 +51,7 @@ func TestJSExceptions(t *testing.T) {
{"ReferenceError", "add()", "add.js", "ReferenceError: add is not defined"},
}

ctx := v8go.NewContext(nil)
ctx := v8.NewContext(nil)
defer ctx.Isolate().Dispose()
defer ctx.Close()

Expand All @@ -73,19 +73,19 @@ func TestJSExceptions(t *testing.T) {
func TestContextRegistry(t *testing.T) {
t.Parallel()

ctx := v8go.NewContext()
ctx := v8.NewContext()
defer ctx.Isolate().Dispose()
defer ctx.Close()

ctxref := ctx.Ref()

c1 := v8go.GetContext(ctxref)
c1 := v8.GetContext(ctxref)
if c1 != nil {
t.Error("expected context to be <nil>")
}

ctx.Register()
c2 := v8go.GetContext(ctxref)
c2 := v8.GetContext(ctxref)
if c2 == nil {
t.Error("expected context, but got <nil>")
}
Expand All @@ -94,7 +94,7 @@ func TestContextRegistry(t *testing.T) {
}
ctx.Deregister()

c3 := v8go.GetContext(ctxref)
c3 := v8.GetContext(ctxref)
if c3 != nil {
t.Error("expected context to be <nil>")
}
Expand All @@ -103,11 +103,11 @@ func TestContextRegistry(t *testing.T) {
func TestMemoryLeak(t *testing.T) {
t.Parallel()

iso := v8go.NewIsolate()
iso := v8.NewIsolate()
defer iso.Dispose()

for i := 0; i < 6000; i++ {
ctx := v8go.NewContext(iso)
ctx := v8.NewContext(iso)
obj := ctx.Global()
_ = obj.String()
_, _ = ctx.RunScript("2", "")
Expand All @@ -120,10 +120,10 @@ func TestMemoryLeak(t *testing.T) {

func BenchmarkContext(b *testing.B) {
b.ReportAllocs()
iso := v8go.NewIsolate()
iso := v8.NewIsolate()
defer iso.Dispose()
for n := 0; n < b.N; n++ {
ctx := v8go.NewContext(iso)
ctx := v8.NewContext(iso)
ctx.RunScript(script, "main.js")
str, _ := json.Marshal(makeObject())
cmd := fmt.Sprintf("process(%s)", str)
Expand All @@ -133,7 +133,7 @@ func BenchmarkContext(b *testing.B) {
}

func ExampleContext() {
ctx := v8go.NewContext()
ctx := v8.NewContext()
defer ctx.Isolate().Dispose()
defer ctx.Close()
ctx.RunScript("const add = (a, b) => a + b", "math.js")
Expand All @@ -145,15 +145,15 @@ func ExampleContext() {
}

func ExampleContext_isolate() {
iso := v8go.NewIsolate()
iso := v8.NewIsolate()
defer iso.Dispose()
ctx1 := v8go.NewContext(iso)
ctx1 := v8.NewContext(iso)
defer ctx1.Close()
ctx1.RunScript("const foo = 'bar'", "context_one.js")
val, _ := ctx1.RunScript("foo", "foo.js")
fmt.Println(val)

ctx2 := v8go.NewContext(iso)
ctx2 := v8.NewContext(iso)
defer ctx2.Close()
_, err := ctx2.RunScript("foo", "context_two.js")
fmt.Println(err)
Expand All @@ -163,11 +163,11 @@ func ExampleContext_isolate() {
}

func ExampleContext_globalTemplate() {
iso := v8go.NewIsolate()
iso := v8.NewIsolate()
defer iso.Dispose()
obj := v8go.NewObjectTemplate(iso)
obj := v8.NewObjectTemplate(iso)
obj.Set("version", "v1.0.0")
ctx := v8go.NewContext(iso, obj)
ctx := v8.NewContext(iso, obj)
defer ctx.Close()
val, _ := ctx.RunScript("version", "main.js")
fmt.Println(val)
Expand Down
10 changes: 5 additions & 5 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"fmt"
"testing"

"rogchap.com/v8go"
v8 "rogchap.com/v8go"
)

func TestErrorFormatting(t *testing.T) {
Expand All @@ -21,8 +21,8 @@ func TestErrorFormatting(t *testing.T) {
stringVerb string
quoteVerb string
}{
{"WithStack", &v8go.JSError{Message: "msg", StackTrace: "stack"}, "msg", "stack", "msg", `"msg"`},
{"WithoutStack", &v8go.JSError{Message: "msg"}, "msg", "msg", "msg", `"msg"`},
{"WithStack", &v8.JSError{Message: "msg", StackTrace: "stack"}, "msg", "stack", "msg", `"msg"`},
{"WithoutStack", &v8.JSError{Message: "msg"}, "msg", "msg", "msg", `"msg"`},
}

for _, tt := range tests {
Expand All @@ -47,7 +47,7 @@ func TestErrorFormatting(t *testing.T) {

func TestJSErrorOutput(t *testing.T) {
t.Parallel()
ctx := v8go.NewContext(nil)
ctx := v8.NewContext(nil)
defer ctx.Isolate().Dispose()
defer ctx.Close()

Expand All @@ -72,7 +72,7 @@ func TestJSErrorOutput(t *testing.T) {
t.Error("expected error but got <nil>")
return
}
e, ok := err.(*v8go.JSError)
e, ok := err.(*v8.JSError)
if !ok {
t.Errorf("expected error of type JSError, got %T", err)
}
Expand Down
Loading

0 comments on commit f550375

Please sign in to comment.