forked from dop251/goja
/
main.go
127 lines (111 loc) · 2.48 KB
/
main.go
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
crand "crypto/rand"
"encoding/binary"
"flag"
"fmt"
"io/ioutil"
"log"
"math/rand"
"os"
"runtime/debug"
"runtime/pprof"
"time"
"github.com/dop251/goja"
"github.com/dop251/goja_nodejs/console"
"github.com/dop251/goja_nodejs/require"
)
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
var timelimit = flag.Int("timelimit", 0, "max time to run (in seconds)")
func readSource(filename string) ([]byte, error) {
if filename == "" || filename == "-" {
return ioutil.ReadAll(os.Stdin)
}
return ioutil.ReadFile(filename)
}
func load(vm *goja.Runtime, call goja.FunctionCall) goja.Value {
p := call.Argument(0).String()
b, err := readSource(p)
if err != nil {
panic(vm.ToValue(fmt.Sprintf("Could not read %s: %v", p, err)))
}
v, err := vm.RunScript(p, string(b))
if err != nil {
panic(err)
}
return v
}
func newRandSource() goja.RandSource {
var seed int64
if err := binary.Read(crand.Reader, binary.LittleEndian, &seed); err != nil {
panic(fmt.Errorf("Could not read random bytes: %v", err))
}
return rand.New(rand.NewSource(seed)).Float64
}
func run() error {
filename := flag.Arg(0)
src, err := readSource(filename)
if err != nil {
return err
}
if filename == "" || filename == "-" {
filename = "<stdin>"
}
vm := goja.New()
vm.SetRandSource(newRandSource())
new(require.Registry).Enable(vm)
console.Enable(vm)
vm.Set("load", func(call goja.FunctionCall) goja.Value {
return load(vm, call)
})
vm.Set("readFile", func(name string) (string, error) {
b, err := ioutil.ReadFile(name)
if err != nil {
return "", err
}
return string(b), nil
})
if *timelimit > 0 {
time.AfterFunc(time.Duration(*timelimit)*time.Second, func() {
vm.Interrupt("timeout")
})
}
//log.Println("Compiling...")
prg, err := goja.Compile(filename, string(src), false)
if err != nil {
return err
}
//log.Println("Running...")
_, err = vm.RunProgram(prg)
//log.Println("Finished.")
return err
}
func main() {
defer func() {
if x := recover(); x != nil {
debug.Stack()
panic(x)
}
}()
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
if err := run(); err != nil {
//fmt.Printf("err type: %T\n", err)
switch err := err.(type) {
case *goja.Exception:
fmt.Println(err.String())
case *goja.InterruptedError:
fmt.Println(err.String())
default:
fmt.Println(err)
}
os.Exit(64)
}
}