From 33e0f5d8b1c65931e06c62506f7eda3a642edc98 Mon Sep 17 00:00:00 2001 From: Scott Kyle Date: Mon, 12 Oct 2015 02:02:23 -0700 Subject: [PATCH] Setting properties outside transaction should throw Resolves #40 --- src/RJSObject.mm | 2 +- src/object-store/object_accessor.hpp | 4 ++++ tests/ObjectTests.js | 10 ++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/RJSObject.mm b/src/RJSObject.mm index 09dbfb66..a9324bda 100644 --- a/src/RJSObject.mm +++ b/src/RJSObject.mm @@ -79,7 +79,7 @@ bool ObjectSetProperty(JSContextRef ctx, JSObjectRef jsObject, JSStringRef jsPro Object *obj = RJSGetInternal(jsObject); obj->set_property_value(ctx, RJSStringForJSString(jsPropertyName), value, true); } catch (std::exception &ex) { - if (*exception) { + if (exception) { *exception = RJSMakeError(ctx, ex); } return false; diff --git a/src/object-store/object_accessor.hpp b/src/object-store/object_accessor.hpp index dfb24f69..fa6d6366 100644 --- a/src/object-store/object_accessor.hpp +++ b/src/object-store/object_accessor.hpp @@ -98,6 +98,10 @@ namespace realm { { using Accessor = NativeAccessor; + if (!realm->is_in_transaction()) { + throw std::runtime_error("Can only set property values within a transaction."); + } + size_t column = property.table_column; switch (property.type) { case PropertyTypeBool: diff --git a/tests/ObjectTests.js b/tests/ObjectTests.js index 874731a5..1331e9ad 100644 --- a/tests/ObjectTests.js +++ b/tests/ObjectTests.js @@ -44,6 +44,7 @@ var ObjectTests = { var basicTypesValues = [true, 1, 1.1, 1.11, 'string', new Date(1), 'DATA']; var realm = new Realm({schema: [BasicTypesObjectSchema]}); var obj = null; + realm.write(function() { obj = realm.create('BasicTypesObject', basicTypesValues); obj.boolCol = false; @@ -54,6 +55,11 @@ var ObjectTests = { obj.dateCol = new Date(2); obj.dataCol = 'b'; }); + + TestCase.assertThrows(function() { + obj.boolCol = true; + }, 'can only set property values in a write transaction'); + TestCase.assertEqual(obj.boolCol, false, 'wrong bool value'); TestCase.assertEqual(obj.intCol, 2, 'wrong int value'); TestCase.assertEqualWithTolerance(obj.floatCol, 2.2, 0.000001, 'wrong float value'); @@ -90,6 +96,10 @@ var ObjectTests = { }); TestCase.assertEqual(realm.objects('TestObject').length, 2); + TestCase.assertThrows(function() { + obj.objectCol1 = obj.objectCol; + }, 'can only set property values in a write transaction'); + // set/reuse object property realm.write(function() { obj.objectCol1 = obj.objectCol;