-
Notifications
You must be signed in to change notification settings - Fork 586
/
global_test.go
352 lines (290 loc) · 11 KB
/
global_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
package otto
import (
"fmt"
"math"
"strings"
"testing"
)
func TestGlobal(t *testing.T) {
tt(t, func() {
test, vm := test()
runtime := vm.vm.runtime
{
call := func(obj interface{}, src string, argumentList ...interface{}) Value {
var tgt *Object
switch obj := obj.(type) {
case Value:
tgt = obj.Object()
case *Object:
tgt = obj
case *object:
tgt = objectValue(obj).Object()
default:
panic("Here be dragons.")
}
value, err := tgt.Call(src, argumentList...)
is(err, nil)
return value
}
// FIXME enterGlobalScope
if false {
value := runtime.scope.lexical.getBinding(classObjectName, false).object().call(UndefinedValue(), []Value{toValue(runtime.newObject())}, false, nativeFrame)
is(value.IsObject(), true)
is(value, "[object Object]")
is(value.object().prototype == runtime.global.ObjectPrototype, true)
is(value.object().prototype == runtime.global.Object.get("prototype").object(), true)
is(value.object().get("toString"), "function toString() { [native code] }")
is(call(value.Object(), "hasOwnProperty", "hasOwnProperty"), false)
is(call(value.object().get("toString").object().prototype, "toString"), "function () { [native code] }") // TODO Is this right?
is(value.object().get("toString").object().get("toString"), "function toString() { [native code] }")
is(value.object().get("toString").object().get("toString").object(), "function toString() { [native code] }")
is(call(value.object(), "propertyIsEnumerable", "isPrototypeOf"), false)
value.object().put("xyzzy", stringValue("Nothing happens."), false)
is(call(value, "propertyIsEnumerable", "isPrototypeOf"), false)
is(call(value, "propertyIsEnumerable", "xyzzy"), true)
is(value.object().get("xyzzy"), "Nothing happens.")
is(call(runtime.scope.lexical.getBinding(classObjectName, false), "isPrototypeOf", value), false)
is(call(runtime.scope.lexical.getBinding(classObjectName, false).object().get("prototype"), "isPrototypeOf", value), true)
is(call(runtime.scope.lexical.getBinding(classFunctionName, false), "isPrototypeOf", value), false)
is(runtime.newObject().prototype == runtime.global.Object.get("prototype").object(), true)
abc := runtime.newBoolean(boolValue(true))
is(objectValue(abc), "true") // TODO Call primitive?
}
}
test(`new Number().constructor == Number`, true)
test(`this.hasOwnProperty`, "function hasOwnProperty() { [native code] }")
test(`eval.length === 1`, true)
test(`eval.prototype === undefined`, true)
test(`raise: new eval()`, "TypeError: function eval() { [native code] } is not a constructor")
test(`
[
[ delete undefined, undefined ],
[ delete NaN, NaN ],
[ delete Infinity, Infinity ],
];
`, "false,,false,NaN,false,Infinity")
test(`
Object.getOwnPropertyNames(Function('return this')()).sort();
`, "Array,Boolean,Date,Error,EvalError,Function,Infinity,JSON,Math,NaN,Number,Object,RangeError,ReferenceError,RegExp,String,SyntaxError,TypeError,URIError,console,decodeURI,decodeURIComponent,encodeURI,encodeURIComponent,escape,eval,isFinite,isNaN,parseFloat,parseInt,undefined,unescape")
// __defineGetter__,__defineSetter__,__lookupGetter__,__lookupSetter__,constructor,hasOwnProperty,isPrototypeOf,propertyIsEnumerable,toLocaleString,toString,valueOf
test(`
Object.getOwnPropertyNames(Object.prototype).sort();
`, "constructor,hasOwnProperty,isPrototypeOf,propertyIsEnumerable,toLocaleString,toString,valueOf")
// arguments,caller,length,name,prototype
test(`
Object.getOwnPropertyNames(EvalError).sort();
`, "length,prototype")
test(`
var abc = [];
var def = [EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError];
for (constructor in def) {
abc.push(def[constructor] === def[constructor].prototype.constructor);
}
def = [Array, Boolean, Date, Function, Number, Object, RegExp, String, SyntaxError];
for (constructor in def) {
abc.push(def[constructor] === def[constructor].prototype.constructor);
}
abc;
`, "true,true,true,true,true,true,true,true,true,true,true,true,true,true,true")
test(`
[ Array.prototype.constructor === Array, Array.constructor === Function ];
`, "true,true")
test(`
[ Number.prototype.constructor === Number, Number.constructor === Function ];
`, "true,true")
test(`
[ Function.prototype.constructor === Function, Function.constructor === Function ];
`, "true,true")
})
}
func TestGlobalLength(t *testing.T) {
tt(t, func() {
test, _ := test()
test(`
[ Object.length, Function.length, RegExp.length, Math.length ];
`, "1,1,2,")
})
}
func TestGlobalError(t *testing.T) {
tt(t, func() {
test, _ := test()
test(`
[ TypeError.length, TypeError(), TypeError("Nothing happens.") ];
`, "1,TypeError,TypeError: Nothing happens.")
test(`
[ URIError.length, URIError(), URIError("Nothing happens.") ];
`, "1,URIError,URIError: Nothing happens.")
})
}
func TestGlobalReadOnly(t *testing.T) {
tt(t, func() {
test, _ := test()
test(`Number.POSITIVE_INFINITY`, math.Inf(1))
test(`
Number.POSITIVE_INFINITY = 1;
`, 1)
test(`Number.POSITIVE_INFINITY`, math.Inf(1))
test(`
Number.POSITIVE_INFINITY = 1;
Number.POSITIVE_INFINITY;
`, math.Inf(1))
})
}
func Test_isNaN(t *testing.T) {
tt(t, func() {
test, _ := test()
test(`isNaN(0)`, false)
test(`isNaN("Xyzzy")`, true)
test(`isNaN()`, true)
test(`isNaN(NaN)`, true)
test(`isNaN(Infinity)`, false)
test(`isNaN.length === 1`, true)
test(`isNaN.prototype === undefined`, true)
})
}
func Test_isFinite(t *testing.T) {
tt(t, func() {
test, _ := test()
test(`isFinite(0)`, true)
test(`isFinite("Xyzzy")`, false)
test(`isFinite()`, false)
test(`isFinite(NaN)`, false)
test(`isFinite(Infinity)`, false)
test(`isFinite(new Number(451));`, true)
test(`isFinite.length === 1`, true)
test(`isFinite.prototype === undefined`, true)
})
}
func Test_parseInt(t *testing.T) {
tt(t, func() {
test, _ := test()
test(`parseInt("0")`, 0)
test(`parseInt("11")`, 11)
test(`parseInt(" 11")`, 11)
test(`parseInt("11 ")`, 11)
test(`parseInt(" 11 ")`, 11)
test(`parseInt(" 11\n")`, 11)
test(`parseInt(" 11\n", 16)`, 17)
test(`parseInt("Xyzzy")`, naN)
test(`parseInt(" 0x11\n", 16)`, 17)
test(`parseInt("0x0aXyzzy", 16)`, 10)
test(`parseInt("0x1", 0)`, 1)
test(`parseInt("0x10000000000000000000", 16)`, float64(75557863725914323419136))
test(`parseInt.length === 2`, true)
test(`parseInt.prototype === undefined`, true)
})
}
func Test_parseFloat(t *testing.T) {
tt(t, func() {
test, _ := test()
test(`parseFloat("0")`, 0)
test(`parseFloat("11")`, 11)
test(`parseFloat(" 11")`, 11)
test(`parseFloat("11 ")`, 11)
test(`parseFloat(" 11 ")`, 11)
test(`parseFloat(" 11\n")`, 11)
test(`parseFloat(" 11\n", 16)`, 11)
test(`parseFloat("11.1")`, 11.1)
test(`parseFloat("Xyzzy")`, naN)
test(`parseFloat(" 0x11\n", 16)`, 0)
test(`parseFloat("0x0a")`, 0)
test(`parseFloat("0x0aXyzzy")`, 0)
test(`parseFloat("Infinity")`, infinity)
test(`parseFloat("infinity")`, naN)
test(`parseFloat("0x")`, 0)
test(`parseFloat("11x")`, 11)
test(`parseFloat("Infinity1")`, infinity)
test(`parseFloat.length === 1`, true)
test(`parseFloat.prototype === undefined`, true)
})
}
func Test_encodeURI(t *testing.T) {
tt(t, func() {
test, _ := test()
test(`encodeURI("http://example.com/ Nothing happens.")`, "http://example.com/%20Nothing%20happens.")
test(`encodeURI("http://example.com/ _^#")`, "http://example.com/%20_%5E#")
test(`encodeURI(String.fromCharCode("0xE000"))`, "%EE%80%80")
test(`encodeURI(String.fromCharCode("0xFFFD"))`, "%EF%BF%BD")
test(`raise: encodeURI(String.fromCharCode("0xDC00"))`, "URIError: URI malformed")
test(`encodeURI.length === 1`, true)
test(`encodeURI.prototype === undefined`, true)
})
}
func Test_encodeURIComponent(t *testing.T) {
tt(t, func() {
test, _ := test()
test(`encodeURIComponent("http://example.com/ Nothing happens.")`, "http%3A%2F%2Fexample.com%2F%20Nothing%20happens.")
test(`encodeURIComponent("http://example.com/ _^#")`, "http%3A%2F%2Fexample.com%2F%20_%5E%23")
})
}
func Test_decodeURI(t *testing.T) {
tt(t, func() {
test, _ := test()
test(`decodeURI(encodeURI("http://example.com/ Nothing happens."))`, "http://example.com/ Nothing happens.")
test(`decodeURI(encodeURI("http://example.com/ _^#"))`, "http://example.com/ _^#")
test(`raise: decodeURI("http://example.com/ _^#%")`, "URIError: URI malformed")
test(`raise: decodeURI("%DF%7F")`, "URIError: URI malformed")
for _, check := range strings.Fields("+ %3B %2F %3F %3A %40 %26 %3D %2B %24 %2C %23") {
test(fmt.Sprintf(`decodeURI("%s")`, check), check)
}
test(`decodeURI.length === 1`, true)
test(`decodeURI.prototype === undefined`, true)
})
}
func Test_decodeURIComponent(t *testing.T) {
tt(t, func() {
test, _ := test()
test(`decodeURIComponent(encodeURI("http://example.com/ Nothing happens."))`, "http://example.com/ Nothing happens.")
test(`decodeURIComponent(encodeURI("http://example.com/ _^#"))`, "http://example.com/ _^#")
test(`decodeURIComponent.length === 1`, true)
test(`decodeURIComponent.prototype === undefined`, true)
test(`
var global = Function('return this')();
var abc = Object.getOwnPropertyDescriptor(global, "decodeURIComponent");
[ abc.value === global.decodeURIComponent, abc.writable, abc.enumerable, abc.configurable ];
`, "true,true,false,true")
})
}
func TestGlobal_skipEnumeration(t *testing.T) {
tt(t, func() {
test, _ := test()
test(`
var found = [];
for (var test in this) {
if (false ||
test === 'NaN' ||
test === 'undefined' ||
test === 'Infinity' ||
false) {
found.push(test)
}
}
found.length;
`, 0)
test(`
var found = [];
for (var test in this) {
if (false ||
test === 'Object' ||
test === 'Function' ||
test === 'String' ||
test === 'Number' ||
test === 'Array' ||
test === 'Boolean' ||
test === 'Date' ||
test === 'RegExp' ||
test === 'Error' ||
test === 'EvalError' ||
test === 'RangeError' ||
test === 'ReferenceError' ||
test === 'SyntaxError' ||
test === 'TypeError' ||
test === 'URIError' ||
false) {
found.push(test)
}
}
found.length;
`, 0)
})
}