From 8b5a8a731cd602a13c8145296f194b6f177a5fa5 Mon Sep 17 00:00:00 2001 From: Ari Lazier Date: Thu, 22 Oct 2015 17:59:05 -0700 Subject: [PATCH] fix naming --- ReactNative/RealmReact.mm | 5 +- src/RealmRPC.hpp | 8 +- src/RealmRPC.mm | 217 +++++++++++++++++++------------------- 3 files changed, 113 insertions(+), 117 deletions(-) diff --git a/ReactNative/RealmReact.mm b/ReactNative/RealmReact.mm index ab496eeb..2a50d1ce 100644 --- a/ReactNative/RealmReact.mm +++ b/ReactNative/RealmReact.mm @@ -26,8 +26,8 @@ extern "C" { #import @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; }]; diff --git a/src/RealmRPC.hpp b/src/RealmRPC.hpp index 8aec9f4e..69a8b02f 100644 --- a/src/RealmRPC.hpp +++ b/src/RealmRPC.hpp @@ -38,10 +38,10 @@ class RPCServer { json perform_request(std::string name, json &args); private: - JSGlobalContextRef _context; - std::map _requests; - std::map _objects; - RPCObjectID _sessionID; + JSGlobalContextRef m_context; + std::map m_requests; + std::map m_objects; + RPCObjectID m_session_id; RPCObjectID store_object(JSObjectRef object); diff --git a/src/RealmRPC.mm b/src/RealmRPC.mm index 277aacbc..3eabb593 100644 --- a/src/RealmRPC.mm +++ b/src/RealmRPC.mm @@ -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(); - RJSGetInternal(_objects[realmId])->get()->begin_transaction(); + m_requests["/begin_transaction"] = [this](const json dict) { + RPCObjectID realm_id = dict["realmId"].get(); + RJSGetInternal(m_objects[realm_id])->get()->begin_transaction(); return json::object(); }; - _requests["/cancel_transaction"] = [this](const json dict) { - RPCObjectID realmId = dict["realmId"].get(); - RJSGetInternal(_objects[realmId])->get()->cancel_transaction(); + m_requests["/cancel_transaction"] = [this](const json dict) { + RPCObjectID realm_id = dict["realmId"].get(); + RJSGetInternal(m_objects[realm_id])->get()->cancel_transaction(); return json::object(); }; - _requests["/commit_transaction"] = [this](const json dict) { - RPCObjectID realmId = dict["realmId"].get(); - RJSGetInternal(_objects[realmId])->get()->commit_transaction(); + m_requests["/commit_transaction"] = [this](const json dict) { + RPCObjectID realm_id = dict["realmId"].get(); + RJSGetInternal(m_objects[realm_id])->get()->commit_transaction(); return json::object(); }; - _requests["/call_method"] = [this](const json dict) { - JSObjectRef object = _objects[dict["id"].get()]; - JSStringRef methodString = RJSStringForString(dict["name"].get()); - JSObjectRef function = RJSValidatedObjectProperty(_context, object, methodString); - JSStringRelease(methodString); + m_requests["/call_method"] = [this](const json dict) { + JSObjectRef object = m_objects[dict["id"].get()]; + JSStringRef method_string = RJSStringForString(dict["name"].get()); + 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(); json name = dict["name"]; JSValueRef value = NULL; JSValueRef exception = NULL; if (name.is_number()) { - value = JSObjectGetPropertyAtIndex(_context, _objects[oid], name.get(), &exception); + value = JSObjectGetPropertyAtIndex(m_context, m_objects[oid], name.get(), &exception); } else { - JSStringRef propString = RJSStringForString(name.get()); - value = JSObjectGetProperty(_context, _objects[oid], propString, &exception); - JSStringRelease(propString); + JSStringRef prop_string = RJSStringForString(name.get()); + 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(); json name = dict["name"]; JSValueRef value = deserialize_json_value(dict["value"]); JSValueRef exception = NULL; if (name.is_number()) { - JSObjectSetPropertyAtIndex(_context, _objects[oid], name.get(), value, &exception); + JSObjectSetPropertyAtIndex(m_context, m_objects[oid], name.get(), value, &exception); } else { - JSStringRef propString = RJSStringForString(name.get()); - JSObjectSetProperty(_context, _objects[oid], propString, value, 0, &exception); - JSStringRelease(propString); + static JSStringRef prop_string = RJSStringForString(name.get()); + 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(); - 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()) { + if (name == "/create_session" || m_session_id == args["sessionId"].get()) { 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(jsObject); + if (JSValueIsObjectOfClass(m_context, value, RJSObjectClass())) { + realm::Object *object = RJSGetInternal(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(jsObject); + else if (JSValueIsObjectOfClass(m_context, value, RJSListClass())) { + realm::List *list = RJSGetInternal(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(jsObject); + else if (JSValueIsObjectOfClass(m_context, value, RJSResultsClass())) { + realm::Results *results = RJSGetInternal(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 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()]; + return m_objects[oid.get()]; } json value = dict["value"]; @@ -313,59 +310,59 @@ JSValueRef RPCServer::deserialize_json_value(const json dict) std::string type_string = type.get(); 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()); - JSObjectRef date = JSObjectMakeDate(_context, 1, &time, &exception); + JSValueRef time = JSValueMakeNumber(m_context, value.get()); + 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()); + return JSValueMakeBoolean(m_context, value.get()); } else if (value.is_number()) { - return JSValueMakeNumber(_context, value.get()); + return JSValueMakeNumber(m_context, value.get()); } else if (value.is_string()) { - return RJSValueForString(_context, value.get()); + return RJSValueForString(m_context, value.get()); } 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); }