nitro icon indicating copy to clipboard operation
nitro copied to clipboard

HybridObject shows as JSObject(__type) in Hermes memory dumps

Open mfazekas opened this issue 1 month ago • 1 comments

Feature Request/Enhancement

When creating Nitro's HybridObjects and taking Hermes memory dumps, with react native dev tools Take snapshot , objects are showing up as JSObject(__type) instead of their actual type names (e.g., HybridObject<MyType>), making memory profiling difficult (hard to search for those objects).

Image

Hermes seems to use JSObject::getHeuristicTypeName() to name objects.

The following diff adds a dummy constructor with proper name, to the #ifdef NITRO_DEBUG part of HybridObject::toObject but not sure what side effects that could have. But it makes the dump much nicer

+++ b/packages/react-native-nitro-modules/cpp/core/HybridObject.cpp
@@ -94,6 +94,28 @@ jsi::Value HybridObject::toObject(jsi::Runtime& runtime) {
                                   .value = jsi::String::createFromUtf8(runtime, typeName),
                                   .writable = false,
                               });
+  
+  jsi::Function constructor =
+      jsi::Function::createFromHostFunction(
+        runtime,
+        jsi::PropNameID::forAscii(runtime, typeName),
+        0, // paramCount
+        [](jsi::Runtime& rt, const jsi::Value&, const jsi::Value* args, size_t count) -> jsi::Value {
+          throw jsi::JSError(rt, "This constructor cannot be called");
+          return jsi::Value::undefined();
+        }
+      );
+  ObjectUtils::defineProperty(
+    runtime,
+    object,
+    "constructor",
+    PlainPropertyDescriptor{
+      .configurable = true,
+      .enumerable = false,
+      .value = std::move(constructor),
+      .writable = false,
+    }
+  );
 #endif
Image

Are there any existing workarounds?

No response

Additional information

mfazekas avatar Nov 01 '25 16:11 mfazekas

interesting. I think we'd probably want constructors here anyways, would be a good addition (in favor of the current way of autolinking)

mrousavy avatar Nov 02 '25 22:11 mrousavy