Skip to content

Commit

Permalink
Refactor to use class static which is reset when env is destroyed
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Duncalf committed Sep 5, 2022
1 parent 5d3e50a commit e32fc5b
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/jsi/jsi_class.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ class ObjectWrap {
// Also, may need to suppress destruction.
inline static std::optional<JsiFunc> 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<fbjsi::String> 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
Expand Down Expand Up @@ -310,9 +314,10 @@ class ObjectWrap {
.asFunction(env));

js::Context<js::realmjsi::Types>::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) {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -665,7 +672,8 @@ class ObjectWrap {
} // namespace realmjsi

template <typename ClassType>
class ObjectWrap<realmjsi::Types, ClassType> : public realm::js::realmjsi::ObjectWrap<ClassType> {};
class ObjectWrap<realmjsi::Types, ClassType> : public realm::js::realmjsi::ObjectWrap<ClassType> {
};

template <realmjsi::ArgumentsMethodType F>
fbjsi::Value wrap(fbjsi::Runtime& rt, const fbjsi::Value& thisVal, const fbjsi::Value* args, size_t count)
Expand Down

0 comments on commit e32fc5b

Please sign in to comment.