@@ -20,7 +20,7 @@ import "rogchap.com/v8go"
2020### Running a script
2121
2222``` go
23- ctx , _ := v8go.NewContext (nil ) // creates a new V8 context with a new Isolate aka VM
23+ ctx , _ := v8go.NewContext () // creates a new V8 context with a new Isolate aka VM
2424ctx.RunScript (" const add = (a, b) => a + b" , " math.js" ) // executes a script on the global context
2525ctx.RunScript (" const result = add(3, 4)" , " main.js" ) // any functions previously added to the context can be called
2626val , _ := ctx.RunScript (" result" , " value.js" ) // return a value in JavaScript back to Go
@@ -30,16 +30,45 @@ fmt.Printf("addition result: %s", val)
3030### One VM, many contexts
3131
3232``` go
33- vm , _ := v8go.NewIsolate () // creates a new JavaScript VM
34- ctx1 , _ := v8go.NewContext (vm ) // new context within the VM
33+ iso , _ := v8go.NewIsolate () // creates a new JavaScript VM
34+ ctx1 , _ := v8go.NewContext (iso ) // new context within the VM
3535ctx1.RunScript (" const multiply = (a, b) => a * b" , " math.js" )
3636
37- ctx2 , _ := v8go.NewContext (vm ) // another context on the same VM
37+ ctx2 , _ := v8go.NewContext (iso ) // another context on the same VM
3838if _ , err := ctx2.RunScript (" multiply(3, 4)" , " main.js" ); err != nil {
3939 // this will error as multiply is not defined in this context
4040}
4141```
4242
43+ ### JavaScript function with Go callback
44+
45+ ``` go
46+ iso , _ := v8go.NewIsolate () // create a new VM
47+ // a template that represents a JS function
48+ printfn , _ := v8go.NewFunctionTemplate (iso, func (info *v8go.FunctionCallbackInfo ) *v8go.Value {
49+ fmt.Printf (" %v " , info.Args ()) // when the JS function is called this Go callback will execute
50+ return nil // you can return a value back to the JS caller if required
51+ })
52+ global , _ := v8go.NewObjectTemplate (iso) // a template that represents a JS Object
53+ 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
55+ ctx.RunScript (" print('foo')" , " print.js" ) // will execute the Go callback with a single argunent 'foo'
56+ ```
57+
58+ ### Update a JavaScript object from Go
59+
60+ ``` go
61+ ctx , _ := v8go.NewContext () // new context with a default VM
62+ obj := ctx.Global () // get the global object from the context
63+ obj.Set (" version" , " v1.0.0" ) // set the property "version" on the object
64+ val , _ := ctx.RunScript (" version" , " version.js" ) // global object will have the property set within the JS VM
65+ fmt.Printf (" version: %s " , val)
66+
67+ if obj.Has (" version" ) { // check if a property exists on the object
68+ obj.Delete (" version" ) // remove the property from the object
69+ }
70+ ```
71+
4372### JavaScript errors
4473
4574``` go
@@ -58,7 +87,6 @@ if err != nil {
5887### Terminate long running scripts
5988
6089``` go
61-
6290vals := make (chan *v8go.Value , 1 )
6391errs := make (chan error , 1 )
6492
@@ -85,7 +113,7 @@ case <- time.After(200 * time.Milliseconds):
85113
86114## Documentation
87115
88- Go Reference: https://pkg.go.dev/rogchap.com/v8go
116+ Go Reference & more examples : https://pkg.go.dev/rogchap.com/v8go
89117
90118### Support
91119
0 commit comments