load-element.js 6.22 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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
// Copyright 2015 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.

// Flags: --strong-mode --allow-natives-syntax

function getSloppyArguments() {
  return arguments;
}

function getObjects() {
  "use strict";
  return [
    {},
    Object(""),
    [],
    (function(){}),
    (class Foo {}),
    getSloppyArguments(),
    arguments,
    new Date()
  ];
}

//TODO(conradw): add tests for non-inheritance once semantics are implemented.
function getNonInheritingObjects() {
  "use strong";
  return [
    Object(""),
    [],
    new Uint32Array(0)
  ];
}

function readFromObjectElementSloppy(o) {
  return o[0];
}

function readFromObjectElementSparseSloppy(o) {
  return o[100000];
}

function readFromObjectElementNonSmiSloppy(o) {
  return o[3000000000];
}

function readFromObjectNonIndexSloppy(o) {
  return o[5000000000];
}

function readFromObjectElementVarSloppy(o) {
  var a = 0;
  return o[a];
}

function readFromObjectElementSparseVarSloppy(o) {
  var a = 100000;
  return o[a];
}

function readFromObjectElementNonSmiVarSloppy(o) {
  var a = 3000000000;
  return o[a];
}

function readFromObjectNonIndexVarSloppy(o) {
  var a = 5000000000;
  return o[a];
}

function readFromObjectElementStrong(o) {
  "use strong";
  return o[0];
}

function readFromObjectElementSparseStrong(o) {
  "use strong";
  return o[100000];
}

function readFromObjectElementNonSmiStrong(o) {
  "use strong";
  return o[3000000000];
}

function readFromObjectNonIndexStrong(o) {
  "use strong";
  return o[5000000000];
}

function readFromObjectElementLetStrong(o) {
  "use strong";
  let a = 0;
  return o[a];
}

function readFromObjectElementSparseLetStrong(o) {
  "use strong";
  let a = 100000;
  return o[a];
}

function readFromObjectElementNonSmiLetStrong(o) {
  "use strong";
  let a = 3000000000;
  return o[a];
}

function readFromObjectNonIndexLetStrong(o) {
  "use strong";
  let a = 5000000000;
  return o[a];
}

function getDescs(x) {
  return [
    {value: x},
    {configurable: true, enumerable: true, writable: true, value: x},
    {configurable: true, enumerable: true, get: (function() {return x}) },
  ];
}

function assertStrongSemantics(func, object) {
  %DeoptimizeFunction(func);
  %ClearFunctionTypeFeedback(func);
  assertThrows(function(){func(object)}, TypeError);
  assertThrows(function(){func(object)}, TypeError);
  assertThrows(function(){func(object)}, TypeError);
  %OptimizeFunctionOnNextCall(func);
  assertThrows(function(){func(object)}, TypeError);
  %DeoptimizeFunction(func);
  assertThrows(function(){func(object)}, TypeError);
}

function assertSloppySemantics(func, object) {
  %DeoptimizeFunction(func);
  %ClearFunctionTypeFeedback(func);
  assertDoesNotThrow(function(){func(object)});
  assertDoesNotThrow(function(){func(object)});
  assertDoesNotThrow(function(){func(object)});
  %OptimizeFunctionOnNextCall(func);
  assertDoesNotThrow(function(){func(object)});
  %DeoptimizeFunction(func);
  assertDoesNotThrow(function(){func(object)});
}

(function () {
  "use strict";

  let goodKeys = [
    "0",
    "100000",
    "3000000000",
    "5000000000"
  ]

  let badKeys = [
    "bar",
    "1",
    "100001",
    "3000000001",
    "5000000001"
  ];

  let values = [
    "string",
    1,
    100001,
    30000000001,
    50000000001,
    NaN,
    {},
    undefined
  ];

  let literals = [0, NaN, true, ""];

  let badAccessorDescs = [
    { set: (function(){}) },
    { configurable: true, enumerable: true, set: (function(){}) }
  ];

  let readSloppy = [
    readFromObjectElementSloppy,
    readFromObjectElementSparseSloppy,
    readFromObjectElementNonSmiSloppy,
    readFromObjectNonIndexSloppy,
    readFromObjectElementVarSloppy,
    readFromObjectElementSparseVarSloppy,
    readFromObjectElementNonSmiVarSloppy,
    readFromObjectNonIndexVarSloppy
  ];

  let readStrong = [
    readFromObjectElementStrong,
    readFromObjectElementSparseStrong,
    readFromObjectElementNonSmiStrong,
    readFromObjectNonIndexStrong,
    readFromObjectElementLetStrong,
    readFromObjectElementSparseLetStrong,
    readFromObjectElementNonSmiLetStrong,
    readFromObjectNonIndexLetStrong
  ];

  let dummyProto = {};
  for (let key of goodKeys) {
    Object.defineProperty(dummyProto, key, { value: undefined });
  }

  let dummyAccessorProto = {};
  for (let key of goodKeys) {
    Object.defineProperty(dummyAccessorProto, key, { set: (function(){}) })
  }

  // String literals/objects should not throw on character index access
  assertDoesNotThrow(function() {"use strong"; return "string"[0]; });
  assertDoesNotThrow(function() {"use strong"; return Object("string")[0]; });

  // Attempting to access a property on an object with no defined properties
  // should throw.
  for (let object of getObjects().concat(getNonInheritingObjects(), literals)) {
    for (let func of readStrong) {
      assertStrongSemantics(func, object);
    }
    for (let func of readSloppy) {
      assertSloppySemantics(func, object);
    }
  }
  for (let object of getObjects()) {
    // Accessing a property which is on the prototype chain of the object should
    // not throw.
    object.__proto__ = dummyProto;
    for (let key of goodKeys) {
      for (let func of readStrong.concat(readSloppy)) {
        assertSloppySemantics(func, object);
      }
    }
  }
  // Properties with accessor descriptors missing 'get' should throw on access.
  for (let desc of badAccessorDescs) {
    for (let key of goodKeys) {
      for (let object of getObjects()) {
        Object.defineProperty(object, key, desc);
        for (let func of readStrong) {
          assertStrongSemantics(func, object);
        }
        for (let func of readSloppy) {
          assertSloppySemantics(func, object);
        }
      }
    }
  }
  // The same behaviour should be expected for bad accessor properties on the
  // prototype chain.
  for (let object of getObjects()) {
    object.__proto__ = dummyAccessorProto;
    for (let func of readStrong) {
      assertStrongSemantics(func, object);
    }
    for (let func of readSloppy) {
      assertSloppySemantics(func, object);
    }
  }
  assertThrows(function(){"use strong"; typeof ({})[1];}, TypeError);
  assertThrows(
    function(){"use strong"; typeof ({})[1] === "undefined"}, TypeError);
})();