@@ -20,7 +20,7 @@ import "rogchap.com/v8go"
20
20
### Running a script
21
21
22
22
``` 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
24
24
ctx.RunScript (" const add = (a, b) => a + b" , " math.js" ) // executes a script on the global context
25
25
ctx.RunScript (" const result = add(3, 4)" , " main.js" ) // any functions previously added to the context can be called
26
26
val , _ := ctx.RunScript (" result" , " value.js" ) // return a value in JavaScript back to Go
@@ -30,16 +30,45 @@ fmt.Printf("addition result: %s", val)
30
30
### One VM, many contexts
31
31
32
32
``` 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
35
35
ctx1.RunScript (" const multiply = (a, b) => a * b" , " math.js" )
36
36
37
- ctx2 , _ := v8go.NewContext (vm ) // another context on the same VM
37
+ ctx2 , _ := v8go.NewContext (iso ) // another context on the same VM
38
38
if _ , err := ctx2.RunScript (" multiply(3, 4)" , " main.js" ); err != nil {
39
39
// this will error as multiply is not defined in this context
40
40
}
41
41
```
42
42
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
+
43
72
### JavaScript errors
44
73
45
74
``` go
@@ -58,7 +87,6 @@ if err != nil {
58
87
### Terminate long running scripts
59
88
60
89
``` go
61
-
62
90
vals := make (chan *v8go.Value , 1 )
63
91
errs := make (chan error , 1 )
64
92
@@ -85,7 +113,7 @@ case <- time.After(200 * time.Milliseconds):
85
113
86
114
## Documentation
87
115
88
- Go Reference: https://pkg.go.dev/rogchap.com/v8go
116
+ Go Reference & more examples : https://pkg.go.dev/rogchap.com/v8go
89
117
90
118
### Support
91
119
0 commit comments