Permalink
Browse files

Add JSCHelper function to easily install native hooks outside of JSCE…

…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...
1 parent 10d41b5 commit f5345601d9c16e5b70c2534acdb567e0e1b28134 @astreet astreet committed with Facebook Github Bot 9 Jul 7, 2016
Showing with 38 additions and 6 deletions.
  1. +1 −6 ReactCommon/cxxreact/JSCExecutor.cpp
  2. +9 −0 ReactCommon/cxxreact/JSCHelpers.cpp
  3. +28 −0 ReactCommon/cxxreact/JSCHelpers.h
@@ -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);
}
}
@@ -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");
+ }
+}
+
} }
@@ -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.