forked from rogchap/v8go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context_test.go
160 lines (138 loc) · 3.53 KB
/
context_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
// Copyright 2019 Roger Chapman and the v8go contributors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package v8go_test
import (
"encoding/json"
"fmt"
"testing"
"rogchap.com/v8go"
)
func TestContextExec(t *testing.T) {
t.Parallel()
ctx, _ := v8go.NewContext(nil)
ctx.RunScript(`const add = (a, b) => a + b`, "add.js")
val, _ := ctx.RunScript(`add(3, 4)`, "main.js")
rtn := val.String()
if rtn != "7" {
t.Errorf("script returned an unexpected value: expected %q, got %q", "7", rtn)
}
_, err := ctx.RunScript(`add`, "func.js")
if err != nil {
t.Errorf("error not expected: %v", err)
}
iso, _ := ctx.Isolate()
ctx2, _ := v8go.NewContext(iso)
_, err = ctx2.RunScript(`add`, "ctx2.js")
if err == nil {
t.Error("error expected but was <nil>")
}
}
func TestJSExceptions(t *testing.T) {
t.Parallel()
tests := [...]struct {
name string
source string
origin string
err string
}{
{"SyntaxError", "bad js syntax", "syntax.js", "SyntaxError: Unexpected identifier"},
{"ReferenceError", "add()", "add.js", "ReferenceError: add is not defined"},
}
ctx, _ := v8go.NewContext(nil)
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
_, err := ctx.RunScript(tt.source, tt.origin)
if err == nil {
t.Error("error expected but got <nil>")
return
}
if err.Error() != tt.err {
t.Errorf("expected %q, got %q", tt.err, err.Error())
}
})
}
}
func TestContextRegistry(t *testing.T) {
t.Parallel()
ctx, _ := v8go.NewContext()
ctxref := ctx.Ref()
c1 := v8go.GetContext(ctxref)
if c1 != nil {
t.Error("expected context to be <nil>")
}
ctx.Register()
c2 := v8go.GetContext(ctxref)
if c2 == nil {
t.Error("expected context, but got <nil>")
}
if c2 != ctx {
t.Errorf("contexts should match %p != %p", c2, ctx)
}
ctx.Deregister()
c3 := v8go.GetContext(ctxref)
if c3 != nil {
t.Error("expected context to be <nil>")
}
}
func TestMemoryLeak(t *testing.T) {
t.Parallel()
iso, _ := v8go.NewIsolate()
for i := 0; i < 6000; i++ {
ctx, _ := v8go.NewContext(iso)
obj := ctx.Global()
_ = obj.String()
_, _ = ctx.RunScript("2", "")
ctx.Close()
}
if n := iso.GetHeapStatistics().NumberOfNativeContexts; n >= 6000 {
t.Errorf("Context not being GC'd, got %d native contexts", n)
}
}
func BenchmarkContext(b *testing.B) {
b.ReportAllocs()
vm, _ := v8go.NewIsolate()
defer vm.Close()
for n := 0; n < b.N; n++ {
ctx, _ := v8go.NewContext(vm)
ctx.RunScript(script, "main.js")
str, _ := json.Marshal(makeObject())
cmd := fmt.Sprintf("process(%s)", str)
ctx.RunScript(cmd, "cmd.js")
ctx.Close()
}
}
func ExampleContext() {
ctx, _ := v8go.NewContext()
ctx.RunScript("const add = (a, b) => a + b", "math.js")
ctx.RunScript("const result = add(3, 4)", "main.js")
val, _ := ctx.RunScript("result", "value.js")
fmt.Println(val)
// Output:
// 7
}
func ExampleContext_isolate() {
iso, _ := v8go.NewIsolate()
ctx1, _ := v8go.NewContext(iso)
ctx1.RunScript("const foo = 'bar'", "context_one.js")
val, _ := ctx1.RunScript("foo", "foo.js")
fmt.Println(val)
ctx2, _ := v8go.NewContext(iso)
_, err := ctx2.RunScript("foo", "context_two.js")
fmt.Println(err)
// Output:
// bar
// ReferenceError: foo is not defined
}
func ExampleContext_globalTemplate() {
iso, _ := v8go.NewIsolate()
obj, _ := v8go.NewObjectTemplate(iso)
obj.Set("version", "v1.0.0")
ctx, _ := v8go.NewContext(iso, obj)
val, _ := ctx.RunScript("version", "main.js")
fmt.Println(val)
// Output:
// v1.0.0
}