reflect-apply.js 7.97 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
// Copyright 2014 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.


(function testReflectApplyArity() {
  assertEquals(3, Reflect.apply.length);
})();


(function testReflectApplyNonConstructor() {
  assertThrows(function() {
    new Reflect.apply(function(){}, null, []);
  }, TypeError);
})();


(function testAppliedReceiverSloppy() {
  function returnThis() { return this; }
  var receiver = {};

  assertSame(this, Reflect.apply(returnThis, void 0, []));
  assertSame(this, Reflect.apply(returnThis, null, []));
  assertSame(this, Reflect.apply(returnThis, this, []));
  assertSame(receiver, Reflect.apply(returnThis, receiver, []));

  // Wrap JS values
  assertSame(String.prototype,
             Object.getPrototypeOf(Reflect.apply(returnThis, "str", [])));
  assertSame(Number.prototype,
             Object.getPrototypeOf(Reflect.apply(returnThis, 123, [])));
  assertSame(Boolean.prototype,
             Object.getPrototypeOf(Reflect.apply(returnThis, true, [])));
  assertSame(Symbol.prototype,
             Object.getPrototypeOf(
                Reflect.apply(returnThis, Symbol("test"), [])));
})();


(function testAppliedReceiverStrict() {
  function returnThis() { 'use strict'; return this; }
  var receiver = {};

  assertSame(void 0, Reflect.apply(returnThis, void 0, []));
  assertSame(this, Reflect.apply(returnThis, this, []));
  assertSame(receiver, Reflect.apply(returnThis, receiver, []));

  // Don't wrap value types
  var regexp = /123/;
  var symbol = Symbol("test");
  assertSame("str", Reflect.apply(returnThis, "str", []));
  assertSame(123, Reflect.apply(returnThis, 123, []));
  assertSame(true, Reflect.apply(returnThis, true, []));
  assertSame(regexp, Reflect.apply(returnThis, regexp, []));
  assertSame(symbol, Reflect.apply(returnThis, symbol, []));
})();


(function testAppliedArgumentsLength() {
  function returnLengthStrict() { 'use strict'; return arguments.length; }
  function returnLengthSloppy() { return arguments.length; }

  assertEquals(0, Reflect.apply(returnLengthStrict, this, []));
  assertEquals(0, Reflect.apply(returnLengthSloppy, this, []));
  assertEquals(0, Reflect.apply(returnLengthStrict, this, {}));
  assertEquals(0, Reflect.apply(returnLengthSloppy, this, {}));

  for (var i = 0; i < 256; ++i) {
    assertEquals(i, Reflect.apply(returnLengthStrict, this, new Array(i)));
    assertEquals(i, Reflect.apply(returnLengthSloppy, this, new Array(i)));
    assertEquals(i, Reflect.apply(returnLengthStrict, this, { length: i }));
    assertEquals(i, Reflect.apply(returnLengthSloppy, this, { length: i }));
  }
})();


(function testAppliedArgumentsLengthThrows() {
  function noopStrict() { 'use strict'; }
  function noopSloppy() { }
  function MyError() {}

  var argsList = {};
  Object.defineProperty(argsList, "length", {
    get: function() { throw new MyError(); }
  });

  assertThrows(function() {
    Reflect.apply(noopStrict, this, argsList);
  }, MyError);

  assertThrows(function() {
    Reflect.apply(noopSloppy, this, argsList);
  }, MyError);
})();


(function testAppliedArgumentsElementThrows() {
  function noopStrict() { 'use strict'; }
  function noopSloppy() { }
  function MyError() {}

  var argsList = { length: 1 };
  Object.defineProperty(argsList, "0", {
    get: function() { throw new MyError(); }
  });

  assertThrows(function() {
    Reflect.apply(noopStrict, this, argsList);
  }, MyError);

  assertThrows(function() {
    Reflect.apply(noopSloppy, this, argsList);
  }, MyError);
})();


(function testAppliedNonFunctionStrict() {
  'use strict';
  assertThrows(function() { Reflect.apply(void 0); }, TypeError);
  assertThrows(function() { Reflect.apply(null); }, TypeError);
  assertThrows(function() { Reflect.apply(123); }, TypeError);
  assertThrows(function() { Reflect.apply("str"); }, TypeError);
  assertThrows(function() { Reflect.apply(Symbol("x")); }, TypeError);
  assertThrows(function() { Reflect.apply(/123/); }, TypeError);
  assertThrows(function() { Reflect.apply(NaN); }, TypeError);
  assertThrows(function() { Reflect.apply({}); }, TypeError);
  assertThrows(function() { Reflect.apply([]); }, TypeError);
})();


(function testAppliedNonFunctionSloppy() {
  assertThrows(function() { Reflect.apply(void 0); }, TypeError);
  assertThrows(function() { Reflect.apply(null); }, TypeError);
  assertThrows(function() { Reflect.apply(123); }, TypeError);
  assertThrows(function() { Reflect.apply("str"); }, TypeError);
  assertThrows(function() { Reflect.apply(Symbol("x")); }, TypeError);
  assertThrows(function() { Reflect.apply(/123/); }, TypeError);
  assertThrows(function() { Reflect.apply(NaN); }, TypeError);
  assertThrows(function() { Reflect.apply({}); }, TypeError);
  assertThrows(function() { Reflect.apply([]); }, TypeError);
})();


(function testAppliedArgumentsNonList() {
  function noopStrict() { 'use strict'; }
  function noopSloppy() {}
  var R = void 0;
  assertThrows(function() { Reflect.apply(noopStrict, R, null); }, TypeError);
  assertThrows(function() { Reflect.apply(noopSloppy, R, null); }, TypeError);
  assertThrows(function() { Reflect.apply(noopStrict, R, 1); }, TypeError);
  assertThrows(function() { Reflect.apply(noopSloppy, R, 1); }, TypeError);
  assertThrows(function() { Reflect.apply(noopStrict, R, "BAD"); }, TypeError);
  assertThrows(function() { Reflect.apply(noopSloppy, R, "BAD"); }, TypeError);
  assertThrows(function() { Reflect.apply(noopStrict, R, true); }, TypeError);
  assertThrows(function() { Reflect.apply(noopSloppy, R, true); }, TypeError);
  var sym = Symbol("x");
  assertThrows(function() { Reflect.apply(noopStrict, R, sym); }, TypeError);
  assertThrows(function() { Reflect.apply(noopSloppy, R, sym); }, TypeError);
})();


(function testAppliedArgumentValue() {
  function returnFirstStrict(a) { 'use strict'; return a; }
  function returnFirstSloppy(a) { return a; }
  function returnLastStrict(a) {
    'use strict'; return arguments[arguments.length - 1]; }
  function returnLastSloppy(a) { return arguments[arguments.length - 1]; }
  function returnSumStrict() {
    'use strict';
    var sum = arguments[0];
    for (var i = 1; i < arguments.length; ++i) {
      sum += arguments[i];
    }
    return sum;
  }
  function returnSumSloppy() {
    var sum = arguments[0];
    for (var i = 1; i < arguments.length; ++i) {
      sum += arguments[i];
    }
    return sum;
  }

  assertEquals("OK!", Reflect.apply(returnFirstStrict, this, ["OK!"]));
  assertEquals("OK!", Reflect.apply(returnFirstSloppy, this, ["OK!"]));
  assertEquals("OK!", Reflect.apply(returnFirstStrict, this,
                                    { 0: "OK!", length: 1 }));
  assertEquals("OK!", Reflect.apply(returnFirstSloppy, this,
                                    { 0: "OK!", length: 1 }));
  assertEquals("OK!", Reflect.apply(returnLastStrict, this,
                                    [0, 1, 2, 3, 4, 5, 6, 7, 8, "OK!"]));
  assertEquals("OK!", Reflect.apply(returnLastSloppy, this,
                                    [0, 1, 2, 3, 4, 5, 6, 7, 8, "OK!"]));
  assertEquals("OK!", Reflect.apply(returnLastStrict, this,
                                    { 9: "OK!", length: 10 }));
  assertEquals("OK!", Reflect.apply(returnLastSloppy, this,
                                    { 9: "OK!", length: 10 }));
  assertEquals("TEST", Reflect.apply(returnSumStrict, this,
                                     ["T", "E", "S", "T"]));
  assertEquals("TEST!!", Reflect.apply(returnSumStrict, this,
                                       ["T", "E", "S", "T", "!", "!"]));
  assertEquals(10, Reflect.apply(returnSumStrict, this,
                                 { 0: 1, 1: 2, 2: 3, 3: 4, length: 4 }));
  assertEquals("TEST", Reflect.apply(returnSumSloppy, this,
                                     ["T", "E", "S", "T"]));
  assertEquals("TEST!!", Reflect.apply(returnSumSloppy, this,
                                       ["T", "E", "S", "T", "!", "!"]));
  assertEquals(10, Reflect.apply(returnSumSloppy, this,
                                 { 0: 1, 1: 2, 2: 3, 3: 4, length: 4 }));
})();