messages.js 15.5 KB
Newer Older
1 2 3 4
// 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.

5
// Flags: --allow-natives-syntax --stack-size=100 --harmony
6

7 8 9 10 11 12
function test(f, expected, type) {
  try {
    f();
  } catch (e) {
    assertInstanceof(e, type);
    assertEquals(expected, e.message);
13
    return;
14
  }
15
  assertUnreachable("Exception expected");
16 17
}

18 19 20 21 22 23 24 25 26 27 28 29
const typedArrayConstructors = [
  Uint8Array,
  Int8Array,
  Uint16Array,
  Int16Array,
  Uint32Array,
  Int32Array,
  Float32Array,
  Float64Array,
  Uint8ClampedArray
];

30 31 32 33 34 35 36 37 38 39 40
// === Error ===

// kCyclicProto
test(function() {
  var o = {};
  o.__proto__ = o;
}, "Cyclic __proto__ value", Error);


// === TypeError ===

41 42
// kApplyNonFunction
test(function() {
43
  Reflect.apply(1, []);
44 45 46
}, "Function.prototype.apply was called on 1, which is a number " +
   "and not a function", TypeError);

47 48 49 50
test(function() {
  var a = [1, 2];
  Object.freeze(a);
  a.splice(1, 1, [1]);
51 52
}, "Cannot assign to read only property '1' of object '[object Array]'",
   TypeError);
53 54 55 56 57

test(function() {
  var a = [1];
  Object.seal(a);
  a.shift();
58
}, "Cannot delete property '0' of [object Array]", TypeError);
59

60 61 62 63 64
// kCalledNonCallable
test(function() {
  [].forEach(1);
}, "1 is not a function", TypeError);

65 66 67 68 69
// kCalledOnNonObject
test(function() {
  Object.defineProperty(1, "x", {});
}, "Object.defineProperty called on non-object", TypeError);

70 71 72 73 74 75 76 77 78 79 80 81
test(function() {
  (function() {}).apply({}, 1);
}, "CreateListFromArrayLike called on non-object", TypeError);

test(function() {
  Reflect.apply(function() {}, {}, 1);
}, "CreateListFromArrayLike called on non-object", TypeError);

test(function() {
  Reflect.construct(function() {}, 1);
}, "CreateListFromArrayLike called on non-object", TypeError);

82
// kCalledOnNullOrUndefined
83 84 85 86
test(function() {
  String.prototype.includes.call(null);
}, "String.prototype.includes called on null or undefined", TypeError);

87 88 89 90 91 92 93 94
test(function() {
  String.prototype.match.call(null);
}, "String.prototype.match called on null or undefined", TypeError);

test(function() {
  String.prototype.search.call(null);
}, "String.prototype.search called on null or undefined", TypeError);

95 96
test(function() {
  Array.prototype.shift.call(null);
97
}, "Cannot convert undefined or null to object", TypeError);
98

99
test(function() {
100 101 102 103 104 105 106 107 108 109
  String.prototype.trim.call(null);
}, "String.prototype.trim called on null or undefined", TypeError);

test(function() {
  String.prototype.trimLeft.call(null);
}, "String.prototype.trimLeft called on null or undefined", TypeError);

test(function() {
  String.prototype.trimRight.call(null);
}, "String.prototype.trimRight called on null or undefined", TypeError);
110

111
// kCannotFreezeArrayBufferView
112
test(function() {
113 114
  Object.freeze(new Uint16Array(1));
}, "Cannot freeze array buffer views with elements", TypeError);
115 116 117 118 119 120 121 122

// kConstAssign
test(function() {
  "use strict";
  const a = 1;
  a = 2;
}, "Assignment to constant variable.", TypeError);

123 124
// kCannotConvertToPrimitive
test(function() {
125 126
  var o = { toString: function() { return this } };
  [].join(o);
127 128
}, "Cannot convert object to primitive value", TypeError);

129
// kConstructorNotFunction
130 131 132 133 134 135 136 137
test(function() {
  Map();
}, "Constructor Map requires 'new'", TypeError);

test(function() {
  Set();
}, "Constructor Set requires 'new'", TypeError);

138 139 140 141
test(function() {
  Uint16Array(1);
}, "Constructor Uint16Array requires 'new'", TypeError);

142 143 144 145 146 147 148 149
test(function() {
  WeakSet();
}, "Constructor WeakSet requires 'new'", TypeError);

test(function() {
  WeakMap();
}, "Constructor WeakMap requires 'new'", TypeError);

150 151 152 153 154
// kDataViewNotArrayBuffer
test(function() {
  new DataView(1);
}, "First argument to DataView constructor must be an ArrayBuffer", TypeError);

155 156 157 158 159 160
// kDefineDisallowed
test(function() {
  "use strict";
  var o = {};
  Object.preventExtensions(o);
  Object.defineProperty(o, "x", { value: 1 });
vabr's avatar
vabr committed
161
}, "Cannot define property x, object is not extensible", TypeError);
162

163 164 165 166
// kDetachedOperation
for (constructor of typedArrayConstructors) {
  test(() => {
    const ta = new constructor([1]);
167
    %ArrayBufferDetach(ta.buffer);
168
    ta.find(() => {});
169
  }, "Cannot perform %TypedArray%.prototype.find on a detached ArrayBuffer", TypeError);
170 171 172

  test(() => {
    const ta = new constructor([1]);
173
    %ArrayBufferDetach(ta.buffer);
174
    ta.findIndex(() => {});
175
  }, "Cannot perform %TypedArray%.prototype.findIndex on a detached ArrayBuffer", TypeError);
176 177
}

178 179 180 181 182 183
// kFirstArgumentNotRegExp
test(function() {
  "a".startsWith(/a/);
}, "First argument to String.prototype.startsWith " +
   "must not be a regular expression", TypeError);

184 185 186 187 188
test(function() {
  "a".includes(/a/);
}, "First argument to String.prototype.includes " +
   "must not be a regular expression", TypeError);

189 190 191 192 193
// kFlagsGetterNonObject
test(function() {
  Object.getOwnPropertyDescriptor(RegExp.prototype, "flags").get.call(1);
}, "RegExp.prototype.flags getter called on non-object 1", TypeError);

194 195 196 197 198
// kFunctionBind
test(function() {
  Function.prototype.bind.call(1);
}, "Bind must be called on a function", TypeError);

199 200 201 202 203 204 205 206 207 208
// kGeneratorRunning
test(function() {
  var iter;
  function* generator() { yield iter.next(); }
  var iter = generator();
  iter.next();
}, "Generator is already running", TypeError);

// kIncompatibleMethodReceiver
test(function() {
209 210 211
  Set.prototype.add.call([]);
}, "Method Set.prototype.add called on incompatible receiver [object Array]",
TypeError);
212

213 214 215 216 217
test(function() {
  WeakSet.prototype.add.call([]);
}, "Method WeakSet.prototype.add called on incompatible receiver [object Array]",
TypeError);

218 219 220 221 222
test(function() {
  WeakSet.prototype.delete.call([]);
}, "Method WeakSet.prototype.delete called on incompatible receiver [object Array]",
TypeError);

223 224 225 226 227
test(function() {
  WeakMap.prototype.set.call([]);
}, "Method WeakMap.prototype.set called on incompatible receiver [object Array]",
TypeError);

228 229 230 231 232
test(function() {
  WeakMap.prototype.delete.call([]);
}, "Method WeakMap.prototype.delete called on incompatible receiver [object Array]",
TypeError);

233 234 235 236 237 238
// kNonCallableInInstanceOfCheck
test(function() {
  1 instanceof {};
}, "Right-hand side of 'instanceof' is not callable", TypeError);

// kNonObjectInInstanceOfCheck
239 240
test(function() {
  1 instanceof 1;
241
}, "Right-hand side of 'instanceof' is not an object", TypeError);
242 243 244 245 246 247 248 249 250 251 252 253 254 255

// kInstanceofNonobjectProto
test(function() {
  function f() {}
  var o = new f();
  f.prototype = 1;
  o instanceof f;
}, "Function has non-object prototype '1' in instanceof check", TypeError);

// kInvalidInOperatorUse
test(function() {
  1 in 1;
}, "Cannot use 'in' operator to search for '1' in 1", TypeError);

256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
// kInvalidWeakMapKey
test(function() {
  new WeakMap([[1, 1]]);
}, "Invalid value used as weak map key", TypeError);

test(function() {
  new WeakMap().set(1, 1);
}, "Invalid value used as weak map key", TypeError);

// kInvalidWeakSetValue
test(function() {
  new WeakSet([1]);
}, "Invalid value used in weak set", TypeError);

test(function() {
  new WeakSet().add(1);
}, "Invalid value used in weak set", TypeError);

274 275 276 277 278 279 280 281 282 283 284 285
// kIteratorResultNotAnObject
test(function() {
  var obj = {};
  obj[Symbol.iterator] = function() { return { next: function() { return 1 }}};
  Array.from(obj);
}, "Iterator result 1 is not an object", TypeError);

// kIteratorValueNotAnObject
test(function() {
  new Map([1]);
}, "Iterator value 1 is not an entry object", TypeError);

286 287 288 289 290 291 292 293 294 295 296 297 298 299
test(function() {
  let holeyDoubleArray = [, 123.123];
  assertTrue(%HasDoubleElements(holeyDoubleArray));
  assertTrue(%HasHoleyElements(holeyDoubleArray));
  new Map(holeyDoubleArray);
}, "Iterator value undefined is not an entry object", TypeError);

test(function() {
  let holeyDoubleArray = [, 123.123];
  assertTrue(%HasDoubleElements(holeyDoubleArray));
  assertTrue(%HasHoleyElements(holeyDoubleArray));
  new WeakMap(holeyDoubleArray);
}, "Iterator value undefined is not an entry object", TypeError);

300 301 302 303 304
// kNotConstructor
test(function() {
  new Symbol();
}, "Symbol is not a constructor", TypeError);

305 306
// kNotDateObject
test(function() {
307
  Date.prototype.getHours.call(1);
308 309
}, "this is not a Date object.", TypeError);

310
// kNotGeneric
311 312 313
test(() => String.prototype.toString.call(1),
    "String.prototype.toString requires that 'this' be a String",
    TypeError);
314

315 316 317
test(() => String.prototype.valueOf.call(1),
    "String.prototype.valueOf requires that 'this' be a String",
    TypeError);
318

319 320 321
test(() => Boolean.prototype.toString.call(1),
    "Boolean.prototype.toString requires that 'this' be a Boolean",
    TypeError);
322

323 324 325
test(() => Boolean.prototype.valueOf.call(1),
    "Boolean.prototype.valueOf requires that 'this' be a Boolean",
    TypeError);
326

327 328 329
test(() => Number.prototype.toString.call({}),
    "Number.prototype.toString requires that 'this' be a Number",
    TypeError);
330

331 332 333
test(() => Number.prototype.valueOf.call({}),
    "Number.prototype.valueOf requires that 'this' be a Number",
    TypeError);
334

335 336 337
test(() => Function.prototype.toString.call(1),
    "Function.prototype.toString requires that 'this' be a Function",
    TypeError);
338 339 340 341 342

// kNotTypedArray
test(function() {
  Uint16Array.prototype.forEach.call(1);
}, "this is not a typed array.", TypeError);
343 344 345 346 347 348 349 350 351 352 353

// kObjectGetterExpectingFunction
test(function() {
  ({}).__defineGetter__("x", 0);
}, "Object.prototype.__defineGetter__: Expecting function", TypeError);

// kObjectGetterCallable
test(function() {
  Object.defineProperty({}, "x", { get: 1 });
}, "Getter must be a function: 1", TypeError);

354 355 356 357 358 359
// kObjectNotExtensible
test(function() {
  "use strict";
  var o = {};
  Object.freeze(o);
  o.a = 1;
vabr's avatar
vabr committed
360
}, "Cannot add property a, object is not extensible", TypeError);
361

362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
// kObjectSetterExpectingFunction
test(function() {
  ({}).__defineSetter__("x", 0);
}, "Object.prototype.__defineSetter__: Expecting function", TypeError);

// kObjectSetterCallable
test(function() {
  Object.defineProperty({}, "x", { set: 1 });
}, "Setter must be a function: 1", TypeError);

// kPropertyDescObject
test(function() {
  Object.defineProperty({}, "x", 1);
}, "Property description must be an object: 1", TypeError);

377
// kPropertyNotFunction
378 379 380 381 382
test(function() {
  Map.prototype.set = 0;
  new Map([[1, 2]]);
}, "'0' returned for property 'set' of object '#<Map>' is not a function", TypeError);

383 384
test(function() {
  Set.prototype.add = 0;
385
  new Set([1]);
386
}, "'0' returned for property 'add' of object '#<Set>' is not a function", TypeError);
387

388 389 390 391 392 393 394 395 396 397
test(function() {
  WeakMap.prototype.set = 0;
  new WeakMap([[{}, 1]]);
}, "'0' returned for property 'set' of object '#<WeakMap>' is not a function", TypeError);

test(function() {
  WeakSet.prototype.add = 0;
  new WeakSet([{}]);
}, "'0' returned for property 'add' of object '#<WeakSet>' is not a function", TypeError);

398 399 400 401 402 403 404 405 406 407 408 409 410
// kProtoObjectOrNull
test(function() {
  Object.setPrototypeOf({}, 1);
}, "Object prototype may only be an Object or null: 1", TypeError);

// kRedefineDisallowed
test(function() {
  "use strict";
  var o = {};
  Object.defineProperty(o, "x", { value: 1, configurable: false });
  Object.defineProperty(o, "x", { value: 2 });
}, "Cannot redefine property: x", TypeError);

411 412 413 414 415
// kReduceNoInitial
test(function() {
  [].reduce(function() {});
}, "Reduce of empty array with no initial value", TypeError);

416 417 418 419 420
// kResolverNotAFunction
test(function() {
  new Promise(1);
}, "Promise resolver 1 is not a function", TypeError);

421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
// kStrictDeleteProperty
test(function() {
  "use strict";
  var o = {};
  Object.defineProperty(o, "p", { value: 1, writable: false });
  delete o.p;
}, "Cannot delete property 'p' of #<Object>", TypeError);

// kStrictPoisonPill
test(function() {
  "use strict";
  arguments.callee;
}, "'caller', 'callee', and 'arguments' properties may not be accessed on " +
   "strict mode functions or the arguments objects for calls to them",
   TypeError);

// kStrictReadOnlyProperty
test(function() {
  "use strict";
  (1).a = 1;
441
}, "Cannot create property 'a' on number '1'", TypeError);
442

443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
// kSymbolToString
test(function() {
  "" + Symbol();
}, "Cannot convert a Symbol value to a string", TypeError);

// kSymbolToNumber
test(function() {
  1 + Symbol();
}, "Cannot convert a Symbol value to a number", TypeError);

// kUndefinedOrNullToObject
test(function() {
  Array.prototype.toString.call(null);
}, "Cannot convert undefined or null to object", TypeError);

458 459 460
// kValueAndAccessor
test(function() {
  Object.defineProperty({}, "x", { get: function(){}, value: 1});
461 462
}, "Invalid property descriptor. Cannot both specify accessors " +
   "and a value or writable attribute, #<Object>", TypeError);
463

464

465
// === SyntaxError ===
466

467 468
// kInvalidRegExpFlags
test(function() {
469 470
  eval("/a/x.test(\"a\");");
}, "Invalid regular expression flags", SyntaxError);
471

472 473 474 475 476
// kInvalidOrUnexpectedToken
test(function() {
  eval("'\n'");
}, "Invalid or unexpected token", SyntaxError);

477
//kJsonParseUnexpectedEOS
478 479
test(function() {
  JSON.parse("{")
480
}, "Unexpected end of JSON input", SyntaxError);
481

482
// kJsonParseUnexpectedTokenAt
483 484
test(function() {
  JSON.parse("/")
485
}, "Unexpected token / in JSON at position 0", SyntaxError);
486

487
// kJsonParseUnexpectedTokenNumberAt
488 489
test(function() {
  JSON.parse("{ 1")
490
}, "Unexpected number in JSON at position 2", SyntaxError);
491

492
// kJsonParseUnexpectedTokenStringAt
493 494
test(function() {
  JSON.parse('"""')
495
}, "Unexpected string in JSON at position 2", SyntaxError);
496

497 498 499 500 501 502 503 504
// kMalformedRegExp
test(function() {
  /(/.test("a");
}, "Invalid regular expression: /(/: Unterminated group", SyntaxError);

// kParenthesisInArgString
test(function() {
  new Function(")", "");
505
}, "Arg string terminates parameters early", SyntaxError);
506

507 508
// === ReferenceError ===

509
// kNotDefined
510 511 512 513 514
test(function() {
  "use strict";
  o;
}, "o is not defined", ReferenceError);

515 516
// === RangeError ===

517 518 519 520 521
// kInvalidOffset
test(function() {
  new Uint8Array(new ArrayBuffer(1),2);
}, "Start offset 2 is outside the bounds of the buffer", RangeError);

522 523 524 525
// kArrayLengthOutOfRange
test(function() {
  "use strict";
  Object.defineProperty([], "length", { value: 1E100 });
526
}, "Invalid array length", RangeError);
527

528 529 530 531 532 533 534 535 536 537
// kInvalidArrayBufferLength
test(function() {
  new ArrayBuffer(-1);
}, "Invalid array buffer length", RangeError);

// kInvalidArrayLength
test(function() {
  [].length = -1;
}, "Invalid array length", RangeError);

538 539 540 541 542 543 544 545 546 547
// kInvalidCodePoint
test(function() {
  String.fromCodePoint(-1);
}, "Invalid code point -1", RangeError);

// kInvalidCountValue
test(function() {
  "a".repeat(-1);
}, "Invalid count value", RangeError);

548 549 550
// kInvalidArrayBufferLength
test(function() {
  new Uint16Array(-1);
551
}, "Invalid typed array length: -1", RangeError);
552

553
// kThrowInvalidStringLength
554 555 556 557 558 559 560 561
test(function() {
  "a".padEnd(1 << 30);
}, "Invalid string length", RangeError);

test(function() {
  "a".padStart(1 << 30);
}, "Invalid string length", RangeError);

562 563 564 565
test(function() {
  "a".repeat(1 << 30);
}, "Invalid string length", RangeError);

566 567 568 569
test(function() {
  new Array(1 << 30).join();
}, "Invalid string length", RangeError);

570 571 572 573 574 575
// kNormalizationForm
test(function() {
  "".normalize("ABC");
}, "The normalization form should be one of NFC, NFD, NFKC, NFKD.", RangeError);

// kNumberFormatRange
576
test(function() {
577 578
  Number(1).toFixed(101);
}, "toFixed() digits argument must be between 0 and 100", RangeError);
579 580

test(function() {
581 582
  Number(1).toExponential(101);
}, "toExponential() argument must be between 0 and 100", RangeError);
583

584 585 586 587 588
// kStackOverflow
test(function() {
  function f() { f(Array(1000)); }
  f();
}, "Maximum call stack size exceeded", RangeError);
589 590 591

// kToPrecisionFormatRange
test(function() {
592 593
  Number(1).toPrecision(101);
}, "toPrecision() argument must be between 1 and 100", RangeError);
594 595 596 597 598 599 600 601 602 603 604 605 606

// kToPrecisionFormatRange
test(function() {
  Number(1).toString(100);
}, "toString() radix argument must be between 2 and 36", RangeError);


// === URIError ===

// kURIMalformed
test(function() {
  decodeURI("%%");
}, "URI malformed", URIError);