From 6af98acd1159c92cf148b0b14d61faa122341288 Mon Sep 17 00:00:00 2001 From: Ari Lazier Date: Fri, 13 May 2016 17:21:11 -0700 Subject: [PATCH] combine other classes --- src/js_list.hpp | 8 +-- src/js_object_accessor.hpp | 2 +- src/js_realm.hpp | 117 +++++++++++++++++-------------------- src/js_realm_object.hpp | 26 ++++----- src/js_results.hpp | 2 +- src/jsc/jsc_init.cpp | 2 +- src/node/node_init.cpp | 2 +- 7 files changed, 73 insertions(+), 86 deletions(-) diff --git a/src/js_list.hpp b/src/js_list.hpp index 455e58fa..18f07777 100644 --- a/src/js_list.hpp +++ b/src/js_list.hpp @@ -94,7 +94,7 @@ void ListClass::get_index(ContextType ctx, ObjectType object, uint32_t index, 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)); + return_value.set(RealmObjectClass::create_instance(ctx, realm_object)); } template @@ -130,7 +130,7 @@ void ListClass::pop(ContextType ctx, ObjectType this_object, size_t argc, con size_t index = size - 1; auto realm_object = realm::Object(list->get_realm(), list->get_object_schema(), list->get(index)); - return_value.set(RealmObject::create_instance(ctx, realm_object)); + return_value.set(RealmObjectClass::create_instance(ctx, realm_object)); list->remove(index); } } @@ -159,7 +159,7 @@ void ListClass::shift(ContextType ctx, ObjectType this_object, size_t argc, c else { auto realm_object = realm::Object(list->get_realm(), list->get_object_schema(), list->get(0)); - return_value.set(RealmObject::create_instance(ctx, realm_object)); + return_value.set(RealmObjectClass::create_instance(ctx, realm_object)); list->remove(0); } } @@ -190,7 +190,7 @@ void ListClass::splice(ContextType ctx, ObjectType this_object, size_t argc, for (size_t i = 0; i < remove; i++) { auto realm_object = realm::Object(list->get_realm(), list->get_object_schema(), list->get(index)); - removed_objects.push_back(RealmObject::create_instance(ctx, realm_object)); + removed_objects.push_back(RealmObjectClass::create_instance(ctx, realm_object)); list->remove(index); } for (size_t i = 2; i < argc; i++) { diff --git a/src/js_object_accessor.hpp b/src/js_object_accessor.hpp index 6b5e9752..4bde7211 100644 --- a/src/js_object_accessor.hpp +++ b/src/js_object_accessor.hpp @@ -125,7 +125,7 @@ struct NativeAccessor { throw std::runtime_error("object is not a Realm Object"); } static ValueType from_object(ContextType ctx, realm::Object realm_object) { - return RealmObject::create_instance(ctx, realm_object); + return RealmObjectClass::create_instance(ctx, realm_object); } static size_t list_size(ContextType ctx, ValueType &value) { diff --git a/src/js_realm.hpp b/src/js_realm.hpp index 33d0ded4..d7b427ee 100644 --- a/src/js_realm.hpp +++ b/src/js_realm.hpp @@ -116,7 +116,7 @@ class RealmDelegate : public BindingContext { } } - friend class Realm; + friend class RealmClass; }; std::string default_path(); @@ -124,7 +124,8 @@ void set_default_path(std::string path); void delete_all_realms(); template -class Realm { +class RealmClass : public ClassDefinition { + public: using GlobalContextType = typename T::GlobalContext; using ContextType = typename T::Context; using FunctionType = typename T::Function; @@ -136,7 +137,6 @@ class Realm { using ReturnValue = js::ReturnValue; using NativeAccessor = realm::NativeAccessor; - public: static FunctionType create_constructor(ContextType); // methods @@ -166,6 +166,37 @@ class Realm { static void get_default_path(ContextType, ObjectType, ReturnValue &); static void set_default_path(ContextType, ObjectType, ValueType value); + std::string const name = "Realm"; + + MethodMap const static_methods = { + {"schemaVersion", wrap}, + {"clearTestState", wrap}, + {"copyBundledRealmFiles", wrap}, + }; + + PropertyMap const static_properties = { + {"defaultPath", {wrap, wrap}}, + }; + + MethodMap const methods = { + {"objects", wrap}, + {"create", wrap}, + {"delete", wrap}, + {"deleteAll", wrap}, + {"write", wrap}, + {"addListener", wrap}, + {"removeListener", wrap}, + {"removeAllListeners", wrap}, + {"close", wrap}, + }; + + PropertyMap const properties = { + {"path", {wrap, nullptr}}, + {"schemaVersion", {wrap, nullptr}}, + {"schema", {wrap, nullptr}}, + {"readOnly", {wrap, nullptr}}, + }; + private: static std::string validated_notification_name(ContextType ctx, const ValueType &value) { std::string name = Value::validated_to_string(ctx, value, "notification name"); @@ -200,45 +231,7 @@ class Realm { }; template -struct RealmClass : ClassDefinition { - using Realm = js::Realm; - - std::string const name = "Realm"; - - ConstructorType* const constructor = Realm::constructor; - - MethodMap const static_methods = { - {"schemaVersion", wrap}, - {"clearTestState", wrap}, - {"copyBundledRealmFiles", wrap}, - }; - - PropertyMap const static_properties = { - {"defaultPath", {wrap, wrap}}, - }; - - MethodMap const methods = { - {"objects", wrap}, - {"create", wrap}, - {"delete", wrap}, - {"deleteAll", wrap}, - {"write", wrap}, - {"addListener", wrap}, - {"removeListener", wrap}, - {"removeAllListeners", wrap}, - {"close", wrap}, - }; - - PropertyMap const properties = { - {"path", {wrap, nullptr}}, - {"schemaVersion", {wrap, nullptr}}, - {"schema", {wrap, nullptr}}, - {"readOnly", {wrap, nullptr}}, - }; -}; - -template -inline typename T::Function Realm::create_constructor(ContextType ctx) { +inline typename T::Function RealmClass::create_constructor(ContextType ctx) { FunctionType realm_constructor = ObjectWrap>::create_constructor(ctx); FunctionType collection_constructor = ObjectWrap>::create_constructor(ctx); FunctionType list_constructor = ObjectWrap>::create_constructor(ctx); @@ -280,7 +273,7 @@ static void convert_outdated_datetime_columns(const SharedRealm &realm) { } template -void Realm::constructor(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[]) { +void RealmClass::constructor(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[]) { realm::Realm::Config config; typename Schema::ObjectDefaultsMap defaults; typename Schema::ConstructorMap constructors; @@ -396,7 +389,7 @@ void Realm::constructor(ContextType ctx, ObjectType this_object, size_t argc, } template -void Realm::schema_version(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { +void RealmClass::schema_version(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { validate_argument_count(argc, 1, 2); realm::Realm::Config config; @@ -417,52 +410,52 @@ void Realm::schema_version(ContextType ctx, ObjectType this_object, size_t ar } template -void Realm::clear_test_state(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { +void RealmClass::clear_test_state(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { validate_argument_count(argc, 0); delete_all_realms(); } template -void Realm::copy_bundled_realm_files(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { +void RealmClass::copy_bundled_realm_files(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { validate_argument_count(argc, 0); realm::copy_bundled_realm_files(); } template -void Realm::get_default_path(ContextType ctx, ObjectType object, ReturnValue &return_value) { +void RealmClass::get_default_path(ContextType ctx, ObjectType object, ReturnValue &return_value) { return_value.set(realm::js::default_path()); } template -void Realm::set_default_path(ContextType ctx, ObjectType object, ValueType value) { +void RealmClass::set_default_path(ContextType ctx, ObjectType object, ValueType value) { js::set_default_path(Value::validated_to_string(ctx, value, "defaultPath")); } template -void Realm::get_path(ContextType ctx, ObjectType object, ReturnValue &return_value) { +void RealmClass::get_path(ContextType ctx, ObjectType object, ReturnValue &return_value) { std::string path = get_internal>(object)->get()->config().path; return_value.set(path); } template -void Realm::get_schema_version(ContextType ctx, ObjectType object, ReturnValue &return_value) { +void RealmClass::get_schema_version(ContextType ctx, ObjectType object, ReturnValue &return_value) { double version = get_internal>(object)->get()->config().schema_version; return_value.set(version); } template -void Realm::get_schema(ContextType ctx, ObjectType object, ReturnValue &return_value) { +void RealmClass::get_schema(ContextType ctx, ObjectType object, ReturnValue &return_value) { auto schema = get_internal>(object)->get()->config().schema.get(); return_value.set(Schema::object_for_schema(ctx, *schema)); } template -void Realm::get_read_only(ContextType ctx, ObjectType object, ReturnValue &return_value) { +void RealmClass::get_read_only(ContextType ctx, ObjectType object, ReturnValue &return_value) { return_value.set(get_internal>(object)->get()->config().read_only); } template -void Realm::objects(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { +void RealmClass::objects(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { validate_argument_count(argc, 1); SharedRealm realm = *get_internal>(this_object); @@ -472,7 +465,7 @@ void Realm::objects(ContextType ctx, ObjectType this_object, size_t argc, con } template -void Realm::create(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { +void RealmClass::create(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { validate_argument_count(argc, 2, 3); SharedRealm realm = *get_internal>(this_object); @@ -495,11 +488,11 @@ void Realm::create(ContextType ctx, ObjectType this_object, size_t argc, cons } auto realm_object = realm::Object::create(ctx, realm, *object_schema, object, update); - return_value.set(RealmObject::create_instance(ctx, realm_object)); + return_value.set(RealmObjectClass::create_instance(ctx, realm_object)); } template -void Realm::delete_one(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { +void RealmClass::delete_one(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { validate_argument_count(argc, 1); SharedRealm realm = *get_internal>(this_object); @@ -542,7 +535,7 @@ void Realm::delete_one(ContextType ctx, ObjectType this_object, size_t argc, } template -void Realm::delete_all(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { +void RealmClass::delete_all(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { validate_argument_count(argc, 0); SharedRealm realm = *get_internal>(this_object); @@ -557,7 +550,7 @@ void Realm::delete_all(ContextType ctx, ObjectType this_object, size_t argc, } template -void Realm::write(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { +void RealmClass::write(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { validate_argument_count(argc, 1); SharedRealm realm = *get_internal>(this_object); @@ -577,7 +570,7 @@ void Realm::write(ContextType ctx, ObjectType this_object, size_t argc, const } template -void Realm::add_listener(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { +void RealmClass::add_listener(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { validate_argument_count(argc, 2); validated_notification_name(ctx, arguments[0]); @@ -588,7 +581,7 @@ void Realm::add_listener(ContextType ctx, ObjectType this_object, size_t argc } template -void Realm::remove_listener(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { +void RealmClass::remove_listener(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { validate_argument_count(argc, 2); validated_notification_name(ctx, arguments[0]); @@ -599,7 +592,7 @@ void Realm::remove_listener(ContextType ctx, ObjectType this_object, size_t a } template -void Realm::remove_all_listeners(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { +void RealmClass::remove_all_listeners(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { validate_argument_count(argc, 0, 1); if (argc) { validated_notification_name(ctx, arguments[0]); @@ -610,7 +603,7 @@ void Realm::remove_all_listeners(ContextType ctx, ObjectType this_object, siz } template -void Realm::close(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { +void RealmClass::close(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { validate_argument_count(argc, 0); SharedRealm realm = *get_internal>(this_object); diff --git a/src/js_realm_object.hpp b/src/js_realm_object.hpp index a4839741..846d3388 100644 --- a/src/js_realm_object.hpp +++ b/src/js_realm_object.hpp @@ -29,7 +29,7 @@ namespace realm { namespace js { template -class RealmObject { +struct RealmObjectClass : ClassDefinition { using ContextType = typename T::Context; using FunctionType = typename T::Function; using ObjectType = typename T::Object; @@ -40,7 +40,6 @@ class RealmObject { using Function = js::Function; using ReturnValue = js::ReturnValue; - public: static ObjectType create_instance(ContextType, realm::Object &); static void get_property(ContextType, ObjectType, const String &, ReturnValue &); @@ -48,32 +47,27 @@ class RealmObject { static std::vector get_property_names(ContextType, ObjectType); static void is_valid(ContextType, ObjectType, size_t, const ValueType [], ReturnValue &); -}; - -template -struct RealmObjectClass : ClassDefinition { - using RealmObject = js::RealmObject; const std::string name = "RealmObject"; const StringPropertyType string_accessor = { - wrap, - wrap, - wrap, + wrap, + wrap, + wrap, }; MethodMap const methods = { - {"isValid", wrap}, + {"isValid", wrap}, }; }; template -void RealmObject::is_valid(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { +void RealmObjectClass::is_valid(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { return_value.set(get_internal>(this_object)->is_valid()); } template -typename T::Object RealmObject::create_instance(ContextType ctx, realm::Object &realm_object) { +typename T::Object RealmObjectClass::create_instance(ContextType ctx, realm::Object &realm_object) { static String prototype_string = "prototype"; auto delegate = get_delegate(realm_object.realm().get()); @@ -97,7 +91,7 @@ typename T::Object RealmObject::create_instance(ContextType ctx, realm::Objec } template -void RealmObject::get_property(ContextType ctx, ObjectType object, const String &property, ReturnValue &return_value) { +void RealmObjectClass::get_property(ContextType ctx, ObjectType object, const String &property, ReturnValue &return_value) { try { auto realm_object = get_internal>(object); auto result = realm_object->template get_property_value(ctx, property); @@ -108,7 +102,7 @@ void RealmObject::get_property(ContextType ctx, ObjectType object, const Stri } template -bool RealmObject::set_property(ContextType ctx, ObjectType object, const String &property, ValueType value) { +bool RealmObjectClass::set_property(ContextType ctx, ObjectType object, const String &property, ValueType value) { auto realm_object = get_internal>(object); try { realm_object->set_property_value(ctx, property, value, true); @@ -120,7 +114,7 @@ bool RealmObject::set_property(ContextType ctx, ObjectType object, const Stri } template -std::vector> RealmObject::get_property_names(ContextType ctx, ObjectType object) { +std::vector> RealmObjectClass::get_property_names(ContextType ctx, ObjectType object) { auto realm_object = get_internal>(object); auto &properties = realm_object->get_object_schema().properties; diff --git a/src/js_results.hpp b/src/js_results.hpp index da7f1a25..9d025c86 100644 --- a/src/js_results.hpp +++ b/src/js_results.hpp @@ -206,7 +206,7 @@ void ResultsClass::get_index(ContextType ctx, ObjectType object, uint32_t ind } auto realm_object = realm::Object(results->get_realm(), results->get_object_schema(), results->get(index)); - return_value.set(RealmObject::create_instance(ctx, realm_object)); + return_value.set(RealmObjectClass::create_instance(ctx, realm_object)); } template diff --git a/src/jsc/jsc_init.cpp b/src/jsc/jsc_init.cpp index a1ba8a9e..872f0606 100644 --- a/src/jsc/jsc_init.cpp +++ b/src/jsc/jsc_init.cpp @@ -28,7 +28,7 @@ using namespace realm; using namespace realm::jsc; JSObjectRef RJSConstructorCreate(JSContextRef ctx) { - return js::Realm::create_constructor(ctx); + return js::RealmClass::create_constructor(ctx); } void RJSInitializeInContext(JSContextRef ctx) { diff --git a/src/node/node_init.cpp b/src/node/node_init.cpp index da21c3ff..df1613eb 100644 --- a/src/node/node_init.cpp +++ b/src/node/node_init.cpp @@ -23,7 +23,7 @@ namespace node { static void init(v8::Local exports) { v8::Isolate* isolate = v8::Isolate::GetCurrent(); - v8::Local realm_constructor = js::Realm::create_constructor(isolate); + v8::Local realm_constructor = js::RealmClass::create_constructor(isolate); Nan::Set(exports, realm_constructor->GetName(), realm_constructor); }