Andy Street 6ec5b953cb Red box c++ exceptions thrown while executing sync hooks
Summary: Also adds support for creating JS Error objects. When we clean up the way global functions are installed on JSC, we'll be able to use this pattern to auto-catch all c++ exceptions at this level.

Reviewed By: lexs

Differential Revision: D3276663

fbshipit-source-id: bf3561690013019a25e2be5a4d1b69ba9e002c72
2016-05-10 05:03:30 -07:00

53 lines
1.2 KiB
C++

// Copyright 2004-present Facebook. All Rights Reserved.
#pragma once
#include <JavaScriptCore/JSContextRef.h>
#include <JavaScriptCore/JSObjectRef.h>
#include <JavaScriptCore/JSValueRef.h>
#include <stdexcept>
#include <algorithm>
#include "Value.h"
namespace facebook {
namespace react {
struct JsException : std::runtime_error {
using std::runtime_error::runtime_error;
};
inline void throwJSExecutionException(const char* msg) {
throw JsException(msg);
}
template <typename... Args>
inline void throwJSExecutionException(const char* fmt, Args... args) {
int msgSize = snprintf(nullptr, 0, fmt, args...);
msgSize = std::min(512, msgSize + 1);
char *msg = (char*) alloca(msgSize);
snprintf(msg, msgSize, fmt, args...);
throw JsException(msg);
}
void installGlobalFunction(
JSGlobalContextRef ctx,
const char* name,
JSObjectCallAsFunctionCallback callback);
JSValueRef makeJSCException(
JSContextRef ctx,
const char* exception_text);
JSValueRef evaluateScript(
JSContextRef ctx,
JSStringRef script,
JSStringRef sourceURL);
JSValueRef makeJSError(JSContextRef ctx, const char *error);
JSValueRef translatePendingCppExceptionToJSError(JSContextRef ctx, const char *exceptionLocation);
} }