forked from lazytiger/go-v8
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathv8_binding_test.go
204 lines (168 loc) · 4.95 KB
/
v8_binding_test.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
package v8
import "reflect"
import "testing"
import "runtime"
func Test_Bind_Variadic(t *testing.T) {
template := engine.NewObjectTemplate()
template.Bind("Call", func(arg1, arg2 string, args ...string) *Value {
val := engine.NewObject()
obj := val.ToObject()
obj.SetProperty("a1", engine.NewString(arg1))
obj.SetProperty("a2", engine.NewString(arg2))
array := engine.NewArray(len(args))
arrayObj := array.ToObject()
for i, arg := range args {
arrayObj.SetElement(i, engine.NewString(arg))
}
obj.SetProperty("as", array)
return val
})
engine.NewContext(template).Scope(func(cs ContextScope) {
script := engine.Compile([]byte(`
a = Call("aaa", "bbb");
if (a.a1 != "aaa" || a.a2 != "bbb") {
throw "value should be {\"a1\":\"aaa\",\"a2\":\"bbb\"} not " + JSON.stringify(a);
}
a = Call("aaa", "bbb", "ccc");
if (a.a1 != "aaa" || a.a2 != "bbb" || a.as.length != 1 || a.as[0] != "ccc") {
throw "value should be {\"a1\":\"aaa\",\"a2\":\"bbb\",\"as\":[\"ccc\"]} not " + JSON.stringify(a);
}
a = Call("aaa", "bbb", "ccc", "ddd");
if (a.a1 != "aaa" || a.a2 != "bbb" || a.as.length != 2 || a.as[0] != "ccc" || a.as[1] != "ddd") {
throw "value should be {\"a1\":\"aaa\",\"a2\":\"bbb\",\"as\":[\"ccc\",\"ddd\"]} not " + JSON.stringify(a);
}
"ok"
`), nil)
var retVal *Value
if err := cs.TryCatch(func() {
retVal = cs.Run(script)
}); err != nil {
t.Fatal(err)
}
if !retVal.IsString() || retVal.ToString() != "ok" {
t.Fatalf("value should be \"ok\" not %s", ToJSON(retVal))
}
})
runtime.GC()
}
func Test_Bind_Function(t *testing.T) {
template := engine.NewObjectTemplate()
goFunc1 := func(text string, obj *Object, callback *Function) {
t.Log("fetch")
for i := 0; i < 10; i++ {
t.Log(i)
callback.Call(engine.NewString(text), obj.Value)
runtime.GC()
}
}
goFunc2 := func(text1, text2 string) {
t.Logf("print(%s, %s)", text1, text2)
}
template.Bind("fetch", goFunc1)
template.Bind("print", goFunc2)
engine.NewContext(template).Scope(func(cs ContextScope) {
cs.Eval(`
var testObj = {Name: function() {
return "test object"
}};
fetch("test", testObj, function(text, obj) {
print(text, obj.Name())
});`)
})
runtime.GC()
}
type BindingTest struct {
First int32
Second uint32
}
func Test_Bind_Struct(t *testing.T) {
template := engine.NewObjectTemplate()
template.Bind("BindingTest", BindingTest{})
template.Bind("Test", func() BindingTest {
return BindingTest{-1, 1}
})
engine.NewContext(template).Scope(func(cs ContextScope) {
if err := cs.TryCatch(func() {
retVal := cs.Eval(`Test()`)
if retVal.ToObject().GetProperty("First").ToInt32() != -1 {
t.Fatalf(`retVal.ToObject().GetProperty("First").ToInt32() != -1`)
}
if retVal.ToObject().GetProperty("Second").ToUint32() != 1 {
t.Fatalf(`retVal.ToObject().GetProperty("Second").ToUint32() != 1`)
}
}); err != nil {
t.Fatal(err)
}
})
}
func Test_Bind_Integers(t *testing.T) {
template := engine.NewObjectTemplate()
template.Bind("BindingTest", BindingTest{})
engine.NewContext(template).Scope(func(cs ContextScope) {
cs.Eval(`
function getProduct(x) {
return x.First * x.Second
}
`)
getProduct := cs.Eval(`(function() { return getProduct } )()`)
if !getProduct.IsFunction() {
t.Fatalf("getProduct should be function pointer")
}
var retVal *Value
testObj := &BindingTest{3, 3}
if err := cs.TryCatch(func() {
reflectedObj := reflect.ValueOf(testObj)
retVal = getProduct.ToFunction().Call(engine.GoValueToJsValue(reflectedObj))
}); err != nil {
t.Fatal(err)
}
if retVal.ToInteger() != 9 {
t.Fatalf("value should be 9 not %s", ToJSON(retVal))
}
})
runtime.GC()
}
func Test_Bind_MapArgument(t *testing.T) {
template := engine.NewObjectTemplate()
template.Bind("Call", func(m map[string]string) string {
if m != nil {
return m["key"]
} else {
return "nil"
}
})
var errStr string
engine.NewContext(template).Scope(func(cs ContextScope) {
retVal := cs.Eval(`Call({"key":"value"})`)
if !retVal.IsString() || retVal.ToString() != "value" {
errStr = "value should be \"value\" not " + string(ToJSON(retVal))
}
})
// t.Fatal() in context scopes causes dead locks of Test_ThreadSafeX in v8_engine_test.go.
if errStr != "" {
t.Fatal(errStr)
}
}
func Test_Bind_InvalidMapArgument(t *testing.T) {
template := engine.NewObjectTemplate()
template.Bind("Call", func(m map[string]string) string {
if m != nil {
return m["key"]
} else {
return "nil"
}
})
for _, arg := range []string{"", "true", "111", `"aaa"`} {
var errStr string
engine.NewContext(template).Scope(func(cs ContextScope) {
retVal := cs.Eval(`Call(` + arg + `)`)
if !retVal.IsString() || retVal.ToString() != "nil" {
errStr = "value should be \"nil\" not " + string(ToJSON(retVal)) + " @" + arg
}
})
// t.Fatal() in context scopes causes dead locks of Test_ThreadSafeX in v8_engine_test.go.
if errStr != "" {
t.Fatal(errStr)
}
}
}