forked from dckc/go-duktape
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathduktape_test.go
142 lines (118 loc) · 2.76 KB
/
duktape_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
package duktape
import "reflect"
import "testing"
func TestEvalString(t *testing.T) {
ctx := NewContext()
ctx.EvalString(`"Golang love Duktape!"`)
expect(t, Type(ctx.GetType(-1)).IsString(), true)
expect(t, ctx.GetString(-1), "Golang love Duktape!")
ctx.DestroyHeap()
}
func TestEvalFunc(t *testing.T) {
ctx := NewContext()
ctx.PevalString(`(function (x) { return x + x; })`)
expect(t, ctx.IsCallable(-1), true)
expect(t, Type(ctx.GetType(-1)).IsObject(), true)
ctx.PushInt(5)
ctx.Pcall(1)
expect(t, ctx.GetInt(-1), 10)
ctx.DestroyHeap()
}
func TestEvalWith(t *testing.T) {
ctx := NewContext()
obj := MethodSuite{
"hi": func(d *Context) int {
x := d.GetInt(-2)
d.PushString("hi! " + string(48 + x))
return 1
},
}
err := ctx.EvalWith("(function(o) { return o.hi(1, 2, 3) })", obj)
expect(t, err, nil)
actual := ctx.GetString(-1)
expect(t, actual, "hi! 2")
ctx.DestroyHeap()
}
// from duktape examples
func TestMyAddTwo(t *testing.T) {
obj := MethodSuite{
"add": func(d *Context) int {
top := d.GetTop()
a := d.GetNumber(top - 2)
b := d.GetNumber(top - 1)
d.PushNumber(a + b)
return 1
},
}
ctx := NewContext()
ctx.PushGlobalObject()
// Hmm... a property value can outlive an object. look out!
ctx.EvalWith("(function(o) { return o.add })", obj)
ctx.PutPropString(-2, "adder")
ctx.PevalString(`adder(2, 3);`)
res := ctx.GetNumber(-1)
ctx.Pop()
expect(t, res, float64(5))
ctx.DestroyHeap()
}
func TestGoClosure(t *testing.T) {
sharedState := 0
obj := MethodSuite{
"inc": func(d *Context) int {
sharedState++
d.PushInt(sharedState)
return 1
},
"dec": func(d *Context) int {
sharedState--
d.PushInt(sharedState)
return 1
},
}
ctx := NewContext()
ctx.EvalWith(`
(function(o) {
o.inc();
o.inc();
o.dec();
o.inc();
return o.inc();
})`, obj)
res := ctx.GetNumber(-1)
expect(t, res, float64(3))
// check for leaks
ctx.Gc(0)
ctx.Gc(0)
expect(t, len(objectMap), 0)
ctx.DestroyHeap()
}
type SampleObject struct {
X int
}
func TestGoObject(t *testing.T) {
ctx := NewContext()
ctx.PushGlobalObject()
ctx.PushGoObject(SampleObject{42})
ctx.PutPropString(-2, "y")
ctx.Pop()
obj := MethodSuite{
"tst": func(d *Context) int {
so := d.GetGoObject(-1).(SampleObject)
d.PushInt(so.X)
return 1
},
}
ctx.EvalWith("(function(o) { return o.tst(y); })", obj)
res := ctx.GetNumber(-1)
expect(t, res, float64(42))
// check for leaks
ctx.PevalString("y = null")
ctx.Gc(0)
ctx.Gc(0)
expect(t, len(objectMap), 0)
}
func expect(t *testing.T, a interface{}, b interface{}) {
if a != b {
t.Errorf("Expected %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
}
}