2016-12-19 10:13:48 +00:00
|
|
|
// Copyright 2016 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
// This is intended for permanent JS behavior changes for mocking out
|
|
|
|
// non-deterministic behavior. For temporary suppressions, please refer to
|
|
|
|
// v8_suppressions.js.
|
|
|
|
// This file is loaded before each correctness test cases and won't get
|
|
|
|
// minimized.
|
|
|
|
|
|
|
|
|
|
|
|
// This will be overridden in the test cases. The override can be minimized.
|
2018-02-22 03:06:23 +00:00
|
|
|
var prettyPrinted = function prettyPrinted(msg) { return msg; };
|
2016-12-19 10:13:48 +00:00
|
|
|
|
|
|
|
// Mock Math.random.
|
2020-02-18 08:41:57 +00:00
|
|
|
(function() {
|
2020-04-23 17:48:44 +00:00
|
|
|
let index = 1;
|
2017-01-25 14:59:39 +00:00
|
|
|
Math.random = function() {
|
2020-04-23 17:48:44 +00:00
|
|
|
const x = Math.sin(index++) * 10000;
|
|
|
|
return x - Math.floor(x);
|
2017-01-25 14:59:39 +00:00
|
|
|
}
|
|
|
|
})();
|
2016-12-19 10:13:48 +00:00
|
|
|
|
2020-04-19 09:22:36 +00:00
|
|
|
// Mock Math.pow. Work around an optimization for -0.5.
|
|
|
|
(function() {
|
|
|
|
const origMathPow = Math.pow;
|
|
|
|
Math.pow = function(a, b) {
|
|
|
|
if (b === -0.5) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return origMathPow(a, b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
2016-12-19 10:13:48 +00:00
|
|
|
// Mock Date.
|
2020-02-18 08:41:57 +00:00
|
|
|
(function() {
|
|
|
|
let index = 0;
|
|
|
|
let mockDate = 1477662728696;
|
|
|
|
const mockDateNow = function() {
|
|
|
|
index = (index + 1) % 10;
|
|
|
|
mockDate = mockDate + index + 1;
|
|
|
|
return mockDate;
|
2017-01-25 14:59:39 +00:00
|
|
|
}
|
2016-12-19 10:13:48 +00:00
|
|
|
|
2020-02-18 08:41:57 +00:00
|
|
|
const origDate = Date;
|
2020-02-20 15:57:23 +00:00
|
|
|
const construct = Reflect.construct;
|
2020-02-18 08:41:57 +00:00
|
|
|
const constructDate = function(args) {
|
|
|
|
let result;
|
|
|
|
if (args.length) {
|
2020-02-20 15:57:23 +00:00
|
|
|
result = construct(origDate, args);
|
2017-03-02 15:40:45 +00:00
|
|
|
} else {
|
2020-02-18 08:41:57 +00:00
|
|
|
result = new origDate(mockDateNow());
|
2017-03-02 15:40:45 +00:00
|
|
|
}
|
|
|
|
result.constructor = function(...args) { return constructDate(args); }
|
|
|
|
Object.defineProperty(
|
|
|
|
result, "constructor", { configurable: false, writable: false });
|
2020-02-18 08:41:57 +00:00
|
|
|
return result;
|
2017-03-02 15:40:45 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 09:42:38 +00:00
|
|
|
origDate.prototype.constructor = function(...args) {
|
|
|
|
return constructDate(args);
|
|
|
|
};
|
|
|
|
|
2017-01-25 14:59:39 +00:00
|
|
|
var handler = {
|
2020-02-18 08:41:57 +00:00
|
|
|
apply: function(target, thisArg, args) {
|
|
|
|
return constructDate(args);
|
2017-03-02 15:40:45 +00:00
|
|
|
},
|
2020-02-18 08:41:57 +00:00
|
|
|
construct: function(target, args, newTarget) {
|
|
|
|
return constructDate(args);
|
2017-01-25 14:59:39 +00:00
|
|
|
},
|
|
|
|
get: function(target, property, receiver) {
|
|
|
|
if (property == "now") {
|
|
|
|
return mockDateNow;
|
|
|
|
}
|
2018-02-24 02:05:22 +00:00
|
|
|
if (property == "prototype") {
|
2020-02-18 08:41:57 +00:00
|
|
|
return origDate.prototype;
|
2018-02-24 02:05:22 +00:00
|
|
|
}
|
2017-01-25 14:59:39 +00:00
|
|
|
},
|
|
|
|
}
|
2016-12-19 10:13:48 +00:00
|
|
|
|
2017-01-25 14:59:39 +00:00
|
|
|
Date = new Proxy(Date, handler);
|
|
|
|
})();
|
2016-12-19 10:13:48 +00:00
|
|
|
|
2019-12-16 14:40:40 +00:00
|
|
|
// Mock performance methods.
|
2020-02-18 08:41:57 +00:00
|
|
|
performance.now = function() { return 1.2; };
|
|
|
|
performance.measureMemory = function() { return []; };
|
2019-12-16 14:40:40 +00:00
|
|
|
|
|
|
|
// Mock readline so that test cases don't hang.
|
2020-02-18 08:41:57 +00:00
|
|
|
readline = function() { return "foo"; };
|
2017-02-06 11:42:11 +00:00
|
|
|
|
2017-01-24 08:18:02 +00:00
|
|
|
// Mock stack traces.
|
2020-02-18 08:41:57 +00:00
|
|
|
Error.prepareStackTrace = function(error, structuredStackTrace) {
|
2017-01-24 08:18:02 +00:00
|
|
|
return "";
|
2017-01-25 14:59:39 +00:00
|
|
|
};
|
2017-01-27 08:49:39 +00:00
|
|
|
Object.defineProperty(
|
|
|
|
Error, 'prepareStackTrace', { configurable: false, writable: false });
|
2017-01-24 08:18:02 +00:00
|
|
|
|
2017-01-24 10:15:55 +00:00
|
|
|
// Mock buffer access in float typed arrays because of varying NaN patterns.
|
2020-02-18 08:41:57 +00:00
|
|
|
(function() {
|
2020-01-31 12:06:33 +00:00
|
|
|
const origIsNaN = isNaN;
|
2020-02-18 08:41:57 +00:00
|
|
|
const deNaNify = function(value) { return origIsNaN(value) ? 1 : value; };
|
|
|
|
const mock = function(type) {
|
|
|
|
|
|
|
|
// Remove NaN values from parameters to "set" function.
|
|
|
|
const set = type.prototype.set;
|
|
|
|
type.prototype.set = function(array, offset) {
|
|
|
|
if (Array.isArray(array)) {
|
|
|
|
array = array.map(deNaNify);
|
|
|
|
}
|
|
|
|
set.apply(this, [array, offset]);
|
|
|
|
};
|
|
|
|
|
2020-01-31 12:06:33 +00:00
|
|
|
const handler = {
|
2020-02-18 08:41:57 +00:00
|
|
|
// Remove NaN values from parameters to constructor.
|
2017-02-07 09:47:45 +00:00
|
|
|
construct: function(target, args) {
|
2020-01-31 12:06:33 +00:00
|
|
|
for (let i = 0; i < args.length; i++) {
|
2020-02-18 08:41:57 +00:00
|
|
|
if (args[i] != null &&
|
|
|
|
typeof args[i][Symbol.iterator] === 'function') {
|
|
|
|
// Consume iterators.
|
|
|
|
args[i] = Array.from(args[i]);
|
|
|
|
}
|
|
|
|
if (Array.isArray(args[i])) {
|
|
|
|
args[i] = args[i].map(deNaNify);
|
2020-01-31 12:06:33 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-18 08:41:57 +00:00
|
|
|
|
2020-01-31 12:06:33 +00:00
|
|
|
const obj = new (
|
2020-02-18 08:41:57 +00:00
|
|
|
Function.prototype.bind.call(type, null, ...args));
|
2017-06-02 11:57:46 +00:00
|
|
|
return new Proxy(obj, {
|
|
|
|
get: function(x, prop) {
|
|
|
|
if (typeof x[prop] == "function")
|
2020-02-18 08:41:57 +00:00
|
|
|
return x[prop].bind(obj);
|
2017-06-02 11:57:46 +00:00
|
|
|
return x[prop];
|
|
|
|
},
|
2020-02-18 08:41:57 +00:00
|
|
|
// Remove NaN values that get assigned.
|
|
|
|
set: function(target, prop, value, receiver) {
|
|
|
|
target[prop] = deNaNify(value);
|
|
|
|
return value;
|
|
|
|
}
|
2017-06-02 11:57:46 +00:00
|
|
|
});
|
2017-02-07 09:47:45 +00:00
|
|
|
},
|
|
|
|
};
|
2020-02-18 08:41:57 +00:00
|
|
|
return new Proxy(type, handler);
|
2017-02-07 09:47:45 +00:00
|
|
|
}
|
2017-01-24 10:15:55 +00:00
|
|
|
|
2017-02-07 09:47:45 +00:00
|
|
|
Float32Array = mock(Float32Array);
|
|
|
|
Float64Array = mock(Float64Array);
|
|
|
|
})();
|
2017-01-24 10:15:55 +00:00
|
|
|
|
2020-06-19 13:14:12 +00:00
|
|
|
// Mock buffer access via DataViews because of varying NaN patterns.
|
|
|
|
(function() {
|
|
|
|
const origIsNaN = isNaN;
|
|
|
|
const deNaNify = function(value) { return origIsNaN(value) ? 1 : value; };
|
|
|
|
const origSetFloat32 = DataView.prototype.setFloat32;
|
|
|
|
DataView.prototype.setFloat32 = function(offset, value, ...rest) {
|
|
|
|
origSetFloat32.call(this, offset, deNaNify(value), ...rest);
|
|
|
|
};
|
|
|
|
const origSetFloat64 = DataView.prototype.setFloat64;
|
|
|
|
DataView.prototype.setFloat64 = function(offset, value, ...rest) {
|
|
|
|
origSetFloat64.call(this, offset, deNaNify(value), ...rest);
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
2016-12-19 10:13:48 +00:00
|
|
|
// Mock Worker.
|
2020-02-18 08:41:57 +00:00
|
|
|
(function() {
|
|
|
|
let index = 0;
|
2017-01-25 14:59:39 +00:00
|
|
|
// TODO(machenbach): Randomize this for each test case, but keep stable
|
|
|
|
// during comparison. Also data and random above.
|
2020-02-18 08:41:57 +00:00
|
|
|
const workerMessages = [
|
2017-01-25 14:59:39 +00:00
|
|
|
undefined, 0, -1, "", "foo", 42, [], {}, [0], {"x": 0}
|
|
|
|
];
|
|
|
|
Worker = function(code){
|
|
|
|
try {
|
2018-02-22 03:06:23 +00:00
|
|
|
print(prettyPrinted(eval(code)));
|
2017-01-25 14:59:39 +00:00
|
|
|
} catch(e) {
|
2018-02-22 03:06:23 +00:00
|
|
|
print(prettyPrinted(e));
|
2017-01-25 14:59:39 +00:00
|
|
|
}
|
|
|
|
this.getMessage = function(){
|
|
|
|
index = (index + 1) % 10;
|
|
|
|
return workerMessages[index];
|
|
|
|
}
|
|
|
|
this.postMessage = function(msg){
|
2018-02-22 03:06:23 +00:00
|
|
|
print(prettyPrinted(msg));
|
2017-01-25 14:59:39 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
})();
|
2020-04-16 17:39:15 +00:00
|
|
|
|
|
|
|
// Mock Realm.
|
|
|
|
Realm.eval = function(realm, code) { return eval(code) };
|
2020-04-24 23:24:31 +00:00
|
|
|
|
|
|
|
// Mock the nondeterministic parts of WeakRef and FinalizationRegistry.
|
|
|
|
WeakRef.prototype.deref = function() { };
|
|
|
|
FinalizationRegistry = function(callback) { };
|
|
|
|
FinalizationRegistry.prototype.register = function(target, holdings) { };
|
|
|
|
FinalizationRegistry.prototype.unregister = function(unregisterToken) { };
|
|
|
|
FinalizationRegistry.prototype.cleanupSome = function() { };
|
|
|
|
FinalizationRegistry.prototype[Symbol.toStringTag] = "FinalizationRegistry";
|