fix naming

This commit is contained in:
Ari Lazier 2015-10-22 17:59:05 -07:00
parent c3c9cee0ee
commit 8b5a8a731c
3 changed files with 113 additions and 117 deletions

View File

@ -26,8 +26,8 @@ extern "C" {
#import <dlfcn.h>
@interface NSObject (RCTJavaScriptContext)
- (instancetype)initWithJSContext:(JSGlobalContextRef)context;
- (JSGlobalContextRef)ctx;
- (instancetype)initWithJSContext:(JSGlobalContextRef)context;
- (JSGlobalContextRef)ctx;
@end
JSGlobalContextRef RealmReactGetJSGlobalContextForExecutor(id executor, bool create) {
@ -117,7 +117,6 @@ JSGlobalContextRef RealmReactGetJSGlobalContextForExecutor(id executor, bool cre
message:@"Invalid RPC request"];
}
[response setValue:@"http://localhost:8081" forAdditionalHeader:@"Access-Control-Allow-Origin"];
return response;
}];

View File

@ -38,10 +38,10 @@ class RPCServer {
json perform_request(std::string name, json &args);
private:
JSGlobalContextRef _context;
std::map<std::string, RPCRequest> _requests;
std::map<RPCObjectID, JSObjectRef> _objects;
RPCObjectID _sessionID;
JSGlobalContextRef m_context;
std::map<std::string, RPCRequest> m_requests;
std::map<RPCObjectID, JSObjectRef> m_objects;
RPCObjectID m_session_id;
RPCObjectID store_object(JSObjectRef object);

View File

@ -39,27 +39,27 @@ static const char * const RealmObjectTypesNotification = "ObjectTypesNOTIFICATIO
static const char * const RealmObjectTypesResults = "ObjectTypesRESULTS";
RPCServer::RPCServer() {
_context = JSGlobalContextCreate(NULL);
m_context = JSGlobalContextCreate(NULL);
// JavaScriptCore crashes when trying to walk up the native stack to print the stacktrace.
// FIXME: Avoid having to do this!
static void (*setIncludesNativeCallStack)(JSGlobalContextRef, bool) = (void (*)(JSGlobalContextRef, bool))dlsym(RTLD_DEFAULT, "JSGlobalContextSetIncludesNativeCallStackWhenReportingExceptions");
if (setIncludesNativeCallStack) {
setIncludesNativeCallStack(_context, false);
setIncludesNativeCallStack(m_context, false);
}
_requests["/create_session"] = [this](const json dict) {
[RealmJS initializeContext:_context];
m_requests["/create_session"] = [this](const json dict) {
[RealmJS initializeContext:m_context];
JSStringRef realm_string = RJSStringForString("Realm");
JSObjectRef realm_constructor = RJSValidatedObjectProperty(_context, JSContextGetGlobalObject(_context), realm_string);
JSObjectRef realm_constructor = RJSValidatedObjectProperty(m_context, JSContextGetGlobalObject(m_context), realm_string);
JSStringRelease(realm_string);
_sessionID = store_object(realm_constructor);
return (json){{"result", _sessionID}};
m_session_id = store_object(realm_constructor);
return (json){{"result", m_session_id}};
};
_requests["/create_realm"] = [this](const json dict) {
JSObjectRef realm_constructor = _sessionID ? _objects[_sessionID] : NULL;
m_requests["/create_realm"] = [this](const json dict) {
JSObjectRef realm_constructor = m_session_id ? m_objects[m_session_id] : NULL;
if (!realm_constructor) {
throw std::runtime_error("Realm constructor not found!");
}
@ -73,119 +73,116 @@ RPCServer::RPCServer() {
}
JSValueRef exception = NULL;
JSObjectRef realmObject = JSObjectCallAsConstructor(_context, realm_constructor, arg_count, arg_values, &exception);
JSObjectRef realm_object = JSObjectCallAsConstructor(m_context, realm_constructor, arg_count, arg_values, &exception);
if (exception) {
return (json){{"error", RJSStringForValue(_context, exception)}};
return (json){{"error", RJSStringForValue(m_context, exception)}};
}
RPCObjectID realmId = store_object(realmObject);
return (json){{"result", realmId}};
RPCObjectID realm_id = store_object(realm_object);
return (json){{"result", realm_id}};
};
_requests["/begin_transaction"] = [this](const json dict) {
RPCObjectID realmId = dict["realmId"].get<RPCObjectID>();
RJSGetInternal<realm::SharedRealm *>(_objects[realmId])->get()->begin_transaction();
m_requests["/begin_transaction"] = [this](const json dict) {
RPCObjectID realm_id = dict["realmId"].get<RPCObjectID>();
RJSGetInternal<realm::SharedRealm *>(m_objects[realm_id])->get()->begin_transaction();
return json::object();
};
_requests["/cancel_transaction"] = [this](const json dict) {
RPCObjectID realmId = dict["realmId"].get<RPCObjectID>();
RJSGetInternal<realm::SharedRealm *>(_objects[realmId])->get()->cancel_transaction();
m_requests["/cancel_transaction"] = [this](const json dict) {
RPCObjectID realm_id = dict["realmId"].get<RPCObjectID>();
RJSGetInternal<realm::SharedRealm *>(m_objects[realm_id])->get()->cancel_transaction();
return json::object();
};
_requests["/commit_transaction"] = [this](const json dict) {
RPCObjectID realmId = dict["realmId"].get<RPCObjectID>();
RJSGetInternal<realm::SharedRealm *>(_objects[realmId])->get()->commit_transaction();
m_requests["/commit_transaction"] = [this](const json dict) {
RPCObjectID realm_id = dict["realmId"].get<RPCObjectID>();
RJSGetInternal<realm::SharedRealm *>(m_objects[realm_id])->get()->commit_transaction();
return json::object();
};
_requests["/call_method"] = [this](const json dict) {
JSObjectRef object = _objects[dict["id"].get<RPCObjectID>()];
JSStringRef methodString = RJSStringForString(dict["name"].get<std::string>());
JSObjectRef function = RJSValidatedObjectProperty(_context, object, methodString);
JSStringRelease(methodString);
m_requests["/call_method"] = [this](const json dict) {
JSObjectRef object = m_objects[dict["id"].get<RPCObjectID>()];
JSStringRef method_string = RJSStringForString(dict["name"].get<std::string>());
JSObjectRef function = RJSValidatedObjectProperty(m_context, object, method_string);
JSStringRelease(method_string);
json args = dict["arguments"];
size_t argCount = args.size();
JSValueRef argValues[argCount];
for (size_t i = 0; i < argCount; i++) {
argValues[i] = deserialize_json_value(args[i]);
size_t count = args.size();
JSValueRef arg_values[count];
for (size_t i = 0; i < count; i++) {
arg_values[i] = deserialize_json_value(args[i]);
}
JSValueRef exception = NULL;
JSValueRef result = JSObjectCallAsFunction(_context, function, object, argCount, argValues, &exception);
JSValueRef result = JSObjectCallAsFunction(m_context, function, object, count, arg_values, &exception);
if (exception) {
return (json){{"error", RJSStringForValue(_context, exception)}};
return (json){{"error", RJSStringForValue(m_context, exception)}};
}
return (json){{"result", serialize_json_value(result)}};
};
_requests["/get_property"] = [this](const json dict) {
m_requests["/get_property"] = [this](const json dict) {
RPCObjectID oid = dict["id"].get<RPCObjectID>();
json name = dict["name"];
JSValueRef value = NULL;
JSValueRef exception = NULL;
if (name.is_number()) {
value = JSObjectGetPropertyAtIndex(_context, _objects[oid], name.get<unsigned int>(), &exception);
value = JSObjectGetPropertyAtIndex(m_context, m_objects[oid], name.get<unsigned int>(), &exception);
}
else {
JSStringRef propString = RJSStringForString(name.get<std::string>());
value = JSObjectGetProperty(_context, _objects[oid], propString, &exception);
JSStringRelease(propString);
JSStringRef prop_string = RJSStringForString(name.get<std::string>());
value = JSObjectGetProperty(m_context, m_objects[oid], prop_string, &exception);
JSStringRelease(prop_string);
}
if (exception) {
return (json){{"error", RJSStringForValue(_context, exception)}};
return (json){{"error", RJSStringForValue(m_context, exception)}};
}
return (json){{"result", serialize_json_value(value)}};
};
_requests["/set_property"] = [this](const json dict) {
m_requests["/set_property"] = [this](const json dict) {
RPCObjectID oid = dict["id"].get<RPCObjectID>();
json name = dict["name"];
JSValueRef value = deserialize_json_value(dict["value"]);
JSValueRef exception = NULL;
if (name.is_number()) {
JSObjectSetPropertyAtIndex(_context, _objects[oid], name.get<unsigned int>(), value, &exception);
JSObjectSetPropertyAtIndex(m_context, m_objects[oid], name.get<unsigned int>(), value, &exception);
}
else {
JSStringRef propString = RJSStringForString(name.get<std::string>());
JSObjectSetProperty(_context, _objects[oid], propString, value, 0, &exception);
JSStringRelease(propString);
static JSStringRef prop_string = RJSStringForString(name.get<std::string>());
JSObjectSetProperty(m_context, m_objects[oid], prop_string, value, 0, &exception);
}
if (exception) {
return (json){{"error", RJSStringForValue(_context, exception)}};
return (json){{"error", RJSStringForValue(m_context, exception)}};
}
return json::object();
};
_requests["/dispose_object"] = [this](const json dict) {
m_requests["/dispose_object"] = [this](const json dict) {
RPCObjectID oid = dict["id"].get<RPCObjectID>();
JSValueUnprotect(_context, _objects[oid]);
_objects.erase(oid);
JSValueUnprotect(m_context, m_objects[oid]);
m_objects.erase(oid);
return json::object();
};
_requests["/clear_test_state"] = [this](const json dict) {
for (auto object : _objects) {
m_requests["/clear_test_state"] = [this](const json dict) {
for (auto object : m_objects) {
// The session ID points to the Realm constructor object, which should remain.
if (object.first == _sessionID) {
if (object.first == m_session_id) {
continue;
}
JSValueUnprotect(_context, object.second);
_objects.erase(object.first);
JSValueUnprotect(m_context, object.second);
m_objects.erase(object.first);
}
JSGarbageCollect(_context);
JSGarbageCollect(m_context);
[RealmJS clearTestState];
return json::object();
};
}
RPCServer::~RPCServer() {
for (auto item : _objects) {
JSValueUnprotect(_context, item.second);
for (auto item : m_objects) {
JSValueUnprotect(m_context, item.second);
}
JSGlobalContextRelease(_context);
JSGlobalContextRelease(m_context);
}
json RPCServer::perform_request(std::string name, json &args) {
@ -193,10 +190,10 @@ json RPCServer::perform_request(std::string name, json &args) {
__block json response;
dispatch_sync(dispatch_get_main_queue(), ^{
try {
RPCRequest action = _requests[name];
RPCRequest action = m_requests[name];
assert(action);
if (name == "/create_session" || _sessionID == args["sessionId"].get<RPCObjectID>()) {
if (name == "/create_session" || m_session_id == args["sessionId"].get<RPCObjectID>()) {
response = action(args);
assert(response.is_object());
}
@ -213,81 +210,81 @@ json RPCServer::perform_request(std::string name, json &args) {
RPCObjectID RPCServer::store_object(JSObjectRef object) {
static RPCObjectID s_next_id = 1;
RPCObjectID next_id = s_next_id++;
JSValueProtect(_context, object);
_objects[next_id] = object;
JSValueProtect(m_context, object);
m_objects[next_id] = object;
return next_id;
}
json RPCServer::serialize_json_value(JSValueRef value) {
switch (JSValueGetType(_context, value)) {
switch (JSValueGetType(m_context, value)) {
case kJSTypeUndefined:
return json::object();
case kJSTypeNull:
return {{"value", json(nullptr)}};
case kJSTypeBoolean:
return {{"value", JSValueToBoolean(_context, value)}};
return {{"value", JSValueToBoolean(m_context, value)}};
case kJSTypeNumber:
return {{"value", JSValueToNumber(_context, value, NULL)}};
return {{"value", JSValueToNumber(m_context, value, NULL)}};
case kJSTypeString:
return {{"value", RJSStringForValue(_context, value).c_str()}};
return {{"value", RJSStringForValue(m_context, value)}};
case kJSTypeObject:
break;
}
JSObjectRef jsObject = JSValueToObject(_context, value, NULL);
JSObjectRef js_object = JSValueToObject(m_context, value, NULL);
if (JSValueIsObjectOfClass(_context, value, RJSObjectClass())) {
realm::Object *object = RJSGetInternal<realm::Object *>(jsObject);
if (JSValueIsObjectOfClass(m_context, value, RJSObjectClass())) {
realm::Object *object = RJSGetInternal<realm::Object *>(js_object);
return {
{"type", RJSTypeGet(realm::PropertyTypeObject).c_str()},
{"id", store_object(jsObject)},
{"type", RJSTypeGet(realm::PropertyTypeObject)},
{"id", store_object(js_object)},
{"schema", serialize_object_schema(object->object_schema)}
};
}
else if (JSValueIsObjectOfClass(_context, value, RJSListClass())) {
realm::List *list = RJSGetInternal<realm::List *>(jsObject);
else if (JSValueIsObjectOfClass(m_context, value, RJSListClass())) {
realm::List *list = RJSGetInternal<realm::List *>(js_object);
return {
{"type", RJSTypeGet(realm::PropertyTypeArray)},
{"id", store_object(jsObject)},
{"id", store_object(js_object)},
{"size", list->link_view->size()},
{"schema", serialize_object_schema(list->object_schema)}
};
}
else if (JSValueIsObjectOfClass(_context, value, RJSResultsClass())) {
realm::Results *results = RJSGetInternal<realm::Results *>(jsObject);
else if (JSValueIsObjectOfClass(m_context, value, RJSResultsClass())) {
realm::Results *results = RJSGetInternal<realm::Results *>(js_object);
return {
{"type", RealmObjectTypesResults},
{"id", store_object(jsObject)},
{"id", store_object(js_object)},
{"size", results->size()},
{"schema", serialize_object_schema(results->object_schema)}
};
}
else if (JSValueIsObjectOfClass(_context, value, RJSNotificationClass())) {
else if (JSValueIsObjectOfClass(m_context, value, RJSNotificationClass())) {
return {
{"type", RealmObjectTypesNotification},
{"id", store_object(jsObject)},
{"id", store_object(js_object)},
};
}
else if (RJSIsValueArray(_context, value)) {
size_t length = RJSValidatedListLength(_context, jsObject);
else if (RJSIsValueArray(m_context, value)) {
size_t length = RJSValidatedListLength(m_context, js_object);
std::vector<json> array;
for (unsigned int i = 0; i < length; i++) {
array.push_back(serialize_json_value(JSObjectGetPropertyAtIndex(_context, jsObject, i, NULL)));
array.push_back(serialize_json_value(JSObjectGetPropertyAtIndex(m_context, js_object, i, NULL)));
}
return {{"value", array}};
}
else if (RJSIsValueDate(_context, value)) {
else if (RJSIsValueDate(m_context, value)) {
return {
{"type", RJSTypeGet(realm::PropertyTypeDate)},
{"value", RJSValidatedValueToNumber(_context, value)},
{"value", RJSValidatedValueToNumber(m_context, value)},
};
}
assert(0);
}
json RPCServer::serialize_object_schema(realm::ObjectSchema &objectSchema) {
json RPCServer::serialize_object_schema(realm::ObjectSchema &object_schema) {
json properties = json::array();
for (realm::Property prop : objectSchema.properties) {
for (realm::Property prop : object_schema.properties) {
properties.push_back({
{"name", prop.name},
{"type", RJSTypeGet(prop.type)},
@ -295,7 +292,7 @@ json RPCServer::serialize_object_schema(realm::ObjectSchema &objectSchema) {
}
return {
{"name", objectSchema.name},
{"name", object_schema.name},
{"properties", properties},
};
}
@ -304,7 +301,7 @@ JSValueRef RPCServer::deserialize_json_value(const json dict)
{
json oid = dict["id"];
if (oid.is_number()) {
return _objects[oid.get<RPCObjectID>()];
return m_objects[oid.get<RPCObjectID>()];
}
json value = dict["value"];
@ -313,59 +310,59 @@ JSValueRef RPCServer::deserialize_json_value(const json dict)
std::string type_string = type.get<std::string>();
if (type_string == RealmObjectTypesFunction) {
// FIXME: Make this actually call the function by its id once we need it to.
JSStringRef jsBody = JSStringCreateWithUTF8CString("");
JSObjectRef jsFunction = JSObjectMakeFunction(_context, NULL, 0, NULL, jsBody, NULL, 1, NULL);
JSStringRelease(jsBody);
JSStringRef js_body = JSStringCreateWithUTF8CString("");
JSObjectRef js_function = JSObjectMakeFunction(m_context, NULL, 0, NULL, js_body, NULL, 1, NULL);
JSStringRelease(js_body);
return jsFunction;
return js_function;
}
else if (type_string == RJSTypeGet(realm::PropertyTypeDate)) {
JSValueRef exception = NULL;
JSValueRef time = JSValueMakeNumber(_context, value.get<double>());
JSObjectRef date = JSObjectMakeDate(_context, 1, &time, &exception);
JSValueRef time = JSValueMakeNumber(m_context, value.get<double>());
JSObjectRef date = JSObjectMakeDate(m_context, 1, &time, &exception);
if (exception) {
throw RJSException(_context, exception);
throw RJSException(m_context, exception);
}
return date;
}
}
if (value.is_null()) {
return JSValueMakeNull(_context);
return JSValueMakeNull(m_context);
}
else if (value.is_boolean()) {
return JSValueMakeBoolean(_context, value.get<bool>());
return JSValueMakeBoolean(m_context, value.get<bool>());
}
else if (value.is_number()) {
return JSValueMakeNumber(_context, value.get<double>());
return JSValueMakeNumber(m_context, value.get<double>());
}
else if (value.is_string()) {
return RJSValueForString(_context, value.get<std::string>());
return RJSValueForString(m_context, value.get<std::string>());
}
else if (value.is_array()) {
size_t count = value.size();
JSValueRef jsValues[count];
JSValueRef js_values[count];
for (size_t i = 0; i < count; i++) {
jsValues[i] = deserialize_json_value(value.at(i));
js_values[i] = deserialize_json_value(value.at(i));
}
return JSObjectMakeArray(_context, count, jsValues, NULL);
return JSObjectMakeArray(m_context, count, js_values, NULL);
}
else if (value.is_object()) {
JSObjectRef jsObject = JSObjectMake(_context, NULL, NULL);
JSObjectRef js_object = JSObjectMake(m_context, NULL, NULL);
for (json::iterator it = value.begin(); it != value.end(); ++it) {
JSValueRef jsValue = deserialize_json_value(it.value());
JSStringRef jsKey = JSStringCreateWithUTF8CString(it.key().c_str());
JSValueRef js_value = deserialize_json_value(it.value());
JSStringRef js_key = JSStringCreateWithUTF8CString(it.key().c_str());
JSObjectSetProperty(_context, jsObject, jsKey, jsValue, 0, NULL);
JSStringRelease(jsKey);
JSObjectSetProperty(m_context, js_object, js_key, js_value, 0, NULL);
JSStringRelease(js_key);
}
return jsObject;
return js_object;
}
return JSValueMakeUndefined(_context);
return JSValueMakeUndefined(m_context);
}