Skip to content

Commit f550375

Browse files
authored
Recommend importing as v8 instead of the default of v8go (#169)
1 parent 9ecb72f commit f550375

12 files changed

+312
-312
lines changed

README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
## Usage
1515

1616
```go
17-
import "rogchap.com/v8go"
17+
import v8 "rogchap.com/v8go"
1818
```
1919

2020
### Running a script
2121

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

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

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

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

5858
### Update a JavaScript object from Go
5959

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

8989
```go
90-
vals := make(chan *v8go.Value, 1)
90+
vals := make(chan *v8.Value, 1)
9191
errs := make(chan error, 1)
9292

9393
go func() {

context_test.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import (
99
"fmt"
1010
"testing"
1111

12-
"rogchap.com/v8go"
12+
v8 "rogchap.com/v8go"
1313
)
1414

1515
func TestContextExec(t *testing.T) {
1616
t.Parallel()
17-
ctx := v8go.NewContext(nil)
17+
ctx := v8.NewContext(nil)
1818
defer ctx.Isolate().Dispose()
1919
defer ctx.Close()
2020

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

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

54-
ctx := v8go.NewContext(nil)
54+
ctx := v8.NewContext(nil)
5555
defer ctx.Isolate().Dispose()
5656
defer ctx.Close()
5757

@@ -73,19 +73,19 @@ func TestJSExceptions(t *testing.T) {
7373
func TestContextRegistry(t *testing.T) {
7474
t.Parallel()
7575

76-
ctx := v8go.NewContext()
76+
ctx := v8.NewContext()
7777
defer ctx.Isolate().Dispose()
7878
defer ctx.Close()
7979

8080
ctxref := ctx.Ref()
8181

82-
c1 := v8go.GetContext(ctxref)
82+
c1 := v8.GetContext(ctxref)
8383
if c1 != nil {
8484
t.Error("expected context to be <nil>")
8585
}
8686

8787
ctx.Register()
88-
c2 := v8go.GetContext(ctxref)
88+
c2 := v8.GetContext(ctxref)
8989
if c2 == nil {
9090
t.Error("expected context, but got <nil>")
9191
}
@@ -94,7 +94,7 @@ func TestContextRegistry(t *testing.T) {
9494
}
9595
ctx.Deregister()
9696

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

106-
iso := v8go.NewIsolate()
106+
iso := v8.NewIsolate()
107107
defer iso.Dispose()
108108

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

121121
func BenchmarkContext(b *testing.B) {
122122
b.ReportAllocs()
123-
iso := v8go.NewIsolate()
123+
iso := v8.NewIsolate()
124124
defer iso.Dispose()
125125
for n := 0; n < b.N; n++ {
126-
ctx := v8go.NewContext(iso)
126+
ctx := v8.NewContext(iso)
127127
ctx.RunScript(script, "main.js")
128128
str, _ := json.Marshal(makeObject())
129129
cmd := fmt.Sprintf("process(%s)", str)
@@ -133,7 +133,7 @@ func BenchmarkContext(b *testing.B) {
133133
}
134134

135135
func ExampleContext() {
136-
ctx := v8go.NewContext()
136+
ctx := v8.NewContext()
137137
defer ctx.Isolate().Dispose()
138138
defer ctx.Close()
139139
ctx.RunScript("const add = (a, b) => a + b", "math.js")
@@ -145,15 +145,15 @@ func ExampleContext() {
145145
}
146146

147147
func ExampleContext_isolate() {
148-
iso := v8go.NewIsolate()
148+
iso := v8.NewIsolate()
149149
defer iso.Dispose()
150-
ctx1 := v8go.NewContext(iso)
150+
ctx1 := v8.NewContext(iso)
151151
defer ctx1.Close()
152152
ctx1.RunScript("const foo = 'bar'", "context_one.js")
153153
val, _ := ctx1.RunScript("foo", "foo.js")
154154
fmt.Println(val)
155155

156-
ctx2 := v8go.NewContext(iso)
156+
ctx2 := v8.NewContext(iso)
157157
defer ctx2.Close()
158158
_, err := ctx2.RunScript("foo", "context_two.js")
159159
fmt.Println(err)
@@ -163,11 +163,11 @@ func ExampleContext_isolate() {
163163
}
164164

165165
func ExampleContext_globalTemplate() {
166-
iso := v8go.NewIsolate()
166+
iso := v8.NewIsolate()
167167
defer iso.Dispose()
168-
obj := v8go.NewObjectTemplate(iso)
168+
obj := v8.NewObjectTemplate(iso)
169169
obj.Set("version", "v1.0.0")
170-
ctx := v8go.NewContext(iso, obj)
170+
ctx := v8.NewContext(iso, obj)
171171
defer ctx.Close()
172172
val, _ := ctx.RunScript("version", "main.js")
173173
fmt.Println(val)

errors_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"fmt"
99
"testing"
1010

11-
"rogchap.com/v8go"
11+
v8 "rogchap.com/v8go"
1212
)
1313

1414
func TestErrorFormatting(t *testing.T) {
@@ -21,8 +21,8 @@ func TestErrorFormatting(t *testing.T) {
2121
stringVerb string
2222
quoteVerb string
2323
}{
24-
{"WithStack", &v8go.JSError{Message: "msg", StackTrace: "stack"}, "msg", "stack", "msg", `"msg"`},
25-
{"WithoutStack", &v8go.JSError{Message: "msg"}, "msg", "msg", "msg", `"msg"`},
24+
{"WithStack", &v8.JSError{Message: "msg", StackTrace: "stack"}, "msg", "stack", "msg", `"msg"`},
25+
{"WithoutStack", &v8.JSError{Message: "msg"}, "msg", "msg", "msg", `"msg"`},
2626
}
2727

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

4848
func TestJSErrorOutput(t *testing.T) {
4949
t.Parallel()
50-
ctx := v8go.NewContext(nil)
50+
ctx := v8.NewContext(nil)
5151
defer ctx.Isolate().Dispose()
5252
defer ctx.Close()
5353

@@ -72,7 +72,7 @@ func TestJSErrorOutput(t *testing.T) {
7272
t.Error("expected error but got <nil>")
7373
return
7474
}
75-
e, ok := err.(*v8go.JSError)
75+
e, ok := err.(*v8.JSError)
7676
if !ok {
7777
t.Errorf("expected error of type JSError, got %T", err)
7878
}

0 commit comments

Comments
 (0)