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
|
package main
import (
"fmt"
"github.com/robertkrimen/otto"
"testing"
)
//go get -u go.starlark.net/cmd/starlark
func Test1 (t *testing.T) {
var cmd string = `
console.log("123456");
/* var arr = [1,2,3]; */
var arr = getList();
console.log(arr);
for(var i=0;i<arr.length;i++ ){
console.log(arr[i]);
}
var ok = "it is OK ";
function isOK(input ) {
if (input == null) return false;
//数组长度大于等于3;
return input.length && input.length>3;
}
`
//fmt.Println(cmd)
vm := otto.New()
vm.Set("getList", func(call otto.FunctionCall) otto.Value {
w := []int{1,3,3}
res , _ := vm.ToValue(w)
return res
})
_,err := vm.Run(cmd)
get, err := vm.Get("ok")
if err != nil {
panic(err)
}
fmt.Println(get)
s := []int{1,2,3,4,5}
res, err := vm.Call("isOK", s)
if err!=nil {
panic(err)
}
fmt.Printf("res = %#v \n",res)
}
/*
=== RUN Test1
123456
1,3,3
1
3
3
it is OK
res = otto.Value{kind:4, value:false}
--- PA
*/
|