Add JSCHelper function to easily install native hooks outside of JSCExecutor

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
This commit is contained in:
Andy Street 2016-07-07 04:48:45 -07:00 committed by Facebook Github Bot 9
parent 10d41b50c2
commit f5345601d9
3 changed files with 38 additions and 6 deletions

View File

@ -66,12 +66,7 @@ inline JSObjectCallAsFunctionCallback exceptionWrapMethod() {
auto executor = static_cast<JSCExecutor*>(JSObjectGetPrivate(globalObj)); auto executor = static_cast<JSCExecutor*>(JSObjectGetPrivate(globalObj));
return (executor->*method)(argumentCount, arguments); return (executor->*method)(argumentCount, arguments);
} catch (...) { } catch (...) {
try { *exception = translatePendingCppExceptionToJSError(ctx, function);
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");
}
return JSValueMakeUndefined(ctx); return JSValueMakeUndefined(ctx);
} }
} }

View File

@ -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");
}
}
} } } }

View File

@ -52,5 +52,33 @@ JSValueRef evaluateScript(
JSValueRef makeJSError(JSContextRef ctx, const char *error); JSValueRef makeJSError(JSContextRef ctx, const char *error);
JSValueRef translatePendingCppExceptionToJSError(JSContextRef ctx, const char *exceptionLocation); 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;
}
} } } }