This repository was archived by the owner on Jan 23, 2024. It is now read-only.
forked from dop251/goja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuiltin_map_test.go
166 lines (145 loc) · 2.89 KB
/
builtin_map_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
package goja
import (
"fmt"
"hash/maphash"
"testing"
)
func TestMapEvilIterator(t *testing.T) {
const SCRIPT = `
'use strict';
var o = {};
function Iter(value) {
this.value = value;
this.idx = 0;
}
Iter.prototype.next = function() {
var idx = this.idx;
if (idx === 0) {
this.idx++;
return this.value;
}
return {done: true};
}
o[Symbol.iterator] = function() {
return new Iter({});
}
assert.throws(TypeError, function() {
new Map(o);
});
o[Symbol.iterator] = function() {
return new Iter({value: []});
}
function t(prefix) {
var m = new Map(o);
assert.sameValue(1, m.size, prefix+": m.size");
assert.sameValue(true, m.has(undefined), prefix+": m.has(undefined)");
assert.sameValue(undefined, m.get(undefined), prefix+": m.get(undefined)");
}
t("standard adder");
var count = 0;
var origSet = Map.prototype.set;
Map.prototype.set = function() {
count++;
origSet.apply(this, arguments);
}
t("custom adder");
assert.sameValue(1, count, "count");
undefined;
`
testScriptWithTestLib(SCRIPT, _undefined, t)
}
func ExampleObject_Export_map() {
vm := New()
m, err := vm.RunString(`
new Map([[1, true], [2, false]]);
`)
if err != nil {
panic(err)
}
exp := m.Export()
fmt.Printf("%T, %v\n", exp, exp)
// Output: [][2]interface {}, [[1 true] [2 false]]
}
func ExampleRuntime_ExportTo_mapToMap() {
vm := New()
m, err := vm.RunString(`
new Map([[1, true], [2, false]]);
`)
if err != nil {
panic(err)
}
exp := make(map[int]bool)
err = vm.ExportTo(m, &exp)
if err != nil {
panic(err)
}
fmt.Println(exp)
// Output: map[1:true 2:false]
}
func ExampleRuntime_ExportTo_mapToSlice() {
vm := New()
m, err := vm.RunString(`
new Map([[1, true], [2, false]]);
`)
if err != nil {
panic(err)
}
exp := make([][]interface{}, 0)
err = vm.ExportTo(m, &exp)
if err != nil {
panic(err)
}
fmt.Println(exp)
// Output: [[1 true] [2 false]]
}
func ExampleRuntime_ExportTo_mapToTypedSlice() {
vm := New()
m, err := vm.RunString(`
new Map([[1, true], [2, false]]);
`)
if err != nil {
panic(err)
}
exp := make([][2]interface{}, 0)
err = vm.ExportTo(m, &exp)
if err != nil {
panic(err)
}
fmt.Println(exp)
// Output: [[1 true] [2 false]]
}
func BenchmarkMapDelete(b *testing.B) {
var key1 Value = asciiString("a")
var key2 Value = asciiString("b")
one := intToValue(1)
two := intToValue(2)
for i := 0; i < b.N; i++ {
m := newOrderedMap(&maphash.Hash{})
m.set(key1, one)
m.set(key2, two)
if !m.remove(key1) {
b.Fatal("remove() returned false")
}
}
}
func BenchmarkMapDeleteJS(b *testing.B) {
prg, err := Compile("test.js", `
var m = new Map([['a',1], ['b', 2]]);
var result = m.delete('a');
if (!result || m.size !== 1) {
throw new Error("Fail!");
}
`,
false)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
vm := New()
_, err := vm.RunProgram(prg)
if err != nil {
b.Fatal(err)
}
}
}