Error when assigning to Result index or length

Added some tests. Turns out our Chrome layer already did this too, so no changes were needed there.

Resolves #70
This commit is contained in:
Scott Kyle 2015-10-21 15:25:15 -07:00
parent d50e3ae68c
commit bd2d1559d1
2 changed files with 43 additions and 1 deletions

View File

@ -52,6 +52,27 @@ JSValueRef ResultsGetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef
}
}
bool ResultsSetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSValueRef *jsException) {
try {
std::string indexStr = RJSStringForJSString(propertyName);
if (indexStr != "length") {
std::stol(RJSStringForJSString(propertyName));
}
// attempts to assign to 'length' or an index should throw an exception
throw std::runtime_error("Results objects are readonly");
}
catch (std::invalid_argument &exp) {
// for stol failure this could be another property that is handled externally, so ignore
}
catch (std::exception &exp) {
if (jsException) {
*jsException = RJSMakeError(ctx, exp);
}
}
return false;
}
void ResultsPropertyNames(JSContextRef ctx, JSObjectRef object, JSPropertyNameAccumulatorRef propertyNames) {
Results *results = RJSGetInternal<Results *>(object);
char str[32];
@ -124,6 +145,6 @@ static const JSStaticFunction RJSResultsFuncs[] = {
};
JSClassRef RJSResultsClass() {
static JSClassRef s_objectClass = RJSCreateWrapperClass<Results *>("Results", ResultsGetProperty, NULL, RJSResultsFuncs, ResultsPropertyNames);
static JSClassRef s_objectClass = RJSCreateWrapperClass<Results *>("Results", ResultsGetProperty, ResultsSetProperty, RJSResultsFuncs, ResultsPropertyNames);
return s_objectClass;
}

View File

@ -50,6 +50,27 @@ module.exports = BaseTest.extend({
TestCase.assertTrue(Object.getPrototypeOf(people[0]) === schemas.PersonObject.prototype);
TestCase.assertTrue(people[0] instanceof schemas.PersonObject);
},
testResultsReadonly: function() {
var realm = new Realm({schema: [schemas.TestObject]});
var objects = realm.objects('TestObject');
realm.write(function() {
realm.create('TestObject', [1]);
});
TestCase.assertThrows(function() {
objects[-1] = [0];
});
TestCase.assertThrows(function() {
objects[0] = [0];
});
TestCase.assertThrows(function() {
objects[1] = [0];
});
TestCase.assertThrows(function() {
objects.length = 0;
});
},
testResultsInvalidProperty: function() {
var realm = new Realm({schema: [schemas.TestObject]});
var objects = realm.objects('TestObject');