realm-js/src/js_init.cpp

86 lines
3.7 KiB
C++
Raw Normal View History

/* 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"
#import "platform.hpp"
2015-08-13 16:12:48 +00:00
2015-10-15 01:52:55 +00:00
#include "shared_realm.hpp"
#include <algorithm>
2015-10-15 01:52:55 +00:00
extern "C" {
2015-08-13 16:12:48 +00:00
JSValueRef RJSTypeGet(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception) {
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;
realmTypesDefinition.className = "PropTypes";
2015-08-13 16:12:48 +00:00
JSStaticValue types[] = {
{ "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 },
{ "LIST", RJSTypeGet, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
2015-08-13 16:12:48 +00:00
{ NULL, NULL, NULL, 0 }
};
realmTypesDefinition.staticValues = types;
return JSClassCreate(&realmTypesDefinition);
}
static JSValueRef ClearTestState(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef *exception) {
RJSClearTestState();
return NULL;
}
JSObjectRef RJSConstructorCreate(JSContextRef ctx) {
JSObjectRef realmObject = JSObjectMake(ctx, RJSRealmConstructorClass(), NULL);
2015-09-29 21:50:55 +00:00
JSObjectRef typesObject = JSObjectMake(ctx, RJSRealmTypeClass(), NULL);
JSValueRef exception = NULL;
2015-09-29 22:14:39 +00:00
JSStringRef typeString = JSStringCreateWithUTF8CString("Types");
JSPropertyAttributes attributes = kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete;
JSObjectSetProperty(ctx, realmObject, typeString, typesObject, attributes, &exception);
2015-09-29 21:50:55 +00:00
JSStringRelease(typeString);
assert(!exception);
2015-09-29 21:50:55 +00:00
JSStringRef clearTestStateString = JSStringCreateWithUTF8CString("clearTestState");
JSObjectRef clearTestStateFunction = JSObjectMakeFunctionWithCallback(ctx, clearTestStateString, ClearTestState);
JSObjectSetProperty(ctx, realmObject, clearTestStateString, clearTestStateFunction, attributes, &exception);
JSStringRelease(clearTestStateString);
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
JSObjectSetProperty(ctx, globalObject, nameString, realmObject, attributes, &exception);
JSStringRelease(nameString);
2015-08-13 16:12:48 +00:00
assert(!exception);
}
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-10-15 01:52:55 +00:00
}