-
Notifications
You must be signed in to change notification settings - Fork 0
/
goatsejs.go
104 lines (98 loc) · 3.25 KB
/
goatsejs.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
// Put JS and Go functions in goatse object
package main
import (
"github.com/robertkrimen/otto"
"log"
"sort"
"strings"
)
// Go functions here to attach to `goatse` object in JS environment.
func getGoatseGoFunctions() map[string]func(otto.FunctionCall) otto.Value {
var prepared map[string]func(otto.FunctionCall) otto.Value
prepared = map[string]func(otto.FunctionCall) otto.Value{}
prepared["_log"] = goatsejs_log
prepared["_sleep"] = goatsejs_sleep
prepared["_randInt"] = goatsejs_randInt
prepared["_exec"] = goatsejs_exec
prepared["_getenv"] = goatsejs_getenv
prepared["_argv"] = goatsejs_argv
prepared["_where"] = goatsejs_where
prepared["_exists"] = goatsejs_exists
prepared["_rename"] = goatsejs_rename
prepared["_mkdirs"] = goatsejs_mkdirs
prepared["_rmtree"] = goatsejs_rmtree
prepared["_require"] = goatsejs_requirejs
prepared["_httpMethod"] = goatsejs_httpmethod
prepared["_saveToFile"] = goatsejs_saveToFile
prepared["_loadFromFile"] = goatsejs_loadFromFile
prepared["_md5SumFile"] = goatsejs_md5SumFile
prepared["_fileSize"] = goatsejs_fileSize
prepared["_query_find"] = goatsejs_query_find
prepared["_query_attr"] = goatsejs_query_attr
prepared["_query_children"] = goatsejs_query_children
prepared["_query_text"] = goatsejs_query_text
prepared["_getDOIMeta"] = goatsejs_getDOIMeta
prepared["_memberSample"] = goatsejs_memberSample
prepared["_notOpenSample"] = goatsejs_notOpenSample
prepared["_strings_entry"] = goatsejs_strings_entry
prepared["_strings_urlescape"] = goatsejs_strings_urlescape
prepared["_strings_urlunescape"] = goatsejs_strings_urlunescape
return prepared
}
// Combined source in order "goatse_js_builtins"
func getGoatseJSFunctions() (script string) {
var (
scriptNames []string
scripts []string
contents []byte
err error
)
script = "// Sources JS\r\n"
scriptNames = getAllSourceJS()
scripts = make([]string, len(scriptNames))
for i, scriptn := range scriptNames {
contents, err = Asset("goatse_js_builtins/" + scriptn)
if err != nil {
panic("Failed to builtin JS: " + err.Error())
}
scripts[i] = "// " + scriptn + ":\r\n" + string(contents)
}
script = strings.Join(scripts, "\r\n\r\n")
return script
}
// Iterates over JS source file names.
func getAllSourceJS() (allJSSource []string) {
var (
err error
)
allJSSource, err = AssetDir("goatse_js_builtins")
if err != nil {
panic(err.Error())
}
sort.Strings(allJSSource)
return allJSSource
}
// Create JS `goatse` object. Add Go funcs. Run JS source to add JS built-ins.
func registerGoatseJS(ovm *otto.Otto) {
var (
goatse_gofuncs map[string]func(otto.FunctionCall) otto.Value
goatse_obj *otto.Object
JSSource string
err error
)
goatse_obj, err = ovm.Object("goatse = {}")
logFatal(err, "Failed to make object: ")
goatse_gofuncs = getGoatseGoFunctions()
for name, function := range goatse_gofuncs {
err = goatse_obj.Set(name, function)
logFatal(err, "Failed to put "+name+" in goatse: ")
}
JSSource = getGoatseJSFunctions()
_, err = ovm.Run(JSSource)
logFatal(err, "Failed to put JS functions in object: ")
}
func logFatal(err error, msg string) {
if err != nil {
log.Fatal(msg + err.Error())
}
}