object-literals-method.js 6.28 KB
Newer Older
1 2 3 4
// 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.

5
// Flags: --allow-natives-syntax
6 7


8
(function TestBasics() {
9 10 11 12 13 14 15 16 17
  var object = {
    method() {
      return 42;
    }
  };
  assertEquals(42, object.method());
})();


18 19 20 21 22 23 24 25 26 27
(function TestThis() {
  var object = {
    method() {
      assertEquals(object, this);
    }
  };
  object.method();
})();


28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
(function TestDescriptor() {
  var object = {
    method() {
      return 42;
    }
  };

  var desc = Object.getOwnPropertyDescriptor(object, 'method');
  assertTrue(desc.enumerable);
  assertTrue(desc.configurable);
  assertTrue(desc.writable);
  assertEquals('function', typeof desc.value);

  assertEquals(42, desc.value());
})();


(function TestProto() {
  var object = {
47
    method() {}
48 49 50 51 52 53 54 55
  };

  assertEquals(Function.prototype, Object.getPrototypeOf(object.method));
})();


(function TestNotConstructable() {
  var object = {
56
    method() {}
57 58 59 60 61 62 63 64 65 66
  };

  assertThrows(function() {
    new object.method;
  });
})();


(function TestFunctionName() {
  var object = {
67
    method() {},
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
    1() {},
    2.0() {}
  };
  var f = object.method;
  assertEquals('method', f.name);
  var g = object[1];
  assertEquals('1', g.name);
  var h = object[2];
  assertEquals('2', h.name);
})();


(function TestNoBinding() {
  var method = 'local';
  var calls = 0;
  var object = {
    method() {
      calls++;
      assertEquals('local', method);
    }
  };
  object.method();
  assertEquals(1, calls);
})();


(function TestNoPrototype() {
  var object = {
96
    method() {}
97 98 99 100 101 102 103 104 105 106
  };
  var f = object.method;
  assertFalse(f.hasOwnProperty('prototype'));
  assertEquals(undefined, f.prototype);

  f.prototype = 42;
  assertEquals(42, f.prototype);
})();


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
(function TestNoRestrictedPropertiesStrict() {
  var obj = {
    method() { "use strict"; }
  };
  assertFalse(obj.method.hasOwnProperty("arguments"));
  assertThrows(function() { return obj.method.arguments; }, TypeError);
  assertThrows(function() { obj.method.arguments = {}; }, TypeError);

  assertFalse(obj.method.hasOwnProperty("caller"));
  assertThrows(function() { return obj.method.caller; }, TypeError);
  assertThrows(function() { obj.method.caller = {}; }, TypeError);
})();


(function TestNoRestrictedPropertiesSloppy() {
  var obj = {
    method() {}
  };
  assertFalse(obj.method.hasOwnProperty("arguments"));
  assertThrows(function() { return obj.method.arguments; }, TypeError);
  assertThrows(function() { obj.method.arguments = {}; }, TypeError);

  assertFalse(obj.method.hasOwnProperty("caller"));
  assertThrows(function() { return obj.method.caller; }, TypeError);
  assertThrows(function() { obj.method.caller = {}; }, TypeError);
})();


135 136 137 138 139 140 141 142 143 144 145 146
(function TestToString() {
  var object = {
    method() { 42; }
  };
  assertEquals('method() { 42; }', object.method.toString());
})();


(function TestOptimized() {
  var object = {
    method() { return 42; }
  };
147
  %PrepareFunctionForOptimization(object.method);
148 149 150 151 152 153
  assertEquals(42, object.method());
  assertEquals(42, object.method());
  %OptimizeFunctionOnNextCall(object.method);
  assertEquals(42, object.method());
  assertFalse(object.method.hasOwnProperty('prototype'));
})();
154 155 156 157 158 159


///////////////////////////////////////////////////////////////////////////////


var GeneratorFunction = function*() {}.__proto__.constructor;
160
var GeneratorPrototype = Object.getPrototypeOf(function*() {}).prototype;
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


function assertIteratorResult(value, done, result) {
  assertEquals({value: value, done: done}, result);
}


(function TestGeneratorBasics() {
  var object = {
    *method() {
      yield 1;
    }
  };
  var g = object.method();
  assertIteratorResult(1, false, g.next());
  assertIteratorResult(undefined, true, g.next());
})();


(function TestGeneratorThis() {
  var object = {
    *method() {
      yield this;
    }
  };
  var g = object.method();
  assertIteratorResult(object, false, g.next());
  assertIteratorResult(undefined, true, g.next());
})();


(function TestGeneratorSymbolIterator() {
  var object = {
    *method() {}
  };
  var g = object.method();
  assertEquals(g, g[Symbol.iterator]());
})();


(function TestGeneratorDescriptor() {
  var object = {
    *method() {
      yield 1;
    }
  };

  var desc = Object.getOwnPropertyDescriptor(object, 'method');
  assertTrue(desc.enumerable);
  assertTrue(desc.configurable);
  assertTrue(desc.writable);
  assertEquals('function', typeof desc.value);

  var g = desc.value();
  assertIteratorResult(1, false, g.next());
  assertIteratorResult(undefined, true, g.next());
})();


220 221 222 223 224 225 226 227 228 229 230 231 232
(function TestGeneratorPrototypeDescriptor() {
  var object = {
    *method() {}
  };

  var desc = Object.getOwnPropertyDescriptor(object.method, 'prototype');
  assertFalse(desc.enumerable);
  assertFalse(desc.configurable);
  assertTrue(desc.writable);
  assertEquals(GeneratorPrototype, Object.getPrototypeOf(desc.value));
})();


233 234 235 236 237 238 239 240 241 242
(function TestGeneratorProto() {
  var object = {
    *method() {}
  };

  assertEquals(GeneratorFunction.prototype,
               Object.getPrototypeOf(object.method));
})();


243
(function TestGeneratorNotConstructable() {
244 245 246 247 248 249
  var object = {
    *method() {
      yield 1;
    }
  };

250
  assertThrows(()=>new object.method());
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
})();


(function TestGeneratorName() {
  var object = {
    *method() {},
    *1() {},
    *2.0() {}
  };
  var f = object.method;
  assertEquals('method', f.name);
  var g = object[1];
  assertEquals('1', g.name);
  var h = object[2];
  assertEquals('2', h.name);
})();


(function TestGeneratorNoBinding() {
  var method = 'local';
  var calls = 0;
  var object = {
    *method() {
      calls++;
      assertEquals('local', method);
    }
  };
  var g = object.method();
  assertIteratorResult(undefined, true, g.next());
  assertEquals(1, calls);
})();


(function TestGeneratorToString() {
  var object = {
    *method() { yield 1; }
  };
  assertEquals('*method() { yield 1; }', object.method.toString());
})();
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313


(function TestProtoName() {
  var object = {
    __proto__() {
      return 1;
    }
  };
  assertEquals(Object.prototype, Object.getPrototypeOf(object));
  assertEquals(1, object.__proto__());
})();


(function TestProtoName2() {
  var p = {};
  var object = {
    __proto__() {
      return 1;
    },
    __proto__: p
  };
  assertEquals(p, Object.getPrototypeOf(object));
  assertEquals(1, object.__proto__());
})();