diff --git a/src/js_realm.hpp b/src/js_realm.hpp index 17b02634..f6cdc13f 100644 --- a/src/js_realm.hpp +++ b/src/js_realm.hpp @@ -105,7 +105,7 @@ class RealmDelegate : public BindingContext { arguments[0] = realm_object; arguments[1] = Value::from_string(m_context, notification_name); - for (auto callback : m_notifications) { + for (auto &callback : m_notifications) { Function::call(m_context, callback, realm_object, 2, arguments); } } diff --git a/src/jsc/jsc_protected.hpp b/src/jsc/jsc_protected.hpp index 79f2b020..a330ba3a 100644 --- a/src/jsc/jsc_protected.hpp +++ b/src/jsc/jsc_protected.hpp @@ -21,52 +21,60 @@ #include "jsc_types.hpp" namespace realm { -namespace jsc { - -template -class Protected { - const MemberType m_value; - - public: - Protected(MemberType value) : m_value(value) {} - - operator MemberType() const { - return m_value; - } -}; - -} // jsc - namespace js { template<> -class Protected : public jsc::Protected { +class Protected { + JSGlobalContextRef m_context; + public: - Protected(JSGlobalContextRef ctx) : jsc::Protected(ctx) { - JSGlobalContextRetain(*this); + Protected(const Protected &other) : Protected(other.m_context) {} + Protected(Protected &&other) : m_context(other.m_context) { + other.m_context = nullptr; + } + Protected(JSGlobalContextRef ctx) : m_context(ctx) { + JSGlobalContextRetain(m_context); } ~Protected() { - JSGlobalContextRelease(*this); + if (m_context) { + JSGlobalContextRelease(m_context); + } + } + operator JSGlobalContextRef() const { + return m_context; } }; template<> -class Protected : public jsc::Protected { - const JSGlobalContextRef m_context; +class Protected { + JSGlobalContextRef m_context; + JSValueRef m_value; public: - Protected(JSContextRef ctx, JSValueRef value) : jsc::Protected(value), m_context(JSContextGetGlobalContext(ctx)) { - JSValueProtect(m_context, *this); + Protected(const Protected &other) : Protected(other.m_context, other.m_value) {} + Protected(Protected &&other) : m_context(other.m_context), m_value(other.m_value) { + other.m_context = nullptr; + other.m_value = nullptr; + } + Protected(JSContextRef ctx, JSValueRef value) : m_context(JSContextGetGlobalContext(ctx)), m_value(value) { + JSValueProtect(m_context, m_value); } ~Protected() { - JSValueUnprotect(m_context, *this); + if (m_value) { + JSValueUnprotect(m_context, m_value); + } + } + operator JSValueRef() const { + return m_value; } }; template<> class Protected : public Protected { public: - Protected(JSContextRef ctx, JSObjectRef object) : Protected(ctx, object) {} + Protected(const Protected &other) : Protected(other) {} + Protected(Protected &&other) : Protected(std::move(other)) {} + Protected(JSContextRef ctx, JSObjectRef value) : Protected(ctx, value) {} operator JSObjectRef() const { JSValueRef value = static_cast(*this);