forked from rogchap/v8go
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
object_test.go
247 lines (218 loc) · 6.26 KB
/
object_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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Copyright 2021 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 (
"fmt"
"testing"
v8 "github.com/tommie/v8go"
)
func TestObjectMethodCall(t *testing.T) {
t.Parallel()
ctx := v8.NewContext()
iso := ctx.Isolate()
defer iso.Dispose()
defer ctx.Close()
val, _ := ctx.RunScript(`class Obj { constructor(input) { this.input = input, this.prop = "" } print() { return this.input.toString() } }; new Obj("some val")`, "")
obj, _ := val.AsObject()
val, err := obj.MethodCall("print")
fatalIf(t, err)
if val.String() != "some val" {
t.Errorf("unexpected value: %q", val)
}
_, err = obj.MethodCall("prop")
if err == nil {
t.Errorf("expected an error, got none")
}
val, err = ctx.RunScript(`class Obj2 { print(str) { return str.toString() }; get fails() { throw "error" } }; new Obj2()`, "")
fatalIf(t, err)
obj, _ = val.AsObject()
arg, _ := v8.NewValue(iso, "arg")
val, err = obj.MethodCall("print", arg)
fatalIf(t, err)
if val.String() != "arg" {
t.Errorf("unexpected value: %q", val)
}
_, err = obj.MethodCall("fails")
if err == nil {
t.Errorf("expected an error, got none")
}
}
func TestObjectSet(t *testing.T) {
t.Parallel()
ctx := v8.NewContext()
defer ctx.Isolate().Dispose()
defer ctx.Close()
symIter := v8.SymbolIterator(ctx.Isolate())
val, _ := ctx.RunScript("const foo = {}; foo", "")
obj, _ := val.AsObject()
if err := obj.Set("bar", "baz"); err != nil {
t.Errorf("unexpected error: %v", err)
}
baz, _ := ctx.RunScript("foo.bar", "")
if baz.String() != "baz" {
t.Errorf("unexpected value: %q", baz)
}
if err := obj.Set("", "zero"); err != nil {
t.Errorf("unexpected error: %v", err)
}
val, err := ctx.RunScript("foo['']", "")
fatalIf(t, err)
if val.String() != "zero" {
t.Errorf("unexpected value: %q", val)
}
if err := obj.Set("a", nil); err == nil {
t.Error("expected error but got <nil>")
}
if err := obj.Set("a", 0); err == nil {
t.Error("expected error but got <nil>")
}
if err := obj.SetSymbol(symIter, "sym"); err != nil {
t.Errorf("unexpected error: %v", err)
}
if err := obj.SetIdx(10, "ten"); err != nil {
t.Errorf("unexpected error: %v", err)
}
if err := obj.SetIdx(10, t); err == nil {
t.Error("expected error but got <nil>")
}
if ten, _ := ctx.RunScript("foo[10]", ""); ten.String() != "ten" {
t.Errorf("unexpected value: %q", ten)
}
}
func TestObjectInternalFields(t *testing.T) {
iso := v8.NewIsolate()
defer iso.Dispose()
ctx := v8.NewContext(iso)
defer ctx.Close()
tmpl := v8.NewObjectTemplate(iso)
obj, err := tmpl.NewInstance(ctx)
fatalIf(t, err)
if count := obj.InternalFieldCount(); count != 0 {
t.Errorf("expected 0 got %v", count)
}
if recoverPanic(func() { obj.GetInternalField(0) }) == nil {
t.Error("expected panic")
}
tmpl = v8.NewObjectTemplate(iso)
tmpl.SetInternalFieldCount(1)
if count := tmpl.InternalFieldCount(); count != 1 {
t.Errorf("expected 1 got %v", count)
}
obj, err = tmpl.NewInstance(ctx)
fatalIf(t, err)
if count := obj.InternalFieldCount(); count != 1 {
t.Errorf("expected 1 got %v", count)
}
if v := obj.GetInternalField(0); !v.SameValue(v8.Undefined(iso)) {
t.Errorf("unexpected value: %q", v)
}
if err := obj.SetInternalField(0, t); err == nil {
t.Error("expected unsupported value error")
}
err = obj.SetInternalField(0, "baz")
fatalIf(t, err)
if v := obj.GetInternalField(0); v.String() != "baz" {
t.Errorf("unexpected value: %q", v)
}
if recoverPanic(func() { obj.SetInternalField(1, "baz") }) == nil {
t.Error("expected panic from index out of bounds")
}
}
func TestObjectGet(t *testing.T) {
t.Parallel()
ctx := v8.NewContext()
defer ctx.Isolate().Dispose()
defer ctx.Close()
symIter := v8.SymbolIterator(ctx.Isolate())
val, err := ctx.RunScript("const foo = { bar: 'baz', [Symbol.iterator]: 'gee'}; foo", "")
if err != nil {
t.Fatalf("RunScript failed: %v", err)
}
obj, _ := val.AsObject()
if bar, _ := obj.Get("bar"); bar.String() != "baz" {
t.Errorf("unexpected value: %q", bar)
}
if baz, _ := obj.Get("baz"); !baz.IsUndefined() {
t.Errorf("unexpected value: %q", baz)
}
if got, _ := obj.GetSymbol(symIter); got.String() != "gee" {
t.Errorf("unexpected value: %q", got)
}
ctx.RunScript("foo[5] = 5", "")
if five, _ := obj.GetIdx(5); five.Integer() != 5 {
t.Errorf("unexpected value: %q", five)
}
if u, _ := obj.GetIdx(55); !u.IsUndefined() {
t.Errorf("unexpected value: %q", u)
}
}
func TestObjectHas(t *testing.T) {
t.Parallel()
ctx := v8.NewContext()
defer ctx.Isolate().Dispose()
defer ctx.Close()
symIter := v8.SymbolIterator(ctx.Isolate())
val, _ := ctx.RunScript("const foo = {a: 1, '2': 2, [Symbol.iterator]: 3}; foo", "")
obj, _ := val.AsObject()
if !obj.Has("a") {
t.Error("expected true, got false")
}
if obj.Has("c") {
t.Error("expected false, got true")
}
if !obj.HasSymbol(symIter) {
t.Error("expected true, got false")
}
if !obj.HasIdx(2) {
t.Error("expected true, got false")
}
if obj.HasIdx(1) {
t.Error("expected false, got true")
}
}
func TestObjectDelete(t *testing.T) {
t.Parallel()
ctx := v8.NewContext()
defer ctx.Isolate().Dispose()
defer ctx.Close()
symIter := v8.SymbolIterator(ctx.Isolate())
val, _ := ctx.RunScript("const foo = { bar: 'baz', '2': 2, [Symbol.iterator]: 3}; foo", "")
obj, _ := val.AsObject()
if !obj.Has("bar") {
t.Error("expected property to exist")
}
if !obj.Delete("bar") {
t.Error("expected delete to return true, got false")
}
if obj.Has("bar") {
t.Error("expected property to be deleted")
}
if !obj.DeleteSymbol(symIter) {
t.Error("expected delete to return true, got false")
}
if obj.HasSymbol(symIter) {
t.Error("expected property to be deleted")
}
if !obj.DeleteIdx(2) {
t.Error("expected delete to return true, got false")
}
}
func ExampleObject_global() {
iso := v8.NewIsolate()
defer iso.Dispose()
ctx := v8.NewContext(iso)
defer ctx.Close()
global := ctx.Global()
console := v8.NewObjectTemplate(iso)
logfn := v8.NewFunctionTemplate(iso, func(info *v8.FunctionCallbackInfo) *v8.Value {
fmt.Println(info.Args()[0])
return nil
})
console.Set("log", logfn)
consoleObj, _ := console.NewInstance(ctx)
global.Set("console", consoleObj)
ctx.RunScript("console.log('foo')", "")
// Output:
// foo
}