forked from veonik/squircy2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm.go
297 lines (266 loc) · 7.38 KB
/
vm.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package script
import (
"crypto/sha1"
"fmt"
"time"
"github.com/robertkrimen/otto"
"github.com/tyler-sommer/squircy2/squircy/data"
"github.com/tyler-sommer/squircy2/squircy/event"
)
// timer represents a function call to be performed after a delay.
type timer struct {
t *time.Timer
dur time.Duration
repeat bool
call otto.FunctionCall
}
// jsVM is a small wrapper around Otto VM, facilitating the event loop
// and setTimeout/setInterval functionality.
type jsVm struct {
*otto.Otto
registry map[*timer]*timer
ready chan *timer
quit chan struct{}
}
func newJavascriptVm(m *ScriptManager) *jsVm {
jsVm := &jsVm{otto.New(), make(map[*timer]*timer), make(chan *timer), make(chan struct{})}
getFnName := func(fn otto.Value) (name string) {
if fn.IsFunction() {
name = fmt.Sprintf("__Handler%x", sha1.Sum([]byte(fn.String())))
} else {
name = fn.String()
}
return
}
newTimer := func(call otto.FunctionCall, repeat bool) (*timer, otto.Value, error) {
delay, _ := call.Argument(1).ToInteger()
if delay <= 0 {
delay = 1
}
res := &timer{nil, time.Duration(delay) * time.Millisecond, repeat, call}
jsVm.registry[res] = res
res.t = time.AfterFunc(res.dur, func() {
jsVm.ready <- res
})
val, err := jsVm.ToValue(res)
if err != nil {
return nil, otto.UndefinedValue(), err
}
return res, val, nil
}
clearTimer := func(call otto.FunctionCall) otto.Value {
ti, _ := call.Argument(0).Export()
if ti, ok := ti.(*timer); ok {
ti.t.Stop()
delete(jsVm.registry, ti)
}
return otto.UndefinedValue()
}
jsVm.Set("setTimeout", func(call otto.FunctionCall) otto.Value {
_, v, err := newTimer(call, false)
if err != nil {
return otto.UndefinedValue()
}
return v
})
jsVm.Set("setInterval", func(call otto.FunctionCall) otto.Value {
_, v, err := newTimer(call, true)
if err != nil {
return otto.UndefinedValue()
}
return v
})
jsVm.Set("clearTimeout", clearTimer)
jsVm.Set("clearInterval", clearTimer)
jsVm.Set("Http", &m.httpHelper)
v, _ := jsVm.Object("Http")
v.Set("Send", func(call otto.FunctionCall) otto.Value {
o := call.Argument(0).Object()
if o == nil {
fmt.Printf("Expected argument 0 to be object, got: %v\n", o)
return otto.UndefinedValue()
}
urlVal, err := o.Get("url")
if err != nil {
fmt.Printf("'url' is a required option\n")
return otto.UndefinedValue()
}
url, err := urlVal.ToString()
if err != nil {
fmt.Printf("Could not coerce 'url' into string\n")
return otto.UndefinedValue()
}
typVal, _ := o.Get("type")
typ, _ := typVal.ToString()
successCb, _ := o.Get("success")
datVal, _ := o.Get("data")
dat, _ := datVal.ToString()
headerVal, _ := o.Get("headers")
headers := make([]string, 0)
if headerVal.IsString() {
h, _ := headerVal.ToString()
headers = append(headers, h)
} else if headerVal.IsObject() {
h := headerVal.Object()
for _, k := range h.Keys() {
v, _ := h.Get(k)
headers = append(headers, fmt.Sprintf("%s: %s", k, v))
}
}
go func() {
var res string
switch typ {
case "post":
res = m.httpHelper.Post(url, dat, headers...)
default:
res = m.httpHelper.Get(url, headers...)
}
if successCb.IsFunction() {
successCb.Call(successCb, res)
}
}()
return otto.UndefinedValue()
})
jsVm.Set("Config", &m.configHelper)
jsVm.Set("Irc", &m.ircHelper)
jsVm.Set("Os", &m.osHelper)
jsVm.Set("Math", &m.mathHelper)
v, _ = jsVm.Object("Math")
v.Set("rand", (&m.mathHelper).Rand)
v.Set("round", (&m.mathHelper).Round)
v.Set("ceil", (&m.mathHelper).Ceil)
v.Set("floor", (&m.mathHelper).Floor)
jsVm.Set("bind", func(call otto.FunctionCall) otto.Value {
eventType := call.Argument(0).String()
fn := call.Argument(1)
fnName := getFnName(fn)
if fn.IsFunction() {
m.jsDriver.vm.Set(fnName, func(call otto.FunctionCall) otto.Value {
fn.Call(call.This, call.ArgumentList)
return otto.UndefinedValue()
})
}
m.scriptHelper.Bind(Javascript, event.EventType(eventType), fnName)
val, _ := otto.ToValue(fnName)
return val
})
jsVm.Set("unbind", func(call otto.FunctionCall) otto.Value {
eventType := call.Argument(0).String()
fnName := getFnName(call.Argument(1))
m.scriptHelper.Unbind(Javascript, event.EventType(eventType), fnName)
return otto.UndefinedValue()
})
jsVm.Set("trigger", func(call otto.FunctionCall) otto.Value {
eventType := call.Argument(0).String()
dat, _ := call.Argument(1).Export()
if dat == nil {
dat = make(map[string]interface{}, 0)
}
m.scriptHelper.Trigger(event.EventType(eventType), dat.(map[string]interface{}))
return otto.UndefinedValue()
})
jsVm.Set("use", func(call otto.FunctionCall) otto.Value {
coll := call.Argument(0).String()
// Todo: get the Database properly
db := data.NewGenericRepository(m.repo.database, coll)
obj, _ := jsVm.Object("({})")
obj.Set("Save", func(call otto.FunctionCall) otto.Value {
exp, _ := call.Argument(0).Export()
var model data.GenericModel
switch t := exp.(type) {
case data.GenericModel:
model = t
case map[string]interface{}:
model = data.GenericModel(t)
}
switch t := model["ID"].(type) {
case int64:
model["ID"] = int(t)
case int:
model["ID"] = t
case float64:
model["ID"] = int(t)
}
db.Save(model)
id, _ := jsVm.ToValue(model["ID"])
return id
})
obj.Set("Delete", func(call otto.FunctionCall) otto.Value {
i, _ := call.Argument(0).ToInteger()
db.Delete(int(i))
res, _ := jsVm.ToValue(true)
return res
})
obj.Set("Fetch", func(call otto.FunctionCall) otto.Value {
i, _ := call.Argument(0).ToInteger()
val := db.Fetch(int(i))
v, err := jsVm.ToValue(val)
if err != nil {
panic(err)
}
return v
})
obj.Set("FetchAll", func(call otto.FunctionCall) otto.Value {
vals := db.FetchAll()
v, err := jsVm.ToValue(vals)
if err != nil {
m.l.Println("An error occurred: ", err)
}
return v
})
obj.Set("Index", func(call otto.FunctionCall) otto.Value {
exp, _ := call.Argument(0).Export()
cols := make([]string, 0)
for _, val := range exp.([]interface{}) {
cols = append(cols, val.(string))
}
db.Index(cols)
return otto.UndefinedValue()
})
obj.Set("Query", func(call otto.FunctionCall) otto.Value {
qry, _ := call.Argument(0).Export()
vals := db.Query(qry)
v, err := jsVm.ToValue(vals)
if err != nil {
m.l.Println("An error occurred: ", err)
}
return v
})
return obj.Value()
})
go jsVm.Loop()
return jsVm
}
// Loop kicks off the VM's event loop.
func (vm *jsVm) Loop() {
for {
select {
case ti := <-vm.ready:
var args []interface{}
if len(ti.call.ArgumentList) > 2 {
tmp := ti.call.ArgumentList[2:]
// args[1] will end up being "this" when the function is invoked,
// so we need to offset each actual argument by one
args = make([]interface{}, 2+len(tmp))
for i, value := range tmp {
args[i+2] = value
}
} else {
args = make([]interface{}, 1)
}
args[0] = ti.call.ArgumentList[0]
// Since we are calling "Function.call.call", we pass the function
// to be called in as the first argument, which means args[1] will
// end up being the function's "this" binding, and any further values
// in args will be passed to the function as arguments.
_, err := vm.Call("Function.call.call", nil, args...)
if err == nil && ti.repeat {
ti.t.Reset(ti.dur)
} else {
delete(vm.registry, ti)
}
case <-vm.quit:
return
}
}
}