1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #include "jsapi-tests/tests.h"
6 
7 using JS::CreateError;
8 using JS::Rooted;
9 using JS::ObjectValue;
10 using JS::Value;
11 
12 enum SymbolExceptionType {
13     NONE,
14     SYMBOL_ITERATOR,
15     SYMBOL_FOO,
16     SYMBOL_EMPTY,
17 };
18 
BEGIN_TEST(testUncaughtSymbol)19 BEGIN_TEST(testUncaughtSymbol)
20 {
21     CHECK(!execDontReport("throw Symbol.iterator;", __FILE__, __LINE__));
22     CHECK(GetSymbolExceptionType(cx) == SYMBOL_ITERATOR);
23 
24     CHECK(!execDontReport("throw Symbol('foo');", __FILE__, __LINE__));
25     CHECK(GetSymbolExceptionType(cx) == SYMBOL_FOO);
26 
27     CHECK(!execDontReport("throw Symbol();", __FILE__, __LINE__));
28     CHECK(GetSymbolExceptionType(cx) == SYMBOL_EMPTY);
29 
30     return true;
31 }
32 
33 static SymbolExceptionType
GetSymbolExceptionType(JSContext * cx)34 GetSymbolExceptionType(JSContext* cx)
35 {
36     JS::RootedValue exn(cx);
37     MOZ_RELEASE_ASSERT(JS_GetPendingException(cx, &exn));
38     MOZ_RELEASE_ASSERT(exn.isSymbol());
39     JS_ClearPendingException(cx);
40 
41     js::ErrorReport report(cx);
42     MOZ_RELEASE_ASSERT(report.init(cx, exn, js::ErrorReport::WithSideEffects));
43 
44     if (strcmp(report.toStringResult().c_str(), "uncaught exception: Symbol(Symbol.iterator)") == 0)
45         return SYMBOL_ITERATOR;
46     if (strcmp(report.toStringResult().c_str(), "uncaught exception: Symbol(foo)") == 0)
47         return SYMBOL_FOO;
48     if (strcmp(report.toStringResult().c_str(), "uncaught exception: Symbol()") == 0)
49         return SYMBOL_EMPTY;
50     MOZ_CRASH("Unexpected symbol");
51 }
52 
53 END_TEST(testUncaughtSymbol)
54