blob: 8796d05f16e5be3afadc562c64fce372253ca670 [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
6// Flags: --harmony-simd --harmony-instanceof
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
150// kInstanceofFunctionExpected
151test(function() {
152 1 instanceof 1;
Ben Murdochda12d292016-06-02 14:46:10 +0100153}, "Right-hand side of 'instanceof' is not an object", TypeError);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000154
155// kInstanceofNonobjectProto
156test(function() {
157 function f() {}
158 var o = new f();
159 f.prototype = 1;
160 o instanceof f;
161}, "Function has non-object prototype '1' in instanceof check", TypeError);
162
163// kInvalidInOperatorUse
164test(function() {
165 1 in 1;
166}, "Cannot use 'in' operator to search for '1' in 1", TypeError);
167
168// kIteratorResultNotAnObject
169test(function() {
170 var obj = {};
171 obj[Symbol.iterator] = function() { return { next: function() { return 1 }}};
172 Array.from(obj);
173}, "Iterator result 1 is not an object", TypeError);
174
175// kIteratorValueNotAnObject
176test(function() {
177 new Map([1]);
178}, "Iterator value 1 is not an entry object", TypeError);
179
180// kNotConstructor
181test(function() {
182 new Symbol();
183}, "Symbol is not a constructor", TypeError);
184
185// kNotDateObject
186test(function() {
187 Date.prototype.getHours.call(1);
188}, "this is not a Date object.", TypeError);
189
190// kNotGeneric
191test(function() {
192 String.prototype.toString.call(1);
193}, "String.prototype.toString is not generic", TypeError);
194
195test(function() {
196 String.prototype.valueOf.call(1);
197}, "String.prototype.valueOf is not generic", TypeError);
198
199test(function() {
200 Boolean.prototype.toString.call(1);
201}, "Boolean.prototype.toString is not generic", TypeError);
202
203test(function() {
204 Boolean.prototype.valueOf.call(1);
205}, "Boolean.prototype.valueOf is not generic", TypeError);
206
207test(function() {
208 Number.prototype.toString.call({});
209}, "Number.prototype.toString is not generic", TypeError);
210
211test(function() {
212 Number.prototype.valueOf.call({});
213}, "Number.prototype.valueOf is not generic", TypeError);
214
215test(function() {
216 Function.prototype.toString.call(1);
217}, "Function.prototype.toString is not generic", TypeError);
218
219// kNotTypedArray
220test(function() {
221 Uint16Array.prototype.forEach.call(1);
222}, "this is not a typed array.", TypeError);
223
224// kObjectGetterExpectingFunction
225test(function() {
226 ({}).__defineGetter__("x", 0);
227}, "Object.prototype.__defineGetter__: Expecting function", TypeError);
228
229// kObjectGetterCallable
230test(function() {
231 Object.defineProperty({}, "x", { get: 1 });
232}, "Getter must be a function: 1", TypeError);
233
234// kObjectNotExtensible
235test(function() {
236 "use strict";
237 var o = {};
238 Object.freeze(o);
239 o.a = 1;
240}, "Can't add property a, object is not extensible", TypeError);
241
242// kObjectSetterExpectingFunction
243test(function() {
244 ({}).__defineSetter__("x", 0);
245}, "Object.prototype.__defineSetter__: Expecting function", TypeError);
246
247// kObjectSetterCallable
248test(function() {
249 Object.defineProperty({}, "x", { set: 1 });
250}, "Setter must be a function: 1", TypeError);
251
252// kPropertyDescObject
253test(function() {
254 Object.defineProperty({}, "x", 1);
255}, "Property description must be an object: 1", TypeError);
256
257// kPropertyNotFunction
258test(function() {
259 Set.prototype.add = 0;
260 new Set(1);
261}, "'0' returned for property 'add' of object '#<Set>' is not a function", TypeError);
262
263// kProtoObjectOrNull
264test(function() {
265 Object.setPrototypeOf({}, 1);
266}, "Object prototype may only be an Object or null: 1", TypeError);
267
268// kRedefineDisallowed
269test(function() {
270 "use strict";
271 var o = {};
272 Object.defineProperty(o, "x", { value: 1, configurable: false });
273 Object.defineProperty(o, "x", { value: 2 });
274}, "Cannot redefine property: x", TypeError);
275
276// kReduceNoInitial
277test(function() {
278 [].reduce(function() {});
279}, "Reduce of empty array with no initial value", TypeError);
280
281// kResolverNotAFunction
282test(function() {
283 new Promise(1);
284}, "Promise resolver 1 is not a function", TypeError);
285
286// kStrictDeleteProperty
287test(function() {
288 "use strict";
289 var o = {};
290 Object.defineProperty(o, "p", { value: 1, writable: false });
291 delete o.p;
292}, "Cannot delete property 'p' of #<Object>", TypeError);
293
294// kStrictPoisonPill
295test(function() {
296 "use strict";
297 arguments.callee;
298}, "'caller', 'callee', and 'arguments' properties may not be accessed on " +
299 "strict mode functions or the arguments objects for calls to them",
300 TypeError);
301
302// kStrictReadOnlyProperty
303test(function() {
304 "use strict";
305 (1).a = 1;
306}, "Cannot create property 'a' on number '1'", TypeError);
307
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000308// kSymbolToString
309test(function() {
310 "" + Symbol();
311}, "Cannot convert a Symbol value to a string", TypeError);
312
313// kSymbolToNumber
314test(function() {
315 1 + Symbol();
316}, "Cannot convert a Symbol value to a number", TypeError);
317
318// kSimdToNumber
319test(function() {
320 1 + SIMD.Float32x4(1, 2, 3, 4);
321}, "Cannot convert a SIMD value to a number", TypeError);
322
323// kUndefinedOrNullToObject
324test(function() {
325 Array.prototype.toString.call(null);
326}, "Cannot convert undefined or null to object", TypeError);
327
328// kValueAndAccessor
329test(function() {
330 Object.defineProperty({}, "x", { get: function(){}, value: 1});
331}, "Invalid property descriptor. Cannot both specify accessors " +
332 "and a value or writable attribute, #<Object>", TypeError);
333
334
335// === SyntaxError ===
336
337// kInvalidRegExpFlags
338test(function() {
339 eval("/a/x.test(\"a\");");
340}, "Invalid regular expression flags", SyntaxError);
341
Ben Murdochda12d292016-06-02 14:46:10 +0100342// kInvalidOrUnexpectedToken
343test(function() {
344 eval("'\n'");
345}, "Invalid or unexpected token", SyntaxError);
346
Ben Murdoch097c5b22016-05-18 11:27:45 +0100347//kJsonParseUnexpectedEOS
348test(function() {
349 JSON.parse("{")
350}, "Unexpected end of JSON input", SyntaxError);
351
352// kJsonParseUnexpectedTokenAt
353test(function() {
354 JSON.parse("/")
355}, "Unexpected token / in JSON at position 0", SyntaxError);
356
357// kJsonParseUnexpectedTokenNumberAt
358test(function() {
359 JSON.parse("{ 1")
360}, "Unexpected number in JSON at position 2", SyntaxError);
361
362// kJsonParseUnexpectedTokenStringAt
363test(function() {
364 JSON.parse('"""')
365}, "Unexpected string in JSON at position 2", SyntaxError);
366
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000367// kMalformedRegExp
368test(function() {
369 /(/.test("a");
370}, "Invalid regular expression: /(/: Unterminated group", SyntaxError);
371
372// kParenthesisInArgString
373test(function() {
374 new Function(")", "");
375}, "Function arg string contains parenthesis", SyntaxError);
376
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000377// === ReferenceError ===
378
379// kNotDefined
380test(function() {
381 "use strict";
382 o;
383}, "o is not defined", ReferenceError);
384
385// === RangeError ===
386
387// kArrayLengthOutOfRange
388test(function() {
389 "use strict";
390 Object.defineProperty([], "length", { value: 1E100 });
391}, "Invalid array length", RangeError);
392
393// kInvalidArrayBufferLength
394test(function() {
395 new ArrayBuffer(-1);
396}, "Invalid array buffer length", RangeError);
397
398// kInvalidArrayLength
399test(function() {
400 [].length = -1;
401}, "Invalid array length", RangeError);
402
403// kInvalidCodePoint
404test(function() {
405 String.fromCodePoint(-1);
406}, "Invalid code point -1", RangeError);
407
408// kInvalidCountValue
409test(function() {
410 "a".repeat(-1);
411}, "Invalid count value", RangeError);
412
413// kInvalidArrayBufferLength
414test(function() {
415 new Uint16Array(-1);
416}, "Invalid typed array length", RangeError);
417
418// kNormalizationForm
419test(function() {
420 "".normalize("ABC");
421}, "The normalization form should be one of NFC, NFD, NFKC, NFKD.", RangeError);
422
423// kNumberFormatRange
424test(function() {
425 Number(1).toFixed(100);
426}, "toFixed() digits argument must be between 0 and 20", RangeError);
427
428test(function() {
429 Number(1).toExponential(100);
430}, "toExponential() argument must be between 0 and 20", RangeError);
431
432// kStackOverflow
433test(function() {
434 function f() { f(Array(1000)); }
435 f();
436}, "Maximum call stack size exceeded", RangeError);
437
438// kToPrecisionFormatRange
439test(function() {
440 Number(1).toPrecision(100);
441}, "toPrecision() argument must be between 1 and 21", RangeError);
442
443// kToPrecisionFormatRange
444test(function() {
445 Number(1).toString(100);
446}, "toString() radix argument must be between 2 and 36", RangeError);
447
448
449// === URIError ===
450
451// kURIMalformed
452test(function() {
453 decodeURI("%%");
454}, "URI malformed", URIError);