Home | History | Annotate | Download | only in foozzie
      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.
     13 var __PrettyPrint = function __PrettyPrint(msg) { print(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 handler = {
     36     construct: function(target, args, newTarget) {
     37       if (args.length > 0) {
     38         return new (
     39             Function.prototype.bind.apply(origDate, [null].concat(args)));
     40       } else {
     41         return new origDate(mockDateNow());
     42       }
     43     },
     44     get: function(target, property, receiver) {
     45       if (property == "now") {
     46         return mockDateNow;
     47       }
     48     },
     49   }
     50 
     51   Date = new Proxy(Date, handler);
     52 })();
     53 
     54 // Mock performace.now().
     55 (function () {
     56   performance.now = function () { return 1.2; }
     57 })();
     58 
     59 // Mock stack traces.
     60 Error.prepareStackTrace = function (error, structuredStackTrace) {
     61   return "";
     62 };
     63 Object.defineProperty(
     64     Error, 'prepareStackTrace', { configurable: false, writable: false });
     65 
     66 // Mock buffer access in float typed arrays because of varying NaN patterns.
     67 // Note, for now we just use noop forwarding proxies, because they already
     68 // turn off optimizations.
     69 (function () {
     70   var mock = function(arrayType) {
     71     var handler = {
     72       construct: function(target, args) {
     73         return new Proxy(
     74             Function.prototype.bind.apply(arrayType, [null].concat(args)), {});
     75       },
     76     };
     77     return new Proxy(arrayType, handler);
     78   }
     79 
     80   Float32Array = mock(Float32Array);
     81   Float64Array = mock(Float64Array);
     82 })();
     83 
     84 // Mock Worker.
     85 (function () {
     86   var index = 0;
     87   // TODO(machenbach): Randomize this for each test case, but keep stable
     88   // during comparison. Also data and random above.
     89   var workerMessages = [
     90     undefined, 0, -1, "", "foo", 42, [], {}, [0], {"x": 0}
     91   ];
     92   Worker = function(code){
     93     try {
     94       __PrettyPrint(eval(code));
     95     } catch(e) {
     96       __PrettyPrint(e);
     97     }
     98     this.getMessage = function(){
     99       index = (index + 1) % 10;
    100       return workerMessages[index];
    101     }
    102     this.postMessage = function(msg){
    103       __PrettyPrint(msg);
    104     }
    105   };
    106 })();
    107