Skip to content

Commit

Permalink
examples: Move ReportAndClearException to boilerplate
Browse files Browse the repository at this point in the history
This is a useful helper for quickly debugging embedding mistakes so more it
to the boilerplate code.
  • Loading branch information
moztcampbell committed Sep 1, 2020
1 parent 90389cd commit b27cd35
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
19 changes: 19 additions & 0 deletions examples/boilerplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,25 @@ JSObject* boilerplate::CreateGlobal(JSContext* cx) {
JS::FireOnNewGlobalHook, options);
}

// Helper to read current exception and dump to stderr.
//
// NOTE: This must be called with a JSAutoRealm (or equivalent) on the stack.
void boilerplate::ReportAndClearException(JSContext* cx) {
JS::ExceptionStack stack(cx);
if (!JS::StealPendingExceptionStack(cx, &stack)) {
fprintf(stderr, "Uncatchable exception thrown, out of memory or something");
exit(1);
}

JS::ErrorReportBuilder report(cx);
if (!report.init(cx, stack, JS::ErrorReportBuilder::WithSideEffects)) {
fprintf(stderr, "Couldn't build error report");
exit(1);
}

JS::PrintError(cx, stderr, report, false);
}

// Initialize the JS environment, create a JSContext and run the example
// function in that context. By default the self-hosting environment is
// initialized as it is needed to run any JavaScript). If the 'initSelfHosting'
Expand Down
2 changes: 2 additions & 0 deletions examples/boilerplate.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ extern const JSClassOps DefaultGlobalClassOps;

JSObject* CreateGlobal(JSContext* cx);

void ReportAndClearException(JSContext* cx);

bool RunExample(bool (*task)(JSContext*), bool initSelfHosting = true);

} // namespace boilerplate
22 changes: 3 additions & 19 deletions examples/repl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,6 @@ class ReplGlobal {
constexpr JSClass ReplGlobal::klass;
constexpr JSFunctionSpec ReplGlobal::functions[];

static void die(const char* why) {
std::cerr << "fatal error:" << why << std::endl;
exit(1);
}

std::string FormatString(JSContext* cx, JS::HandleString string) {
std::string buf = "\"";

Expand Down Expand Up @@ -131,19 +126,6 @@ std::string FormatResult(JSContext* cx, JS::HandleValue value) {
return bytes.get();
}

static void ReportAndClearException(JSContext* cx) {
/* Get exception object before printing and clearing exception. */
JS::ExceptionStack stack(cx);
if (!JS::StealPendingExceptionStack(cx, &stack))
die("Uncatchable exception thrown, out of memory or something");

JS::ErrorReportBuilder report(cx);
if (!report.init(cx, stack, JS::ErrorReportBuilder::WithSideEffects))
die("Couldn't build error report");

JS::PrintError(cx, stderr, report, false);
}

JSObject* ReplGlobal::create(JSContext* cx) {
JS::RealmOptions options;
JS::RootedObject global(cx,
Expand Down Expand Up @@ -207,7 +189,9 @@ void ReplGlobal::loop(JSContext* cx, JS::HandleObject global) {
buffer.length()));

if (!EvalAndPrint(cx, buffer, startline)) {
if (!priv(global)->m_shouldQuit) ReportAndClearException(cx);
if (!priv(global)->m_shouldQuit) {
boilerplate::ReportAndClearException(cx);
}
}

js::RunJobs(cx);
Expand Down

0 comments on commit b27cd35

Please sign in to comment.