Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1 | // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 5 | // Flags: --stack-size=100 --harmony |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 6 | // Flags: --harmony-simd |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7 | |
| 8 | function test(f, expected, type) { |
| 9 | try { |
| 10 | f(); |
| 11 | } catch (e) { |
| 12 | assertInstanceof(e, type); |
| 13 | assertEquals(expected, e.message); |
| 14 | return; |
| 15 | } |
| 16 | assertUnreachable("Exception expected"); |
| 17 | } |
| 18 | |
| 19 | // === Error === |
| 20 | |
| 21 | // kCyclicProto |
| 22 | test(function() { |
| 23 | var o = {}; |
| 24 | o.__proto__ = o; |
| 25 | }, "Cyclic __proto__ value", Error); |
| 26 | |
| 27 | |
| 28 | // === TypeError === |
| 29 | |
| 30 | // kApplyNonFunction |
| 31 | test(function() { |
| 32 | Function.prototype.apply.call(1, []); |
| 33 | }, "Function.prototype.apply was called on 1, which is a number " + |
| 34 | "and not a function", TypeError); |
| 35 | |
| 36 | // kArrayFunctionsOnFrozen |
| 37 | test(function() { |
| 38 | var a = [1, 2]; |
| 39 | Object.freeze(a); |
| 40 | a.splice(1, 1, [1]); |
| 41 | }, "Cannot modify frozen array elements", TypeError); |
| 42 | |
| 43 | // kArrayFunctionsOnSealed |
| 44 | test(function() { |
| 45 | var a = [1]; |
| 46 | Object.seal(a); |
| 47 | a.shift(); |
| 48 | }, "Cannot add/remove sealed array elements", TypeError); |
| 49 | |
| 50 | // kCalledNonCallable |
| 51 | test(function() { |
| 52 | [].forEach(1); |
| 53 | }, "1 is not a function", TypeError); |
| 54 | |
| 55 | // kCalledOnNonObject |
| 56 | test(function() { |
| 57 | Object.defineProperty(1, "x", {}); |
| 58 | }, "Object.defineProperty called on non-object", TypeError); |
| 59 | |
| 60 | test(function() { |
| 61 | (function() {}).apply({}, 1); |
| 62 | }, "CreateListFromArrayLike called on non-object", TypeError); |
| 63 | |
| 64 | test(function() { |
| 65 | Reflect.apply(function() {}, {}, 1); |
| 66 | }, "CreateListFromArrayLike called on non-object", TypeError); |
| 67 | |
| 68 | test(function() { |
| 69 | Reflect.construct(function() {}, 1); |
| 70 | }, "CreateListFromArrayLike called on non-object", TypeError); |
| 71 | |
| 72 | // kCalledOnNullOrUndefined |
| 73 | test(function() { |
| 74 | Array.prototype.shift.call(null); |
| 75 | }, "Array.prototype.shift called on null or undefined", TypeError); |
| 76 | |
| 77 | // kCannotFreezeArrayBufferView |
| 78 | test(function() { |
| 79 | Object.freeze(new Uint16Array(1)); |
| 80 | }, "Cannot freeze array buffer views with elements", TypeError); |
| 81 | |
| 82 | // kConstAssign |
| 83 | test(function() { |
| 84 | "use strict"; |
| 85 | const a = 1; |
| 86 | a = 2; |
| 87 | }, "Assignment to constant variable.", TypeError); |
| 88 | |
| 89 | // kCannotConvertToPrimitive |
| 90 | test(function() { |
| 91 | var o = { toString: function() { return this } }; |
| 92 | [].join(o); |
| 93 | }, "Cannot convert object to primitive value", TypeError); |
| 94 | |
| 95 | // kCircularStructure |
| 96 | test(function() { |
| 97 | var o = {}; |
| 98 | o.o = o; |
| 99 | JSON.stringify(o); |
| 100 | }, "Converting circular structure to JSON", TypeError); |
| 101 | |
| 102 | // kConstructorNotFunction |
| 103 | test(function() { |
| 104 | Uint16Array(1); |
| 105 | }, "Constructor Uint16Array requires 'new'", TypeError); |
| 106 | |
| 107 | // kDataViewNotArrayBuffer |
| 108 | test(function() { |
| 109 | new DataView(1); |
| 110 | }, "First argument to DataView constructor must be an ArrayBuffer", TypeError); |
| 111 | |
| 112 | // kDefineDisallowed |
| 113 | test(function() { |
| 114 | "use strict"; |
| 115 | var o = {}; |
| 116 | Object.preventExtensions(o); |
| 117 | Object.defineProperty(o, "x", { value: 1 }); |
| 118 | }, "Cannot define property:x, object is not extensible.", TypeError); |
| 119 | |
| 120 | // kFirstArgumentNotRegExp |
| 121 | test(function() { |
| 122 | "a".startsWith(/a/); |
| 123 | }, "First argument to String.prototype.startsWith " + |
| 124 | "must not be a regular expression", TypeError); |
| 125 | |
| 126 | // kFlagsGetterNonObject |
| 127 | test(function() { |
| 128 | Object.getOwnPropertyDescriptor(RegExp.prototype, "flags").get.call(1); |
| 129 | }, "RegExp.prototype.flags getter called on non-object 1", TypeError); |
| 130 | |
| 131 | // kFunctionBind |
| 132 | test(function() { |
| 133 | Function.prototype.bind.call(1); |
| 134 | }, "Bind must be called on a function", TypeError); |
| 135 | |
| 136 | // kGeneratorRunning |
| 137 | test(function() { |
| 138 | var iter; |
| 139 | function* generator() { yield iter.next(); } |
| 140 | var iter = generator(); |
| 141 | iter.next(); |
| 142 | }, "Generator is already running", TypeError); |
| 143 | |
| 144 | // kIncompatibleMethodReceiver |
| 145 | test(function() { |
| 146 | Set.prototype.add.call([]); |
| 147 | }, "Method Set.prototype.add called on incompatible receiver [object Array]", |
| 148 | TypeError); |
| 149 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 150 | // kNonCallableInInstanceOfCheck |
| 151 | test(function() { |
| 152 | 1 instanceof {}; |
| 153 | }, "Right-hand side of 'instanceof' is not callable", TypeError); |
| 154 | |
| 155 | // kNonObjectInInstanceOfCheck |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 156 | test(function() { |
| 157 | 1 instanceof 1; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 158 | }, "Right-hand side of 'instanceof' is not an object", TypeError); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 159 | |
| 160 | // kInstanceofNonobjectProto |
| 161 | test(function() { |
| 162 | function f() {} |
| 163 | var o = new f(); |
| 164 | f.prototype = 1; |
| 165 | o instanceof f; |
| 166 | }, "Function has non-object prototype '1' in instanceof check", TypeError); |
| 167 | |
| 168 | // kInvalidInOperatorUse |
| 169 | test(function() { |
| 170 | 1 in 1; |
| 171 | }, "Cannot use 'in' operator to search for '1' in 1", TypeError); |
| 172 | |
| 173 | // kIteratorResultNotAnObject |
| 174 | test(function() { |
| 175 | var obj = {}; |
| 176 | obj[Symbol.iterator] = function() { return { next: function() { return 1 }}}; |
| 177 | Array.from(obj); |
| 178 | }, "Iterator result 1 is not an object", TypeError); |
| 179 | |
| 180 | // kIteratorValueNotAnObject |
| 181 | test(function() { |
| 182 | new Map([1]); |
| 183 | }, "Iterator value 1 is not an entry object", TypeError); |
| 184 | |
| 185 | // kNotConstructor |
| 186 | test(function() { |
| 187 | new Symbol(); |
| 188 | }, "Symbol is not a constructor", TypeError); |
| 189 | |
| 190 | // kNotDateObject |
| 191 | test(function() { |
| 192 | Date.prototype.getHours.call(1); |
| 193 | }, "this is not a Date object.", TypeError); |
| 194 | |
| 195 | // kNotGeneric |
| 196 | test(function() { |
| 197 | String.prototype.toString.call(1); |
| 198 | }, "String.prototype.toString is not generic", TypeError); |
| 199 | |
| 200 | test(function() { |
| 201 | String.prototype.valueOf.call(1); |
| 202 | }, "String.prototype.valueOf is not generic", TypeError); |
| 203 | |
| 204 | test(function() { |
| 205 | Boolean.prototype.toString.call(1); |
| 206 | }, "Boolean.prototype.toString is not generic", TypeError); |
| 207 | |
| 208 | test(function() { |
| 209 | Boolean.prototype.valueOf.call(1); |
| 210 | }, "Boolean.prototype.valueOf is not generic", TypeError); |
| 211 | |
| 212 | test(function() { |
| 213 | Number.prototype.toString.call({}); |
| 214 | }, "Number.prototype.toString is not generic", TypeError); |
| 215 | |
| 216 | test(function() { |
| 217 | Number.prototype.valueOf.call({}); |
| 218 | }, "Number.prototype.valueOf is not generic", TypeError); |
| 219 | |
| 220 | test(function() { |
| 221 | Function.prototype.toString.call(1); |
| 222 | }, "Function.prototype.toString is not generic", TypeError); |
| 223 | |
| 224 | // kNotTypedArray |
| 225 | test(function() { |
| 226 | Uint16Array.prototype.forEach.call(1); |
| 227 | }, "this is not a typed array.", TypeError); |
| 228 | |
| 229 | // kObjectGetterExpectingFunction |
| 230 | test(function() { |
| 231 | ({}).__defineGetter__("x", 0); |
| 232 | }, "Object.prototype.__defineGetter__: Expecting function", TypeError); |
| 233 | |
| 234 | // kObjectGetterCallable |
| 235 | test(function() { |
| 236 | Object.defineProperty({}, "x", { get: 1 }); |
| 237 | }, "Getter must be a function: 1", TypeError); |
| 238 | |
| 239 | // kObjectNotExtensible |
| 240 | test(function() { |
| 241 | "use strict"; |
| 242 | var o = {}; |
| 243 | Object.freeze(o); |
| 244 | o.a = 1; |
| 245 | }, "Can't add property a, object is not extensible", TypeError); |
| 246 | |
| 247 | // kObjectSetterExpectingFunction |
| 248 | test(function() { |
| 249 | ({}).__defineSetter__("x", 0); |
| 250 | }, "Object.prototype.__defineSetter__: Expecting function", TypeError); |
| 251 | |
| 252 | // kObjectSetterCallable |
| 253 | test(function() { |
| 254 | Object.defineProperty({}, "x", { set: 1 }); |
| 255 | }, "Setter must be a function: 1", TypeError); |
| 256 | |
| 257 | // kPropertyDescObject |
| 258 | test(function() { |
| 259 | Object.defineProperty({}, "x", 1); |
| 260 | }, "Property description must be an object: 1", TypeError); |
| 261 | |
| 262 | // kPropertyNotFunction |
| 263 | test(function() { |
| 264 | Set.prototype.add = 0; |
| 265 | new Set(1); |
| 266 | }, "'0' returned for property 'add' of object '#<Set>' is not a function", TypeError); |
| 267 | |
| 268 | // kProtoObjectOrNull |
| 269 | test(function() { |
| 270 | Object.setPrototypeOf({}, 1); |
| 271 | }, "Object prototype may only be an Object or null: 1", TypeError); |
| 272 | |
| 273 | // kRedefineDisallowed |
| 274 | test(function() { |
| 275 | "use strict"; |
| 276 | var o = {}; |
| 277 | Object.defineProperty(o, "x", { value: 1, configurable: false }); |
| 278 | Object.defineProperty(o, "x", { value: 2 }); |
| 279 | }, "Cannot redefine property: x", TypeError); |
| 280 | |
| 281 | // kReduceNoInitial |
| 282 | test(function() { |
| 283 | [].reduce(function() {}); |
| 284 | }, "Reduce of empty array with no initial value", TypeError); |
| 285 | |
| 286 | // kResolverNotAFunction |
| 287 | test(function() { |
| 288 | new Promise(1); |
| 289 | }, "Promise resolver 1 is not a function", TypeError); |
| 290 | |
| 291 | // kStrictDeleteProperty |
| 292 | test(function() { |
| 293 | "use strict"; |
| 294 | var o = {}; |
| 295 | Object.defineProperty(o, "p", { value: 1, writable: false }); |
| 296 | delete o.p; |
| 297 | }, "Cannot delete property 'p' of #<Object>", TypeError); |
| 298 | |
| 299 | // kStrictPoisonPill |
| 300 | test(function() { |
| 301 | "use strict"; |
| 302 | arguments.callee; |
| 303 | }, "'caller', 'callee', and 'arguments' properties may not be accessed on " + |
| 304 | "strict mode functions or the arguments objects for calls to them", |
| 305 | TypeError); |
| 306 | |
| 307 | // kStrictReadOnlyProperty |
| 308 | test(function() { |
| 309 | "use strict"; |
| 310 | (1).a = 1; |
| 311 | }, "Cannot create property 'a' on number '1'", TypeError); |
| 312 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 313 | // kSymbolToString |
| 314 | test(function() { |
| 315 | "" + Symbol(); |
| 316 | }, "Cannot convert a Symbol value to a string", TypeError); |
| 317 | |
| 318 | // kSymbolToNumber |
| 319 | test(function() { |
| 320 | 1 + Symbol(); |
| 321 | }, "Cannot convert a Symbol value to a number", TypeError); |
| 322 | |
| 323 | // kSimdToNumber |
| 324 | test(function() { |
| 325 | 1 + SIMD.Float32x4(1, 2, 3, 4); |
| 326 | }, "Cannot convert a SIMD value to a number", TypeError); |
| 327 | |
| 328 | // kUndefinedOrNullToObject |
| 329 | test(function() { |
| 330 | Array.prototype.toString.call(null); |
| 331 | }, "Cannot convert undefined or null to object", TypeError); |
| 332 | |
| 333 | // kValueAndAccessor |
| 334 | test(function() { |
| 335 | Object.defineProperty({}, "x", { get: function(){}, value: 1}); |
| 336 | }, "Invalid property descriptor. Cannot both specify accessors " + |
| 337 | "and a value or writable attribute, #<Object>", TypeError); |
| 338 | |
| 339 | |
| 340 | // === SyntaxError === |
| 341 | |
| 342 | // kInvalidRegExpFlags |
| 343 | test(function() { |
| 344 | eval("/a/x.test(\"a\");"); |
| 345 | }, "Invalid regular expression flags", SyntaxError); |
| 346 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 347 | // kInvalidOrUnexpectedToken |
| 348 | test(function() { |
| 349 | eval("'\n'"); |
| 350 | }, "Invalid or unexpected token", SyntaxError); |
| 351 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 352 | //kJsonParseUnexpectedEOS |
| 353 | test(function() { |
| 354 | JSON.parse("{") |
| 355 | }, "Unexpected end of JSON input", SyntaxError); |
| 356 | |
| 357 | // kJsonParseUnexpectedTokenAt |
| 358 | test(function() { |
| 359 | JSON.parse("/") |
| 360 | }, "Unexpected token / in JSON at position 0", SyntaxError); |
| 361 | |
| 362 | // kJsonParseUnexpectedTokenNumberAt |
| 363 | test(function() { |
| 364 | JSON.parse("{ 1") |
| 365 | }, "Unexpected number in JSON at position 2", SyntaxError); |
| 366 | |
| 367 | // kJsonParseUnexpectedTokenStringAt |
| 368 | test(function() { |
| 369 | JSON.parse('"""') |
| 370 | }, "Unexpected string in JSON at position 2", SyntaxError); |
| 371 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 372 | // kMalformedRegExp |
| 373 | test(function() { |
| 374 | /(/.test("a"); |
| 375 | }, "Invalid regular expression: /(/: Unterminated group", SyntaxError); |
| 376 | |
| 377 | // kParenthesisInArgString |
| 378 | test(function() { |
| 379 | new Function(")", ""); |
| 380 | }, "Function arg string contains parenthesis", SyntaxError); |
| 381 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 382 | // === ReferenceError === |
| 383 | |
| 384 | // kNotDefined |
| 385 | test(function() { |
| 386 | "use strict"; |
| 387 | o; |
| 388 | }, "o is not defined", ReferenceError); |
| 389 | |
| 390 | // === RangeError === |
| 391 | |
| 392 | // kArrayLengthOutOfRange |
| 393 | test(function() { |
| 394 | "use strict"; |
| 395 | Object.defineProperty([], "length", { value: 1E100 }); |
| 396 | }, "Invalid array length", RangeError); |
| 397 | |
| 398 | // kInvalidArrayBufferLength |
| 399 | test(function() { |
| 400 | new ArrayBuffer(-1); |
| 401 | }, "Invalid array buffer length", RangeError); |
| 402 | |
| 403 | // kInvalidArrayLength |
| 404 | test(function() { |
| 405 | [].length = -1; |
| 406 | }, "Invalid array length", RangeError); |
| 407 | |
| 408 | // kInvalidCodePoint |
| 409 | test(function() { |
| 410 | String.fromCodePoint(-1); |
| 411 | }, "Invalid code point -1", RangeError); |
| 412 | |
| 413 | // kInvalidCountValue |
| 414 | test(function() { |
| 415 | "a".repeat(-1); |
| 416 | }, "Invalid count value", RangeError); |
| 417 | |
| 418 | // kInvalidArrayBufferLength |
| 419 | test(function() { |
| 420 | new Uint16Array(-1); |
| 421 | }, "Invalid typed array length", RangeError); |
| 422 | |
| 423 | // kNormalizationForm |
| 424 | test(function() { |
| 425 | "".normalize("ABC"); |
| 426 | }, "The normalization form should be one of NFC, NFD, NFKC, NFKD.", RangeError); |
| 427 | |
| 428 | // kNumberFormatRange |
| 429 | test(function() { |
| 430 | Number(1).toFixed(100); |
| 431 | }, "toFixed() digits argument must be between 0 and 20", RangeError); |
| 432 | |
| 433 | test(function() { |
| 434 | Number(1).toExponential(100); |
| 435 | }, "toExponential() argument must be between 0 and 20", RangeError); |
| 436 | |
| 437 | // kStackOverflow |
| 438 | test(function() { |
| 439 | function f() { f(Array(1000)); } |
| 440 | f(); |
| 441 | }, "Maximum call stack size exceeded", RangeError); |
| 442 | |
| 443 | // kToPrecisionFormatRange |
| 444 | test(function() { |
| 445 | Number(1).toPrecision(100); |
| 446 | }, "toPrecision() argument must be between 1 and 21", RangeError); |
| 447 | |
| 448 | // kToPrecisionFormatRange |
| 449 | test(function() { |
| 450 | Number(1).toString(100); |
| 451 | }, "toString() radix argument must be between 2 and 36", RangeError); |
| 452 | |
| 453 | |
| 454 | // === URIError === |
| 455 | |
| 456 | // kURIMalformed |
| 457 | test(function() { |
| 458 | decodeURI("%%"); |
| 459 | }, "URI malformed", URIError); |