ObjectArray -> List

This commit is contained in:
Ari Lazier 2015-10-13 14:41:51 -07:00
parent f4715da2a4
commit 9cb9960af5
4 changed files with 44 additions and 44 deletions

View File

@ -24,60 +24,60 @@
using RJSAccessor = realm::NativeAccessor<JSValueRef, JSContextRef>; using RJSAccessor = realm::NativeAccessor<JSValueRef, JSContextRef>;
using namespace realm; using namespace realm;
size_t ObjectArray::size() { size_t List::size() {
return link_view->size(); return link_view->size();
} }
Row ObjectArray::get(std::size_t row_ndx) { Row List::get(std::size_t row_ndx) {
verify_valid_row(row_ndx); verify_valid_row(row_ndx);
return link_view->get(row_ndx); return link_view->get(row_ndx);
} }
void ObjectArray::set(std::size_t row_ndx, std::size_t target_row_ndx) { void List::set(std::size_t row_ndx, std::size_t target_row_ndx) {
verify_valid_row(row_ndx); verify_valid_row(row_ndx);
link_view->set(row_ndx, target_row_ndx); link_view->set(row_ndx, target_row_ndx);
} }
void ObjectArray::verify_valid_row(std::size_t row_ndx) { void List::verify_valid_row(std::size_t row_ndx) {
size_t size = link_view->size(); size_t size = link_view->size();
if (row_ndx >= size) { if (row_ndx >= size) {
throw std::out_of_range(std::string("Index ") + std::to_string(row_ndx) + " is outside of range 0..." + std::to_string(size) + "."); throw std::out_of_range(std::string("Index ") + std::to_string(row_ndx) + " is outside of range 0..." + std::to_string(size) + ".");
} }
} }
void ObjectArray::verify_attached() { void List::verify_attached() {
if (!link_view->is_attached()) { if (!link_view->is_attached()) {
throw std::runtime_error("Tableview is not attached"); throw std::runtime_error("Tableview is not attached");
} }
link_view->sync_if_needed(); link_view->sync_if_needed();
} }
static inline ObjectArray * RJSVerifiedArray(JSObjectRef object) { static inline List * RJSVerifiedArray(JSObjectRef object) {
ObjectArray *array = RJSGetInternal<ObjectArray *>(object); List *list = RJSGetInternal<List *>(object);
array->verify_attached(); list->verify_attached();
return array; return list;
} }
static inline ObjectArray * RJSVerifiedMutableArray(JSObjectRef object) { static inline List * RJSVerifiedMutableArray(JSObjectRef object) {
ObjectArray *array = RJSVerifiedArray(object); List *list = RJSVerifiedArray(object);
if (!array->realm->is_in_transaction()) { if (!list->realm->is_in_transaction()) {
throw std::runtime_error("Can only mutate lists within a transaction."); throw std::runtime_error("Can only mutate lists within a transaction.");
} }
return array; return list;
} }
JSValueRef ArrayGetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* jsException) { JSValueRef ArrayGetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* jsException) {
try { try {
// index subscripting // index subscripting
ObjectArray *array = RJSVerifiedArray(object); List *list = RJSVerifiedArray(object);
size_t size = array->size(); size_t size = list->size();
std::string indexStr = RJSStringForJSString(propertyName); std::string indexStr = RJSStringForJSString(propertyName);
if (indexStr == "length") { if (indexStr == "length") {
return JSValueMakeNumber(ctx, size); return JSValueMakeNumber(ctx, size);
} }
return RJSObjectCreate(ctx, Object(array->realm, array->object_schema, array->get(RJSValidatedPositiveIndex(indexStr)))); return RJSObjectCreate(ctx, Object(list->realm, list->object_schema, list->get(RJSValidatedPositiveIndex(indexStr))));
} }
catch (std::out_of_range &exp) { catch (std::out_of_range &exp) {
// getters for nonexistent properties in JS should always return undefined // getters for nonexistent properties in JS should always return undefined
@ -97,13 +97,13 @@ JSValueRef ArrayGetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef pr
bool ArraySetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSValueRef* jsException) { bool ArraySetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSValueRef* jsException) {
try { try {
ObjectArray *array = RJSVerifiedMutableArray(object); List *list = RJSVerifiedMutableArray(object);
std::string indexStr = RJSStringForJSString(propertyName); std::string indexStr = RJSStringForJSString(propertyName);
if (indexStr == "length") { if (indexStr == "length") {
throw std::runtime_error("The 'length' property is readonly."); throw std::runtime_error("The 'length' property is readonly.");
} }
array->set(RJSValidatedPositiveIndex(indexStr), RJSAccessor::to_object_index(ctx, array->realm, const_cast<JSValueRef &>(value), array->object_schema.name, false)); list->set(RJSValidatedPositiveIndex(indexStr), RJSAccessor::to_object_index(ctx, list->realm, const_cast<JSValueRef &>(value), list->object_schema.name, false));
return true; return true;
} }
catch (std::invalid_argument &exp) { catch (std::invalid_argument &exp) {
@ -119,8 +119,8 @@ bool ArraySetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef property
} }
void ArrayPropertyNames(JSContextRef ctx, JSObjectRef object, JSPropertyNameAccumulatorRef propertyNames) { void ArrayPropertyNames(JSContextRef ctx, JSObjectRef object, JSPropertyNameAccumulatorRef propertyNames) {
ObjectArray *array = RJSVerifiedArray(object); List *list = RJSVerifiedArray(object);
size_t size = array->size(); size_t size = list->size();
char str[32]; char str[32];
for (size_t i = 0; i < size; i++) { for (size_t i = 0; i < size; i++) {
@ -133,7 +133,7 @@ void ArrayPropertyNames(JSContextRef ctx, JSObjectRef object, JSPropertyNameAccu
JSValueRef ArrayPush(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) { JSValueRef ArrayPush(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) {
try { try {
ObjectArray *array = RJSVerifiedMutableArray(thisObject); List *array = RJSVerifiedMutableArray(thisObject);
RJSValidateArgumentCountIsAtLeast(argumentCount, 1); RJSValidateArgumentCountIsAtLeast(argumentCount, 1);
for (size_t i = 0; i < argumentCount; i++) { for (size_t i = 0; i < argumentCount; i++) {
array->link_view->add(RJSAccessor::to_object_index(ctx, array->realm, const_cast<JSValueRef &>(arguments[i]), array->object_schema.name, false)); array->link_view->add(RJSAccessor::to_object_index(ctx, array->realm, const_cast<JSValueRef &>(arguments[i]), array->object_schema.name, false));
@ -150,16 +150,16 @@ JSValueRef ArrayPush(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObj
JSValueRef ArrayPop(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) { JSValueRef ArrayPop(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) {
try { try {
ObjectArray *array = RJSVerifiedMutableArray(thisObject); List *list = RJSVerifiedMutableArray(thisObject);
RJSValidateArgumentCount(argumentCount, 0); RJSValidateArgumentCount(argumentCount, 0);
size_t size = array->size(); size_t size = list->size();
if (size == 0) { if (size == 0) {
return JSValueMakeUndefined(ctx); return JSValueMakeUndefined(ctx);
} }
size_t index = size - 1; size_t index = size - 1;
JSValueRef obj = RJSObjectCreate(ctx, Object(array->realm, array->object_schema, array->get(index))); JSValueRef obj = RJSObjectCreate(ctx, Object(list->realm, list->object_schema, list->get(index)));
array->link_view->remove(index); list->link_view->remove(index);
return obj; return obj;
} }
catch (std::exception &exp) { catch (std::exception &exp) {
@ -172,7 +172,7 @@ JSValueRef ArrayPop(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObje
JSValueRef ArrayUnshift(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) { JSValueRef ArrayUnshift(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) {
try { try {
ObjectArray *array = RJSVerifiedMutableArray(thisObject); List *array = RJSVerifiedMutableArray(thisObject);
RJSValidateArgumentCountIsAtLeast(argumentCount, 1); RJSValidateArgumentCountIsAtLeast(argumentCount, 1);
for (size_t i = 0; i < argumentCount; i++) { for (size_t i = 0; i < argumentCount; i++) {
array->link_view->insert(i, RJSAccessor::to_object_index(ctx, array->realm, const_cast<JSValueRef &>(arguments[i]), array->object_schema.name, false)); array->link_view->insert(i, RJSAccessor::to_object_index(ctx, array->realm, const_cast<JSValueRef &>(arguments[i]), array->object_schema.name, false));
@ -189,13 +189,13 @@ JSValueRef ArrayUnshift(JSContextRef ctx, JSObjectRef function, JSObjectRef this
JSValueRef ArrayShift(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) { JSValueRef ArrayShift(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) {
try { try {
ObjectArray *array = RJSVerifiedMutableArray(thisObject); List *list = RJSVerifiedMutableArray(thisObject);
RJSValidateArgumentCount(argumentCount, 0); RJSValidateArgumentCount(argumentCount, 0);
if (array->size() == 0) { if (list->size() == 0) {
return JSValueMakeUndefined(ctx); return JSValueMakeUndefined(ctx);
} }
JSValueRef obj = RJSObjectCreate(ctx, Object(array->realm, array->object_schema, array->get(0))); JSValueRef obj = RJSObjectCreate(ctx, Object(list->realm, list->object_schema, list->get(0)));
array->link_view->remove(0); list->link_view->remove(0);
return obj; return obj;
} }
catch (std::exception &exp) { catch (std::exception &exp) {
@ -208,8 +208,8 @@ JSValueRef ArrayShift(JSContextRef ctx, JSObjectRef function, JSObjectRef thisOb
JSValueRef ArraySplice(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) { JSValueRef ArraySplice(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) {
try { try {
ObjectArray *array = RJSVerifiedMutableArray(thisObject); List *list = RJSVerifiedMutableArray(thisObject);
size_t size = array->size(); size_t size = list->size();
RJSValidateArgumentCountIsAtLeast(argumentCount, 2); RJSValidateArgumentCountIsAtLeast(argumentCount, 2);
long index = std::min<long>(RJSValidatedValueToNumber(ctx, arguments[0]), size); long index = std::min<long>(RJSValidatedValueToNumber(ctx, arguments[0]), size);
@ -222,11 +222,11 @@ JSValueRef ArraySplice(JSContextRef ctx, JSObjectRef function, JSObjectRef thisO
std::vector<JSObjectRef> removedObjects(remove); std::vector<JSObjectRef> removedObjects(remove);
for (size_t i = 0; i < remove; i++) { for (size_t i = 0; i < remove; i++) {
removedObjects[i] = RJSObjectCreate(ctx, Object(array->realm, array->object_schema, array->get(index))); removedObjects[i] = RJSObjectCreate(ctx, Object(list->realm, list->object_schema, list->get(index)));
array->link_view->remove(index); list->link_view->remove(index);
} }
for (size_t i = 2; i < argumentCount; i++) { for (size_t i = 2; i < argumentCount; i++) {
array->link_view->insert(index + i - 2, RJSAccessor::to_object_index(ctx, array->realm, const_cast<JSValueRef &>(arguments[i]), array->object_schema.name, false)); list->link_view->insert(index + i - 2, RJSAccessor::to_object_index(ctx, list->realm, const_cast<JSValueRef &>(arguments[i]), list->object_schema.name, false));
} }
return JSObjectMakeArray(ctx, remove, removedObjects.data(), jsException); return JSObjectMakeArray(ctx, remove, removedObjects.data(), jsException);
} }
@ -238,8 +238,8 @@ JSValueRef ArraySplice(JSContextRef ctx, JSObjectRef function, JSObjectRef thisO
return NULL; return NULL;
} }
JSObjectRef RJSArrayCreate(JSContextRef ctx, realm::ObjectArray *array) { JSObjectRef RJSArrayCreate(JSContextRef ctx, realm::List *list) {
return RJSWrapObject<ObjectArray *>(ctx, RJSArrayClass(), array); return RJSWrapObject<List *>(ctx, RJSArrayClass(), list);
} }
const JSStaticFunction RJSArrayFuncs[] = { const JSStaticFunction RJSArrayFuncs[] = {

View File

@ -21,8 +21,8 @@
#import <realm/link_view.hpp> #import <realm/link_view.hpp>
namespace realm { namespace realm {
struct ObjectArray { struct List {
ObjectArray(SharedRealm &r, ObjectSchema &s, LinkViewRef l) : realm(r), object_schema(s), link_view(l) {} List(SharedRealm &r, ObjectSchema &s, LinkViewRef l) : realm(r), object_schema(s), link_view(l) {}
// FIXME - all should be const // FIXME - all should be const
SharedRealm realm; SharedRealm realm;
ObjectSchema &object_schema; ObjectSchema &object_schema;
@ -38,6 +38,6 @@ namespace realm {
extern const JSStaticFunction RJSArrayFuncs[]; extern const JSStaticFunction RJSArrayFuncs[];
JSClassRef RJSArrayClass(); JSClassRef RJSArrayClass();
JSObjectRef RJSArrayCreate(JSContextRef ctx, realm::ObjectArray *array); JSObjectRef RJSArrayCreate(JSContextRef ctx, realm::List *list);
JSValueRef ArrayGetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* jsException); JSValueRef ArrayGetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* jsException);

View File

@ -68,7 +68,7 @@ JSValueRef ObjectGetProperty(JSContextRef ctx, JSObjectRef jsObject, JSStringRef
} }
case PropertyTypeArray: { case PropertyTypeArray: {
auto arrayObjectSchema = obj->realm->config().schema->find(prop->object_type); auto arrayObjectSchema = obj->realm->config().schema->find(prop->object_type);
return RJSArrayCreate(ctx, new ObjectArray(obj->realm, *arrayObjectSchema, static_cast<LinkViewRef>(obj->row.get_linklist(prop->table_column)))); return RJSArrayCreate(ctx, new List(obj->realm, *arrayObjectSchema, static_cast<LinkViewRef>(obj->row.get_linklist(prop->table_column))));
} }
} }
return NULL; return NULL;

View File

@ -250,13 +250,13 @@ using RPCRequest = std::function<NSDictionary *(NSDictionary *dictionary)>;
}; };
} }
else if (JSValueIsObjectOfClass(_context, value, RJSArrayClass())) { else if (JSValueIsObjectOfClass(_context, value, RJSArrayClass())) {
realm::ObjectArray *array = RJSGetInternal<realm::ObjectArray *>(jsObject); realm::List *list = RJSGetInternal<realm::List *>(jsObject);
RPCObjectID oid = [self storeObject:jsObject]; RPCObjectID oid = [self storeObject:jsObject];
return @{ return @{
@"type": @(RJSTypeGet(realm::PropertyTypeArray).c_str()), @"type": @(RJSTypeGet(realm::PropertyTypeArray).c_str()),
@"id": @(oid), @"id": @(oid),
@"size": @(array->link_view->size()), @"size": @(list->link_view->size()),
@"schema": [self objectSchemaToJSONObject:array->object_schema] @"schema": [self objectSchemaToJSONObject:list->object_schema]
}; };
} }
else if (JSValueIsObjectOfClass(_context, value, RJSResultsClass())) { else if (JSValueIsObjectOfClass(_context, value, RJSResultsClass())) {