forked from dop251/goja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
object_test.go
458 lines (389 loc) · 8.2 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
package goja
import (
"fmt"
"reflect"
"testing"
)
func TestDefineProperty(t *testing.T) {
r := New()
o := r.NewObject()
err := o.DefineDataProperty("data", r.ToValue(42), FLAG_TRUE, FLAG_TRUE, FLAG_TRUE)
if err != nil {
t.Fatal(err)
}
err = o.DefineAccessorProperty("accessor_ro", r.ToValue(func() int {
return 1
}), nil, FLAG_TRUE, FLAG_TRUE)
if err != nil {
t.Fatal(err)
}
err = o.DefineAccessorProperty("accessor_rw",
r.ToValue(func(call FunctionCall) Value {
return o.Get("__hidden")
}),
r.ToValue(func(call FunctionCall) (ret Value) {
o.Set("__hidden", call.Argument(0))
return
}),
FLAG_TRUE, FLAG_TRUE)
if err != nil {
t.Fatal(err)
}
if v := o.Get("accessor_ro"); v.ToInteger() != 1 {
t.Fatalf("Unexpected accessor value: %v", v)
}
err = o.Set("accessor_ro", r.ToValue(2))
if err == nil {
t.Fatal("Expected an error")
}
if ex, ok := err.(*Exception); ok {
if msg := ex.Error(); msg != "TypeError: Cannot assign to read only property 'accessor_ro'" {
t.Fatalf("Unexpected error: '%s'", msg)
}
} else {
t.Fatalf("Unexected error type: %T", err)
}
err = o.Set("accessor_rw", 42)
if err != nil {
t.Fatal(err)
}
if v := o.Get("accessor_rw"); v.ToInteger() != 42 {
t.Fatalf("Unexpected value: %v", v)
}
}
func TestPropertyOrder(t *testing.T) {
const SCRIPT = `
var o = {};
var sym1 = Symbol(1);
var sym2 = Symbol(2);
o[sym2] = 1;
o[4294967294] = 1;
o[2] = 1;
o[1] = 1;
o[0] = 1;
o["02"] = 1;
o[4294967295] = 1;
o["01"] = 1;
o["00"] = 1;
o[sym1] = 1;
var expected = ["0", "1", "2", "4294967294", "02", "4294967295", "01", "00", sym2, sym1];
var actual = Reflect.ownKeys(o);
if (actual.length !== expected.length) {
throw new Error("Unexpected length: "+actual.length);
}
for (var i = 0; i < actual.length; i++) {
if (actual[i] !== expected[i]) {
throw new Error("Unexpected list: " + actual);
}
}
`
testScript1(SCRIPT, _undefined, t)
}
func TestDefinePropertiesSymbol(t *testing.T) {
const SCRIPT = `
var desc = {};
desc[Symbol.toStringTag] = {value: "Test"};
var o = {};
Object.defineProperties(o, desc);
o[Symbol.toStringTag] === "Test";
`
testScript1(SCRIPT, valueTrue, t)
}
func TestObjectShorthandProperties(t *testing.T) {
const SCRIPT = `
var b = 1;
var a = {b, get() {return "c"}};
assert.sameValue(a.b, b, "#1");
assert.sameValue(a.get(), "c", "#2");
var obj = {
w\u0069th() { return 42; }
};
assert.sameValue(obj['with'](), 42, 'property exists');
`
testScript1(TESTLIB+SCRIPT, _undefined, t)
}
func TestObjectAssign(t *testing.T) {
const SCRIPT = `
assert.sameValue(Object.assign({ b: 1 }, { get a() {
Object.defineProperty(this, "b", {
value: 3,
enumerable: false
});
}, b: 2 }).b, 1, "#1");
assert.sameValue(Object.assign({ b: 1 }, { get a() {
delete this.b;
}, b: 2 }).b, 1, "#2");
`
testScript1(TESTLIB+SCRIPT, _undefined, t)
}
func TestExportCircular(t *testing.T) {
vm := New()
o := vm.NewObject()
o.Set("o", o)
v := o.Export()
if m, ok := v.(map[string]interface{}); ok {
if reflect.ValueOf(m["o"]).Pointer() != reflect.ValueOf(v).Pointer() {
t.Fatal("Unexpected value")
}
} else {
t.Fatal("Unexpected type")
}
res, err := vm.RunString(`var a = []; a[0] = a;`)
if err != nil {
t.Fatal(err)
}
v = res.Export()
if a, ok := v.([]interface{}); ok {
if reflect.ValueOf(a[0]).Pointer() != reflect.ValueOf(v).Pointer() {
t.Fatal("Unexpected value")
}
} else {
t.Fatal("Unexpected type")
}
}
type test_s struct {
S *test_s1
}
type test_s1 struct {
S *test_s
}
func TestExportToCircular(t *testing.T) {
vm := New()
o := vm.NewObject()
o.Set("o", o)
var m map[string]interface{}
err := vm.ExportTo(o, &m)
if err != nil {
t.Fatal(err)
}
type K string
type T map[K]T
var m1 T
err = vm.ExportTo(o, &m1)
if err != nil {
t.Fatal(err)
}
type A []A
var a A
res, err := vm.RunString("var a = []; a[0] = a;")
if err != nil {
t.Fatal(err)
}
err = vm.ExportTo(res, &a)
if err != nil {
t.Fatal(err)
}
if &a[0] != &a[0][0] {
t.Fatal("values do not match")
}
o = vm.NewObject()
o.Set("S", o)
var s test_s
err = vm.ExportTo(o, &s)
if err != nil {
t.Fatal(err)
}
if s.S.S != &s {
t.Fatalf("values do not match: %v, %v", s.S.S, &s)
}
type test_s2 struct {
S interface{}
S1 *test_s2
}
var s2 test_s2
o.Set("S1", o)
err = vm.ExportTo(o, &s2)
if err != nil {
t.Fatal(err)
}
if m, ok := s2.S.(map[string]interface{}); ok {
if reflect.ValueOf(m["S"]).Pointer() != reflect.ValueOf(m).Pointer() {
t.Fatal("Unexpected m.S")
}
} else {
t.Fatalf("Unexpected s2.S type: %T", s2.S)
}
if s2.S1 != &s2 {
t.Fatal("Unexpected s2.S1")
}
o1 := vm.NewObject()
o1.Set("S", o)
o1.Set("S1", o)
err = vm.ExportTo(o1, &s2)
if err != nil {
t.Fatal(err)
}
if s2.S1.S1 != s2.S1 {
t.Fatal("Unexpected s2.S1.S1")
}
}
func TestExportWrappedMap(t *testing.T) {
vm := New()
m := map[string]interface{}{
"test": "failed",
}
exported := vm.ToValue(m).Export()
if exportedMap, ok := exported.(map[string]interface{}); ok {
exportedMap["test"] = "passed"
if v := m["test"]; v != "passed" {
t.Fatalf("Unexpected m[\"test\"]: %v", v)
}
} else {
t.Fatalf("Unexpected export type: %T", exported)
}
}
func TestExportToWrappedMap(t *testing.T) {
vm := New()
m := map[string]interface{}{
"test": "failed",
}
var exported map[string]interface{}
err := vm.ExportTo(vm.ToValue(m), &exported)
if err != nil {
t.Fatal(err)
}
exported["test"] = "passed"
if v := m["test"]; v != "passed" {
t.Fatalf("Unexpected m[\"test\"]: %v", v)
}
}
func TestExportToWrappedMapCustom(t *testing.T) {
type CustomMap map[string]bool
vm := New()
m := CustomMap{}
var exported CustomMap
err := vm.ExportTo(vm.ToValue(m), &exported)
if err != nil {
t.Fatal(err)
}
exported["test"] = true
if v := m["test"]; v != true {
t.Fatalf("Unexpected m[\"test\"]: %v", v)
}
}
func ExampleObject_Delete() {
vm := New()
obj := vm.NewObject()
_ = obj.Set("test", true)
before := obj.Get("test")
_ = obj.Delete("test")
after := obj.Get("test")
fmt.Printf("before: %v, after: %v", before, after)
// Output: before: true, after: <nil>
}
func BenchmarkPut(b *testing.B) {
v := &Object{}
o := &baseObject{
val: v,
extensible: true,
}
v.self = o
o.init()
var key Value = asciiString("test")
var val Value = valueInt(123)
for i := 0; i < b.N; i++ {
v.setOwn(key, val, false)
}
}
func BenchmarkPutStr(b *testing.B) {
v := &Object{}
o := &baseObject{
val: v,
extensible: true,
}
o.init()
v.self = o
var val Value = valueInt(123)
for i := 0; i < b.N; i++ {
o.setOwnStr("test", val, false)
}
}
func BenchmarkGet(b *testing.B) {
v := &Object{}
o := &baseObject{
val: v,
extensible: true,
}
o.init()
v.self = o
var n Value = asciiString("test")
for i := 0; i < b.N; i++ {
v.get(n, nil)
}
}
func BenchmarkGetStr(b *testing.B) {
v := &Object{}
o := &baseObject{
val: v,
extensible: true,
}
v.self = o
o.init()
for i := 0; i < b.N; i++ {
o.getStr("test", nil)
}
}
func _toString(v Value) string {
switch v := v.(type) {
case asciiString:
return string(v)
default:
return ""
}
}
func BenchmarkToString1(b *testing.B) {
v := asciiString("test")
for i := 0; i < b.N; i++ {
v.toString()
}
}
func BenchmarkToString2(b *testing.B) {
v := asciiString("test")
for i := 0; i < b.N; i++ {
_toString(v)
}
}
func BenchmarkConv(b *testing.B) {
count := int64(0)
for i := 0; i < b.N; i++ {
count += valueInt(123).ToInteger()
}
if count == 0 {
b.Fatal("zero")
}
}
func BenchmarkToUTF8String(b *testing.B) {
var s valueString = asciiString("test")
for i := 0; i < b.N; i++ {
_ = s.String()
}
}
func BenchmarkAdd(b *testing.B) {
var x, y Value
x = valueInt(2)
y = valueInt(2)
for i := 0; i < b.N; i++ {
if xi, ok := x.(valueInt); ok {
if yi, ok := y.(valueInt); ok {
x = xi + yi
}
}
}
}
func BenchmarkAddString(b *testing.B) {
var x, y Value
tst := asciiString("22")
x = asciiString("2")
y = asciiString("2")
for i := 0; i < b.N; i++ {
var z Value
if xi, ok := x.(valueString); ok {
if yi, ok := y.(valueString); ok {
z = xi.concat(yi)
}
}
if !z.StrictEquals(tst) {
b.Fatalf("Unexpected result %v", x)
}
}
}