Skip to content

Commit

Permalink
fix: Unwrap a lot of NITRO_DEBUGs
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Nov 12, 2024
1 parent 5038497 commit 1baa513
Show file tree
Hide file tree
Showing 9 changed files with 0 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ int levelToAndroidLevel(LogLevel level) {
}

void Logger::nativeLog(LogLevel level, const char* tag, const std::string& message) {
#ifdef NITRO_DEBUG
int logLevel = levelToAndroidLevel(level);
std::string combinedTag = "Nitro." + std::string(tag);
__android_log_print(logLevel, combinedTag.c_str(), "%s", message.c_str());
#endif
}

} // namespace margelo::nitro
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,9 @@ class DefaultConstructableObject {
jni::local_ref<T> create() const {
// Calls the class's default constructor
auto instance = _javaClass->newObject(_defaultConstructor);
#ifdef NITRO_DEBUG
if (instance == nullptr) [[unlikely]] {
throw std::runtime_error("Failed to create an instance of \"JHybridTestObjectSwiftKotlinSpec\" - the constructor returned null!");
}
#endif
return instance;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ void JHybridObjectRegistry::registerHybridObjectConstructor(jni::alias_ref<jni::
HybridObjectRegistry::registerHybridObjectConstructor(hybridObjectName, [=]() -> std::shared_ptr<HybridObject> {
// 1. Call the Java initializer function
jni::local_ref<JHybridObject::javaobject> hybridObject = sharedInitializer->call();
#ifdef NITRO_DEBUG
if (hybridObject == nullptr) [[unlikely]] {
throw std::runtime_error("Failed to create HybridObject \"" + hybridObjectName + "\" - the constructor returned null!");
}
#endif

// 2. Make the resulting HybridObject a global (shared) reference
jni::global_ref<JHybridObject::javaobject> globalHybridObject = jni::make_global(hybridObject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ class HybridFunction final {
static inline std::shared_ptr<THybrid> getHybridObjectNativeState(jsi::Runtime& runtime, const jsi::Value& value, FunctionKind funcKind,
const std::string& funcName) {
// 1. Convert jsi::Value to jsi::Object
#ifdef NITRO_DEBUG
if (!value.isObject()) [[unlikely]] {
throw jsi::JSError(runtime, "Cannot " + getHybridFuncDebugInfo<THybrid>(funcKind, funcName) +
" - `this` is not bound! Suggestions:\n"
Expand All @@ -182,7 +181,6 @@ class HybridFunction final {
"- Did you accidentally call `" +
funcName + "` on the prototype directly?");
}
#endif
jsi::Object object = value.getObject(runtime);

// 2. Check if it even has any kind of `NativeState`
Expand Down
2 changes: 0 additions & 2 deletions packages/react-native-nitro-modules/cpp/jsi/JSICache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,9 @@ JSICacheReference JSICache::getOrCreateCache(jsi::Runtime& runtime) {
Logger::log(LogLevel::Warning, TAG, "JSICache was created, but it is no longer strong!");
}

#ifdef NITRO_DEBUG
if (runtime.global().hasProperty(runtime, CACHE_PROP_NAME)) [[unlikely]] {
throw std::runtime_error("The Runtime \"" + getRuntimeId(runtime) + "\" already has a global cache! (\"" + CACHE_PROP_NAME + "\")");
}
#endif

// Cache doesn't exist yet.
Logger::log(LogLevel::Info, TAG, "Creating new JSICache<T> for runtime %s..", getRuntimeId(runtime).c_str());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,18 @@ using namespace facebook;
template <typename T>
struct JSIConverter<T, std::enable_if_t<is_shared_ptr_to_v<T, jsi::MutableBuffer>>> final {
static inline std::shared_ptr<ArrayBuffer> fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
#ifdef NITRO_DEBUG
if (!arg.isObject()) [[unlikely]] {
throw std::invalid_argument("Value \"" + arg.toString(runtime).utf8(runtime) +
"\" is not an ArrayBuffer - "
"in fact, it's not even an object!");
}
#endif

jsi::Object object = arg.asObject(runtime);
#ifdef NITRO_DEBUG
if (!object.isArrayBuffer(runtime)) [[unlikely]] {
throw std::invalid_argument("Object \"" + arg.toString(runtime).utf8(runtime) +
"\" is not an ArrayBuffer! "
"Are you maybe passing a TypedArray (e.g. Uint8Array)? Try to pass it's `.buffer` value.");
}
#endif

JSICacheReference cache = JSICache::getOrCreateCache(runtime);
auto borrowingArrayBuffer = cache.makeShared(object.getArrayBuffer(runtime));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ struct JSIConverter<T, std::enable_if_t<is_shared_ptr_to_v<T, jsi::HostObject>>>
using TPointee = typename T::element_type;

static inline T fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
#ifdef NITRO_DEBUG
if (!arg.isObject()) [[unlikely]] {
if (arg.isUndefined()) [[unlikely]] {
throw jsi::JSError(runtime, invalidTypeErrorMessage("undefined", "It is undefined!"));
Expand All @@ -29,10 +28,8 @@ struct JSIConverter<T, std::enable_if_t<is_shared_ptr_to_v<T, jsi::HostObject>>>
throw jsi::JSError(runtime, invalidTypeErrorMessage(stringRepresentation, "It is not an object!"));
}
}
#endif
jsi::Object object = arg.asObject(runtime);

#ifdef NITRO_DEBUG
if (!object.isHostObject<TPointee>(runtime)) [[unlikely]] {
if (!object.isHostObject(runtime)) [[unlikely]] {
std::string stringRepresentation = arg.toString(runtime).utf8(runtime);
Expand All @@ -42,7 +39,6 @@ struct JSIConverter<T, std::enable_if_t<is_shared_ptr_to_v<T, jsi::HostObject>>>
throw jsi::JSError(runtime, invalidTypeErrorMessage(stringRepresentation, "It is a different HostObject<T>!"));
}
}
#endif
return object.getHostObject<TPointee>(runtime);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ struct JSIConverter<T, std::enable_if_t<is_shared_ptr_to_v<T, jsi::NativeState>>
using TPointee = typename T::element_type;

static inline T fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
#ifdef NITRO_DEBUG
if (!arg.isObject()) [[unlikely]] {
if (arg.isUndefined()) [[unlikely]] {
throw jsi::JSError(runtime, invalidTypeErrorMessage("undefined", "It is undefined!"));
Expand All @@ -34,10 +33,8 @@ struct JSIConverter<T, std::enable_if_t<is_shared_ptr_to_v<T, jsi::NativeState>>
throw jsi::JSError(runtime, invalidTypeErrorMessage(stringRepresentation, "It is not an object!"));
}
}
#endif
jsi::Object object = arg.asObject(runtime);

#ifdef NITRO_DEBUG
if (!object.hasNativeState<TPointee>(runtime)) [[unlikely]] {
if (!object.hasNativeState(runtime)) [[unlikely]] {
std::string stringRepresentation = arg.toString(runtime).utf8(runtime);
Expand All @@ -47,7 +44,6 @@ struct JSIConverter<T, std::enable_if_t<is_shared_ptr_to_v<T, jsi::NativeState>>
throw jsi::JSError(runtime, invalidTypeErrorMessage(stringRepresentation, "It has a different NativeState<T>!"));
}
}
#endif
std::shared_ptr<jsi::NativeState> nativeState = object.getNativeState(runtime);
return std::dynamic_pointer_cast<TPointee>(nativeState);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ std::vector<std::string> HybridObjectRegistry::getAllHybridObjectNames() {
void HybridObjectRegistry::registerHybridObjectConstructor(const std::string& hybridObjectName, HybridObjectConstructorFn&& constructorFn) {
Logger::log(LogLevel::Info, TAG, "Registering HybridObject \"%s\"...", hybridObjectName.c_str());
auto& map = HybridObjectRegistry::getRegistry();
#ifdef NITRO_DEBUG
if (map.contains(hybridObjectName)) [[unlikely]] {
auto message =
"HybridObject \"" + std::string(hybridObjectName) +
Expand All @@ -42,7 +41,6 @@ void HybridObjectRegistry::registerHybridObjectConstructor(const std::string& hy
"- If you just registered your own HybridObject, maybe you accidentally called `registerHybridObjectConstructor(...)` twice?";
throw std::runtime_error(message);
}
#endif
map.insert({hybridObjectName, std::move(constructorFn)});
Logger::log(LogLevel::Info, TAG, "Successfully registered HybridObject \"%s\"!", hybridObjectName.c_str());
}
Expand Down

0 comments on commit 1baa513

Please sign in to comment.