From 58d50bb3c58db19c941d5a4e1edff50f9b030fba Mon Sep 17 00:00:00 2001 From: Ari Lazier Date: Wed, 30 Mar 2016 10:23:15 -0700 Subject: [PATCH] refactor realm --- src/js_realm.hpp | 51 +++++++++++++++++++++---------------------- src/jsc/js_compat.hpp | 15 ++++++++----- src/jsc/jsc_list.cpp | 6 +---- src/jsc/jsc_realm.cpp | 2 +- 4 files changed, 36 insertions(+), 38 deletions(-) diff --git a/src/js_realm.hpp b/src/js_realm.hpp index 6d00359a..29d6b04d 100644 --- a/src/js_realm.hpp +++ b/src/js_realm.hpp @@ -111,31 +111,12 @@ private: }; template -static RealmDelegate *get_delegate(Realm *realm) { +RealmDelegate *get_delegate(Realm *realm) { return dynamic_cast *>(realm->m_binding_context.get()); } -static inline std::string default_path(); -static inline void set_default_path(std::string path); - - -std::string RJSValidatedObjectTypeForValue(SharedRealm &realm, JSContextRef ctx, JSValueRef value) { - if (JSValueIsObject(ctx, value) && JSObjectIsConstructor(ctx, (JSObjectRef)value)) { - JSObjectRef constructor = (JSObjectRef)value; - - auto delegate = js::get_delegate(realm.get()); - for (auto pair : delegate->m_constructors) { - if (pair.second == constructor) { - return pair.first; - } - } - - throw std::runtime_error("Constructor was not registered in the schema for this Realm"); - } - - return RJSValidatedStringForValue(ctx, value, "objectType"); -} - +std::string default_path(); +void set_default_path(std::string path); template class Realm : public BindingContext { @@ -163,6 +144,24 @@ public: } return name; } + + // converts constructor object or type name to type name + static std::string validated_object_type_for_value(SharedRealm &realm, JSContextRef ctx, JSValueRef value) { + if (ValueIsObject(ctx, value) && ValueIsConstructor(ctx, value)) { + ObjectType constructor = ValueToObject(ctx, value); + + auto delegate = get_delegate(realm.get()); + for (auto pair : delegate->m_constructors) { + if (pair.second == constructor) { + return pair.first; + } + } + throw std::runtime_error("Constructor was not registered in the schema for this Realm"); + } + return RJSValidatedStringForValue(ctx, value, "objectType"); + } + + }; template @@ -171,7 +170,7 @@ void Realm::Objects(ContextType ctx, ObjectType thisObject, size_t argumentCo RJSValidateArgumentCount(argumentCount, 1); SharedRealm sharedRealm = *RJSGetInternal(thisObject); - std::string className = RJSValidatedObjectTypeForValue(sharedRealm, ctx, arguments[0]); + std::string className = validated_object_type_for_value(sharedRealm, ctx, arguments[0]); returnObject = RJSResultsCreate(ctx, sharedRealm, className); } catch (std::exception &exp) { @@ -185,7 +184,7 @@ void Realm::Create(ContextType ctx, ObjectType thisObject, size_t argumentCou RJSValidateArgumentRange(argumentCount, 2, 3); SharedRealm sharedRealm = *RJSGetInternal(thisObject); - std::string className = RJSValidatedObjectTypeForValue(sharedRealm, ctx, arguments[0]); + std::string className = validated_object_type_for_value(sharedRealm, ctx, arguments[0]); auto &schema = sharedRealm->config().schema; auto object_schema = schema->find(className); @@ -221,7 +220,7 @@ void Realm::Delete(ContextType ctx, ObjectType thisObject, size_t argumentCou } auto arg = RJSValidatedValueToObject(ctx, arguments[0]); - if (RJSValueIsObjectOfClass(ctx, arg, realm::js::results_class())) { + if (RJSValueIsObjectOfClass(ctx, arg, realm::js::object_class())) { Object *object = RJSGetInternal(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()); @@ -243,7 +242,7 @@ void Realm::Delete(ContextType ctx, ObjectType thisObject, size_t argumentCou Results *results = RJSGetInternal(arg); results->clear(); } - else if(RJSValueIsObjectOfClass(ctx, arg, list_class())) { + else if(RJSValueIsObjectOfClass(ctx, arg, realm::js::list_class())) { List *list = RJSGetInternal(arg); list->delete_all(); } diff --git a/src/jsc/js_compat.hpp b/src/jsc/js_compat.hpp index c7453f19..0cd54352 100644 --- a/src/jsc/js_compat.hpp +++ b/src/jsc/js_compat.hpp @@ -30,7 +30,10 @@ static inline bool ValueIsBoolean(jsc::Types::Context ctx, jsc::Types::Value val static inline bool ValueIsNumber(jsc::Types::Context ctx, jsc::Types::Value value) { return JSValueIsNumber(ctx, value); } static inline bool ValueIsString(jsc::Types::Context ctx, jsc::Types::Value value) { return JSValueIsString(ctx, value); } static inline bool ValueIsObject(jsc::Types::Context ctx, jsc::Types::Value value) { return JSValueIsObject(ctx, value); } - +static inline bool ValueIsConstructor(jsc::Types::Context ctx, jsc::Types::Value value) { return ValueIsObject(ctx, value) && JSObjectIsConstructor(ctx, (JSObjectRef)value); } + +static inline jsc::Types::Object ValueToObject(jsc::Types::Context ctx, jsc::Types::Value value) { return (JSObjectRef)value; } + static inline void ValueProtect(jsc::Types::Context ctx, jsc::Types::Value value) { JSValueProtect(ctx, value); } static inline void ValueUnprotect(jsc::Types::Context ctx, jsc::Types::Value value) { JSValueUnprotect(ctx, value); } @@ -42,7 +45,7 @@ static inline void GlobalContextProtect(jsc::Types::GlobalContext ctx) { JSGloba static inline void GlobalContextUnprotect(jsc::Types::GlobalContext ctx) { JSGlobalContextRelease(ctx); } template -static jsc::Types::Object WrapObject(jsc::Types::Context ctx, jsc::Types::ObjectClass objectClass, T internal, jsc::Types::Object prototype = nullptr) { +jsc::Types::Object WrapObject(jsc::Types::Context ctx, jsc::Types::ObjectClass objectClass, T internal, jsc::Types::Object prototype = nullptr) { JSObjectRef ref = JSObjectMake(ctx, objectClass, (void *)internal); if (prototype) { JSObjectSetPrototype(ctx, ref, prototype); @@ -50,9 +53,9 @@ static jsc::Types::Object WrapObject(jsc::Types::Context ctx, jsc::Types::Object return ref; } -static inline jsc::Types::ObjectClass realm_class(); -static inline jsc::Types::ObjectClass list_class(); -static inline jsc::Types::ObjectClass object_class(); -static inline jsc::Types::ObjectClass results_class(); +jsc::Types::ObjectClass realm_class(); +jsc::Types::ObjectClass list_class(); +jsc::Types::ObjectClass object_class(); +jsc::Types::ObjectClass results_class(); }} diff --git a/src/jsc/jsc_list.cpp b/src/jsc/jsc_list.cpp index 1a7781f3..5f4f2802 100644 --- a/src/jsc/jsc_list.cpp +++ b/src/jsc/jsc_list.cpp @@ -117,8 +117,4 @@ JSClassRef RJSListClass() { return s_listClass; } -namespace realm { -namespace js { -JSClassRef list_class() { return RJSListClass(); }; -} -} +JSClassRef realm::js::list_class() { return RJSListClass(); }; diff --git a/src/jsc/jsc_realm.cpp b/src/jsc/jsc_realm.cpp index 6e939381..258ffe51 100644 --- a/src/jsc/jsc_realm.cpp +++ b/src/jsc/jsc_realm.cpp @@ -42,7 +42,7 @@ static JSValueRef GetDefaultPath(JSContextRef ctx, JSObjectRef object, JSStringR static bool SetDefaultPath(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSValueRef* jsException) { try { - realm::js::set_default_path(RJSValidatedStringForValue(ctx, value, "defaultPath")); + js::set_default_path(RJSValidatedStringForValue(ctx, value, "defaultPath")); } catch (std::exception &ex) { if (jsException) {