-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule.go
More file actions
59 lines (49 loc) · 1.45 KB
/
module.go
File metadata and controls
59 lines (49 loc) · 1.45 KB
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
package js
import (
"errors"
"github.com/dop251/goja"
"github.com/sohaha/zlsgo/ztype"
)
func (vm *VM) RunModule(code []byte, rendered ...func(*goja.Runtime) (goja.Value, error)) (result ztype.Type, err error) {
p, err := vm.getProgram(code, true)
if err != nil {
return ztype.New(nil), err
}
return vm.RunProgram(p, func(r *goja.Runtime) (result goja.Value, err error) {
defer r.GlobalObject().Delete("exports")
if len(rendered) == 0 {
exports := r.GlobalObject().Get("exports")
return exports, nil
}
for i := range rendered {
result, err = rendered[i](r)
if err != nil {
return
}
}
return
})
}
func (vm *VM) RunModuleForMethod(code []byte, method string, args ...interface{}) (result interface{}, err error) {
return vm.RunModule(code, func(r *goja.Runtime) (goja.Value, error) {
fn, ok := goja.AssertFunction(r.GlobalObject().Get("exports").ToObject(r).Get(method))
if !ok {
return nil, errors.New("method " + method + " not found")
}
return vm.runMethod(r, fn, args)
})
}
func (vm *VM) runMethod(r *goja.Runtime, method goja.Callable, args []interface{}) (goja.Value, error) {
values := make([]goja.Value, 0, len(args))
for i := range args {
switch v := args[i].(type) {
case ztype.Map:
values = append(values, r.ToValue(map[string]interface{}(v)))
default:
values = append(values, r.ToValue(v))
}
}
return vm.timeout(r, func() (goja.Value, error) {
return method(goja.Undefined(), values...)
})
}