/
script_console.go
72 lines (55 loc) · 1.03 KB
/
script_console.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
package common
import (
"fmt"
"github.com/dop251/goja"
"strings"
)
type console struct{}
func (c *console) error(msgs ...string) {
Error(fmt.Errorf("%s", strings.Join(msgs, " ")))
}
func (c *console) info(msgs ...string) {
Info(strings.Join(msgs, " "))
}
func (c *console) debug(msgs ...string) {
Debug(strings.Join(msgs, " "))
}
func (c *console) warn(msgs ...string) {
Warn(strings.Join(msgs, " "))
}
func (c *console) log(msgs ...string) {
Info(strings.Join(msgs, " "))
}
func registerConsole(vm *goja.Runtime) error {
c := &console{}
obj := vm.NewObject()
err := obj.Set("error", c.error)
if Error(err) {
return err
}
err = obj.Set("debug", c.debug)
if Error(err) {
return err
}
err = obj.Set("warn", c.warn)
if Error(err) {
return err
}
err = obj.Set("info", c.info)
if Error(err) {
return err
}
err = obj.Set("log", c.log)
if Error(err) {
return err
}
err = obj.Set("table", c.table)
if Error(err) {
return err
}
err = vm.Set("console", obj)
if Error(err) {
return err
}
return nil
}