blob: 30abc197e9b941bc80a038633409af23a1996dcb [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// 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 Murdochda12d292016-06-02 14:46:10 +01005// Flags: --stack-size=100 --harmony
Ben Murdoch61f157c2016-09-16 13:49:30 +01006// Flags: --harmony-simd
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007
8function 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
22test(function() {
23 var o = {};
24 o.__proto__ = o;
25}, "Cyclic __proto__ value", Error);
26
27
28// === TypeError ===
29
30// kApplyNonFunction
31test(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
37test(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
44test(function() {
45 var a = [1];
46 Object.seal(a);
47 a.shift();
48}, "Cannot add/remove sealed array elements", TypeError);
49
50// kCalledNonCallable
51test(function() {
52 [].forEach(1);
53}, "1 is not a function", TypeError);
54
55// kCalledOnNonObject
56test(function() {
57 Object.defineProperty(1, "x", {});
58}, "Object.defineProperty called on non-object", TypeError);
59
60test(function() {
61 (function() {}).apply({}, 1);
62}, "CreateListFromArrayLike called on non-object", TypeError);
63
64test(function() {
65 Reflect.apply(function() {}, {}, 1);
66}, "CreateListFromArrayLike called on non-object", TypeError);
67
68test(function() {
69 Reflect.construct(function() {}, 1);
70}, "CreateListFromArrayLike called on non-object", TypeError);
71
72// kCalledOnNullOrUndefined
73test(function() {
74 Array.prototype.shift.call(null);
75}, "Array.prototype.shift called on null or undefined", TypeError);
76
77// kCannotFreezeArrayBufferView
78test(function() {
79 Object.freeze(new Uint16Array(1));
80}, "Cannot freeze array buffer views with elements", TypeError);
81
82// kConstAssign
83test(function() {
84 "use strict";
85 const a = 1;
86 a = 2;
87}, "Assignment to constant variable.", TypeError);
88
89// kCannotConvertToPrimitive
90test(function() {
91 var o = { toString: function() { return this } };
92 [].join(o);
93}, "Cannot convert object to primitive value", TypeError);
94
95// kCircularStructure
96test(function() {
97 var o = {};
98 o.o = o;
99 JSON.stringify(o);
100}, "Converting circular structure to JSON", TypeError);
101
102// kConstructorNotFunction
103test(function() {
104 Uint16Array(1);
105}, "Constructor Uint16Array requires 'new'", TypeError);
106
107// kDataViewNotArrayBuffer
108test(function() {
109 new DataView(1);
110}, "First argument to DataView constructor must be an ArrayBuffer", TypeError);
111
112// kDefineDisallowed
113test(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
121test(function() {
122 "a".startsWith(/a/);
123}, "First argument to String.prototype.startsWith " +
124 "must not be a regular expression", TypeError);
125
126// kFlagsGetterNonObject
127test(function() {
128 Object.getOwnPropertyDescriptor(RegExp.prototype, "flags").get.call(1);
129}, "RegExp.prototype.flags getter called on non-object 1", TypeError);
130
131// kFunctionBind
132test(function() {
133 Function.prototype.bind.call(1);
134}, "Bind must be called on a function", TypeError);
135
136// kGeneratorRunning
137test(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
145test(function() {
146 Set.prototype.add.call([]);
147}, "Method Set.prototype.add called on incompatible receiver [object Array]",
148TypeError);
149
Ben Murdochc5610432016-08-08 18:44:38 +0100150// kNonCallableInInstanceOfCheck
151test(function() {
152 1 instanceof {};
153}, "Right-hand side of 'instanceof' is not callable", TypeError);
154
155// kNonObjectInInstanceOfCheck
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000156test(function() {
157 1 instanceof 1;
Ben Murdochda12d292016-06-02 14:46:10 +0100158}, "Right-hand side of 'instanceof' is not an object", TypeError);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000159
160// kInstanceofNonobjectProto
161test(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
169test(function() {
170 1 in 1;
171}, "Cannot use 'in' operator to search for '1' in 1", TypeError);
172
173// kIteratorResultNotAnObject
174test(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
181test(function() {
182 new Map([1]);
183}, "Iterator value 1 is not an entry object", TypeError);
184
185// kNotConstructor
186test(function() {
187 new Symbol();
188}, "Symbol is not a constructor", TypeError);
189
190// kNotDateObject
191test(function() {
192 Date.prototype.getHours.call(1);
193}, "this is not a Date object.", TypeError);
194
195// kNotGeneric
196test(function() {
197 String.prototype.toString.call(1);
198}, "String.prototype.toString is not generic", TypeError);
199
200test(function() {
201 String.prototype.valueOf.call(1);
202}, "String.prototype.valueOf is not generic", TypeError);
203
204test(function() {
205 Boolean.prototype.toString.call(1);
206}, "Boolean.prototype.toString is not generic", TypeError);
207
208test(function() {
209 Boolean.prototype.valueOf.call(1);
210}, "Boolean.prototype.valueOf is not generic", TypeError);
211
212test(function() {
213 Number.prototype.toString.call({});
214}, "Number.prototype.toString is not generic", TypeError);
215
216test(function() {
217 Number.prototype.valueOf.call({});
218}, "Number.prototype.valueOf is not generic", TypeError);
219
220test(function() {
221 Function.prototype.toString.call(1);
222}, "Function.prototype.toString is not generic", TypeError);
223
224// kNotTypedArray
225test(function() {
226 Uint16Array.prototype.forEach.call(1);
227}, "this is not a typed array.", TypeError);
228
229// kObjectGetterExpectingFunction
230test(function() {
231 ({}).__defineGetter__("x", 0);
232}, "Object.prototype.__defineGetter__: Expecting function", TypeError);
233
234// kObjectGetterCallable
235test(function() {
236 Object.defineProperty({}, "x", { get: 1 });
237}, "Getter must be a function: 1", TypeError);
238
239// kObjectNotExtensible
240test(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
248test(function() {
249 ({}).__defineSetter__("x", 0);
250}, "Object.prototype.__defineSetter__: Expecting function", TypeError);
251
252// kObjectSetterCallable
253test(function() {
254 Object.defineProperty({}, "x", { set: 1 });
255}, "Setter must be a function: 1", TypeError);
256
257// kPropertyDescObject
258test(function() {
259 Object.defineProperty({}, "x", 1);
260}, "Property description must be an object: 1", TypeError);
261
262// kPropertyNotFunction
263test(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
269test(function() {
270 Object.setPrototypeOf({}, 1);
271}, "Object prototype may only be an Object or null: 1", TypeError);
272
273// kRedefineDisallowed
274test(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
282test(function() {
283 [].reduce(function() {});
284}, "Reduce of empty array with no initial value", TypeError);
285
286// kResolverNotAFunction
287test(function() {
288 new Promise(1);
289}, "Promise resolver 1 is not a function", TypeError);
290
291// kStrictDeleteProperty
292test(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
300test(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
308test(function() {
309 "use strict";
310 (1).a = 1;
311}, "Cannot create property 'a' on number '1'", TypeError);
312
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000313// kSymbolToString
314test(function() {
315 "" + Symbol();
316}, "Cannot convert a Symbol value to a string", TypeError);
317
318// kSymbolToNumber
319test(function() {
320 1 + Symbol();
321}, "Cannot convert a Symbol value to a number", TypeError);
322
323// kSimdToNumber
324test(function() {
325 1 + SIMD.Float32x4(1, 2, 3, 4);
326}, "Cannot convert a SIMD value to a number", TypeError);
327
328// kUndefinedOrNullToObject
329test(function() {
330 Array.prototype.toString.call(null);
331}, "Cannot convert undefined or null to object", TypeError);
332
333// kValueAndAccessor
334test(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
343test(function() {
344 eval("/a/x.test(\"a\");");
345}, "Invalid regular expression flags", SyntaxError);
346
Ben Murdochda12d292016-06-02 14:46:10 +0100347// kInvalidOrUnexpectedToken
348test(function() {
349 eval("'\n'");
350}, "Invalid or unexpected token", SyntaxError);
351
Ben Murdoch097c5b22016-05-18 11:27:45 +0100352//kJsonParseUnexpectedEOS
353test(function() {
354 JSON.parse("{")
355}, "Unexpected end of JSON input", SyntaxError);
356
357// kJsonParseUnexpectedTokenAt
358test(function() {
359 JSON.parse("/")
360}, "Unexpected token / in JSON at position 0", SyntaxError);
361
362// kJsonParseUnexpectedTokenNumberAt
363test(function() {
364 JSON.parse("{ 1")
365}, "Unexpected number in JSON at position 2", SyntaxError);
366
367// kJsonParseUnexpectedTokenStringAt
368test(function() {
369 JSON.parse('"""')
370}, "Unexpected string in JSON at position 2", SyntaxError);
371
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000372// kMalformedRegExp
373test(function() {
374 /(/.test("a");
375}, "Invalid regular expression: /(/: Unterminated group", SyntaxError);
376
377// kParenthesisInArgString
378test(function() {
379 new Function(")", "");
380}, "Function arg string contains parenthesis", SyntaxError);
381
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000382// === ReferenceError ===
383
384// kNotDefined
385test(function() {
386 "use strict";
387 o;
388}, "o is not defined", ReferenceError);
389
390// === RangeError ===
391
392// kArrayLengthOutOfRange
393test(function() {
394 "use strict";
395 Object.defineProperty([], "length", { value: 1E100 });
396}, "Invalid array length", RangeError);
397
398// kInvalidArrayBufferLength
399test(function() {
400 new ArrayBuffer(-1);
401}, "Invalid array buffer length", RangeError);
402
403// kInvalidArrayLength
404test(function() {
405 [].length = -1;
406}, "Invalid array length", RangeError);
407
408// kInvalidCodePoint
409test(function() {
410 String.fromCodePoint(-1);
411}, "Invalid code point -1", RangeError);
412
413// kInvalidCountValue
414test(function() {
415 "a".repeat(-1);
416}, "Invalid count value", RangeError);
417
418// kInvalidArrayBufferLength
419test(function() {
420 new Uint16Array(-1);
421}, "Invalid typed array length", RangeError);
422
423// kNormalizationForm
424test(function() {
425 "".normalize("ABC");
426}, "The normalization form should be one of NFC, NFD, NFKC, NFKD.", RangeError);
427
428// kNumberFormatRange
429test(function() {
430 Number(1).toFixed(100);
431}, "toFixed() digits argument must be between 0 and 20", RangeError);
432
433test(function() {
434 Number(1).toExponential(100);
435}, "toExponential() argument must be between 0 and 20", RangeError);
436
437// kStackOverflow
438test(function() {
439 function f() { f(Array(1000)); }
440 f();
441}, "Maximum call stack size exceeded", RangeError);
442
443// kToPrecisionFormatRange
444test(function() {
445 Number(1).toPrecision(100);
446}, "toPrecision() argument must be between 1 and 21", RangeError);
447
448// kToPrecisionFormatRange
449test(function() {
450 Number(1).toString(100);
451}, "toString() radix argument must be between 2 and 36", RangeError);
452
453
454// === URIError ===
455
456// kURIMalformed
457test(function() {
458 decodeURI("%%");
459}, "URI malformed", URIError);