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::ObjectValue;
9 using JS::Rooted;
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   CHECK(!execDontReport("throw Symbol.iterator;", __FILE__, __LINE__));
21   CHECK(GetSymbolExceptionType(cx) == SYMBOL_ITERATOR);
22 
23   CHECK(!execDontReport("throw Symbol('foo');", __FILE__, __LINE__));
24   CHECK(GetSymbolExceptionType(cx) == SYMBOL_FOO);
25 
26   CHECK(!execDontReport("throw Symbol();", __FILE__, __LINE__));
27   CHECK(GetSymbolExceptionType(cx) == SYMBOL_EMPTY);
28 
29   return true;
30 }
31 
GetSymbolExceptionType(JSContext * cx)32 static SymbolExceptionType GetSymbolExceptionType(JSContext* cx) {
33   JS::RootedValue exn(cx);
34   MOZ_RELEASE_ASSERT(JS_GetPendingException(cx, &exn));
35   MOZ_RELEASE_ASSERT(exn.isSymbol());
36   JS_ClearPendingException(cx);
37 
38   js::ErrorReport report(cx);
39   MOZ_RELEASE_ASSERT(report.init(cx, exn, js::ErrorReport::WithSideEffects));
40 
41   if (strcmp(report.toStringResult().c_str(),
42              "uncaught exception: Symbol(Symbol.iterator)") == 0)
43     return SYMBOL_ITERATOR;
44   if (strcmp(report.toStringResult().c_str(),
45              "uncaught exception: Symbol(foo)") == 0)
46     return SYMBOL_FOO;
47   if (strcmp(report.toStringResult().c_str(), "uncaught exception: Symbol()") ==
48       0)
49     return SYMBOL_EMPTY;
50   MOZ_CRASH("Unexpected symbol");
51 }
52 
53 END_TEST(testUncaughtSymbol)
54