BaseHybridClass* getNativePointer(alias_ref<HybridData::javaobject> hybridData) { static auto pointerField = hybridData->getClass()->getField<jlong>("mNativePointer"); auto* value = reinterpret_cast<BaseHybridClass*>(hybridData->getFieldValue(pointerField)); if (!value) { throwNewJavaException("java/lang/NullPointerException", "java.lang.NullPointerException"); } return value; }
local_ref<HybridData::javaobject> getHybridData(alias_ref<jobject> jthis, JField<HybridData::javaobject> field) { auto hybridData = jthis->getFieldValue(field); if (!hybridData) { throwNewJavaException("java/lang/NullPointerException", "java.lang.NullPointerException"); } return hybridData; }
void throwNewJavaException(const char* throwableName, const char* msg) { // If anything of the fbjni calls fail, an exception of a suitable // form will be thrown, which is what we want. auto throwableClass = findClassLocal(throwableName); auto throwable = throwableClass->newObject( throwableClass->getConstructor<jthrowable(jstring)>(), make_jstring(msg).release()); throwNewJavaException(throwable.get()); }
jint makeJIntOrThrow(int64_t integer) { jint javaint = static_cast<jint>(integer); if (integer != javaint) { throwNewJavaException( exceptions::gUnexpectedNativeTypeExceptionClass, "Value '%lld' doesn't fit into a 32 bit signed int", integer); } return javaint; }
local_ref<jstring> ReadableNativeMapKeySetIterator::nextKey() { if (!hasNextKey()) { throwNewJavaException("com/facebook/react/bridge/InvalidIteratorException", "No such element exists"); } auto ret = make_jstring(iter_->first.c_str()); ++iter_; return ret; }
local_ref<ReadableNativeMap::jhybridobject> ReadableNativeMap::getMapKey(const std::string& key) { auto& value = getMapValue(key); if (value.isNull()) { return local_ref<ReadableNativeMap::jhybridobject>(nullptr); } else if (!value.isObject()) { throwNewJavaException(exceptions::gUnexpectedNativeTypeExceptionClass, "expected Map, got a %s", value.typeName()); } else { return ReadableNativeMap::newObjectCxxArgs(value); } }
local_ref<ReadableNativeMap::jhybridobject> ReadableNativeMap::createWithContents(folly::dynamic&& map) { if (map.isNull()) { return local_ref<jhybridobject>(nullptr); } if (!map.isObject()) { throwNewJavaException(exceptions::gUnexpectedNativeTypeExceptionClass, "expected Map, got a %s", map.typeName()); } return newObjectCxxArgs(std::move(map)); }
int64_t convertDynamicIfIntegral(const folly::dynamic& val) { if (val.isInt()) { return val.getInt(); } double dbl = val.getDouble(); int64_t result = static_cast<int64_t>(dbl); if (dbl != result) { throwNewJavaException( exceptions::gUnexpectedNativeTypeExceptionClass, "Tried to read an int, but got a non-integral double: %f", dbl); } return result; }
size_t JByteBuffer::getDirectSize() const { if (!self()) { throwNewJavaException("java/lang/NullPointerException", "java.lang.NullPointerException"); } int size = Environment::current()->GetDirectBufferCapacity(self()); FACEBOOK_JNI_THROW_PENDING_EXCEPTION(); if (size < 0) { throw std::runtime_error( isDirect() ? "Attempt to get direct size of non-direct byte buffer." : "Error getting direct size of byte buffer."); } return static_cast<size_t>(size); }
uint8_t* JByteBuffer::getDirectBytes() const { if (!self()) { throwNewJavaException("java/lang/NullPointerException", "java.lang.NullPointerException"); } void* bytes = Environment::current()->GetDirectBufferAddress(self()); FACEBOOK_JNI_THROW_PENDING_EXCEPTION(); if (!bytes) { throw std::runtime_error( isDirect() ? "Attempt to get direct bytes of non-direct byte buffer." : "Error getting direct bytes of byte buffer."); } return static_cast<uint8_t*>(bytes); }
static JSValueRef evaluateScriptWithJSC( JSGlobalContextRef ctx, JSStringRef script, JSStringRef sourceURL) { JSValueRef exn; auto result = JSEvaluateScript(ctx, script, nullptr, sourceURL, 0, &exn); if (result == nullptr) { JSValueProtect(ctx, exn); std::string exceptionText = Value(ctx, exn).toString().str(); FBLOGE("Got JS Exception: %s", exceptionText.c_str()); auto line = Value(ctx, JSObjectGetProperty(ctx, JSValueToObject(ctx, exn, nullptr), JSStringCreateWithUTF8CString("line"), nullptr )); std::ostringstream lineInfo; if (line != nullptr && line.isNumber()) { lineInfo << " (line " << line.asInteger() << " in the generated bundle)"; } else { lineInfo << " (no line info)"; } throwNewJavaException("com/facebook/react/bridge/JSExecutionException", (exceptionText + lineInfo.str()).c_str()); } return result; }
void throwNewJavaSecurityException(JNIEnv *env, const char *msg) { throwNewJavaException(env, "java/lang/SecurityException", msg); }
void ReadableNativeMap::mapException(const std::exception& ex) { if (dynamic_cast<const folly::TypeError*>(&ex) != nullptr) { throwNewJavaException(exceptions::gUnexpectedNativeTypeExceptionClass, ex.what()); } }