diff --git a/lib/browser/index.js b/lib/browser/index.js index 1830df93..175fe939 100644 --- a/lib/browser/index.js +++ b/lib/browser/index.js @@ -151,6 +151,7 @@ export default class Realm { // Non-mutating methods: util.createMethods(Realm.prototype, objectTypes.REALM, [ 'close', + 'isValid', ]); // Mutating methods: diff --git a/src/js_realm.hpp b/src/js_realm.hpp index 1c01207e..a3b520ab 100644 --- a/src/js_realm.hpp +++ b/src/js_realm.hpp @@ -459,14 +459,15 @@ void Realm::delete_all(ContextType ctx, ObjectType this_object, size_t argc, template void Realm::is_object_valid(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { - validate_argument_count(argc, 0); + validate_argument_count(argc, 1); ObjectType arg = Value::validated_to_object(ctx, arguments[0]); - if (!Object::template is_instance>(ctx, arg)) { - throw std::runtime_error("Argument to Realm.isValid must be a 'RealmObject'"); + if (Object::template is_instance>(ctx, arg)) { + return_value.set(get_internal>(arg)->is_valid()); + } + else { + throw std::runtime_error("Object is not a Realm Object"); } - auto object = get_internal>(arg); - return_value.set(object->is_valid()); } template diff --git a/src/js_types.hpp b/src/js_types.hpp index 2147039e..0f2af00e 100644 --- a/src/js_types.hpp +++ b/src/js_types.hpp @@ -104,7 +104,7 @@ struct Value { static return_t validated_to_##type(ContextType ctx, const ValueType &value, const char *name = nullptr) { \ if (!is_##type(ctx, value)) { \ std::string prefix = name ? std::string("'") + name + "'" : "JS value"; \ - throw std::invalid_argument(prefix + " must be: " #type); \ + throw std::invalid_argument(prefix + " must be of type: " #type); \ } \ return to_##type(ctx, value); \ } diff --git a/src/jsc/jsc_class.hpp b/src/jsc/jsc_class.hpp index f60080f6..b37040c4 100644 --- a/src/jsc/jsc_class.hpp +++ b/src/jsc/jsc_class.hpp @@ -23,6 +23,8 @@ #include "js_class.hpp" #include "js_util.hpp" +#include "results.hpp" + namespace realm { namespace jsc { @@ -276,6 +278,10 @@ inline JSValueRef ObjectWrap::get_property(JSContextRef ctx, JSObject // Out-of-bounds index getters should just return undefined in JS. return Value::from_undefined(ctx); } + catch (Results::OutOfBoundsIndexException &) { + // Out-of-bounds index getters should just return undefined in JS. + return Value::from_undefined(ctx); + } catch (std::invalid_argument &) { // Property is not a number. } diff --git a/src/node/node_class.hpp b/src/node/node_class.hpp index 757e6228..596d55a6 100644 --- a/src/node/node_class.hpp +++ b/src/node/node_class.hpp @@ -23,6 +23,8 @@ #include "js_class.hpp" #include "js_util.hpp" +#include "results.hpp" + namespace realm { namespace node { @@ -342,6 +344,10 @@ void wrap(uint32_t index, Nan::NAN_INDEX_GETTER_ARGS_TYPE info) { // Out-of-bounds index getters should just return undefined in JS. return_value.set_undefined(); } + catch (Results::OutOfBoundsIndexException &) { + // Out-of-bounds index getters should just return undefined in JS. + return_value.set_undefined(); + } catch (std::exception &e) { Nan::ThrowError(node::Exception::value(isolate, e)); } diff --git a/src/object-store/src/object_accessor.hpp b/src/object-store/src/object_accessor.hpp index c27d7a52..fc86a62c 100644 --- a/src/object-store/src/object_accessor.hpp +++ b/src/object-store/src/object_accessor.hpp @@ -342,8 +342,9 @@ namespace realm { inline void Object::verify_attached() { if (!m_row.is_attached()) { - throw InvalidatedObjectException("Accessing object of type " + m_object_schema->name + " which has been deleted", - m_object_schema->name); + throw InvalidatedObjectException(m_object_schema->name, + "Accessing object of type " + m_object_schema->name + " which has been deleted" + ); } } diff --git a/tests/js/results-tests.js b/tests/js/results-tests.js index 3470a267..4c31eb28 100644 --- a/tests/js/results-tests.js +++ b/tests/js/results-tests.js @@ -50,6 +50,8 @@ module.exports = BaseTest.extend({ testResultsSubscript: function() { var realm = new Realm({schema: [schemas.PersonObject]}); + TestCase.assertEqual(realm.objects('PersonObject')[0], undefined); + realm.write(function() { realm.create('PersonObject', {name: 'name1', age: 1}); realm.create('PersonObject', {name: 'name2', age: 2});