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

This issue has been created 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-06
Star Count 105178
Watcher Count 3671
Fork Count 22482
Issue Count 2231

YOU MAY BE INTERESTED

Issue Title Created Date Comment Count Updated Date
long lines get a line breaker while marshalling to yaml 0 2021-11-27 2022-09-22
Levels / World Building Naming Conventions and Best Practices 0 2021-07-08 2022-09-20
The close/maximise buttons are shown in white 6 2021-08-19 2022-05-02
Add support for optipng parameters. 0 2021-08-01 2022-05-02
Add your stripe key to appdata 1 2021-07-23 2022-05-02
chrome remote desktop issues 1 2021-04-30 2022-09-13
Reusing ARM Toolchain 2 2021-05-21 2022-04-21
A MicroPython module for asyncio button. 2 2021-12-02 2022-09-08
Least Priviledge for Pipelines - add the option to use a Service Principal for each 2 2021-12-03 2022-09-15
how i Can write extra info to node data 4 2020-11-20 2022-06-28
转存分享链接报错 (v0.6.9) System ERROR: 6 2021-03-09 2022-09-15
一直无法在Windows10安装 2 2021-03-10 2022-01-06
[Progress] Truncation/Termination/Rendering API Update 1 2022-08-19 2022-10-04
Add A* pathfinding algorithm 0 2022-03-04 2022-09-24
Add `Fast Fourier Transform` in maths 0 2022-03-04 2022-10-04
File path : './src/fr/mobile/android/developpement/formulaire.md' 0 2021-05-17 2022-07-21
[va11ydette] `hn` is “heading” 1 2021-08-24 2022-09-03
Create monitoring rule that indicates when Fog Overseer has Restarted 0 2022-03-21 2022-07-17
[MU4 Issue] Unable to deselect notes (objects) with Ctrl/Cmd 6 2021-10-25 2022-10-07
Using imported libraries in zokrates.js 2 2022-04-01 2022-08-01
windons上无法启动软件 1 2021-01-16 2022-01-08
Zero-Width emotes are behind animated emotes 1 2021-09-06 2022-09-18
Does not work with routes registered from a package 1 2021-09-01 2022-09-26
Dependabot couldn't access bitbucket-api-fork 0 2021-05-06 2022-10-04
Nonsensical `given` error 0 2022-08-02 2022-08-23
The Chronicles Of Myrtana: Archolos (1467450) 1 2021-12-23 2022-09-25
Rectify Pod DNS Error Experiment Docs 0 2022-09-15 2022-09-29
Make libraries case insensitive? 2 2021-03-26 2022-09-20
The connection used to load this site used TLS 1.0 or TLS 1.1 6 2021-06-08 2022-09-20
Display elements bounding boxes 1 2020-10-28 2022-10-04
Investigate backend options 0 2021-09-28 2022-09-27
Get-MgUserMailFolder cmdlets very spammy in transcript 3 2021-08-16 2022-09-29
Visual overlapping of slider values 1 2021-10-25 2022-09-26
Bug while merging updates from server. 4 2022-01-14 2022-09-02
Windows build fails since 0.8.2 6 2021-09-23 2022-09-13
Using VNC Viewer to connect 8 2021-12-15 2022-09-05
pusher java client can not catch events in PresenceChannel 0 2021-10-25 2022-10-04
Possible bug with Push Notifications Key 3 2021-10-26 2022-09-19
Modify NGW IsManaged function to use tag 0 2022-06-13 2022-08-26
1.x branch - error if hibernate is also configured to use ehcache. Invalid character ':' in value part of property 6 2019-08-04 2022-09-27
[Tutorial proposal] Exploring S1 archive in Colab 8 2021-10-04 2022-10-01
Issue with ingress not being created 10 2021-06-17 2022-08-01
Labels should be merged if present at both node and cluster scope 0 2021-06-14 2022-08-01
[BUG] ADVANCED_PAUSE_RESUME_PRIME should allow float (sub-mm) 3 2022-02-28 2022-09-22
Search failed: "^title: " when trying to create an issue 1 2022-04-05 2022-09-23
Allow editing the default directory for pull-request worktrees from the home folder to a specified folder 4 2022-02-09 2022-09-23
[Question] Is there a way to add IDs to stops in gradients? 1 2021-09-27 2022-10-04
Not able to plot regression line 0 2022-02-24 2022-08-17
c.ClientIP() is incorrect under goroutine 2 2022-07-07 2022-09-13
docs: overriding LOCALE_ID in CurrencyPipe 4 2021-10-20 2022-10-06