Missing Stacktrace and failure with Android: JSError "Error: Exception in HostFunction: <unknown>"

This issue has been tracked since 2022-09-20.

Description

When throwing a jsi::JSError on Android from C++ code, the following information is displayed on the JS side
LOG [Error: Exception in HostFunction: <unknown>], leaving out the information about the exception.

My guess is that I'm missing libraries in CMakeLists but i'm not sure which one.

find_package(fbjni REQUIRED CONFIG)

add_library(reactnativezstd
        SHARED
        ./cpp-adapter.cpp
        ../cpp/react-native-zstd.cpp
        ../cpp/jsi-utils/JSIUtils.cpp
        ../cpp/ZstdHostObject.cpp
        #        To avoid: undefined reference to 'facebook::jsi::Value::~Value()'
        ../node_modules/react-native/ReactCommon/jsi/jsi/jsi.cpp
        )

target_include_directories(
        reactnativezstd
        PUBLIC
        ../externals/zstd/lib
        ../cpp
        ../node_modules/react-native/ReactCommon/jsi
)


target_link_libraries(
        reactnativezstd
        PRIVATE
        libzstd_static
        fbjni::fbjni
)

Version

0.68.2

Output of npx react-native info

System:
    OS: Linux 5.13 KDE neon 5.25
    CPU: (16) x64 Intel(R) Core(TM) i9-10885H CPU @ 2.40GHz
    Memory: 306.43 MB / 30.99 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 14.17.6 - ~/.nvm/versions/node/v14.17.6/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v14.17.6/bin/yarn
    npm: 6.14.15 - ~/.nvm/versions/node/v14.17.6/bin/npm
    Watchman: 4.9.0 - /usr/bin/watchman
  SDKs:
    Android SDK:
      API Levels: 28, 29, 30, 31, 33
      Build Tools: 28.0.3, 29.0.2, 30.0.2, 31.0.0, 33.0.0
      System Images: android-29 | Intel x86 Atom_64, android-30 | Google Play Intel x86 Atom
      Android NDK: 21.4.7075529
  IDEs:
    Android Studio: Not Found
  Languages:
    Java: 11.0.15 - /usr/bin/javac
  npmPackages:
    @react-native-community/cli: Not Found
    react: 17.0.2 => 17.0.2 
    react-native: 0.68.2 => 0.68.2 
  npmGlobalPackages:
    *react-native*: Not Found

Steps to reproduce

  1. Add throw new jsi::JSError(runtime, "Hello"); to your JSI code
  2. Call the function on an Android device

Snack, code example, screenshot, or link to a repository

Repro: https://github.com/Andarius/react-native-zstd/tree/fix/android/example
CMakeLists: https://github.com/Andarius/react-native-zstd/blob/fix/android/android/CMakeLists.txt

app

cortinico wrote this answer on 2022-09-20

0.68.2

Could you retry on a 0.70 project?

    ../externals/zstd/lib

Also can you provide more context on what you're trying to achieve? What is zstd?

Andarius wrote this answer on 2022-09-20

Hi, I will try 0.70 and get back to you.

ZSTD is a compression algorithm developed by Facebook (https://github.com/facebook/zstd).

I'm trying to write my first react-native library with JSI (using react-native-builder-bob) to be able to compress / decompress data (see the example in the README.md).
For now I'm trying to support 0.68 and onwards.

Andarius wrote this answer on 2022-09-20

So I migrated to 0.70.1, however now the app crashes with the following error:

Error: Exception in HostFunction: std::bad_alloc, js engine: hermes
 ERROR  TypeError: Cannot read property 'compress' of undefined, js engine: hermes

My guess is that the cpp-adapter.cpp is not valid anymore ? Or it should work the same way ?

#include <jni.h>
#include <jsi/jsi.h>
#include "ZstdHostObject.h"

using namespace facebook;


void install(jsi::Runtime& runtime) {
    auto hostObject = std::make_shared<rnzstd::ZstdHostObject>();
    auto object = jsi::Object::createFromHostObject(runtime, hostObject); // <= this fails
    runtime.global().setProperty(runtime, "__ZSTDProxy", std::move(object));
}

extern "C"
JNIEXPORT void JNICALL
Java_com_reactnativezstd_ZstdModule_nativeInstall(JNIEnv *env, jobject thiz, jlong jsiPtr) {
    auto runtime = reinterpret_cast<jsi::Runtime *>(jsiPtr);
    if (runtime) {
        install(*runtime);
    }
}
cortinico wrote this answer on 2022-09-20

@Andarius I would suggest you to follow the sample repo for libraries we have here: https://github.com/react-native-community/RNNewArchitectureLibraries/

Andarius wrote this answer on 2022-09-20

@cortinico Thanks for the link! Correct me if I'm wrong but this repo is missing examples with Native C++ code no?

Edit: for reference, the template used from react-native-builder-bob was this one (cpp-library)

cortinico wrote this answer on 2022-09-20

Correct me if I'm wrong but this repo is missing examples with Native C++ code no?

You're right. We haven't published documentation on how to write your Native Module with C++ code yet.

More Details About Repo
Owner Name facebook
Repo Name react-native
Full Name facebook/react-native
Language JavaScript
Created Date 2015-01-09
Updated Date 2022-10-03
Star Count 105144
Watcher Count 3666
Fork Count 22469
Issue Count 2232

YOU MAY BE INTERESTED

Issue Title Created Date Updated Date