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"); } }
static JSValueRef call( JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef *exception) { try { auto executor = Object::getGlobalObject(ctx).getPrivate<JSCExecutor>(); return (executor->*method)(object, propertyName); } catch (...) { *exception = translatePendingCppExceptionToJSError(ctx, object); return Value::makeUndefined(ctx); } }
static JSValueRef call( JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef *exception) { try { auto globalObj = JSContextGetGlobalObject(ctx); auto executor = static_cast<JSCExecutor*>(JSObjectGetPrivate(globalObj)); return (executor->*method)(object, propertyName); } catch (...) { *exception = translatePendingCppExceptionToJSError(ctx, object); return JSValueMakeUndefined(ctx); } }
static JSValueRef call( JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef *exception) { try { auto executor = Object::getGlobalObject(ctx).getPrivate<JSCExecutor>(); return (executor->*method)(argumentCount, arguments); } catch (...) { *exception = translatePendingCppExceptionToJSError(ctx, function); return Value::makeUndefined(ctx); } }
static JSValueRef call( JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef *exception) { try { auto globalObj = JSContextGetGlobalObject(ctx); auto executor = static_cast<JSCExecutor*>(JSObjectGetPrivate(globalObj)); return (executor->*method)(argumentCount, arguments); } catch (...) { *exception = translatePendingCppExceptionToJSError(ctx, function); return JSValueMakeUndefined(ctx); } }
JSValueRef JSCExecutor::nativeCallSyncHook( JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef *exception) { if (argumentCount != 3) { *exception = createErrorString(ctx, "Got wrong number of args for callSyncHook"); return JSValueMakeUndefined(ctx); } unsigned int moduleId = Value(ctx, arguments[0]).asUnsignedInteger(); unsigned int methodId = Value(ctx, arguments[1]).asUnsignedInteger(); std::string argsJson = Value(ctx, arguments[2]).toJSONString(); JSCExecutor *executor; try { executor = s_globalContextRefToJSCExecutor.at(JSContextGetGlobalContext(ctx)); } catch (std::out_of_range& e) { *exception = createErrorString(ctx, "Global JS context didn't map to a valid executor"); return JSValueMakeUndefined(ctx); } try { MethodCallResult result = executor->m_bridge->callSerializableNativeHook( moduleId, methodId, argsJson); if (result.isUndefined) { return JSValueMakeUndefined(ctx); } return Value::fromJSON(ctx, String(folly::toJson(result.result).c_str())); } catch (...) { *exception = translatePendingCppExceptionToJSError(ctx, "nativeCallSyncHook"); return JSValueMakeUndefined(ctx); } }