blob: 8da7e6bd7b950420d214e64b2f880ba9c1080f59 [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
5// Flags: --stack-size=100 --harmony --harmony-reflect --harmony-regexps
6// Flags: --harmony-simd --strong-mode
7
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;
153}, "Expecting a function in instanceof check, but got 1", TypeError);
154
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
308// kStrongImplicitCast
309test(function() {
310 "use strong";
311 "a" + 1;
312}, "In strong mode, implicit conversions are deprecated", TypeError);
313
314// kSymbolToString
315test(function() {
316 "" + Symbol();
317}, "Cannot convert a Symbol value to a string", TypeError);
318
319// kSymbolToNumber
320test(function() {
321 1 + Symbol();
322}, "Cannot convert a Symbol value to a number", TypeError);
323
324// kSimdToNumber
325test(function() {
326 1 + SIMD.Float32x4(1, 2, 3, 4);
327}, "Cannot convert a SIMD value to a number", TypeError);
328
329// kUndefinedOrNullToObject
330test(function() {
331 Array.prototype.toString.call(null);
332}, "Cannot convert undefined or null to object", TypeError);
333
334// kValueAndAccessor
335test(function() {
336 Object.defineProperty({}, "x", { get: function(){}, value: 1});
337}, "Invalid property descriptor. Cannot both specify accessors " +
338 "and a value or writable attribute, #<Object>", TypeError);
339
340
341// === SyntaxError ===
342
343// kInvalidRegExpFlags
344test(function() {
345 eval("/a/x.test(\"a\");");
346}, "Invalid regular expression flags", SyntaxError);
347
348// kMalformedRegExp
349test(function() {
350 /(/.test("a");
351}, "Invalid regular expression: /(/: Unterminated group", SyntaxError);
352
353// kParenthesisInArgString
354test(function() {
355 new Function(")", "");
356}, "Function arg string contains parenthesis", SyntaxError);
357
358// kUnexpectedEOS
359test(function() {
360 JSON.parse("{")
361}, "Unexpected end of input", SyntaxError);
362
363// kUnexpectedToken
364test(function() {
365 JSON.parse("/")
366}, "Unexpected token /", SyntaxError);
367
368// kUnexpectedTokenNumber
369test(function() {
370 JSON.parse("{ 1")
371}, "Unexpected number", SyntaxError);
372
373// kUnexpectedTokenString
374test(function() {
375 JSON.parse('"""')
376}, "Unexpected string", SyntaxError);
377
378
379// === ReferenceError ===
380
381// kNotDefined
382test(function() {
383 "use strict";
384 o;
385}, "o is not defined", ReferenceError);
386
387// === RangeError ===
388
389// kArrayLengthOutOfRange
390test(function() {
391 "use strict";
392 Object.defineProperty([], "length", { value: 1E100 });
393}, "Invalid array length", RangeError);
394
395// kInvalidArrayBufferLength
396test(function() {
397 new ArrayBuffer(-1);
398}, "Invalid array buffer length", RangeError);
399
400// kInvalidArrayLength
401test(function() {
402 [].length = -1;
403}, "Invalid array length", RangeError);
404
405// kInvalidCodePoint
406test(function() {
407 String.fromCodePoint(-1);
408}, "Invalid code point -1", RangeError);
409
410// kInvalidCountValue
411test(function() {
412 "a".repeat(-1);
413}, "Invalid count value", RangeError);
414
415// kInvalidArrayBufferLength
416test(function() {
417 new Uint16Array(-1);
418}, "Invalid typed array length", RangeError);
419
420// kNormalizationForm
421test(function() {
422 "".normalize("ABC");
423}, "The normalization form should be one of NFC, NFD, NFKC, NFKD.", RangeError);
424
425// kNumberFormatRange
426test(function() {
427 Number(1).toFixed(100);
428}, "toFixed() digits argument must be between 0 and 20", RangeError);
429
430test(function() {
431 Number(1).toExponential(100);
432}, "toExponential() argument must be between 0 and 20", RangeError);
433
434// kStackOverflow
435test(function() {
436 function f() { f(Array(1000)); }
437 f();
438}, "Maximum call stack size exceeded", RangeError);
439
440// kToPrecisionFormatRange
441test(function() {
442 Number(1).toPrecision(100);
443}, "toPrecision() argument must be between 1 and 21", RangeError);
444
445// kToPrecisionFormatRange
446test(function() {
447 Number(1).toString(100);
448}, "toString() radix argument must be between 2 and 36", RangeError);
449
450
451// === URIError ===
452
453// kURIMalformed
454test(function() {
455 decodeURI("%%");
456}, "URI malformed", URIError);