Debian Codegraph @

js/src/jit-test/tests/cacheir/binaryarith-date-symbol-toPrimitive-pollution.js

raw
1 function warmup(fun, input_array) {
2 for (var index = 0; index < input_array.length; index++) {
3 input = input_array[index];
4 input_lhs = input[0];
5 input_rhs = input[1];
6 output = input[2];
7 for (var i = 0; i < 30; i++) {
8 var y = fun(input_lhs, input_rhs);
9 assertEq(y, output)
10 }
11 }
12 }
13
14 {
15 // Check that changing the Symbol.toPrimitive function of Date.prototype does not affect the inlined Date subtraction.
16 const testCases = [[new Date("2024-09-20T19:54:27.432Z"), new Date("2024-09-20T19:54:27.427Z"), 5],
17 [new Date("2024-09-20T19:54:27.432Z"), 1726862067427, 5],
18 [1726862067427, new Date("2024-09-20T19:54:27.432Z"), -5]];
19 const funDateSub = (a, b) => { return a - b; }
20 warmup(funDateSub, testCases);
21
22 const originalDateToPrimitive = Date.prototype[Symbol.toPrimitive];
23 let counter = 0;
24 Object.defineProperty(Date.prototype, Symbol.toPrimitive, {
25 value: function(hint) {
26 counter++;
27 return originalDateToPrimitive.call(this, hint);
28 },
29 configurable: true,
30 });
31
32 assertEq(funDateSub(new Date("2024-09-20T19:54:27.432Z"), new Date("2024-09-20T19:54:27.422Z")), 10);
33 assertEq(counter, 2);
34 }
35
36 {
37 // Check that a pre-existing Date.prototype[Symbol.toPrimitive] override does not affect the inlined Date subtraction.
38 let counter = 0;
39 const originalDateToPrimitive = Date.prototype[Symbol.toPrimitive];
40 Object.defineProperty(Date.prototype, Symbol.toPrimitive, {
41 value: function(hint) {
42 counter++;
43 return originalDateToPrimitive.call(this, hint);
44 },
45 });
46
47 const funDateSub = (a, b) => { return a - b; }
48 assertEq(funDateSub(new Date("2024-09-20T19:54:27.432Z"), new Date("2024-09-20T19:54:27.422Z")), 10);
49 assertEq(counter, 2);
50
51 const testCases = [[new Date("2024-09-20T19:54:27.432Z"), new Date("2024-09-20T19:54:27.427Z"), 5],
52 [new Date("2024-09-20T19:54:27.432Z"), 1726862067427, 5],
53 [1726862067427, new Date("2024-09-20T19:54:27.432Z"), -5]];
54 warmup(funDateSub, testCases);
55 counter = 0;
56
57 assertEq(funDateSub(new Date("2024-09-20T19:54:27.432Z"), new Date("2024-09-20T19:54:27.422Z")), 10);
58 assertEq(funDateSub(new Date("2024-09-20T19:54:27.432Z"), 1726862067427), 5);
59 assertEq(counter, 3);
60 }
61
62 {
63 // Check that using extended date classes does not affect the inlined Date subtraction.
64 const testCases = [[new Date("2024-09-20T19:54:27.432Z"), new Date("2024-09-20T19:54:27.427Z"), 5],
65 [new Date("2024-09-20T19:54:27.432Z"), 1726862067427, 5],
66 [1726862067427, new Date("2024-09-20T19:54:27.432Z"), -5]];
67 const funDateSub = (a, b) => { return a - b; }
68 warmup(funDateSub, testCases);
69
70 let counter = 0;
71 class MyDate extends Date {
72 [Symbol.toPrimitive]() {
73 counter++;
74 return super.valueOf();
75 }
76 }
77
78 assertEq(funDateSub(new MyDate("2024-09-20T19:54:27.432Z"), new MyDate("2024-09-20T19:54:27.422Z")), 10);
79 assertEq(funDateSub(1726862067427, new MyDate("2024-09-20T19:54:27.432Z")), -5);
80 assertEq(counter, 3);
81 }