diff --git a/src/js_class.hpp b/src/js_class.hpp index 1de62dba..0e6eb112 100644 --- a/src/js_class.hpp +++ b/src/js_class.hpp @@ -70,6 +70,8 @@ using PropertyMap = std::map>; template struct ClassDefinition { + using Internal = U; + // Every specialization *must* at least have a name. std::string name; }; @@ -89,7 +91,7 @@ struct BaseClassDefinition { StringPropertyType string_accessor; }; -template +template class ObjectWrap; } // js diff --git a/src/js_collection.hpp b/src/js_collection.hpp index 58b8b8b0..2ed53915 100644 --- a/src/js_collection.hpp +++ b/src/js_collection.hpp @@ -27,7 +27,7 @@ namespace js { class Collection {}; template -struct ClassDefinition : BaseClassDefinition { +struct CollectionClass : ClassDefinition, BaseClassDefinition { std::string const name = "Collection"; }; diff --git a/src/js_list.hpp b/src/js_list.hpp index b4283c87..a1152915 100644 --- a/src/js_list.hpp +++ b/src/js_list.hpp @@ -58,7 +58,7 @@ struct List { }; template -struct ClassDefinition : BaseClassDefinition { +struct ListClass : ClassDefinition, BaseClassDefinition> { using List = List; std::string const name = "List"; @@ -83,18 +83,18 @@ struct ClassDefinition : BaseClassDefinition { template typename T::Object List::create_instance(TContext ctx, realm::List &list) { - return create_object(ctx, new realm::List(list)); + return create_object>(ctx, new realm::List(list)); } template void List::GetLength(TContext ctx, TObject object, ReturnValue &return_value) { - auto list = get_internal(object); + auto list = get_internal>(object); return_value.set((uint32_t)list->size()); } template void List::GetIndex(TContext ctx, TObject object, uint32_t index, ReturnValue &return_value) { - auto list = get_internal(object); + auto list = get_internal>(object); auto realm_object = realm::Object(list->get_realm(), list->get_object_schema(), list->get(index)); return_value.set(RealmObject::create_instance(ctx, realm_object)); @@ -102,7 +102,7 @@ void List::GetIndex(TContext ctx, TObject object, uint32_t index, ReturnValue template bool List::SetIndex(TContext ctx, TObject object, uint32_t index, TValue value) { - auto list = get_internal(object); + auto list = get_internal>(object); list->set(ctx, value, index); return true; } @@ -111,7 +111,7 @@ template void List::Push(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) { validate_argument_count_at_least(argc, 1); - auto list = get_internal(this_object); + auto list = get_internal>(this_object); for (size_t i = 0; i < argc; i++) { list->add(ctx, arguments[i]); } @@ -123,7 +123,7 @@ template void List::Pop(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) { validate_argument_count(argc, 0); - auto list = get_internal(this_object); + auto list = get_internal>(this_object); size_t size = list->size(); if (size == 0) { list->verify_in_transaction(); @@ -142,7 +142,7 @@ template void List::Unshift(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) { validate_argument_count_at_least(argc, 1); - auto list = get_internal(this_object); + auto list = get_internal>(this_object); for (size_t i = 0; i < argc; i++) { list->insert(ctx, arguments[i], i); } @@ -154,7 +154,7 @@ template void List::Shift(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) { validate_argument_count(argc, 0); - auto list = get_internal(this_object); + auto list = get_internal>(this_object); if (list->size() == 0) { list->verify_in_transaction(); return_value.set_undefined(); @@ -171,7 +171,7 @@ template void List::Splice(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) { validate_argument_count_at_least(argc, 1); - auto list = get_internal(this_object); + auto list = get_internal>(this_object); size_t size = list->size(); long index = std::min(Value::to_number(ctx, arguments[0]), size); if (index < 0) { @@ -207,7 +207,7 @@ template void List::StaticResults(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) { validate_argument_count(argc, 0); - auto list = get_internal(this_object); + auto list = get_internal>(this_object); return_value.set(Results::create_instance(ctx, *list, false)); } @@ -215,7 +215,7 @@ template void List::Filtered(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) { validate_argument_count_at_least(argc, 1); - auto list = get_internal(this_object); + auto list = get_internal>(this_object); return_value.set(Results::create_filtered(ctx, *list, argc, arguments)); } @@ -223,7 +223,7 @@ template void List::Sorted(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) { validate_argument_count(argc, 1, 2); - auto list = get_internal(this_object); + auto list = get_internal>(this_object); return_value.set(Results::create_sorted(ctx, *list, argc, arguments)); } diff --git a/src/js_object.hpp b/src/js_object.hpp index f2514790..f79ddbce 100644 --- a/src/js_object.hpp +++ b/src/js_object.hpp @@ -48,7 +48,7 @@ struct RealmObject { }; template -struct ClassDefinition : BaseClassDefinition { +struct RealmObjectClass : ClassDefinition, BaseClassDefinition { using RealmObject = RealmObject; const std::string name = "RealmObject"; @@ -66,7 +66,7 @@ typename T::Object RealmObject::create_instance(TContext ctx, realm::Object & auto delegate = get_delegate(realm_object.realm().get()); auto name = realm_object.get_object_schema().name; - auto object = create_object(ctx, new realm::Object(realm_object)); + auto object = create_object>(ctx, new realm::Object(realm_object)); if (!delegate->m_constructors.count(name)) { return object; @@ -87,7 +87,7 @@ typename T::Object RealmObject::create_instance(TContext ctx, realm::Object & template void RealmObject::GetProperty(TContext ctx, TObject object, const String &property, ReturnValue &return_value) { try { - auto realm_object = get_internal(object); + auto realm_object = get_internal>(object); auto result = realm_object->template get_property_value(ctx, property); return_value.set(result); } catch (InvalidPropertyException &ex) { @@ -97,14 +97,14 @@ void RealmObject::GetProperty(TContext ctx, TObject object, const String &pro template bool RealmObject::SetProperty(TContext ctx, TObject object, const String &property, TValue value) { - auto realm_object = get_internal(object); + auto realm_object = get_internal>(object); realm_object->set_property_value(ctx, property, value, true); return true; } template std::vector> RealmObject::GetPropertyNames(TContext ctx, TObject object) { - auto realm_object = get_internal(object); + auto realm_object = get_internal>(object); auto &properties = realm_object->get_object_schema().properties; std::vector names; diff --git a/src/js_object_accessor.hpp b/src/js_object_accessor.hpp index 1c950526..8f3ed85f 100644 --- a/src/js_object_accessor.hpp +++ b/src/js_object_accessor.hpp @@ -102,8 +102,8 @@ struct NativeAccessor { static size_t to_object_index(TContext ctx, SharedRealm realm, TValue &value, const std::string &type, bool try_update) { TObject object = Value::validated_to_object(ctx, value); - if (Object::template is_instance(ctx, object)) { - return get_internal(object)->row().get_index(); + if (Object::template is_instance>(ctx, object)) { + return get_internal>(object)->row().get_index(); } auto object_schema = realm->config().schema->find(type); @@ -116,8 +116,8 @@ struct NativeAccessor { } static size_t to_existing_object_index(TContext ctx, TValue &value) { TObject object = Value::validated_to_object(ctx, value); - if (Object::template is_instance(ctx, object)) { - return get_internal(object)->row().get_index(); + if (Object::template is_instance>(ctx, object)) { + return get_internal>(object)->row().get_index(); } throw std::runtime_error("object is not a Realm Object"); } diff --git a/src/js_realm.hpp b/src/js_realm.hpp index b276661c..4233d4af 100644 --- a/src/js_realm.hpp +++ b/src/js_realm.hpp @@ -37,6 +37,9 @@ namespace realm { namespace js { +template +struct RealmClass; + template class RealmDelegate : public BindingContext { public: @@ -87,7 +90,7 @@ class RealmDelegate : public BindingContext { throw std::runtime_error("Realm no longer exists"); } - TObject realm_object = create_object(m_context, new SharedRealm(realm)); + TObject realm_object = create_object>(m_context, new SharedRealm(realm)); TValue arguments[2]; arguments[0] = realm_object; arguments[1] = Value::from_string(m_context, notification_name); @@ -140,10 +143,10 @@ class Realm { static void SetDefaultPath(TContext, TObject, TValue value); static TObject create_constructor(TContext ctx) { - TObject realm_constructor = ObjectWrap::create_constructor(ctx); - TObject collection_constructor = ObjectWrap::create_constructor(ctx); - TObject list_constructor = ObjectWrap::create_constructor(ctx); - TObject results_constructor = ObjectWrap::create_constructor(ctx); + TObject realm_constructor = ObjectWrap>::create_constructor(ctx); + TObject collection_constructor = ObjectWrap>::create_constructor(ctx); + TObject list_constructor = ObjectWrap>::create_constructor(ctx); + TObject results_constructor = ObjectWrap>::create_constructor(ctx); PropertyAttributes attributes = PropertyAttributes(ReadOnly | DontEnum | DontDelete); Object::set_property(ctx, realm_constructor, "Collection", collection_constructor, attributes); @@ -186,7 +189,7 @@ class Realm { }; template -struct ClassDefinition : BaseClassDefinition { +struct RealmClass : ClassDefinition, BaseClassDefinition { using Realm = Realm; std::string const name = "Realm"; @@ -288,7 +291,7 @@ void Realm::Constructor(TContext ctx, TObject this_object, size_t argc, const delegate->m_defaults = std::move(defaults); delegate->m_constructors = std::move(constructors); - set_internal(this_object, new SharedRealm(realm)); + set_internal>(this_object, new SharedRealm(realm)); } template @@ -330,13 +333,13 @@ void Realm::SetDefaultPath(TContext ctx, TObject object, TValue value) { template void Realm::GetPath(TContext ctx, TObject object, ReturnValue &return_value) { - std::string path = get_internal(object)->get()->config().path; + std::string path = get_internal>(object)->get()->config().path; return_value.set(path); } template void Realm::GetSchemaVersion(TContext ctx, TObject object, ReturnValue &return_value) { - double version = get_internal(object)->get()->config().schema_version; + double version = get_internal>(object)->get()->config().schema_version; return_value.set(version); } @@ -344,7 +347,7 @@ template void Realm::Objects(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) { validate_argument_count(argc, 1); - SharedRealm realm = *get_internal(this_object); + SharedRealm realm = *get_internal>(this_object); std::string type = validated_object_type_for_value(realm, ctx, arguments[0]); return_value.set(Results::create_instance(ctx, realm, type)); @@ -354,7 +357,7 @@ template void Realm::Create(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) { validate_argument_count(argc, 2, 3); - SharedRealm sharedRealm = *get_internal(this_object); + SharedRealm sharedRealm = *get_internal>(this_object); std::string className = validated_object_type_for_value(sharedRealm, ctx, arguments[0]); auto &schema = sharedRealm->config().schema; auto object_schema = schema->find(className); @@ -381,15 +384,15 @@ template void Realm::Delete(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) { validate_argument_count(argc, 1); - SharedRealm realm = *get_internal(this_object); + SharedRealm realm = *get_internal>(this_object); if (!realm->is_in_transaction()) { throw std::runtime_error("Can only delete objects within a transaction."); } TObject arg = Value::validated_to_object(ctx, arguments[0]); - if (Object::template is_instance(ctx, arg)) { - auto object = get_internal(arg); + if (Object::template is_instance>(ctx, arg)) { + auto object = get_internal>(arg); realm::TableRef table = ObjectStore::table_for_object_type(realm->read_group(), object->get_object_schema().name); table->move_last_over(object->row().get_index()); } @@ -398,21 +401,21 @@ void Realm::Delete(TContext ctx, TObject this_object, size_t argc, const TVal for (uint32_t i = length; i--;) { TObject object = Object::validated_get_object(ctx, arg, i); - if (!Object::template is_instance(ctx, object)) { + if (!Object::template is_instance>(ctx, object)) { throw std::runtime_error("Argument to 'delete' must be a Realm object or a collection of Realm objects."); } - auto realm_object = get_internal(object); + auto realm_object = get_internal>(object); realm::TableRef table = ObjectStore::table_for_object_type(realm->read_group(), realm_object->get_object_schema().name); table->move_last_over(realm_object->row().get_index()); } } - else if (Object::template is_instance(ctx, arg)) { - auto results = get_internal(arg); + else if (Object::template is_instance>(ctx, arg)) { + auto results = get_internal>(arg); results->clear(); } - else if (Object::template is_instance(ctx, arg)) { - auto list = get_internal(arg); + else if (Object::template is_instance>(ctx, arg)) { + auto list = get_internal>(arg); list->delete_all(); } else { @@ -424,7 +427,7 @@ template void Realm::DeleteAll(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) { validate_argument_count(argc, 0); - SharedRealm realm = *get_internal(this_object); + SharedRealm realm = *get_internal>(this_object); if (!realm->is_in_transaction()) { throw std::runtime_error("Can only delete objects within a transaction."); @@ -439,7 +442,7 @@ template void Realm::Write(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) { validate_argument_count(argc, 1); - SharedRealm realm = *get_internal(this_object); + SharedRealm realm = *get_internal>(this_object); TFunction callback = Value::validated_to_function(ctx, arguments[0]); try { @@ -462,7 +465,7 @@ void Realm::AddListener(TContext ctx, TObject this_object, size_t argc, const __unused std::string name = validated_notification_name(ctx, arguments[0]); auto callback = Value::validated_to_function(ctx, arguments[1]); - SharedRealm realm = *get_internal(this_object); + SharedRealm realm = *get_internal>(this_object); get_delegate(realm.get())->add_notification(callback); } @@ -473,7 +476,7 @@ void Realm::RemoveListener(TContext ctx, TObject this_object, size_t argc, co __unused std::string name = validated_notification_name(ctx, arguments[0]); auto callback = Value::validated_to_function(ctx, arguments[1]); - SharedRealm realm = *get_internal(this_object); + SharedRealm realm = *get_internal>(this_object); get_delegate(realm.get())->remove_notification(callback); } @@ -484,7 +487,7 @@ void Realm::RemoveAllListeners(TContext ctx, TObject this_object, size_t argc validated_notification_name(ctx, arguments[0]); } - SharedRealm realm = *get_internal(this_object); + SharedRealm realm = *get_internal>(this_object); get_delegate(realm.get())->remove_all_notifications(); } @@ -492,7 +495,7 @@ template void Realm::Close(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) { validate_argument_count(argc, 0); - SharedRealm realm = *get_internal(this_object); + SharedRealm realm = *get_internal>(this_object); realm->close(); } diff --git a/src/js_results.hpp b/src/js_results.hpp index 27865834..a0577b3a 100644 --- a/src/js_results.hpp +++ b/src/js_results.hpp @@ -58,7 +58,7 @@ struct Results { }; template -struct ClassDefinition : BaseClassDefinition { +struct ResultsClass : ClassDefinition, BaseClassDefinition> { using Results = Results; std::string const name = "Results"; @@ -81,7 +81,7 @@ typename T::Object Results::create_instance(TContext ctx, const realm::Result auto new_results = new realm::Results(results); new_results->set_live(live); - return create_object(ctx, new_results); + return create_object>(ctx, new_results); } template @@ -102,7 +102,7 @@ typename T::Object Results::create_instance(TContext ctx, SharedRealm realm, auto results = new realm::Results(realm, *object_schema, *table); results->set_live(live); - return create_object(ctx, results); + return create_object>(ctx, results); } template @@ -110,7 +110,7 @@ typename T::Object Results::create_instance(TContext ctx, SharedRealm realm, auto results = new realm::Results(realm, object_schema, std::move(query)); results->set_live(live); - return create_object(ctx, results); + return create_object>(ctx, results); } template @@ -190,18 +190,18 @@ typename T::Object Results::create_sorted(TContext ctx, const U &collection, } auto results = new realm::Results(realm, object_schema, collection.get_query(), {std::move(columns), std::move(ascending)}); - return create_object(ctx, results); + return create_object>(ctx, results); } template void Results::GetLength(TContext ctx, TObject object, ReturnValue &return_value) { - auto results = get_internal(object); + auto results = get_internal>(object); return_value.set((uint32_t)results->size()); } template void Results::GetIndex(TContext ctx, TObject object, uint32_t index, ReturnValue &return_value) { - auto results = get_internal(object); + auto results = get_internal>(object); auto row = results->get(index); // Return null for deleted objects in a snapshot. @@ -218,7 +218,7 @@ template void Results::StaticResults(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) { validate_argument_count(argc, 0); - auto results = get_internal(this_object); + auto results = get_internal>(this_object); return_value.set(Results::create_instance(ctx, *results, false)); } @@ -226,7 +226,7 @@ template void Results::Filtered(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) { validate_argument_count_at_least(argc, 1); - auto results = get_internal(this_object); + auto results = get_internal>(this_object); return_value.set(create_filtered(ctx, *results, argc, arguments)); } @@ -234,7 +234,7 @@ template void Results::Sorted(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) { validate_argument_count(argc, 1, 2); - auto results = get_internal(this_object); + auto results = get_internal>(this_object); return_value.set(create_sorted(ctx, *results, argc, arguments)); } diff --git a/src/js_types.hpp b/src/js_types.hpp index e53da2ca..a9697efc 100644 --- a/src/js_types.hpp +++ b/src/js_types.hpp @@ -219,17 +219,17 @@ struct Object { static TObject create_date(TContext, double); - template - static TObject create_instance(TContext, U*); + template + static TObject create_instance(TContext, typename ClassType::Internal*); - template + template static bool is_instance(TContext, const TObject &); - template - static U* get_internal(const TObject &); + template + static typename ClassType::Internal* get_internal(const TObject &); - template - static void set_internal(const TObject &, U*); + template + static void set_internal(const TObject &, typename ClassType::Internal*); }; template @@ -280,19 +280,19 @@ struct ReturnValue { void set_undefined(); }; -template -REALM_JS_INLINE typename T::Object create_object(typename T::Context ctx, U* internal = nullptr) { - return Object::template create_instance(ctx, internal); +template +REALM_JS_INLINE typename T::Object create_object(typename T::Context ctx, typename ClassType::Internal* internal = nullptr) { + return Object::template create_instance(ctx, internal); } -template -REALM_JS_INLINE U* get_internal(const typename T::Object &object) { - return Object::template get_internal(object); +template +REALM_JS_INLINE typename ClassType::Internal* get_internal(const typename T::Object &object) { + return Object::template get_internal(object); } -template -REALM_JS_INLINE void set_internal(const typename T::Object &object, U* ptr) { - Object::template set_internal(object, ptr); +template +REALM_JS_INLINE void set_internal(const typename T::Object &object, typename ClassType::Internal* ptr) { + Object::template set_internal(object, ptr); } } // js diff --git a/src/jsc/jsc_class.hpp b/src/jsc/jsc_class.hpp index ff246f58..3299fc5c 100644 --- a/src/jsc/jsc_class.hpp +++ b/src/jsc/jsc_class.hpp @@ -28,7 +28,6 @@ namespace jsc { template using ClassDefinition = js::ClassDefinition; -using BaseClassDefinition = js::BaseClassDefinition; using ConstructorType = js::ConstructorType; using MethodType = js::MethodType; using PropertyType = js::PropertyType; @@ -37,15 +36,16 @@ using StringPropertyType = js::StringPropertyType; using MethodMap = js::MethodMap; using PropertyMap = js::PropertyMap; -template +template class ObjectWrap { public: - operator T*() const { + using Internal = typename ClassType::Internal; + operator Internal*() const { return m_object.get(); } - ObjectWrap& operator=(T* object) { + ObjectWrap& operator=(Internal* object) { if (m_object.get() != object) { - m_object = std::unique_ptr(object); + m_object = std::unique_ptr(object); } return *this; } @@ -60,8 +60,8 @@ public: return js_class; } - static JSObjectRef create_instance(JSContextRef ctx, T* internal = nullptr) { - return JSObjectMake(ctx, get_class(), new ObjectWrap(internal)); + static JSObjectRef create_instance(JSContextRef ctx, Internal* internal = nullptr) { + return JSObjectMake(ctx, get_class(), new ObjectWrap(internal)); } static JSObjectRef create_constructor(JSContextRef ctx) { @@ -77,11 +77,11 @@ public: } private: - static ClassDefinition s_class; + static ClassType s_class; - std::unique_ptr m_object; + std::unique_ptr m_object; - ObjectWrap(T* object = nullptr) : m_object(object) {} + ObjectWrap(Internal* object = nullptr) : m_object(object) {} static JSObjectRef construct(JSContextRef ctx, JSObjectRef constructor, size_t argc, const JSValueRef arguments[], JSValueRef* exception) { if (!s_class.constructor) { @@ -89,7 +89,7 @@ private: return nullptr; } - JSObjectRef this_object = ObjectWrap::create_instance(ctx); + JSObjectRef this_object = ObjectWrap::create_instance(ctx); try { s_class.constructor(ctx, this_object, argc, arguments); } @@ -178,7 +178,7 @@ private: static void finalize(JSObjectRef object) { // This is called for the most derived class before superclasses. - if (auto wrap = static_cast *>(JSObjectGetPrivate(object))) { + if (auto wrap = static_cast *>(JSObjectGetPrivate(object))) { delete wrap; JSObjectSetPrivate(object, nullptr); } @@ -283,14 +283,42 @@ private: } }; -// Make the top-level base class return a NULL JSClassRef. template<> -inline JSClassRef ObjectWrap::get_class() { - return nullptr; -} - +class ObjectWrap { +public: + using Internal = void; + + operator Internal*() const { + return nullptr; + } + + ObjectWrap& operator=(Internal* object) { + return *this; + } + + static JSClassRef get_class() { + return nullptr; + } + + static JSClassRef get_constructor_class() { + return nullptr; + } + + static JSObjectRef create_instance(JSContextRef ctx, Internal* internal = nullptr) { + return nullptr; + } + + static JSObjectRef create_constructor(JSContextRef ctx) { + return nullptr; + } + + static bool has_instance(JSContextRef ctx, JSValueRef value) { + return nullptr; + } +}; + // The declared static variables must be defined as well. -template ClassDefinition ObjectWrap::s_class; +template T ObjectWrap::s_class; } // jsc diff --git a/src/jsc/jsc_types.hpp b/src/jsc/jsc_types.hpp index dcfdc27f..039b9c27 100644 --- a/src/jsc/jsc_types.hpp +++ b/src/jsc/jsc_types.hpp @@ -481,27 +481,27 @@ inline JSObjectRef jsc::Object::create_date(JSContextRef ctx, double time) { } template<> -template -inline JSObjectRef jsc::Object::create_instance(JSContextRef ctx, U* internal) { - return jsc::ObjectWrap::create_instance(ctx, internal); +template +inline JSObjectRef jsc::Object::create_instance(JSContextRef ctx, typename ClassType::Internal* internal) { + return jsc::ObjectWrap::create_instance(ctx, internal); } template<> -template +template inline bool jsc::Object::is_instance(JSContextRef ctx, const JSObjectRef &object) { - return jsc::ObjectWrap::has_instance(ctx, object); + return jsc::ObjectWrap::has_instance(ctx, object); } template<> -template -inline U* jsc::Object::get_internal(const JSObjectRef &object) { - return *static_cast *>(JSObjectGetPrivate(object)); +template +inline typename ClassType::Internal* jsc::Object::get_internal(const JSObjectRef &object) { + return *static_cast *>(JSObjectGetPrivate(object)); } template<> -template -inline void jsc::Object::set_internal(const JSObjectRef &object, U* ptr) { - auto wrap = static_cast *>(JSObjectGetPrivate(object)); +template +inline void jsc::Object::set_internal(const JSObjectRef &object, typename ClassType::Internal* ptr) { + auto wrap = static_cast *>(JSObjectGetPrivate(object)); *wrap = ptr; } diff --git a/src/rpc.cpp b/src/rpc.cpp index f3320d33..bd50187f 100644 --- a/src/rpc.cpp +++ b/src/rpc.cpp @@ -24,6 +24,7 @@ #include "rpc.hpp" #include "jsc_init.hpp" +#include "jsc_types.hpp" #include "js_object.hpp" #include "js_results.hpp" #include "js_realm.hpp" @@ -86,21 +87,21 @@ RPCServer::RPCServer() { }; m_requests["/begin_transaction"] = [this](const json dict) { RPCObjectID realm_id = dict["realmId"].get(); - SharedRealm realm = *jsc::Object::get_internal(m_objects[realm_id]); + SharedRealm realm = *jsc::Object::get_internal>(m_objects[realm_id]); realm->begin_transaction(); return json::object(); }; m_requests["/cancel_transaction"] = [this](const json dict) { RPCObjectID realm_id = dict["realmId"].get(); - SharedRealm realm = *jsc::Object::get_internal(m_objects[realm_id]); + SharedRealm realm = *jsc::Object::get_internal>(m_objects[realm_id]); realm->cancel_transaction(); return json::object(); }; m_requests["/commit_transaction"] = [this](const json dict) { RPCObjectID realm_id = dict["realmId"].get(); - SharedRealm realm = *jsc::Object::get_internal(m_objects[realm_id]); + SharedRealm realm = *jsc::Object::get_internal>(m_objects[realm_id]); realm->commit_transaction(); return json::object(); @@ -220,16 +221,16 @@ json RPCServer::serialize_json_value(JSValueRef js_value) { JSObjectRef js_object = jsc::Value::validated_to_object(m_context, js_value); - if (jsc::Object::is_instance(m_context, js_object)) { - auto object = jsc::Object::get_internal(js_object); + if (jsc::Object::is_instance>(m_context, js_object)) { + auto object = jsc::Object::get_internal>(js_object); return { {"type", RealmObjectTypesObject}, {"id", store_object(js_object)}, {"schema", serialize_object_schema(object->get_object_schema())} }; } - else if (jsc::Object::is_instance(m_context, js_object)) { - auto list = jsc::Object::get_internal(js_object); + else if (jsc::Object::is_instance>(m_context, js_object)) { + auto list = jsc::Object::get_internal>(js_object); return { {"type", RealmObjectTypesList}, {"id", store_object(js_object)}, @@ -237,8 +238,8 @@ json RPCServer::serialize_json_value(JSValueRef js_value) { {"schema", serialize_object_schema(list->get_object_schema())} }; } - else if (jsc::Object::is_instance(m_context, js_object)) { - auto results = jsc::Object::get_internal(js_object); + else if (jsc::Object::is_instance>(m_context, js_object)) { + auto results = jsc::Object::get_internal>(js_object); return { {"type", RealmObjectTypesResults}, {"id", store_object(js_object)},