forked from lithdew/quickjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickjs_test.go
173 lines (127 loc) · 3.57 KB
/
quickjs_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
package quickjs
import (
"errors"
"fmt"
"github.com/stretchr/testify/require"
stdruntime "runtime"
"sync"
"testing"
)
func TestObject(t *testing.T) {
runtime := NewRuntime()
defer runtime.Free()
context := runtime.NewContext()
defer context.Free()
test := context.Object()
test.Set("A", context.String("String A"))
test.Set("B", context.String("String B"))
test.Set("C", context.String("String C"))
context.Globals().Set("test", test)
result, err := context.Eval(`Object.keys(test).map(key => test[key]).join(" ")`)
require.NoError(t, err)
defer result.Free()
require.EqualValues(t, "String A String B String C", result.String())
}
func TestArray(t *testing.T) {
runtime := NewRuntime()
defer runtime.Free()
context := runtime.NewContext()
defer context.Free()
test := context.Array()
for i := int64(0); i < 3; i++ {
test.SetByInt64(i, context.String(fmt.Sprintf("test %d", i)))
}
for i := int64(0); i < test.Len(); i++ {
require.EqualValues(t, fmt.Sprintf("test %d", i), test.GetByUint32(uint32(i)).String())
}
context.Globals().Set("test", test)
result, err := context.Eval(`test.map(v => v.toUpperCase())`)
require.NoError(t, err)
defer result.Free()
require.EqualValues(t, `TEST 0,TEST 1,TEST 2`, result.String())
}
func TestBadSyntax(t *testing.T) {
runtime := NewRuntime()
defer runtime.Free()
context := runtime.NewContext()
defer context.Free()
_, err := context.Eval(`"bad syntax'`)
require.Error(t, err)
}
func TestFunctionThrowError(t *testing.T) {
expected := errors.New("expected error")
runtime := NewRuntime()
defer runtime.Free()
context := runtime.NewContext()
defer context.Free()
context.Globals().SetFunction("A", func(ctx *Context, this Value, args []Value) Value {
return ctx.ThrowError(expected)
})
_, actual := context.Eval("A()")
require.Error(t, actual)
require.EqualValues(t, "Error: "+expected.Error(), actual.Error())
}
func TestFunction(t *testing.T) {
runtime := NewRuntime()
defer runtime.Free()
context := runtime.NewContext()
defer context.Free()
A := make(chan struct{})
B := make(chan struct{})
context.Globals().SetFunction("A", func(ctx *Context, this Value, args []Value) Value {
require.Len(t, args, 4)
require.True(t, args[0].IsString() && args[0].String() == "hello world!")
require.True(t, args[1].IsNumber() && args[1].Int32() == 1)
require.True(t, args[2].IsNumber() && args[2].Int64() == 8)
require.True(t, args[3].IsNull())
close(A)
return ctx.String("A says hello")
})
context.Globals().SetFunction("B", func(ctx *Context, this Value, args []Value) Value {
require.Len(t, args, 0)
close(B)
return ctx.Float64(256)
})
result, err := context.Eval(`A("hello world!", 1, 2 ** 3, null)`)
require.NoError(t, err)
defer result.Free()
require.True(t, result.IsString() && result.String() == "A says hello")
<-A
result, err = context.Eval(`B()`)
require.NoError(t, err)
defer result.Free()
require.True(t, result.IsNumber() && result.Uint32() == 256)
<-B
}
func TestConcurrency(t *testing.T) {
n := 32
m := 10000
var wg sync.WaitGroup
wg.Add(n)
req := make(chan struct{}, n)
res := make(chan int64, m)
for i := 0; i < n; i++ {
go func() {
stdruntime.LockOSThread()
defer wg.Done()
runtime := NewRuntime()
defer runtime.Free()
context := runtime.NewContext()
defer context.Free()
for range req {
result, err := context.Eval(`new Date().getTime()`)
require.NoError(t, err)
res <- result.Int64()
result.Free()
}
}()
}
for i := 0; i < m; i++ {
req <- struct{}{}
}
close(req)
wg.Wait()
for i := 0; i < m; i++ {
<-res
}
}