Skip to content

Commit

Permalink
Support loading main.js or user-specified js
Browse files Browse the repository at this point in the history
  • Loading branch information
mfikes committed Mar 10, 2015
1 parent a63a697 commit 3658759
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
29 changes: 27 additions & 2 deletions iOS/src/GBYManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down
28 changes: 26 additions & 2 deletions iOS/src/GBYManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {

Expand All @@ -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");
Expand Down

0 comments on commit 3658759

Please sign in to comment.