v8_mock.js 3.95 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
// 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.
13
var prettyPrinted = function prettyPrinted(msg) { return msg; };
14 15

// Mock Math.random.
16 17 18 19 20 21 22
(function () {
  var index = 0
  Math.random = function() {
    index = (index + 1) % 10;
    return index / 10.0;
  }
})();
23 24

// Mock Date.
25 26 27 28 29 30 31 32
(function () {
  var index = 0
  var mockDate = 1477662728696
  var mockDateNow = function() {
    index = (index + 1) % 10
    mockDate = mockDate + index + 1
    return mockDate
  }
33

34
  var origDate = Date;
35
  var constructDate = function(args) {
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
    if (args.length == 1) {
      var result = new origDate(args[0]);
    } else if (args.length == 2) {
      var result = new origDate(args[0], args[1]);
    } else if (args.length == 3) {
      var result = new origDate(args[0], args[1], args[2]);
    } else if (args.length == 4) {
      var result = new origDate(args[0], args[1], args[2], args[3]);
    } else if (args.length == 5) {
      var result = new origDate(args[0], args[1], args[2], args[3], args[4]);
    } else if (args.length == 6) {
      var result = new origDate(
          args[0], args[1], args[2], args[3], args[4], args[5]);
    } else if (args.length >= 7) {
      var result = new origDate(
          args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
52 53 54 55 56 57 58 59 60
    } else {
      var result = new origDate(mockDateNow());
    }
    result.constructor = function(...args) { return constructDate(args); }
    Object.defineProperty(
        result, "constructor", { configurable: false, writable: false });
    return result
  }

61
  var handler = {
62 63 64 65 66
    apply: function (target, thisArg, args) {
      return constructDate(args)
    },
    construct: function (target, args, newTarget) {
      return constructDate(args)
67 68 69 70 71
    },
    get: function(target, property, receiver) {
      if (property == "now") {
        return mockDateNow;
      }
72 73 74
      if (property == "prototype") {
        return origDate.prototype
      }
75 76
    },
  }
77

78 79
  Date = new Proxy(Date, handler);
})();
80

81 82 83 84 85
// Mock performace.now().
(function () {
  performance.now = function () { return 1.2; }
})();

86 87 88
// Mock stack traces.
Error.prepareStackTrace = function (error, structuredStackTrace) {
  return "";
89
};
90 91
Object.defineProperty(
    Error, 'prepareStackTrace', { configurable: false, writable: false });
92

93 94 95
// Mock buffer access in float typed arrays because of varying NaN patterns.
// Note, for now we just use noop forwarding proxies, because they already
// turn off optimizations.
96 97 98 99
(function () {
  var mock = function(arrayType) {
    var handler = {
      construct: function(target, args) {
100 101 102 103 104 105 106 107
        var obj = new (Function.prototype.bind.apply(arrayType, [null].concat(args)));
        return new Proxy(obj, {
          get: function(x, prop) {
            if (typeof x[prop] == "function")
              return x[prop].bind(obj)
            return x[prop];
          },
        });
108 109 110 111
      },
    };
    return new Proxy(arrayType, handler);
  }
112

113 114 115
  Float32Array = mock(Float32Array);
  Float64Array = mock(Float64Array);
})();
116

117
// Mock Worker.
118 119 120 121 122 123 124 125 126
(function () {
  var index = 0;
  // TODO(machenbach): Randomize this for each test case, but keep stable
  // during comparison. Also data and random above.
  var workerMessages = [
    undefined, 0, -1, "", "foo", 42, [], {}, [0], {"x": 0}
  ];
  Worker = function(code){
    try {
127
      print(prettyPrinted(eval(code)));
128
    } catch(e) {
129
      print(prettyPrinted(e));
130 131 132 133 134 135
    }
    this.getMessage = function(){
      index = (index + 1) % 10;
      return workerMessages[index];
    }
    this.postMessage = function(msg){
136
      print(prettyPrinted(msg));
137 138 139
    }
  };
})();