| 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 "js/Exception.h" |
| 6 |
#include "jsapi-tests/tests.h" |
| 7 |
|
| 8 |
using JS::CreateError; |
| 9 |
using JS::ObjectValue; |
| 10 |
using JS::Rooted; |
| 11 |
using JS::Value; |
| 12 |
|
| 13 |
enum SymbolExceptionType { |
| 14 |
NONE, |
| 15 |
SYMBOL_ITERATOR, |
| 16 |
SYMBOL_FOO, |
| 17 |
SYMBOL_EMPTY, |
| 18 |
}; |
| 19 |
|
| 20 |
BEGIN_TEST(testUncaughtSymbol) { |
| 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 |
JS::ExceptionStack exnStack(cx); |
| 35 |
MOZ_RELEASE_ASSERT(JS::StealPendingExceptionStack(cx, &exnStack)); |
| 36 |
MOZ_RELEASE_ASSERT(exnStack.exception().isSymbol()); |
| 37 |
|
| 38 |
JS::ErrorReportBuilder report(cx); |
| 39 |
MOZ_RELEASE_ASSERT( |
| 40 |
report.init(cx, exnStack, JS::ErrorReportBuilder::WithSideEffects)); |
| 41 |
|
| 42 |
if (strcmp(report.toStringResult().c_str(), |
| 43 |
"uncaught exception: Symbol(Symbol.iterator)") == 0) { |
| 44 |
return SYMBOL_ITERATOR; |
| 45 |
} |
| 46 |
if (strcmp(report.toStringResult().c_str(), |
| 47 |
"uncaught exception: Symbol(foo)") == 0) { |
| 48 |
return SYMBOL_FOO; |
| 49 |
} |
| 50 |
if (strcmp(report.toStringResult().c_str(), "uncaught exception: Symbol()") == |
| 51 |
0) { |
| 52 |
return SYMBOL_EMPTY; |
| 53 |
} |
| 54 |
MOZ_CRASH("Unexpected symbol"); |
| 55 |
} |
| 56 |
|
| 57 |
END_TEST(testUncaughtSymbol) |