list methods

This commit is contained in:
Ari Lazier 2015-10-07 21:37:51 -06:00
parent 696d6c6f0e
commit d8e7c89478
2 changed files with 56 additions and 9 deletions

View File

@ -92,9 +92,7 @@ static JSGlobalContextRef s_context;
if (exception) { if (exception) {
return @{@"error": @(RJSStringForValue(s_context, exception).c_str())}; return @{@"error": @(RJSStringForValue(s_context, exception).c_str())};
} }
return @{@"result": [self resultForJSValue:propertyValue]};
realm::Property *prop = RJSGetInternal<realm::Object *>(s_objects[objectId])->object_schema.property_for_name(name.UTF8String);
return @{@"result": [self resultForJSValue:propertyValue type:prop->type]};
}; };
s_requests["/set_property"] = [=](NSDictionary *dict) { s_requests["/set_property"] = [=](NSDictionary *dict) {
JSValueRef exception = NULL; JSValueRef exception = NULL;
@ -152,7 +150,7 @@ static JSGlobalContextRef s_context;
return @{@"error": @(RJSStringForValue(s_context, exception).c_str())}; return @{@"error": @(RJSStringForValue(s_context, exception).c_str())};
} }
return @{@"result": [self resultForJSValue:objectValue type:realm::PropertyTypeObject]}; return @{@"result": [self resultForJSValue:objectValue]};
}; };
s_requests["/get_list_size"] = [=](NSDictionary *dict) { s_requests["/get_list_size"] = [=](NSDictionary *dict) {
RPCObjectID listId = [dict[@"listId"] longValue]; RPCObjectID listId = [dict[@"listId"] longValue];
@ -175,7 +173,44 @@ static JSGlobalContextRef s_context;
return @{@"error": @(RJSStringForValue(s_context, exception).c_str())}; return @{@"error": @(RJSStringForValue(s_context, exception).c_str())};
} }
return @{@"result": [self resultForJSValue:objectValue type:realm::PropertyTypeObject]}; return @{@"result": [self resultForJSValue:objectValue]};
};
s_requests["/call_list_method"] = [=](NSDictionary *dict) {
NSString *name = dict[@"name"];
RPCObjectID listId = [dict[@"listId"] longValue];
JSValueRef exception = NULL;
NSArray *arguments = dict[@"arguments"];
JSValueRef argumentValues[arguments.count];
JSContext *context = [JSContext contextWithJSGlobalContextRef:s_context];
for (int i = 0; i < arguments.count; i++) {
argumentValues[i] = [JSValue valueWithObject:arguments[i] inContext:context].JSValueRef;
}
JSValueRef ret;
if ([name isEqualToString:@"push"]) {
ret = ArrayPush(s_context, NULL, s_objects[listId], arguments.count, argumentValues, &exception);
}
else if ([name isEqualToString:@"pop"]) {
ret = ArrayPop(s_context, NULL, s_objects[listId], arguments.count, argumentValues, &exception);
}
else if ([name isEqualToString:@"shift"]) {
ret = ArrayShift(s_context, NULL, s_objects[listId], arguments.count, argumentValues, &exception);
}
else if ([name isEqualToString:@"unshift"]) {
ret = ArrayUnshift(s_context, NULL, s_objects[listId], arguments.count, argumentValues, &exception);
}
else if ([name isEqualToString:@"splice"]) {
ret = ArraySplice(s_context, NULL, s_objects[listId], arguments.count, argumentValues, &exception);
}
else {
return @{@"error": @"invalid command"};
}
if (exception) {
return @{@"error": @(RJSStringForValue(s_context, exception).c_str())};
}
return @{@"result": [self resultForJSValue:ret]};
}; };
// Add a handler to respond to GET requests on any URL // Add a handler to respond to GET requests on any URL
@ -193,7 +228,7 @@ static JSGlobalContextRef s_context;
[response setValue:@"http://localhost:8081" forAdditionalHeader:@"Access-Control-Allow-Origin"]; [response setValue:@"http://localhost:8081" forAdditionalHeader:@"Access-Control-Allow-Origin"];
return response; return response;
}]; }];
[webServer startWithPort:8082 bonjourName:nil]; [webServer startWithPort:8082 bonjourName:nil];
} }
@ -205,7 +240,7 @@ static JSGlobalContextRef s_context;
return next_id; return next_id;
} }
+ (NSDictionary *)resultForJSValue:(JSValueRef)value type:(realm::PropertyType)type { + (NSDictionary *)resultForJSValue:(JSValueRef)value {
switch (JSValueGetType(s_context, value)) { switch (JSValueGetType(s_context, value)) {
case kJSTypeUndefined: case kJSTypeUndefined:
return @{}; return @{};
@ -224,7 +259,7 @@ static JSGlobalContextRef s_context;
JSObjectRef jsObject = JSValueToObject(s_context, value, NULL); JSObjectRef jsObject = JSValueToObject(s_context, value, NULL);
RPCObjectID oid = [self storeObject:jsObject]; RPCObjectID oid = [self storeObject:jsObject];
if (type == realm::PropertyTypeObject) { if (JSValueIsObjectOfClass(s_context, value, RJSObjectClass())) {
realm::Object *object = RJSGetInternal<realm::Object *>(jsObject); realm::Object *object = RJSGetInternal<realm::Object *>(jsObject);
return @{ return @{
@"type": @(RJSTypeGet(realm::PropertyTypeObject).c_str()), @"type": @(RJSTypeGet(realm::PropertyTypeObject).c_str()),
@ -232,7 +267,7 @@ static JSGlobalContextRef s_context;
@"schema": [self objectSchemaToJSONObject:object->object_schema] @"schema": [self objectSchemaToJSONObject:object->object_schema]
}; };
} }
else { else if (JSValueIsObjectOfClass(s_context, value, RJSArrayClass())) {
realm::ObjectArray *array = RJSGetInternal<realm::ObjectArray *>(jsObject); realm::ObjectArray *array = RJSGetInternal<realm::ObjectArray *>(jsObject);
return @{ return @{
@"type": @(RJSTypeGet(realm::PropertyTypeArray).c_str()), @"type": @(RJSTypeGet(realm::PropertyTypeArray).c_str()),
@ -240,6 +275,12 @@ static JSGlobalContextRef s_context;
@"schema": [self objectSchemaToJSONObject:array->object_schema] @"schema": [self objectSchemaToJSONObject:array->object_schema]
}; };
} }
else if (RJSIsValueArray(s_context, value)) {
assert(0);
}
else {
assert(0);
}
} }
+ (NSDictionary *)objectSchemaToJSONObject:(realm::ObjectSchema &)objectSchema { + (NSDictionary *)objectSchemaToJSONObject:(realm::ObjectSchema &)objectSchema {

View File

@ -38,3 +38,9 @@ JSClassRef RJSArrayClass();
JSObjectRef RJSArrayCreate(JSContextRef ctx, realm::ObjectArray *array); JSObjectRef RJSArrayCreate(JSContextRef ctx, realm::ObjectArray *array);
JSValueRef ArrayGetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* jsException); JSValueRef ArrayGetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* jsException);
JSValueRef ArrayPush(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);
JSValueRef ArrayUnshift(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);
JSValueRef ArraySplice(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException);