1// Copyright 2016 the V8 project authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5// This is intended for permanent JS behavior changes for mocking out 6// non-deterministic behavior. For temporary suppressions, please refer to 7// v8_suppressions.js. 8// This file is loaded before each correctness test cases and won't get 9// minimized. 10 11 12// This will be overridden in the test cases. The override can be minimized. 13var prettyPrinted = function prettyPrinted(msg) { return msg; }; 14 15// Mock Math.random. 16(function () { 17 var index = 0 18 Math.random = function() { 19 index = (index + 1) % 10; 20 return index / 10.0; 21 } 22})(); 23 24// Mock Date. 25(function () { 26 var index = 0 27 var mockDate = 1477662728696 28 var mockDateNow = function() { 29 index = (index + 1) % 10 30 mockDate = mockDate + index + 1 31 return mockDate 32 } 33 34 var origDate = Date; 35 var constructDate = function(args) { 36 if (args.length == 1) { 37 var result = new origDate(args[0]); 38 } else if (args.length == 2) { 39 var result = new origDate(args[0], args[1]); 40 } else if (args.length == 3) { 41 var result = new origDate(args[0], args[1], args[2]); 42 } else if (args.length == 4) { 43 var result = new origDate(args[0], args[1], args[2], args[3]); 44 } else if (args.length == 5) { 45 var result = new origDate(args[0], args[1], args[2], args[3], args[4]); 46 } else if (args.length == 6) { 47 var result = new origDate( 48 args[0], args[1], args[2], args[3], args[4], args[5]); 49 } else if (args.length >= 7) { 50 var result = new origDate( 51 args[0], args[1], args[2], args[3], args[4], args[5], args[6]); 52 } else { 53 var result = new origDate(mockDateNow()); 54 } 55 result.constructor = function(...args) { return constructDate(args); } 56 Object.defineProperty( 57 result, "constructor", { configurable: false, writable: false }); 58 return result 59 } 60 61 var handler = { 62 apply: function (target, thisArg, args) { 63 return constructDate(args) 64 }, 65 construct: function (target, args, newTarget) { 66 return constructDate(args) 67 }, 68 get: function(target, property, receiver) { 69 if (property == "now") { 70 return mockDateNow; 71 } 72 if (property == "prototype") { 73 return origDate.prototype 74 } 75 }, 76 } 77 78 Date = new Proxy(Date, handler); 79})(); 80 81// Mock performace.now(). 82(function () { 83 performance.now = function () { return 1.2; } 84})(); 85 86// Mock stack traces. 87Error.prepareStackTrace = function (error, structuredStackTrace) { 88 return ""; 89}; 90Object.defineProperty( 91 Error, 'prepareStackTrace', { configurable: false, writable: false }); 92 93// Mock buffer access in float typed arrays because of varying NaN patterns. 94// Note, for now we just use noop forwarding proxies, because they already 95// turn off optimizations. 96(function () { 97 var mock = function(arrayType) { 98 var handler = { 99 construct: function(target, args) { 100 var obj = new (Function.prototype.bind.apply(arrayType, [null].concat(args))); 101 return new Proxy(obj, { 102 get: function(x, prop) { 103 if (typeof x[prop] == "function") 104 return x[prop].bind(obj) 105 return x[prop]; 106 }, 107 }); 108 }, 109 }; 110 return new Proxy(arrayType, handler); 111 } 112 113 Float32Array = mock(Float32Array); 114 Float64Array = mock(Float64Array); 115})(); 116 117// Mock Worker. 118(function () { 119 var index = 0; 120 // TODO(machenbach): Randomize this for each test case, but keep stable 121 // during comparison. Also data and random above. 122 var workerMessages = [ 123 undefined, 0, -1, "", "foo", 42, [], {}, [0], {"x": 0} 124 ]; 125 Worker = function(code){ 126 try { 127 print(prettyPrinted(eval(code))); 128 } catch(e) { 129 print(prettyPrinted(e)); 130 } 131 this.getMessage = function(){ 132 index = (index + 1) % 10; 133 return workerMessages[index]; 134 } 135 this.postMessage = function(msg){ 136 print(prettyPrinted(msg)); 137 } 138 }; 139})(); 140