From aff11983f340d1479b3a3ba30916fe5c17a18d3d Mon Sep 17 00:00:00 2001 From: Scott Kyle Date: Wed, 28 Oct 2015 23:18:11 -0700 Subject: [PATCH] Create API that returns constructor JSObjectRef Resolves #98 --- src/RJSUtil.hpp | 2 -- src/RJSUtil.mm | 9 --------- src/RealmJS.h | 1 + src/RealmJS.mm | 28 +++++++++++++++++++++------- tests/RealmJSCoreTests.m | 7 +++---- 5 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/RJSUtil.hpp b/src/RJSUtil.hpp index b4d7a2ea..314c87dd 100644 --- a/src/RJSUtil.hpp +++ b/src/RJSUtil.hpp @@ -41,8 +41,6 @@ JSClassRef RJSCreateWrapperClass(const char * name, JSObjectGetPropertyCallback return JSClassCreate(&classDefinition); } -JSObjectRef RJSRegisterGlobalClass(JSContextRef ctx, JSObjectRef globalObject, JSClassRef classRef, const char * name, JSValueRef *exception); - std::string RJSTypeGet(realm::PropertyType propertyType); std::string RJSTypeGet(std::string typeString); diff --git a/src/RJSUtil.mm b/src/RJSUtil.mm index 85a04515..cd52fa43 100644 --- a/src/RJSUtil.mm +++ b/src/RJSUtil.mm @@ -6,15 +6,6 @@ using namespace realm; -JSObjectRef RJSRegisterGlobalClass(JSContextRef ctx, JSObjectRef globalObject, JSClassRef classRef, const char *name, JSValueRef *exception) { - JSObjectRef classObject = JSObjectMake(ctx, classRef, NULL); - JSStringRef nameString = JSStringCreateWithUTF8CString(name); - JSPropertyAttributes attributes = kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete; - JSObjectSetProperty(ctx, globalObject, nameString, classObject, attributes, exception); - JSStringRelease(nameString); - return classObject; -} - JSValueRef RJSMakeError(JSContextRef ctx, RJSException &exp) { JSValueRef value = exp.exception(); return JSObjectMakeError(ctx, 1, &value, NULL); diff --git a/src/RealmJS.h b/src/RealmJS.h index 83f9e943..0c4ba01d 100644 --- a/src/RealmJS.h +++ b/src/RealmJS.h @@ -8,6 +8,7 @@ extern "C" { #endif +JSObjectRef RJSConstructorCreate(JSContextRef ctx); void RJSInitializeInContext(JSContextRef ctx); void RJSClearTestState(void); diff --git a/src/RealmJS.mm b/src/RealmJS.mm index 63dc2806..913a03c7 100644 --- a/src/RealmJS.mm +++ b/src/RealmJS.mm @@ -47,22 +47,36 @@ static JSValueRef ClearTestState(JSContextRef ctx, JSObjectRef function, JSObjec return NULL; } -void RJSInitializeInContext(JSContextRef ctx) { - JSValueRef exception = NULL; - JSObjectRef globalObject = JSContextGetGlobalObject(ctx); - - JSObjectRef globalRealmObject = RJSRegisterGlobalClass(ctx, globalObject, RJSRealmConstructorClass(), "Realm", &exception); +JSObjectRef RJSConstructorCreate(JSContextRef ctx) { + JSObjectRef realmObject = JSObjectMake(ctx, RJSRealmConstructorClass(), NULL); JSObjectRef typesObject = JSObjectMake(ctx, RJSRealmTypeClass(), NULL); + + JSValueRef exception = NULL; JSStringRef typeString = JSStringCreateWithUTF8CString("Types"); JSPropertyAttributes attributes = kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete; - JSObjectSetProperty(ctx, globalRealmObject, typeString, typesObject, attributes, &exception); + JSObjectSetProperty(ctx, realmObject, typeString, typesObject, attributes, &exception); JSStringRelease(typeString); + assert(!exception); JSStringRef clearTestStateString = JSStringCreateWithUTF8CString("clearTestState"); JSObjectRef clearTestStateFunction = JSObjectMakeFunctionWithCallback(ctx, clearTestStateString, ClearTestState); - JSObjectSetProperty(ctx, globalRealmObject, clearTestStateString, clearTestStateFunction, attributes, &exception); + 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; + + JSObjectSetProperty(ctx, globalObject, nameString, realmObject, attributes, &exception); + JSStringRelease(nameString); assert(!exception); } diff --git a/tests/RealmJSCoreTests.m b/tests/RealmJSCoreTests.m index d8ee6572..1362eaa5 100644 --- a/tests/RealmJSCoreTests.m +++ b/tests/RealmJSCoreTests.m @@ -16,13 +16,12 @@ + (XCTestSuite *)defaultTestSuite { XCTestSuite *suite = [super defaultTestSuite]; JSContext *context = [[JSContext alloc] init]; + JSValue *realmConstructor = [JSValue valueWithJSValueRef:RJSConstructorCreate(context.JSGlobalContextRef) inContext:context]; RJSModuleLoader *moduleLoader = [[RJSModuleLoader alloc] initWithContext:context]; NSURL *scriptURL = [[NSBundle bundleForClass:self] URLForResource:@"index" withExtension:@"js"]; - RJSInitializeInContext(context.JSGlobalContextRef); - - // Expose the global Realm object as a global 'realm' CommonJS module. - [moduleLoader addGlobalModuleObject:context[@"Realm"] forName:@"realm"]; + // Expose the Realm constructor as a global 'realm' CommonJS module. + [moduleLoader addGlobalModuleObject:realmConstructor forName:@"realm"]; NSError *error; JSValue *testObjects = [moduleLoader loadModuleFromURL:scriptURL error:&error];