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

View File

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

View File

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