From 15052985f190713f221cab6e097b47b6a512b005 Mon Sep 17 00:00:00 2001 From: Scott Kyle Date: Mon, 2 Nov 2015 22:16:50 -0800 Subject: [PATCH] Remove confusion between propTypes and objectTypes The RPC layer now only speaks in objectTypes, since they don't always equate to propTypes. We were overloading the use of propTypes for no good purpose. --- lib/constants.js | 7 ++++++- lib/lists.js | 6 ++++-- lib/realm.js | 4 ++-- lib/rpc.js | 18 ++++++++---------- src/js_util.cpp | 15 --------------- src/js_util.hpp | 2 -- src/rpc.cpp | 25 +++++++++++++++---------- 7 files changed, 35 insertions(+), 42 deletions(-) diff --git a/lib/constants.js b/lib/constants.js index ff538a0c..841dc5b8 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -17,13 +17,18 @@ let propTypes = {}; }); [ + 'DATA', + 'DATE', 'DICT', 'FUNCTION', + 'LIST', + 'OBJECT', 'REALM', 'RESULTS', + 'UNDEFINED', ].forEach(function(type) { Object.defineProperty(objectTypes, type, { - value: 'ObjectTypes' + type, + value: type.toLowerCase(), }); }); diff --git a/lib/lists.js b/lib/lists.js index 5232f843..3be63612 100644 --- a/lib/lists.js +++ b/lib/lists.js @@ -7,6 +7,8 @@ const constants = require('./constants'); const util = require('./util'); +const {objectTypes} = constants; + module.exports = { create, }; @@ -14,12 +16,12 @@ module.exports = { class List {} // Non-mutating methods: -util.createMethods(List.prototype, constants.propTypes.LIST, [ +util.createMethods(List.prototype, objectTypes.LIST, [ 'snapshot', ]); // Mutating methods: -util.createMethods(List.prototype, constants.propTypes.LIST, [ +util.createMethods(List.prototype, objectTypes.LIST, [ 'pop', 'shift', 'push', diff --git a/lib/realm.js b/lib/realm.js index aecc56de..608e0b08 100644 --- a/lib/realm.js +++ b/lib/realm.js @@ -14,8 +14,8 @@ const util = require('./util'); const {keys, propTypes, objectTypes} = constants; const listenersKey = Symbol(); -rpc.registerTypeConverter(propTypes.LIST, lists.create); -rpc.registerTypeConverter(propTypes.OBJECT, objects.create); +rpc.registerTypeConverter(objectTypes.LIST, lists.create); +rpc.registerTypeConverter(objectTypes.OBJECT, objects.create); rpc.registerTypeConverter(objectTypes.RESULTS, results.create); class Realm { diff --git a/lib/rpc.js b/lib/rpc.js index 43fb140b..00ab8d16 100644 --- a/lib/rpc.js +++ b/lib/rpc.js @@ -9,7 +9,7 @@ const constants = require('./constants'); const DEVICE_HOST = 'localhost:8082'; -const {keys, objectTypes, propTypes} = constants; +const {keys, objectTypes} = constants; const {id: idKey, realm: realmKey} = keys; const typeConverters = {}; @@ -39,8 +39,8 @@ module.exports = { clearTestState, }; -registerTypeConverter(propTypes.DATA, (_, {value}) => base64.decode(value)); -registerTypeConverter(propTypes.DATE, (_, {value}) => new Date(value)); +registerTypeConverter(objectTypes.DATA, (_, {value}) => base64.decode(value)); +registerTypeConverter(objectTypes.DATE, (_, {value}) => new Date(value)); registerTypeConverter(objectTypes.DICT, deserializeDict); function registerTypeConverter(type, handler) { @@ -96,14 +96,12 @@ function clearTestState() { } function serialize(realmId, value) { + if (typeof value == 'undefined') { + return {type: objectTypes.UNDEFINED}; + } if (typeof value == 'function') { return {type: objectTypes.FUNCTION}; } - - if (typeof value === 'undefined') { - return {type: 'undefined'}; - } - if (!value || typeof value != 'object') { return {value: value}; } @@ -118,7 +116,7 @@ function serialize(realmId, value) { } if (value instanceof Date) { - return {type: propTypes.DATE, value: value.getTime()}; + return {type: objectTypes.DATE, value: value.getTime()}; } if (Array.isArray(value)) { @@ -127,7 +125,7 @@ function serialize(realmId, value) { } if (value instanceof ArrayBuffer || ArrayBuffer.isView(value)) { - return {type: propTypes.DATA, value: base64.encode(value)}; + return {type: objectTypes.DATA, value: base64.encode(value)}; } let keys = Object.keys(value); diff --git a/src/js_util.cpp b/src/js_util.cpp index c9c9a96c..ebe71c63 100644 --- a/src/js_util.cpp +++ b/src/js_util.cpp @@ -23,21 +23,6 @@ JSValueRef RJSMakeError(JSContextRef ctx, const std::string &message) { return JSObjectMakeError(ctx, 1, &value, NULL); } -std::string RJSTypeGet(PropertyType propertyType) { - switch (propertyType) { - case PropertyTypeBool: return "bool"; - case PropertyTypeInt: return "int"; - case PropertyTypeFloat: return "float"; - case PropertyTypeDouble:return "double"; - case PropertyTypeString:return "string"; - case PropertyTypeDate: return "date"; - case PropertyTypeData: return "data"; - case PropertyTypeObject:return "object"; - case PropertyTypeArray: return "list"; - default: return nullptr; - } -} - std::string RJSStringForJSString(JSStringRef jsString) { std::string str; size_t maxSize = JSStringGetMaximumUTF8CStringSize(jsString); diff --git a/src/js_util.hpp b/src/js_util.hpp index c530e4e5..000dcf78 100644 --- a/src/js_util.hpp +++ b/src/js_util.hpp @@ -41,8 +41,6 @@ JSClassRef RJSCreateWrapperClass(const char * name, JSObjectGetPropertyCallback return JSClassCreate(&classDefinition); } -std::string RJSTypeGet(realm::PropertyType propertyType); - std::string RJSStringForJSString(JSStringRef jsString); std::string RJSStringForValue(JSContextRef ctx, JSValueRef value); std::string RJSValidatedStringForValue(JSContextRef ctx, JSValueRef value, const char * name = nullptr); diff --git a/src/rpc.cpp b/src/rpc.cpp index c7c024c5..db35c3df 100644 --- a/src/rpc.cpp +++ b/src/rpc.cpp @@ -22,9 +22,14 @@ using RJSAccessor = realm::NativeAccessor; using namespace realm_js; -static const char * const RealmObjectTypesDictionary = "ObjectTypesDICT"; -static const char * const RealmObjectTypesFunction = "ObjectTypesFUNCTION"; -static const char * const RealmObjectTypesResults = "ObjectTypesRESULTS"; +static const char * const RealmObjectTypesData = "data"; +static const char * const RealmObjectTypesDate = "date"; +static const char * const RealmObjectTypesDictionary = "dict"; +static const char * const RealmObjectTypesFunction = "function"; +static const char * const RealmObjectTypesList = "list"; +static const char * const RealmObjectTypesObject = "object"; +static const char * const RealmObjectTypesResults = "results"; +static const char * const RealmObjectTypesUndefined = "undefined"; RPCServer::RPCServer() { m_context = JSGlobalContextCreate(NULL); @@ -219,7 +224,7 @@ json RPCServer::serialize_json_value(JSValueRef value) { if (JSValueIsObjectOfClass(m_context, value, RJSObjectClass())) { realm::Object *object = RJSGetInternal(js_object); return { - {"type", RJSTypeGet(realm::PropertyTypeObject)}, + {"type", RealmObjectTypesObject}, {"id", store_object(js_object)}, {"schema", serialize_object_schema(object->get_object_schema())} }; @@ -227,7 +232,7 @@ json RPCServer::serialize_json_value(JSValueRef value) { else if (JSValueIsObjectOfClass(m_context, value, RJSListClass())) { realm::List *list = RJSGetInternal(js_object); return { - {"type", RJSTypeGet(realm::PropertyTypeArray)}, + {"type", RealmObjectTypesList}, {"id", store_object(js_object)}, {"size", list->size()}, {"schema", serialize_object_schema(list->get_object_schema())} @@ -253,13 +258,13 @@ json RPCServer::serialize_json_value(JSValueRef value) { else if (RJSIsValueArrayBuffer(m_context, value)) { std::string data = RJSAccessor::to_binary(m_context, value); return { - {"type", RJSTypeGet(realm::PropertyTypeData)}, + {"type", RealmObjectTypesData}, {"value", base64_encode((unsigned char *)data.data(), data.size())}, }; } else if (RJSIsValueDate(m_context, value)) { return { - {"type", RJSTypeGet(realm::PropertyTypeDate)}, + {"type", RealmObjectTypesDate}, {"value", RJSValidatedValueToNumber(m_context, value)}, }; } @@ -336,14 +341,14 @@ JSValueRef RPCServer::deserialize_json_value(const json dict) return js_object; } - else if (type_string == RJSTypeGet(realm::PropertyTypeData)) { + else if (type_string == RealmObjectTypesData) { std::string bytes; if (!base64_decode(value.get(), &bytes)) { throw std::runtime_error("Failed to decode base64 encoded data"); } return RJSAccessor::from_binary(m_context, realm::BinaryData(bytes)); } - else if (type_string == RJSTypeGet(realm::PropertyTypeDate)) { + else if (type_string == RealmObjectTypesDate) { JSValueRef exception = NULL; JSValueRef time = JSValueMakeNumber(m_context, value.get()); JSObjectRef date = JSObjectMakeDate(m_context, 1, &time, &exception); @@ -353,7 +358,7 @@ JSValueRef RPCServer::deserialize_json_value(const json dict) } return date; } - else if (type_string == "undefined") { + else if (type_string == RealmObjectTypesUndefined) { return JSValueMakeUndefined(m_context); } assert(0);