Create API that returns constructor JSObjectRef

Resolves #98
This commit is contained in:
Scott Kyle 2015-10-28 23:18:11 -07:00
parent 8cb7e43e2e
commit aff11983f3
5 changed files with 25 additions and 22 deletions

View File

@ -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);

View File

@ -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);

View File

@ -8,6 +8,7 @@
extern "C" {
#endif
JSObjectRef RJSConstructorCreate(JSContextRef ctx);
void RJSInitializeInContext(JSContextRef ctx);
void RJSClearTestState(void);

View File

@ -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);
}

View File

@ -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];