diff --git a/src/js_list.hpp b/src/js_list.hpp index 76a53dd7..dbfdaf83 100644 --- a/src/js_list.hpp +++ b/src/js_list.hpp @@ -41,165 +41,124 @@ struct List { using ObjectType = typename T::Object; using ValueType = typename T::Value; using ReturnType = typename T::Return; - using ExceptionType = typename T::Exception; - static void Push(ContextType ctx, ObjectType thisObject, size_t argCount, const ValueType args[], ReturnType &ret, ExceptionType &exception); - static void Pop(ContextType ctx, ObjectType thisObject, size_t argCount, const ValueType args[], ReturnType &ret, ExceptionType &exception); - static void Unshift(ContextType ctx, ObjectType thisObject, size_t argCount, const ValueType args[], ReturnType &ret, ExceptionType &exception); - static void Shift(ContextType ctx, ObjectType thisObject, size_t argCount, const ValueType args[], ReturnType &ret, ExceptionType &exception); - static void Splice(ContextType ctx, ObjectType thisObject, size_t argCount, const ValueType args[], ReturnType &ret, ExceptionType &exception); - static void StaticResults(ContextType ctx, ObjectType thisObject, size_t argCount, const ValueType args[], ReturnType &ret, ExceptionType &exception); - static void Filtered(ContextType ctx, ObjectType thisObject, size_t argCount, const ValueType args[], ReturnType &ret, ExceptionType &exception); - static void Sorted(ContextType ctx, ObjectType thisObject, size_t argCount, const ValueType args[], ReturnType &ret, ExceptionType &exception); + static void Push(ContextType ctx, ObjectType thisObject, size_t argCount, const ValueType args[], ReturnType &ret); + static void Pop(ContextType ctx, ObjectType thisObject, size_t argCount, const ValueType args[], ReturnType &ret); + static void Unshift(ContextType ctx, ObjectType thisObject, size_t argCount, const ValueType args[], ReturnType &ret); + static void Shift(ContextType ctx, ObjectType thisObject, size_t argCount, const ValueType args[], ReturnType &ret); + static void Splice(ContextType ctx, ObjectType thisObject, size_t argCount, const ValueType args[], ReturnType &ret); + static void StaticResults(ContextType ctx, ObjectType thisObject, size_t argCount, const ValueType args[], ReturnType &ret); + static void Filtered(ContextType ctx, ObjectType thisObject, size_t argCount, const ValueType args[], ReturnType &ret); + static void Sorted(ContextType ctx, ObjectType thisObject, size_t argCount, const ValueType args[], ReturnType &ret); }; template -void List::Push(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { - try { - realm::List *list = RJSGetInternal(thisObject); - RJSValidateArgumentCountIsAtLeast(argumentCount, 1); - for (size_t i = 0; i < argumentCount; i++) { - list->add(ctx, arguments[i]); - } - RJSSetReturnNumber(ctx, returnObject, list->size()); - } - catch (std::exception &exp) { - RJSSetException(ctx, exceptionObject, exp); +void List::Push(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) { + realm::List *list = RJSGetInternal(thisObject); + RJSValidateArgumentCountIsAtLeast(argumentCount, 1); + for (size_t i = 0; i < argumentCount; i++) { + list->add(ctx, arguments[i]); } + RJSSetReturnNumber(ctx, returnObject, list->size()); } template -void List::Pop(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { - try { - realm::List *list = RJSGetInternal(thisObject); - RJSValidateArgumentCount(argumentCount, 0); - - size_t size = list->size(); - if (size == 0) { - list->verify_in_transaction(); - RJSSetReturnUndefined(ctx, returnObject); - } - else { - size_t index = size - 1; - returnObject = RJSObjectCreate(ctx, Object(list->get_realm(), list->get_object_schema(), list->get(index))); - list->remove(index); - } +void List::Pop(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) { + realm::List *list = RJSGetInternal(thisObject); + RJSValidateArgumentCount(argumentCount, 0); + + size_t size = list->size(); + if (size == 0) { + list->verify_in_transaction(); + RJSSetReturnUndefined(ctx, returnObject); } - catch (std::exception &exception) { - RJSSetException(ctx, exceptionObject, exception); + else { + size_t index = size - 1; + returnObject = RJSObjectCreate(ctx, Object(list->get_realm(), list->get_object_schema(), list->get(index))); + list->remove(index); } } template -void List::Unshift(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { - try { - realm::List *list = RJSGetInternal(thisObject); - RJSValidateArgumentCountIsAtLeast(argumentCount, 1); - for (size_t i = 0; i < argumentCount; i++) { - list->insert(ctx, arguments[i], i); - } - RJSSetReturnNumber(ctx, returnObject, list->size()); +void List::Unshift(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) { + realm::List *list = RJSGetInternal(thisObject); + RJSValidateArgumentCountIsAtLeast(argumentCount, 1); + for (size_t i = 0; i < argumentCount; i++) { + list->insert(ctx, arguments[i], i); } - catch (std::exception &exp) { - RJSSetException(ctx, exceptionObject, exp); + RJSSetReturnNumber(ctx, returnObject, list->size()); +} + +template +void List::Shift(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) { + realm::List *list = RJSGetInternal(thisObject); + RJSValidateArgumentCount(argumentCount, 0); + if (list->size() == 0) { + list->verify_in_transaction(); + RJSSetReturnUndefined(ctx, returnObject); + } + else { + returnObject = RJSObjectCreate(ctx, Object(list->get_realm(), list->get_object_schema(), list->get(0))); + list->remove(0); } } template -void List::Shift(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { - try { - realm::List *list = RJSGetInternal(thisObject); - RJSValidateArgumentCount(argumentCount, 0); - if (list->size() == 0) { - list->verify_in_transaction(); - RJSSetReturnUndefined(ctx, returnObject); - } - else { - returnObject = RJSObjectCreate(ctx, Object(list->get_realm(), list->get_object_schema(), list->get(0))); - list->remove(0); - } +void List::Splice(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) { + realm::List *list = RJSGetInternal(thisObject); + size_t size = list->size(); + + RJSValidateArgumentCountIsAtLeast(argumentCount, 1); + long index = std::min(RJSValidatedValueToNumber(ctx, arguments[0]), size); + if (index < 0) { + index = std::max(size + index, 0); } - catch (std::exception &exp) { - RJSSetException(ctx, exceptionObject, exp); + + long remove; + if (argumentCount < 2) { + remove = size - index; } -} - -template -void List::Splice(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { - try { - realm::List *list = RJSGetInternal(thisObject); - size_t size = list->size(); - - RJSValidateArgumentCountIsAtLeast(argumentCount, 1); - long index = std::min(RJSValidatedValueToNumber(ctx, arguments[0]), size); - if (index < 0) { - index = std::max(size + index, 0); - } - - long remove; - if (argumentCount < 2) { - remove = size - index; - } - else { - remove = std::max(RJSValidatedValueToNumber(ctx, arguments[1]), 0); - remove = std::min(remove, size - index); - } - - std::vector removedObjects(remove); - for (size_t i = 0; i < remove; i++) { - removedObjects[i] = RJSObjectCreate(ctx, Object(list->get_realm(), list->get_object_schema(), list->get(index))); - list->remove(index); - } - for (size_t i = 2; i < argumentCount; i++) { - list->insert(ctx, arguments[i], index + i - 2); - } - RJSSetReturnArray(ctx, remove, removedObjects.data(), returnObject); + else { + remove = std::max(RJSValidatedValueToNumber(ctx, arguments[1]), 0); + remove = std::min(remove, size - index); } - catch (std::exception &exp) { - RJSSetException(ctx, exceptionObject, exp); + + std::vector removedObjects(remove); + for (size_t i = 0; i < remove; i++) { + removedObjects[i] = RJSObjectCreate(ctx, Object(list->get_realm(), list->get_object_schema(), list->get(index))); + list->remove(index); } + for (size_t i = 2; i < argumentCount; i++) { + list->insert(ctx, arguments[i], index + i - 2); + } + RJSSetReturnArray(ctx, remove, removedObjects.data(), returnObject); } template -void List::StaticResults(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { - try { - realm::List *list = RJSGetInternal(thisObject); - RJSValidateArgumentCount(argumentCount, 0); - returnObject = RJSResultsCreate(ctx, list->get_realm(), list->get_object_schema(), std::move(list->get_query()), false); - } - catch (std::exception &exp) { - RJSSetException(ctx, exceptionObject, exp); - } +void List::StaticResults(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) { + realm::List *list = RJSGetInternal(thisObject); + RJSValidateArgumentCount(argumentCount, 0); + returnObject = RJSResultsCreate(ctx, list->get_realm(), list->get_object_schema(), std::move(list->get_query()), false); } template -void List::Filtered(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { - try { - realm::List *list = RJSGetInternal(thisObject); - RJSValidateArgumentCountIsAtLeast(argumentCount, 1); - - SharedRealm sharedRealm = *RJSGetInternal(thisObject); - returnObject = RJSResultsCreateFiltered(ctx, sharedRealm, list->get_object_schema(), std::move(list->get_query()), argumentCount, arguments); - } - catch (std::exception &exp) { - RJSSetException(ctx, exceptionObject, exp); - } +void List::Filtered(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) { + realm::List *list = RJSGetInternal(thisObject); + RJSValidateArgumentCountIsAtLeast(argumentCount, 1); + + SharedRealm sharedRealm = *RJSGetInternal(thisObject); + returnObject = RJSResultsCreateFiltered(ctx, sharedRealm, list->get_object_schema(), std::move(list->get_query()), argumentCount, arguments); } template -void List::Sorted(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { - try { - realm::List *list = RJSGetInternal(thisObject); - RJSValidateArgumentRange(argumentCount, 1, 2); - - SharedRealm sharedRealm = *RJSGetInternal(thisObject); - returnObject = RJSResultsCreateSorted(ctx, sharedRealm, list->get_object_schema(), std::move(list->get_query()), argumentCount, arguments); - } - catch (std::exception &exp) { - RJSSetException(ctx, exceptionObject, exp); - } +void List::Sorted(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) { + realm::List *list = RJSGetInternal(thisObject); + RJSValidateArgumentRange(argumentCount, 1, 2); + + SharedRealm sharedRealm = *RJSGetInternal(thisObject); + returnObject = RJSResultsCreateSorted(ctx, sharedRealm, list->get_object_schema(), std::move(list->get_query()), argumentCount, arguments); } } diff --git a/src/js_realm.hpp b/src/js_realm.hpp index 29d6b04d..0702391b 100644 --- a/src/js_realm.hpp +++ b/src/js_realm.hpp @@ -127,15 +127,15 @@ public: using ReturnType = typename T::Return; using ExceptionType = typename T::Exception; - static void Objects(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject); - static void Create(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject); - static void Delete(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject); - static void DeleteAll(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject); - static void Write(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject); - static void AddListener(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject); - static void RemoveListener(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject); - static void RemoveAllListeners(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject); - static void Close(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject); + static void Objects(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject); + static void Create(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject); + static void Delete(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject); + static void DeleteAll(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject); + static void Write(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject); + static void AddListener(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject); + static void RemoveListener(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject); + static void RemoveAllListeners(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject); + static void Close(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject); static std::string validated_notification_name(JSContextRef ctx, JSValueRef value) { std::string name = RJSValidatedStringForValue(ctx, value); @@ -165,123 +165,103 @@ public: }; template -void Realm::Objects(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { - try { - RJSValidateArgumentCount(argumentCount, 1); +void Realm::Objects(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) { + RJSValidateArgumentCount(argumentCount, 1); - SharedRealm sharedRealm = *RJSGetInternal(thisObject); - std::string className = validated_object_type_for_value(sharedRealm, ctx, arguments[0]); - returnObject = RJSResultsCreate(ctx, sharedRealm, className); - } - catch (std::exception &exp) { - RJSSetException(ctx, exceptionObject, exp); - } + SharedRealm sharedRealm = *RJSGetInternal(thisObject); + std::string className = validated_object_type_for_value(sharedRealm, ctx, arguments[0]); + returnObject = RJSResultsCreate(ctx, sharedRealm, className); } template -void Realm::Create(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { - try { - RJSValidateArgumentRange(argumentCount, 2, 3); +void Realm::Create(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) { + RJSValidateArgumentRange(argumentCount, 2, 3); - SharedRealm sharedRealm = *RJSGetInternal(thisObject); - std::string className = validated_object_type_for_value(sharedRealm, ctx, arguments[0]); - auto &schema = sharedRealm->config().schema; - auto object_schema = schema->find(className); + SharedRealm sharedRealm = *RJSGetInternal(thisObject); + std::string className = validated_object_type_for_value(sharedRealm, ctx, arguments[0]); + auto &schema = sharedRealm->config().schema; + auto object_schema = schema->find(className); - if (object_schema == schema->end()) { - throw std::runtime_error("Object type '" + className + "' not found in schema."); - } - - auto object = RJSValidatedValueToObject(ctx, arguments[1]); - if (RJSIsValueArray(ctx, arguments[1])) { - object = RJSDictForPropertyArray(ctx, *object_schema, object); - } - - bool update = false; - if (argumentCount == 3) { - update = RJSValidatedValueToBoolean(ctx, arguments[2]); - } - - returnObject = RJSObjectCreate(ctx, Object::create(ctx, sharedRealm, *object_schema, object, update)); + if (object_schema == schema->end()) { + throw std::runtime_error("Object type '" + className + "' not found in schema."); } - catch (std::exception &exp) { - RJSSetException(ctx, exceptionObject, exp); + + auto object = RJSValidatedValueToObject(ctx, arguments[1]); + if (RJSIsValueArray(ctx, arguments[1])) { + object = RJSDictForPropertyArray(ctx, *object_schema, object); } + + bool update = false; + if (argumentCount == 3) { + update = RJSValidatedValueToBoolean(ctx, arguments[2]); + } + + returnObject = RJSObjectCreate(ctx, Object::create(ctx, sharedRealm, *object_schema, object, update)); } template -void Realm::Delete(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { - try { - RJSValidateArgumentCount(argumentCount, 1); +void Realm::Delete(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) { + RJSValidateArgumentCount(argumentCount, 1); - SharedRealm realm = *RJSGetInternal(thisObject); - if (!realm->is_in_transaction()) { - throw std::runtime_error("Can only delete objects within a transaction."); - } + SharedRealm realm = *RJSGetInternal(thisObject); + if (!realm->is_in_transaction()) { + throw std::runtime_error("Can only delete objects within a transaction."); + } - auto arg = RJSValidatedValueToObject(ctx, arguments[0]); - if (RJSValueIsObjectOfClass(ctx, arg, realm::js::object_class())) { - Object *object = RJSGetInternal(arg); + auto arg = RJSValidatedValueToObject(ctx, arguments[0]); + 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()); + } + else if (RJSIsValueArray(ctx, arg)) { + size_t length = RJSValidatedListLength(ctx, arg); + for (long i = length-1; i >= 0; i--) { + JSObjectRef jsObject = RJSValidatedValueToObject(ctx, RJSValidatedObjectAtIndex(ctx, arg, (unsigned int)i)); + if (!RJSValueIsObjectOfClass(ctx, jsObject, object_class())) { + throw std::runtime_error("Argument to 'delete' must be a Realm object or a collection of Realm objects."); + } + + Object *object = RJSGetInternal(jsObject); realm::TableRef table = ObjectStore::table_for_object_type(realm->read_group(), object->get_object_schema().name); table->move_last_over(object->row().get_index()); } - else if (RJSIsValueArray(ctx, arg)) { - size_t length = RJSValidatedListLength(ctx, arg); - for (long i = length-1; i >= 0; i--) { - JSObjectRef jsObject = RJSValidatedValueToObject(ctx, RJSValidatedObjectAtIndex(ctx, arg, (unsigned int)i)); - if (!RJSValueIsObjectOfClass(ctx, jsObject, object_class())) { - throw std::runtime_error("Argument to 'delete' must be a Realm object or a collection of Realm objects."); - } - - Object *object = RJSGetInternal(jsObject); - realm::TableRef table = ObjectStore::table_for_object_type(realm->read_group(), object->get_object_schema().name); - table->move_last_over(object->row().get_index()); - } - } - else if(RJSValueIsObjectOfClass(ctx, arg, realm::js::results_class())) { - Results *results = RJSGetInternal(arg); - results->clear(); - } - else if(RJSValueIsObjectOfClass(ctx, arg, realm::js::list_class())) { - List *list = RJSGetInternal(arg); - list->delete_all(); - } - else { - throw std::runtime_error("Argument to 'delete' must be a Realm object or a collection of Realm objects."); - } } - catch (std::exception &exp) { - RJSSetException(ctx, exceptionObject, exp); + else if(RJSValueIsObjectOfClass(ctx, arg, realm::js::results_class())) { + Results *results = RJSGetInternal(arg); + results->clear(); + } + else if(RJSValueIsObjectOfClass(ctx, arg, realm::js::list_class())) { + List *list = RJSGetInternal(arg); + list->delete_all(); + } + else { + throw std::runtime_error("Argument to 'delete' must be a Realm object or a collection of Realm objects."); } } template -void Realm::DeleteAll(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { - try { - RJSValidateArgumentCount(argumentCount, 0); +void Realm::DeleteAll(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) { + RJSValidateArgumentCount(argumentCount, 0); - SharedRealm realm = *RJSGetInternal(thisObject); - - if (!realm->is_in_transaction()) { - throw std::runtime_error("Can only delete objects within a transaction."); - } - - for (auto objectSchema : *realm->config().schema) { - ObjectStore::table_for_object_type(realm->read_group(), objectSchema.name)->clear(); - } - } - catch (std::exception &exp) { - RJSSetException(ctx, exceptionObject, exp); - } -} - -template -void Realm::Write(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { SharedRealm realm = *RJSGetInternal(thisObject); - try { - RJSValidateArgumentCount(argumentCount, 1); - auto object = RJSValidatedValueToFunction(ctx, arguments[0]); + if (!realm->is_in_transaction()) { + throw std::runtime_error("Can only delete objects within a transaction."); + } + + for (auto objectSchema : *realm->config().schema) { + ObjectStore::table_for_object_type(realm->read_group(), objectSchema.name)->clear(); + } +} + +template +void Realm::Write(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) { + RJSValidateArgumentCount(argumentCount, 1); + + SharedRealm realm = *RJSGetInternal(thisObject); + auto object = RJSValidatedValueToFunction(ctx, arguments[0]); + try { realm->begin_transaction(); RJSCallFunction(ctx, object, thisObject, 0, NULL); realm->commit_transaction(); @@ -290,66 +270,46 @@ void Realm::Write(ContextType ctx, ObjectType thisObject, size_t argumentCoun if (realm->is_in_transaction()) { realm->cancel_transaction(); } - RJSSetException(ctx, exceptionObject, exp); + throw; } } template -void Realm::AddListener(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { - try { - RJSValidateArgumentCount(argumentCount, 2); - __unused std::string name = validated_notification_name(ctx, arguments[0]); - auto callback = RJSValidatedValueToFunction(ctx, arguments[1]); +void Realm::AddListener(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) { + RJSValidateArgumentCount(argumentCount, 2); + __unused std::string name = validated_notification_name(ctx, arguments[0]); + auto callback = RJSValidatedValueToFunction(ctx, arguments[1]); - SharedRealm realm = *RJSGetInternal(thisObject); - static_cast *>(realm->m_binding_context.get())->add_notification(callback); - } - catch (std::exception &exp) { - RJSSetException(ctx, exceptionObject, exp); - } + SharedRealm realm = *RJSGetInternal(thisObject); + static_cast *>(realm->m_binding_context.get())->add_notification(callback); } template -void Realm::RemoveListener(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { - try { - RJSValidateArgumentCount(argumentCount, 2); - __unused std::string name = validated_notification_name(ctx, arguments[0]); - auto callback = RJSValidatedValueToFunction(ctx, arguments[1]); +void Realm::RemoveListener(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) { + RJSValidateArgumentCount(argumentCount, 2); + __unused std::string name = validated_notification_name(ctx, arguments[0]); + auto callback = RJSValidatedValueToFunction(ctx, arguments[1]); - SharedRealm realm = *RJSGetInternal(thisObject); - static_cast *>(realm->m_binding_context.get())->remove_notification(callback); - } - catch (std::exception &exp) { - RJSSetException(ctx, exceptionObject, exp); - } + SharedRealm realm = *RJSGetInternal(thisObject); + static_cast *>(realm->m_binding_context.get())->remove_notification(callback); } template -void Realm::RemoveAllListeners(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { - try { - RJSValidateArgumentRange(argumentCount, 0, 1); - if (argumentCount) { - validated_notification_name(ctx, arguments[0]); - } +void Realm::RemoveAllListeners(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) { + RJSValidateArgumentRange(argumentCount, 0, 1); + if (argumentCount) { + validated_notification_name(ctx, arguments[0]); + } - SharedRealm realm = *RJSGetInternal(thisObject); - static_cast *>(realm->m_binding_context.get())->remove_all_notifications(); - } - catch (std::exception &exp) { - RJSSetException(ctx, exceptionObject, exp); - } + SharedRealm realm = *RJSGetInternal(thisObject); + static_cast *>(realm->m_binding_context.get())->remove_all_notifications(); } template -void Realm::Close(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { - try { - RJSValidateArgumentCount(argumentCount, 0); - SharedRealm realm = *RJSGetInternal(thisObject); - realm->close(); - } - catch (std::exception &exp) { - RJSSetException(ctx, exceptionObject, exp); - } +void Realm::Close(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) { + RJSValidateArgumentCount(argumentCount, 0); + SharedRealm realm = *RJSGetInternal(thisObject); + realm->close(); } } diff --git a/src/js_util.hpp b/src/js_util.hpp index 83331eb1..8c8789ce 100644 --- a/src/js_util.hpp +++ b/src/js_util.hpp @@ -36,7 +36,8 @@ #define WRAP_CLASS_METHOD(CLASS_NAME, METHOD_NAME) \ JSValueRef CLASS_NAME ## METHOD_NAME(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) { \ JSValueRef returnObject = NULL; \ - CLASS_NAME::METHOD_NAME(ctx, thisObject, argumentCount, arguments, returnObject, *jsException); \ + try { CLASS_NAME::METHOD_NAME(ctx, thisObject, argumentCount, arguments, returnObject); } \ + catch(std::exception &ex) { RJSSetException(ctx, *jsException, ex); } \ return returnObject; \ }