From e32fc5b82711ace47849baefcb0c836b48404653 Mon Sep 17 00:00:00 2001 From: Tom Duncalf Date: Mon, 5 Sep 2022 15:17:13 +0100 Subject: [PATCH] Refactor to use class static which is reset when env is destroyed --- src/jsi/jsi_class.hpp | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/jsi/jsi_class.hpp b/src/jsi/jsi_class.hpp index 851cdba2cbd..5bf6f46411b 100644 --- a/src/jsi/jsi_class.hpp +++ b/src/jsi/jsi_class.hpp @@ -240,6 +240,10 @@ class ObjectWrap { // Also, may need to suppress destruction. inline static std::optional s_ctor; + // Cache the JSI String instance representing the field name of the internal + // C++ object for the lifetime of the current env, as this is a hot path + inline static std::optional s_js_internal_field_name; + /** * @brief callback for invalid access to index setters * Throws an error when a users attemps to write to an index on a type that @@ -310,9 +314,10 @@ class ObjectWrap { .asFunction(env)); js::Context::register_invalidator([] { - // Ensure the static constructor is destructed when the runtime goes away. - // This is to avoid the reassignment of s_ctor throwing because the runtime has disappeared. + // Ensure the static constructor and JSI String reference are destructed when the runtime goes away. + // This is to avoid the reassignment throwing because the runtime has disappeared. s_ctor.reset(); + s_js_internal_field_name.reset(); }); for (auto&& [name, prop] : s_type.static_properties) { @@ -490,9 +495,11 @@ class ObjectWrap { static Internal* get_internal(JsiEnv env, const JsiObj& object) { - static const auto js_internal_field = fbjsi::String::createFromAscii(env, g_internal_field); + if (!s_js_internal_field_name) { + s_js_internal_field_name = fbjsi::String::createFromAscii(env, g_internal_field); + } - auto internal = object->getProperty(env, js_internal_field); + auto internal = object->getProperty(env, *s_js_internal_field_name); if (internal.isUndefined()) { // In the case of a user opening a Realm with a class-based model, // the user defined constructor will get called before the "internal" property has been set. @@ -665,7 +672,8 @@ class ObjectWrap { } // namespace realmjsi template -class ObjectWrap : public realm::js::realmjsi::ObjectWrap {}; +class ObjectWrap : public realm::js::realmjsi::ObjectWrap { +}; template fbjsi::Value wrap(fbjsi::Runtime& rt, const fbjsi::Value& thisVal, const fbjsi::Value* args, size_t count)