From 4ea81e8e24d69dd24d36b518edc6ade52efb274b Mon Sep 17 00:00:00 2001 From: Ari Lazier Date: Wed, 22 Feb 2017 16:11:13 -0800 Subject: [PATCH] support external realm creation --- src/js_realm.hpp | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/js_realm.hpp b/src/js_realm.hpp index a1a50e55..6df9b9f1 100644 --- a/src/js_realm.hpp +++ b/src/js_realm.hpp @@ -148,6 +148,9 @@ class RealmClass : public ClassDefinition> { using NativeAccessor = realm::NativeAccessor; public: + using ObjectDefaultsMap = typename Schema::ObjectDefaultsMap; + using ConstructorMap = typename Schema::ConstructorMap; + static FunctionType create_constructor(ContextType); // methods @@ -173,6 +176,8 @@ public: // static methods static void constructor(ContextType, ObjectType, size_t, const ValueType[]); + static SharedRealm create_shared_realm(ContextType, realm::Realm::Config, bool, ObjectDefaultsMap &&, ConstructorMap &&); + static void schema_version(ContextType, FunctionType, ObjectType, size_t, const ValueType[], ReturnValue &); static void clear_test_state(ContextType, FunctionType, ObjectType, size_t, const ValueType[], ReturnValue &); static void copy_bundled_realm_files(ContextType, FunctionType, ObjectType, size_t, const ValueType[], ReturnValue &); @@ -318,8 +323,8 @@ static inline void convert_outdated_datetime_columns(const SharedRealm &realm) { template 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; + ObjectDefaultsMap defaults; + ConstructorMap constructors; bool schema_updated = false; if (argc == 0) { @@ -412,9 +417,21 @@ void RealmClass::constructor(ContextType ctx, ObjectType this_object, size_t config.path = normalize_path(config.path); ensure_directory_exists_for_file(config.path); + auto realm = create_shared_realm(ctx, config, schema_updated, std::move(defaults), std::move(constructors)); + + // Fix for datetime -> timestamp conversion + convert_outdated_datetime_columns(realm); + + set_internal>(this_object, new SharedRealm(realm)); +} + +template +SharedRealm RealmClass::create_shared_realm(ContextType ctx, realm::Realm::Config config, bool schema_updated, + ObjectDefaultsMap && defaults, ConstructorMap && constructors) { config.execution_context = reinterpret_cast(Context::get_execution_context_id(ctx)); SharedRealm realm = realm::Realm::get_shared_realm(config); + GlobalContextType global_context = Context::get_global_context(ctx); if (!realm->m_binding_context) { realm->m_binding_context.reset(new RealmDelegate(realm, global_context)); @@ -430,10 +447,7 @@ void RealmClass::constructor(ContextType ctx, ObjectType this_object, size_t js_binding_context->m_constructors = std::move(constructors); } - // Fix for datetime -> timestamp conversion - convert_outdated_datetime_columns(realm); - - set_internal>(this_object, new SharedRealm(realm)); + return realm; } template