Skip to content

Commit

Permalink
Add some usage examples to the readme
Browse files Browse the repository at this point in the history
  • Loading branch information
rogchap committed Sep 2, 2019
1 parent 9c123bd commit fc5d9f8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
43 changes: 41 additions & 2 deletions README.md
Expand Up @@ -8,8 +8,47 @@ V8 version: 7.6.303.31
import "rogchap.com/v8go" import "rogchap.com/v8go"
``` ```


## V8 Dependancy ### Running a script

```go
ctx, _ := v8go.NewContext(nil) // 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
fmt.Printf("addition result: %s", val)
```

### One VM, many contexts

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

ctx2, _ := v8go.NewContext(vm) // 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
}
```

### Javascript errors

```go
val, err := ctx.RunScript(src, filename)
if err != nil {
err = err.(v8go.JSError) // JavaScript errors will be returned as the JSError struct
fmt.Println(err.Message) // the message of the exception thrown
fmt.Println(err.Location) // the filename, line number and the column where the error occured
fmt.Println(err.StackTrace) // the full stack trace of the error, if available

fmt.Printf("javascript error: %v", err) // will format the standard error message
fmt.Printf("javascript stack trace: %+v", err) // will format the full error stack trace
}
```


## V8 dependancy
In order to make `v8go` usable as a standard Go package, prebuilt static libraries of V8 In order to make `v8go` usable as a standard Go package, prebuilt static libraries of V8
are included for Linux and OSX ie. you should not require to build V8 yourself. are included for Linux and OSX ie. you *should not* require to build V8 yourself.


V8 requires 64-bit, therfore will not work on 32-bit systems. V8 requires 64-bit, therfore will not work on 32-bit systems.
2 changes: 1 addition & 1 deletion context_test.go
Expand Up @@ -9,7 +9,7 @@ import (
func TestContextExec(t *testing.T) { func TestContextExec(t *testing.T) {
t.Parallel() t.Parallel()
ctx, _ := v8go.NewContext(nil) ctx, _ := v8go.NewContext(nil)
ctx.RunScript(`function add(a, b) { return a + b }`, "add.js") ctx.RunScript(`const add = (a, b) => a + b`, "add.js")
val, _ := ctx.RunScript(`add(3, 4)`, "main.js") val, _ := ctx.RunScript(`add(3, 4)`, "main.js")
rtn := val.String() rtn := val.String()
if rtn != "7" { if rtn != "7" {
Expand Down

0 comments on commit fc5d9f8

Please sign in to comment.