Debian Codegraph @

js/src/jit-test/tests/ion/object-keys-03.js

raw
1
2 // This test case checks that some code can be optimized for non-proxy object,
3 // and than we can correctly fallback if a proxy object ever flow into this
4 // code.
5
6 // Similar functions are part of popular framework such as React and Angular.
7 function shallowEqual(o1, o2) {
8 var k1 = Object.keys(o1);
9 var k2 = Object.keys(o2);
10 if (k1.length != k2.length) {
11 return false;
12 }
13 for (var k = 0; k < k1.length; k++) {
14 if (!Object.hasOwnProperty.call(o2, k1[k]) || !Object.is(o1[k1[k]], o2[k1[k]])) {
15 return false;
16 }
17 }
18 return true;
19 }
20
21 let sideEffectCounter = 0;
22 const payload = {x: 5, a: 1, b: 2, c: 3, d: 4};
23 const handler = {
24 ownKeys(target) {
25 // side-effect that should not be removed.
26 sideEffectCounter++;
27 // answer returned.
28 return Reflect.ownKeys(target);
29 },
30 };
31 const proxy = new Proxy(payload, handler);
32
33 let objs = [
34 {x:0, a: 1, b: 2},
35 {x:1, b: 1, c: 2},
36 {x:2, c: 1, d: 2},
37 {x:3, a: 1, b: 2, c: 3},
38 {x:4, b: 1, c: 2, d: 3},
39 {x:5, a: 1, b: 2, c: 3, d: 4}
40 ];
41
42 // Ion compile shallowEqual ...
43 with({}) {}
44 let iterMax = 1000;
45 for (let i = 0; i < iterMax; i++) {
46 for (let o1 of objs) {
47 for (let o2 of objs) {
48 assertEq(shallowEqual(o1, o2), Object.is(o1, o2));
49 }
50 }
51 }
52
53 assertEq(sideEffectCounter, 0);
54
55 // ... before calling it with a proxy.
56 // This should bailout with a guard failure.
57 shallowEqual(objs[0], proxy);
58
59 // Assert that the proxy's ownKeys function has been called.
60 assertEq(sideEffectCounter, 1);