diff --git a/iOS/src/GBYManager.h b/iOS/src/GBYManager.h index 5c5ed07..ebdf4c2 100644 --- a/iOS/src/GBYManager.h +++ b/iOS/src/GBYManager.h @@ -11,7 +11,8 @@ /** Initializes this `GBYManager`. Calls the supplied initialization function in the - specified namespace. Internally creates an instance of `JSContext`. + specified namespace. Internally creates an instance of `JSContext`, loading + `main.js` from the bundle. @param initFnName The initialization function name. @param namespace The namespace of the initialization function. @@ -21,7 +22,7 @@ /** Initializes this `GBYManager`. Calls the supplied initialization function in the - specified namespace. + specified namespace, loading `main.js` from the bundle. @param initFnName The initialization function name. @param namespace The namespace of the initialization function. @@ -30,6 +31,30 @@ */ - (id)initWithInitFnName:(NSString*)initFnName inNamespace:(NSString*)namespace withContext:(JSContext*)context; +/** + Initializes this `GBYManager`. Calls the supplied initialization function in the + specified namespace, Internally creates an instance of `JSContext`, optionally loading JavaScript. + + @param initFnName The initialization function name. + @param namespace The namespace of the initialization function. + @param context The JavaScriptCore context to use. + @param path Path to optinal JavaScript to load. (Skipped if `nil`.) + @return This `GBYManager` instance. + */ +- (id)initWithInitFnName:(NSString*)initFnName inNamespace:(NSString*)namespace loadingJavaScript:(NSString*)path; + +/** + Initializes this `GBYManager`. Calls the supplied initialization function in the + specified namespace, optionally loading JavaScript. + + @param initFnName The initialization function name. + @param namespace The namespace of the initialization function. + @param context The JavaScriptCore context to use. + @param path Path to optinal JavaScript to load. (Skipped if `nil`.) + @return This `GBYManager` instance. + */ +- (id)initWithInitFnName:(NSString*)initFnName inNamespace:(NSString*)namespace withContext:(JSContext*)context loadingJavaScript:(NSString*)path; + /** Gets the value for a name in a namespace. diff --git a/iOS/src/GBYManager.m b/iOS/src/GBYManager.m index b99e26e..528412f 100644 --- a/iOS/src/GBYManager.m +++ b/iOS/src/GBYManager.m @@ -79,10 +79,28 @@ - (JSContext*)createJSContext - (id)initWithInitFnName:(NSString*)initFnName inNamespace:(NSString*)namespace { - return [self initWithInitFnName:initFnName inNamespace:namespace withContext:[self createJSContext]]; + return [self initWithInitFnName:initFnName + inNamespace:namespace + withContext:[self createJSContext]]; } -- (id)initWithInitFnName:(NSString*)initFnName inNamespace:(NSString*)namespace withContext:(JSContext*)context; +- (id)initWithInitFnName:(NSString*)initFnName inNamespace:(NSString*)namespace withContext:(JSContext*)context +{ + return [self initWithInitFnName:initFnName + inNamespace:namespace + withContext:context + loadingJavaScript:[[NSBundle mainBundle] pathForResource:@"main" ofType:@"js"]]; +} + +- (id)initWithInitFnName:(NSString*)initFnName inNamespace:(NSString*)namespace loadingJavaScript:(NSString*)path +{ + return [self initWithInitFnName:initFnName + inNamespace:namespace + withContext:[self createJSContext] + loadingJavaScript:path]; +} + +- (id)initWithInitFnName:(NSString*)initFnName inNamespace:(NSString*)namespace withContext:(JSContext*)context loadingJavaScript:(NSString*)path { if (self = [super init]) { @@ -94,6 +112,12 @@ - (id)initWithInitFnName:(NSString*)initFnName inNamespace:(NSString*)namespace NSAssert(_context != nil, @"The JavaScript context should not be nil"); + // Load the ClojureScript module + if (path) { + NSString *scriptString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; + NSAssert(scriptString != nil, @"The JavaScript text could not be loaded"); + [self.context evaluateScript:scriptString]; + } JSValue* initFn = [self getValue:initFnName inNamespace:namespace]; NSAssert(!initFn.isUndefined, @"Could not find the app init function");