-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
37 lines (31 loc) · 872 Bytes
/
Copy pathmain.go
File metadata and controls
37 lines (31 loc) · 872 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Example: basic script execution.
//
// The simplest embedding: create a VM with a print provider, run a script, and
// read back its completion value.
package main
import (
"fmt"
"log"
"github.com/iceisfun/gojs"
)
func main() {
// A VM with no providers is a closed sandbox (no console output). Install
// the default print provider so console.log reaches stdout.
vm := gojs.New(
gojs.WithPrintProvider(gojs.NewDefaultPrintProvider()),
)
defer vm.Close()
source := `
function factorial(n) {
return n <= 1 ? 1 : n * factorial(n - 1);
}
console.log("factorial(10) =", factorial(10));
factorial(10); // the program's completion value
`
result, err := vm.RunString("factorial.js", source)
if err != nil {
log.Fatalf("run error: %v", err)
}
// Convert the JS completion value to a Go value.
fmt.Printf("Go sees: %v\n", vm.ToGo(result))
}