Debian Codegraph @

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

raw
1
2 // This test case check some code frequently used in the wild, with some object
3 // (proxy) for which the optimization we have deployed do not work, as the
4 // assumption the Object.keys(...) can be elided without having noticeable
5 // side-effects.
6
7 // Similar functions are part of popular framework such as React and Angular.
8 function shallowEqual(o1, o2) {
9 var k1 = Object.keys(o1);
10 var k2 = Object.keys(o2);
11 if (k1.length != k2.length) {
12 return false;
13 }
14 for (var k = 0; k < k1.length; k++) {
15 if (!Object.hasOwnProperty.call(o2, k1[k]) || !Object.is(o1[k1[k]], o2[k1[k]])) {
16 return false;
17 }
18 }
19 return true;
20 }
21
22 let sideEffectCounter = 0;
23 const payload = {x: 5, a: 1, b: 2, c: 3, d: 4};
24 const handler = {
25 ownKeys(target) {
26 // side-effect that should not be removed.
27 sideEffectCounter++;
28 // answer returned.
29 return Reflect.ownKeys(target);
30 },
31 };
32 const proxy = new Proxy(payload, handler);
33
34 let objs = [
35 {x:0, a: 1, b: 2},
36 {x:1, b: 1, c: 2},
37 {x:2, c: 1, d: 2},
38 {x:3, a: 1, b: 2, c: 3},
39 {x:4, b: 1, c: 2, d: 3},
40 proxy
41 ];
42
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 let expectedSideEffects = 2 * objs.length * iterMax;
54 assertEq(sideEffectCounter, expectedSideEffects);