2015-10-27 20:59:15 +00:00
|
|
|
/* Copyright 2015 Realm Inc - All Rights Reserved
|
|
|
|
* Proprietary and Confidential
|
|
|
|
*/
|
2015-08-13 16:12:48 +00:00
|
|
|
|
2015-11-30 19:47:32 +00:00
|
|
|
#import "js_init.h"
|
2015-11-28 02:36:04 +00:00
|
|
|
#import "js_realm.hpp"
|
|
|
|
#import "js_object.hpp"
|
|
|
|
#import "js_util.hpp"
|
|
|
|
#import "js_schema.hpp"
|
2015-11-24 06:05:06 +00:00
|
|
|
#import "platform.hpp"
|
2015-08-13 16:12:48 +00:00
|
|
|
|
2015-10-15 01:52:55 +00:00
|
|
|
#include "shared_realm.hpp"
|
2015-11-18 22:36:46 +00:00
|
|
|
#include <algorithm>
|
2015-10-15 01:52:55 +00:00
|
|
|
|
2015-11-24 06:05:06 +00:00
|
|
|
extern "C" {
|
|
|
|
|
2015-08-13 16:12:48 +00:00
|
|
|
JSValueRef RJSTypeGet(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception) {
|
2015-11-18 22:36:46 +00:00
|
|
|
std::string str = RJSStringForJSString(propertyName);
|
|
|
|
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
|
|
|
|
return RJSValueForString(ctx, str);
|
2015-08-13 16:12:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSClassRef RJSRealmTypeClass() {
|
|
|
|
JSClassDefinition realmTypesDefinition = kJSClassDefinitionEmpty;
|
2015-09-30 17:41:47 +00:00
|
|
|
realmTypesDefinition.className = "PropTypes";
|
2015-08-13 16:12:48 +00:00
|
|
|
JSStaticValue types[] = {
|
2015-09-30 17:41:47 +00:00
|
|
|
{ "BOOL", RJSTypeGet, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
|
|
|
|
{ "INT", RJSTypeGet, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
|
|
|
|
{ "FLOAT", RJSTypeGet, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
|
|
|
|
{ "DOUBLE", RJSTypeGet, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
|
|
|
|
{ "STRING", RJSTypeGet, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
|
|
|
|
{ "DATE", RJSTypeGet, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
|
|
|
|
{ "DATA", RJSTypeGet, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
|
|
|
|
{ "OBJECT", RJSTypeGet, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
|
2015-09-30 17:51:04 +00:00
|
|
|
{ "LIST", RJSTypeGet, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
|
2015-08-13 16:12:48 +00:00
|
|
|
{ NULL, NULL, NULL, 0 }
|
|
|
|
};
|
|
|
|
realmTypesDefinition.staticValues = types;
|
|
|
|
return JSClassCreate(&realmTypesDefinition);
|
|
|
|
}
|
|
|
|
|
2015-10-16 01:48:13 +00:00
|
|
|
static JSValueRef ClearTestState(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef *exception) {
|
2015-10-26 08:14:33 +00:00
|
|
|
RJSClearTestState();
|
2015-10-15 10:12:28 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-10-29 06:18:11 +00:00
|
|
|
JSObjectRef RJSConstructorCreate(JSContextRef ctx) {
|
|
|
|
JSObjectRef realmObject = JSObjectMake(ctx, RJSRealmConstructorClass(), NULL);
|
2015-09-29 21:50:55 +00:00
|
|
|
JSObjectRef typesObject = JSObjectMake(ctx, RJSRealmTypeClass(), NULL);
|
2015-10-29 06:18:11 +00:00
|
|
|
|
|
|
|
JSValueRef exception = NULL;
|
2015-09-29 22:14:39 +00:00
|
|
|
JSStringRef typeString = JSStringCreateWithUTF8CString("Types");
|
2015-10-15 10:12:28 +00:00
|
|
|
JSPropertyAttributes attributes = kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete;
|
2015-10-29 06:18:11 +00:00
|
|
|
JSObjectSetProperty(ctx, realmObject, typeString, typesObject, attributes, &exception);
|
2015-09-29 21:50:55 +00:00
|
|
|
JSStringRelease(typeString);
|
2015-10-29 06:18:11 +00:00
|
|
|
assert(!exception);
|
2015-09-29 21:50:55 +00:00
|
|
|
|
2015-10-16 01:48:13 +00:00
|
|
|
JSStringRef clearTestStateString = JSStringCreateWithUTF8CString("clearTestState");
|
|
|
|
JSObjectRef clearTestStateFunction = JSObjectMakeFunctionWithCallback(ctx, clearTestStateString, ClearTestState);
|
2015-10-29 06:18:11 +00:00
|
|
|
JSObjectSetProperty(ctx, realmObject, clearTestStateString, clearTestStateFunction, attributes, &exception);
|
2015-10-16 01:48:13 +00:00
|
|
|
JSStringRelease(clearTestStateString);
|
2015-10-29 06:18:11 +00:00
|
|
|
assert(!exception);
|
|
|
|
|
|
|
|
return realmObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RJSInitializeInContext(JSContextRef ctx) {
|
|
|
|
JSObjectRef globalObject = JSContextGetGlobalObject(ctx);
|
|
|
|
JSObjectRef realmObject = RJSConstructorCreate(ctx);
|
|
|
|
|
|
|
|
JSValueRef exception = NULL;
|
|
|
|
JSStringRef nameString = JSStringCreateWithUTF8CString("Realm");
|
|
|
|
JSPropertyAttributes attributes = kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete;
|
2015-10-15 01:52:55 +00:00
|
|
|
|
2015-10-29 06:18:11 +00:00
|
|
|
JSObjectSetProperty(ctx, globalObject, nameString, realmObject, attributes, &exception);
|
|
|
|
JSStringRelease(nameString);
|
2015-08-13 16:12:48 +00:00
|
|
|
assert(!exception);
|
|
|
|
}
|
|
|
|
|
2015-10-26 08:14:33 +00:00
|
|
|
void RJSClearTestState() {
|
2015-10-15 01:52:55 +00:00
|
|
|
realm::Realm::s_global_cache.clear();
|
2015-11-30 03:09:17 +00:00
|
|
|
realm::remove_realm_files_from_directory(realm::default_realm_file_directory());
|
2015-11-24 06:05:06 +00:00
|
|
|
}
|
2015-10-15 01:52:55 +00:00
|
|
|
|
|
|
|
}
|