support getting the schema version from unopened realms

This commit is contained in:
Ari Lazier 2016-03-17 15:49:07 -07:00
parent 071a65b449
commit 324c3d702b
1 changed files with 30 additions and 3 deletions

View File

@ -149,6 +149,13 @@ static bool SetDefaultPath(JSContextRef ctx, JSObjectRef object, JSStringRef pro
return true;
}
inline std::string RJSNormalizePath(std::string path) {
if (path.size() && path[0] != '/') {
return default_realm_file_directory() + "/" + path;
}
return path;
}
JSObjectRef RealmConstructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) {
try {
Realm::Config config;
@ -202,9 +209,7 @@ JSObjectRef RealmConstructor(JSContextRef ctx, JSObjectRef constructor, size_t a
return NULL;
}
if (config.path.size() && config.path[0] != '/') {
config.path = default_realm_file_directory() + "/" + config.path;
}
config.path = RJSNormalizePath(config.path);
ensure_directory_exists_for_file(config.path);
SharedRealm realm = Realm::get_shared_realm(config);
@ -232,6 +237,27 @@ static const JSStaticValue RealmStaticProperties[] = {
{NULL, NULL}
};
JSValueRef RealmSchemaVersion(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) {
try {
RJSValidateArgumentCount(argumentCount, 1);
Realm::Config config;
config.path = RJSNormalizePath(RJSValidatedStringForValue(ctx, arguments[0]));
return JSValueMakeNumber(ctx, Realm::get_schema_version(config));
}
catch (std::exception &exp) {
if (jsException) {
*jsException = RJSMakeError(ctx, exp);
}
}
return NULL;
}
static const JSStaticFunction RealmConstructorFuncs[] = {
{"schemaVersion", RealmSchemaVersion, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{NULL, NULL},
};
JSClassRef RJSRealmConstructorClass() {
JSClassDefinition realmConstructorDefinition = kJSClassDefinitionEmpty;
realmConstructorDefinition.attributes = kJSClassAttributeNoAutomaticPrototype;
@ -239,6 +265,7 @@ JSClassRef RJSRealmConstructorClass() {
realmConstructorDefinition.callAsConstructor = RealmConstructor;
realmConstructorDefinition.hasInstance = RealmHasInstance;
realmConstructorDefinition.staticValues = RealmStaticProperties;
realmConstructorDefinition.staticFunctions = RealmConstructorFuncs;
return JSClassCreate(&realmConstructorDefinition);
}