-
Notifications
You must be signed in to change notification settings - Fork 15
/
jsrunner.go
105 lines (100 loc) · 3.18 KB
/
jsrunner.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
// Package main provides ...
package main
import (
"log"
"os/exec"
"time"
"github.com/netease/airinput/go-airinput"
"github.com/robertkrimen/otto"
"github.com/shirou/gopsutil/cpu"
)
var vm = otto.New()
func init() {
totime := func(msec int64) time.Duration {
return time.Millisecond * time.Duration(msec)
}
vm.Set("press", func(call otto.FunctionCall) otto.Value {
x, _ := call.Argument(0).ToInteger()
y, _ := call.Argument(1).ToInteger()
airinput.Press(int(x), int(y))
return otto.UndefinedValue()
})
vm.Set("move", func(call otto.FunctionCall) otto.Value {
x, _ := call.Argument(0).ToInteger()
y, _ := call.Argument(1).ToInteger()
airinput.Move(int(x), int(y))
return otto.UndefinedValue()
})
vm.Set("release", func(call otto.FunctionCall) otto.Value {
airinput.Release()
return otto.UndefinedValue()
})
vm.Set("tap", func(call otto.FunctionCall) otto.Value {
x, _ := call.Argument(0).ToInteger()
y, _ := call.Argument(1).ToInteger()
msec, _ := call.Argument(2).ToInteger()
airinput.Tap(int(x), int(y), totime(msec))
return otto.UndefinedValue()
})
vm.Set("drag", func(call otto.FunctionCall) otto.Value {
x0, _ := call.Argument(0).ToInteger()
y0, _ := call.Argument(1).ToInteger()
x1, _ := call.Argument(2).ToInteger()
y1, _ := call.Argument(3).ToInteger()
steps, _ := call.Argument(4).ToInteger()
msec, _ := call.Argument(5).ToInteger()
airinput.Drag(int(x0), int(y0), int(x1), int(y1), int(steps), totime(msec))
return otto.UndefinedValue()
})
vm.Set("sleep", func(call otto.FunctionCall) otto.Value {
x0, _ := call.Argument(0).ToInteger()
time.Sleep(time.Millisecond * time.Duration(x0))
return otto.UndefinedValue()
})
vm.Set("pinch", func(call otto.FunctionCall) otto.Value {
ax0, _ := call.Argument(0).ToInteger()
ay0, _ := call.Argument(1).ToInteger()
ax1, _ := call.Argument(2).ToInteger()
ay1, _ := call.Argument(3).ToInteger()
bx0, _ := call.Argument(4).ToInteger()
by0, _ := call.Argument(5).ToInteger()
bx1, _ := call.Argument(6).ToInteger()
by1, _ := call.Argument(7).ToInteger()
steps, _ := call.Argument(8).ToInteger()
msec, _ := call.Argument(9).ToInteger()
airinput.Pinch(
int(ax0), int(ay0), int(ax1), int(ay1),
int(bx0), int(by0), int(bx1), int(by1),
int(steps), totime(msec))
return otto.UndefinedValue()
})
vm.Set("exec", func(call otto.FunctionCall) otto.Value {
if len(call.ArgumentList) == 0 {
return otto.UndefinedValue()
}
params := []string{}
for _, p := range call.ArgumentList[1:] {
params = append(params, p.String())
}
cmd := exec.Command(call.Argument(0).String(), params...)
data, err := cmd.CombinedOutput()
if err != nil {
log.Println("jsrun error", err)
}
result, _ := otto.ToValue(string(data))
return result
})
vm.Set("cpuPercent", func(call otto.FunctionCall) otto.Value {
msec, _ := call.Argument(0).ToInteger()
percpu, _ := call.Argument(1).ToBoolean()
cpup, _ := cpu.CPUPercent(time.Duration(msec)*time.Millisecond, percpu)
result, _ := vm.ToValue(cpup)
return result
})
}
// //abc = 1 + 2
// console.log("The value of abc is " + abc); // 4
// console.log(exec("echo", "-n", "hello"));
func RunJS(code string) (otto.Value, error) {
return vm.Run(code)
}