Debian Codegraph @

js/src/jit-test/tests/cacheir/binaryarith-date-valueOf-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 valueOf 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 originalDateValueOf = Date.prototype.valueOf;
23 let counter = 0;
24 Date.prototype.valueOf = function() {
25 counter++;
26 return originalDateValueOf.call(this);
27 }
28
29 assertEq(funDateSub(new Date("2024-09-20T19:54:27.432Z"), new Date("2024-09-20T19:54:27.422Z")), 10);
30 assertEq(counter, 2);
31 }
32
33 {
34 // Check that a pre-existing Date.prototype.valueOf override does not affect the inlined Date subtraction.
35 let counter = 0;
36 const originalDateValueOf = Date.prototype.valueOf;
37 Date.prototype.valueOf = function() {
38 counter++;
39 return originalDateValueOf.call(this);
40 }
41
42 const funDateSub = (a, b) => { return a - b; }
43 assertEq(funDateSub(new Date("2024-09-20T19:54:27.432Z"), new Date("2024-09-20T19:54:27.422Z")), 10);
44 assertEq(counter, 2);
45
46 const testCases = [[new Date("2024-09-20T19:54:27.432Z"), new Date("2024-09-20T19:54:27.427Z"), 5],
47 [new Date("2024-09-20T19:54:27.432Z"), 1726862067427, 5],
48 [1726862067427, new Date("2024-09-20T19:54:27.432Z"), -5]];
49 warmup(funDateSub, testCases);
50 counter = 0;
51
52 assertEq(funDateSub(new Date("2024-09-20T19:54:27.432Z"), new Date("2024-09-20T19:54:27.422Z")), 10);
53 assertEq(funDateSub(new Date("2024-09-20T19:54:27.432Z"), 1726862067427), 5);
54 assertEq(counter, 3);
55 }
56
57 {
58 // Check that using extended date classes does not affect the inlined Date subtraction.
59 const testCases = [[new Date("2024-09-20T19:54:27.432Z"), new Date("2024-09-20T19:54:27.427Z"), 5],
60 [new Date("2024-09-20T19:54:27.432Z"), 1726862067427, 5],
61 [1726862067427, new Date("2024-09-20T19:54:27.432Z"), -5]];
62 const funDateSub = (a, b) => { return a - b; }
63 warmup(funDateSub, testCases);
64
65 let counter = 0;
66 class MyDate extends Date {
67 valueOf() {
68 counter++;
69 return super.valueOf();
70 }
71 }
72
73 assertEq(funDateSub(new MyDate("2024-09-20T19:54:27.432Z"), new MyDate("2024-09-20T19:54:27.422Z")), 10);
74 assertEq(funDateSub(1726862067427, new MyDate("2024-09-20T19:54:27.432Z")), -5);
75 assertEq(counter, 3);
76 }