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
|
const OriginalHasInstance = Function.prototype[Symbol.hasInstance];
// Ensure that folding doesn't impact user defined @@hasInstance methods.
{
function Test() {
this.x = 1;
}
Object.defineProperty(Test, Symbol.hasInstance,
{writable: true, value: () => false});
function x(t) {
return t instanceof Test;
}
function y() {
let t = new Test;
let b = true;
for (let i = 0; i < 10; i++) {
b = b && x(t);
}
return b;
}
function z() {
let f = 0;
let t = 0;
for (let i = 0; i < 100; i++)
assertEq(y(), false);
}
z();
}
// Ensure that the jitting does not clobber user defined @@hasInstance methods.
{
function a() {
function b() {};
b.__proto__ = a.prototype;
return b;
};
let c = new a();
let t = 0;
let f = 0;
let e = 0;
for (let i = 0; i < 40000; i++) {
if (i == 20000)
Object.defineProperty(a.prototype, Symbol.hasInstance,
{writable: true, value: () => true});
if (i == 30000)
Object.setPrototypeOf(c, Function.prototype);
if (1 instanceof c) {
t++;
} else {
f++;
}
}
assertEq(t, 10000);
assertEq(f, 30000);
}
{
function a() {};
function b() {};
Object.defineProperty(a, Symbol.hasInstance, {writable: true, value: () => true});
assertEq(b instanceof a, true);
for (let _ of Array(10000))
assertEq(b instanceof a, true);
}
{
function a(){};
function b(){};
function c(){};
function d(){};
function e(){};
Object.defineProperty(a, Symbol.hasInstance, {value: () => true });
Object.defineProperty(b, Symbol.hasInstance, {value: () => true });
Object.defineProperty(c, Symbol.hasInstance, {value: () => true });
Object.defineProperty(d, Symbol.hasInstance, {value: () => true });
let funcs = [a, b, c, d];
for (let f of funcs)
assertEq(e instanceof f, true);
for (let _ of Array(10001)) {
for (let f of funcs)
assertEq(e instanceof f, true);
}
}
if (typeof reportCompare === "function")
reportCompare(true, true);
|