Skip to content

Commit

Permalink
Add JSCHelper function to easily install native hooks outside of JSCE…
Browse files Browse the repository at this point in the history
…xecutor

Summary: JSCExecutor has something like this, but for methods on JSCExecutor itself. Open to better ideas around how to share code

Reviewed By: lexs

Differential Revision: D3516314

fbshipit-source-id: 4b1265916c52d582bb0b9348e9b4a099f566d6c9
  • Loading branch information
astreet authored and Facebook Github Bot 9 committed Jul 7, 2016
1 parent 10d41b5 commit f534560
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
7 changes: 1 addition & 6 deletions ReactCommon/cxxreact/JSCExecutor.cpp
Expand Up @@ -66,12 +66,7 @@ inline JSObjectCallAsFunctionCallback exceptionWrapMethod() {
auto executor = static_cast<JSCExecutor*>(JSObjectGetPrivate(globalObj));
return (executor->*method)(argumentCount, arguments);
} catch (...) {
try {
auto functionName = Object(ctx, function).getProperty("name").toString().str();
*exception = translatePendingCppExceptionToJSError(ctx, functionName.c_str());
} catch (...) {
*exception = makeJSError(ctx, "Failed to get function name while handling exception");
}
*exception = translatePendingCppExceptionToJSError(ctx, function);
return JSValueMakeUndefined(ctx);
}
}
Expand Down
9 changes: 9 additions & 0 deletions ReactCommon/cxxreact/JSCHelpers.cpp
Expand Up @@ -113,4 +113,13 @@ JSValueRef translatePendingCppExceptionToJSError(JSContextRef ctx, const char *e
}
}

JSValueRef translatePendingCppExceptionToJSError(JSContextRef ctx, JSObjectRef jsFunctionCause) {
try {
auto functionName = Object(ctx, jsFunctionCause).getProperty("name").toString().str();
return translatePendingCppExceptionToJSError(ctx, functionName.c_str());
} catch (...) {
return makeJSError(ctx, "Failed to get function name while handling exception");
}
}

} }
28 changes: 28 additions & 0 deletions ReactCommon/cxxreact/JSCHelpers.h
Expand Up @@ -52,5 +52,33 @@ JSValueRef evaluateScript(
JSValueRef makeJSError(JSContextRef ctx, const char *error);

JSValueRef translatePendingCppExceptionToJSError(JSContextRef ctx, const char *exceptionLocation);
JSValueRef translatePendingCppExceptionToJSError(JSContextRef ctx, JSObjectRef jsFunctionCause);

template<JSValueRef (method)(JSContextRef ctx,
JSObjectRef function,
JSObjectRef thisObject,
size_t argumentCount,
const JSValueRef arguments[],
JSValueRef *exception)>
inline JSObjectCallAsFunctionCallback exceptionWrapMethod() {
struct funcWrapper {
static JSValueRef call(
JSContextRef ctx,
JSObjectRef function,
JSObjectRef thisObject,
size_t argumentCount,
const JSValueRef arguments[],
JSValueRef *exception) {
try {
return (*method)(ctx, function, thisObject, argumentCount, arguments, exception);
} catch (...) {
*exception = translatePendingCppExceptionToJSError(ctx, function);
return JSValueMakeUndefined(ctx);
}
}
};

return &funcWrapper::call;
}

} }

0 comments on commit f534560

Please sign in to comment.