move exception handling to method wrapper

This commit is contained in:
Ari Lazier 2016-03-30 11:55:13 -07:00
parent 58d50bb3c5
commit 0282e98232
3 changed files with 193 additions and 273 deletions

View File

@ -41,165 +41,124 @@ struct List {
using ObjectType = typename T::Object; using ObjectType = typename T::Object;
using ValueType = typename T::Value; using ValueType = typename T::Value;
using ReturnType = typename T::Return; 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 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, ExceptionType &exception); 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, ExceptionType &exception); 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, ExceptionType &exception); 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, ExceptionType &exception); 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, ExceptionType &exception); 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, ExceptionType &exception); 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, ExceptionType &exception); static void Sorted(ContextType ctx, ObjectType thisObject, size_t argCount, const ValueType args[], ReturnType &ret);
}; };
template<typename T> template<typename T>
void List<T>::Push(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { void List<T>::Push(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) {
try { realm::List *list = RJSGetInternal<realm::List *>(thisObject);
realm::List *list = RJSGetInternal<realm::List *>(thisObject); RJSValidateArgumentCountIsAtLeast(argumentCount, 1);
RJSValidateArgumentCountIsAtLeast(argumentCount, 1); for (size_t i = 0; i < argumentCount; i++) {
for (size_t i = 0; i < argumentCount; i++) { list->add(ctx, arguments[i]);
list->add(ctx, arguments[i]);
}
RJSSetReturnNumber(ctx, returnObject, list->size());
}
catch (std::exception &exp) {
RJSSetException(ctx, exceptionObject, exp);
} }
RJSSetReturnNumber(ctx, returnObject, list->size());
} }
template<typename T> template<typename T>
void List<T>::Pop(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { void List<T>::Pop(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) {
try { realm::List *list = RJSGetInternal<realm::List *>(thisObject);
realm::List *list = RJSGetInternal<realm::List *>(thisObject); RJSValidateArgumentCount(argumentCount, 0);
RJSValidateArgumentCount(argumentCount, 0);
size_t size = list->size(); size_t size = list->size();
if (size == 0) { if (size == 0) {
list->verify_in_transaction(); list->verify_in_transaction();
RJSSetReturnUndefined(ctx, returnObject); 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);
}
} }
catch (std::exception &exception) { else {
RJSSetException(ctx, exceptionObject, exception); size_t index = size - 1;
returnObject = RJSObjectCreate(ctx, Object(list->get_realm(), list->get_object_schema(), list->get(index)));
list->remove(index);
} }
} }
template<typename T> template<typename T>
void List<T>::Unshift(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { void List<T>::Unshift(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) {
try { realm::List *list = RJSGetInternal<realm::List *>(thisObject);
realm::List *list = RJSGetInternal<realm::List *>(thisObject); RJSValidateArgumentCountIsAtLeast(argumentCount, 1);
RJSValidateArgumentCountIsAtLeast(argumentCount, 1); for (size_t i = 0; i < argumentCount; i++) {
for (size_t i = 0; i < argumentCount; i++) { list->insert(ctx, arguments[i], i);
list->insert(ctx, arguments[i], i);
}
RJSSetReturnNumber(ctx, returnObject, list->size());
} }
catch (std::exception &exp) { RJSSetReturnNumber(ctx, returnObject, list->size());
RJSSetException(ctx, exceptionObject, exp); }
template<typename T>
void List<T>::Shift(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) {
realm::List *list = RJSGetInternal<realm::List *>(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<typename T> template<typename T>
void List<T>::Shift(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { void List<T>::Splice(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) {
try { realm::List *list = RJSGetInternal<realm::List *>(thisObject);
realm::List *list = RJSGetInternal<realm::List *>(thisObject); size_t size = list->size();
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);
}
}
catch (std::exception &exp) {
RJSSetException(ctx, exceptionObject, exp);
}
}
template<typename T> RJSValidateArgumentCountIsAtLeast(argumentCount, 1);
void List<T>::Splice(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { long index = std::min<long>(RJSValidatedValueToNumber(ctx, arguments[0]), size);
try { if (index < 0) {
realm::List *list = RJSGetInternal<realm::List *>(thisObject); index = std::max<long>(size + index, 0);
size_t size = list->size();
RJSValidateArgumentCountIsAtLeast(argumentCount, 1);
long index = std::min<long>(RJSValidatedValueToNumber(ctx, arguments[0]), size);
if (index < 0) {
index = std::max<long>(size + index, 0);
}
long remove;
if (argumentCount < 2) {
remove = size - index;
}
else {
remove = std::max<long>(RJSValidatedValueToNumber(ctx, arguments[1]), 0);
remove = std::min<long>(remove, size - index);
}
std::vector<ReturnType> 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);
} }
catch (std::exception &exp) {
RJSSetException(ctx, exceptionObject, exp); long remove;
if (argumentCount < 2) {
remove = size - index;
} }
else {
remove = std::max<long>(RJSValidatedValueToNumber(ctx, arguments[1]), 0);
remove = std::min<long>(remove, size - index);
}
std::vector<ReturnType> 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<typename T> template<typename T>
void List<T>::StaticResults(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { void List<T>::StaticResults(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) {
try { realm::List *list = RJSGetInternal<realm::List *>(thisObject);
realm::List *list = RJSGetInternal<realm::List *>(thisObject); RJSValidateArgumentCount(argumentCount, 0);
RJSValidateArgumentCount(argumentCount, 0); returnObject = RJSResultsCreate(ctx, list->get_realm(), list->get_object_schema(), std::move(list->get_query()), false);
returnObject = RJSResultsCreate(ctx, list->get_realm(), list->get_object_schema(), std::move(list->get_query()), false);
}
catch (std::exception &exp) {
RJSSetException(ctx, exceptionObject, exp);
}
} }
template<typename T> template<typename T>
void List<T>::Filtered(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { void List<T>::Filtered(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) {
try { realm::List *list = RJSGetInternal<realm::List *>(thisObject);
realm::List *list = RJSGetInternal<realm::List *>(thisObject); RJSValidateArgumentCountIsAtLeast(argumentCount, 1);
RJSValidateArgumentCountIsAtLeast(argumentCount, 1);
SharedRealm sharedRealm = *RJSGetInternal<SharedRealm *>(thisObject); SharedRealm sharedRealm = *RJSGetInternal<SharedRealm *>(thisObject);
returnObject = RJSResultsCreateFiltered(ctx, sharedRealm, list->get_object_schema(), std::move(list->get_query()), argumentCount, arguments); returnObject = RJSResultsCreateFiltered(ctx, sharedRealm, list->get_object_schema(), std::move(list->get_query()), argumentCount, arguments);
}
catch (std::exception &exp) {
RJSSetException(ctx, exceptionObject, exp);
}
} }
template<typename T> template<typename T>
void List<T>::Sorted(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { void List<T>::Sorted(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) {
try { realm::List *list = RJSGetInternal<realm::List *>(thisObject);
realm::List *list = RJSGetInternal<realm::List *>(thisObject); RJSValidateArgumentRange(argumentCount, 1, 2);
RJSValidateArgumentRange(argumentCount, 1, 2);
SharedRealm sharedRealm = *RJSGetInternal<SharedRealm *>(thisObject); SharedRealm sharedRealm = *RJSGetInternal<SharedRealm *>(thisObject);
returnObject = RJSResultsCreateSorted(ctx, sharedRealm, list->get_object_schema(), std::move(list->get_query()), argumentCount, arguments); returnObject = RJSResultsCreateSorted(ctx, sharedRealm, list->get_object_schema(), std::move(list->get_query()), argumentCount, arguments);
}
catch (std::exception &exp) {
RJSSetException(ctx, exceptionObject, exp);
}
} }
} }

View File

@ -127,15 +127,15 @@ public:
using ReturnType = typename T::Return; using ReturnType = typename T::Return;
using ExceptionType = typename T::Exception; using ExceptionType = typename T::Exception;
static void Objects(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, ExceptionType &exceptionObject); 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, ExceptionType &exceptionObject); 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, ExceptionType &exceptionObject); 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, ExceptionType &exceptionObject); 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, ExceptionType &exceptionObject); 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, ExceptionType &exceptionObject); 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, ExceptionType &exceptionObject); 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, ExceptionType &exceptionObject); 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) { static std::string validated_notification_name(JSContextRef ctx, JSValueRef value) {
std::string name = RJSValidatedStringForValue(ctx, value); std::string name = RJSValidatedStringForValue(ctx, value);
@ -165,123 +165,103 @@ public:
}; };
template<typename T> template<typename T>
void Realm<T>::Objects(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { void Realm<T>::Objects(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) {
try { RJSValidateArgumentCount(argumentCount, 1);
RJSValidateArgumentCount(argumentCount, 1);
SharedRealm sharedRealm = *RJSGetInternal<SharedRealm *>(thisObject); SharedRealm sharedRealm = *RJSGetInternal<SharedRealm *>(thisObject);
std::string className = validated_object_type_for_value(sharedRealm, ctx, arguments[0]); std::string className = validated_object_type_for_value(sharedRealm, ctx, arguments[0]);
returnObject = RJSResultsCreate(ctx, sharedRealm, className); returnObject = RJSResultsCreate(ctx, sharedRealm, className);
}
catch (std::exception &exp) {
RJSSetException(ctx, exceptionObject, exp);
}
} }
template<typename T> template<typename T>
void Realm<T>::Create(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { void Realm<T>::Create(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) {
try { RJSValidateArgumentRange(argumentCount, 2, 3);
RJSValidateArgumentRange(argumentCount, 2, 3);
SharedRealm sharedRealm = *RJSGetInternal<SharedRealm *>(thisObject); SharedRealm sharedRealm = *RJSGetInternal<SharedRealm *>(thisObject);
std::string className = validated_object_type_for_value(sharedRealm, ctx, arguments[0]); std::string className = validated_object_type_for_value(sharedRealm, ctx, arguments[0]);
auto &schema = sharedRealm->config().schema; auto &schema = sharedRealm->config().schema;
auto object_schema = schema->find(className); auto object_schema = schema->find(className);
if (object_schema == schema->end()) { if (object_schema == schema->end()) {
throw std::runtime_error("Object type '" + className + "' not found in schema."); 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<ValueType>(ctx, sharedRealm, *object_schema, object, update));
} }
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<ValueType>(ctx, sharedRealm, *object_schema, object, update));
} }
template<typename T> template<typename T>
void Realm<T>::Delete(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { void Realm<T>::Delete(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) {
try { RJSValidateArgumentCount(argumentCount, 1);
RJSValidateArgumentCount(argumentCount, 1);
SharedRealm realm = *RJSGetInternal<SharedRealm *>(thisObject); SharedRealm realm = *RJSGetInternal<SharedRealm *>(thisObject);
if (!realm->is_in_transaction()) { if (!realm->is_in_transaction()) {
throw std::runtime_error("Can only delete objects within a transaction."); throw std::runtime_error("Can only delete objects within a transaction.");
} }
auto arg = RJSValidatedValueToObject(ctx, arguments[0]); auto arg = RJSValidatedValueToObject(ctx, arguments[0]);
if (RJSValueIsObjectOfClass(ctx, arg, realm::js::object_class())) { if (RJSValueIsObjectOfClass(ctx, arg, realm::js::object_class())) {
Object *object = RJSGetInternal<Object *>(arg); Object *object = RJSGetInternal<Object *>(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<Object *>(jsObject);
realm::TableRef table = ObjectStore::table_for_object_type(realm->read_group(), object->get_object_schema().name); realm::TableRef table = ObjectStore::table_for_object_type(realm->read_group(), object->get_object_schema().name);
table->move_last_over(object->row().get_index()); 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<Object *>(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<Results *>(arg);
results->clear();
}
else if(RJSValueIsObjectOfClass(ctx, arg, realm::js::list_class())) {
List *list = RJSGetInternal<List *>(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) { else if(RJSValueIsObjectOfClass(ctx, arg, realm::js::results_class())) {
RJSSetException(ctx, exceptionObject, exp); Results *results = RJSGetInternal<Results *>(arg);
results->clear();
}
else if(RJSValueIsObjectOfClass(ctx, arg, realm::js::list_class())) {
List *list = RJSGetInternal<List *>(arg);
list->delete_all();
}
else {
throw std::runtime_error("Argument to 'delete' must be a Realm object or a collection of Realm objects.");
} }
} }
template<typename T> template<typename T>
void Realm<T>::DeleteAll(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { void Realm<T>::DeleteAll(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) {
try { RJSValidateArgumentCount(argumentCount, 0);
RJSValidateArgumentCount(argumentCount, 0);
SharedRealm realm = *RJSGetInternal<SharedRealm *>(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<typename T>
void Realm<T>::Write(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) {
SharedRealm realm = *RJSGetInternal<SharedRealm *>(thisObject); SharedRealm realm = *RJSGetInternal<SharedRealm *>(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<typename T>
void Realm<T>::Write(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) {
RJSValidateArgumentCount(argumentCount, 1);
SharedRealm realm = *RJSGetInternal<SharedRealm *>(thisObject);
auto object = RJSValidatedValueToFunction(ctx, arguments[0]);
try {
realm->begin_transaction(); realm->begin_transaction();
RJSCallFunction(ctx, object, thisObject, 0, NULL); RJSCallFunction(ctx, object, thisObject, 0, NULL);
realm->commit_transaction(); realm->commit_transaction();
@ -290,66 +270,46 @@ void Realm<T>::Write(ContextType ctx, ObjectType thisObject, size_t argumentCoun
if (realm->is_in_transaction()) { if (realm->is_in_transaction()) {
realm->cancel_transaction(); realm->cancel_transaction();
} }
RJSSetException(ctx, exceptionObject, exp); throw;
} }
} }
template<typename T> template<typename T>
void Realm<T>::AddListener(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { void Realm<T>::AddListener(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) {
try { RJSValidateArgumentCount(argumentCount, 2);
RJSValidateArgumentCount(argumentCount, 2); __unused std::string name = validated_notification_name(ctx, arguments[0]);
__unused std::string name = validated_notification_name(ctx, arguments[0]); auto callback = RJSValidatedValueToFunction(ctx, arguments[1]);
auto callback = RJSValidatedValueToFunction(ctx, arguments[1]);
SharedRealm realm = *RJSGetInternal<SharedRealm *>(thisObject); SharedRealm realm = *RJSGetInternal<SharedRealm *>(thisObject);
static_cast<js::RealmDelegate<jsc::Types> *>(realm->m_binding_context.get())->add_notification(callback); static_cast<js::RealmDelegate<jsc::Types> *>(realm->m_binding_context.get())->add_notification(callback);
}
catch (std::exception &exp) {
RJSSetException(ctx, exceptionObject, exp);
}
} }
template<typename T> template<typename T>
void Realm<T>::RemoveListener(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { void Realm<T>::RemoveListener(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) {
try { RJSValidateArgumentCount(argumentCount, 2);
RJSValidateArgumentCount(argumentCount, 2); __unused std::string name = validated_notification_name(ctx, arguments[0]);
__unused std::string name = validated_notification_name(ctx, arguments[0]); auto callback = RJSValidatedValueToFunction(ctx, arguments[1]);
auto callback = RJSValidatedValueToFunction(ctx, arguments[1]);
SharedRealm realm = *RJSGetInternal<SharedRealm *>(thisObject); SharedRealm realm = *RJSGetInternal<SharedRealm *>(thisObject);
static_cast<js::RealmDelegate<jsc::Types> *>(realm->m_binding_context.get())->remove_notification(callback); static_cast<js::RealmDelegate<jsc::Types> *>(realm->m_binding_context.get())->remove_notification(callback);
}
catch (std::exception &exp) {
RJSSetException(ctx, exceptionObject, exp);
}
} }
template<typename T> template<typename T>
void Realm<T>::RemoveAllListeners(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { void Realm<T>::RemoveAllListeners(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) {
try { RJSValidateArgumentRange(argumentCount, 0, 1);
RJSValidateArgumentRange(argumentCount, 0, 1); if (argumentCount) {
if (argumentCount) { validated_notification_name(ctx, arguments[0]);
validated_notification_name(ctx, arguments[0]); }
}
SharedRealm realm = *RJSGetInternal<SharedRealm *>(thisObject); SharedRealm realm = *RJSGetInternal<SharedRealm *>(thisObject);
static_cast<js::RealmDelegate<jsc::Types> *>(realm->m_binding_context.get())->remove_all_notifications(); static_cast<js::RealmDelegate<jsc::Types> *>(realm->m_binding_context.get())->remove_all_notifications();
}
catch (std::exception &exp) {
RJSSetException(ctx, exceptionObject, exp);
}
} }
template<typename T> template<typename T>
void Realm<T>::Close(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { void Realm<T>::Close(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) {
try { RJSValidateArgumentCount(argumentCount, 0);
RJSValidateArgumentCount(argumentCount, 0); SharedRealm realm = *RJSGetInternal<SharedRealm *>(thisObject);
SharedRealm realm = *RJSGetInternal<SharedRealm *>(thisObject); realm->close();
realm->close();
}
catch (std::exception &exp) {
RJSSetException(ctx, exceptionObject, exp);
}
} }
} }

View File

@ -36,7 +36,8 @@
#define WRAP_CLASS_METHOD(CLASS_NAME, METHOD_NAME) \ #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 CLASS_NAME ## METHOD_NAME(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) { \
JSValueRef returnObject = NULL; \ 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; \ return returnObject; \
} }