Skip to content

Commit

Permalink
Fix #7 by guarding against processing require for same path
Browse files Browse the repository at this point in the history
  • Loading branch information
mfikes committed Feb 4, 2015
1 parent 619f8b2 commit 37dd003
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 32 deletions.
10 changes: 5 additions & 5 deletions ObjectiveC/Ambly Demo/Ambly Demo/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@

@interface AppDelegate ()

@property (strong, nonatomic) ABYServer* abyServer;
@property (strong, nonatomic) ABYContextManager* contextManager;
@property (strong, nonatomic) ABYServer* server;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.abyServer = [[ABYServer alloc] init];
[self.abyServer startListening:9999 forContext:[ABYContextManager createJSContext]];
self.contextManager = [[ABYContextManager alloc] init];
self.server = [[ABYServer alloc] init];
[self.server startListening:9999 forContext:self.contextManager.context];
return YES;
}

Expand Down
2 changes: 1 addition & 1 deletion ObjectiveC/src/ABYContextManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
*/
@interface ABYContextManager : NSObject

+ (JSContext*)createJSContext;
@property (strong, nonatomic, readonly) JSContext* context;

@end
68 changes: 42 additions & 26 deletions ObjectiveC/src/ABYContextManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,54 @@
#include <libkern/OSAtomic.h>
#import <JavaScriptCore/JavaScriptCore.h>

@interface ABYContextManager()

@property (strong, nonatomic) JSContext* context;
@property (strong, nonatomic) NSMutableDictionary* requiredPaths;

@end

@implementation ABYContextManager

+ (void)setUpExceptionLogging:(JSContext*)context
-(id)init
{
if (self = [super init]) {
self.requiredPaths = [[NSMutableDictionary alloc] init];
self.context = [[JSContext alloc] init];

[self setUpExceptionLogging];
[self setUpConsoleLog];
[self setUpTimerFunctionality];
[self setUpRequire];
}
return self;
}

- (void)setUpExceptionLogging
{
context.exceptionHandler = ^(JSContext *context, JSValue *exception) {
self.context.exceptionHandler = ^(JSContext *context, JSValue *exception) {
NSString* errorString = [NSString stringWithFormat:@"[%@:%@:%@] %@\n%@", exception[@"sourceURL"], exception[@"line"], exception[@"column"], exception, [exception[@"stack"] toObject]];
NSLog(@"%@", errorString);
};
}

+ (void)setUpConsoleLog:(JSContext*)context
- (void)setUpConsoleLog
{
[context evaluateScript:@"var console = {}"];
context[@"console"][@"log"] = ^(NSString *message) {
[self.context evaluateScript:@"var console = {}"];
self.context[@"console"][@"log"] = ^(NSString *message) {
NSLog(@"JS: %@", message);
};
}

+ (void)setUpTimerFunctionality:(JSContext*)context
- (void)setUpTimerFunctionality
{
static volatile int32_t counter = 0;

NSString* callbackImpl = @"var callbackstore = {};\nvar setTimeout = function( fn, ms ) {\ncallbackstore[setTimeoutFn(ms)] = fn;\n}\nvar runTimeout = function( id ) {\nif( callbackstore[id] )\ncallbackstore[id]();\ncallbackstore[id] = nil;\n}\n";

[context evaluateScript:callbackImpl];
[self.context evaluateScript:callbackImpl];

context[@"setTimeoutFn"] = ^( int ms ) {
self.context[@"setTimeoutFn"] = ^( int ms ) {

int32_t incremented = OSAtomicIncrement32(&counter);

Expand All @@ -44,34 +65,29 @@ + (void)setUpTimerFunctionality:(JSContext*)context
};
}

+ (void)setUpRequire:(JSContext*)context
- (void)setUpRequire
{
// TODO deal with paths in various forms (relative, URLs?)

context[@"require"] = ^(NSString *path) {
__weak typeof(self) weakSelf = self;

self.context[@"require"] = ^(NSString *path) {

JSContext* currentContext = [JSContext currentContext];

NSError* error = nil;
NSString* sourceText = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];

if (!error && sourceText) {
[currentContext evaluateScript:sourceText withSourceURL:[NSURL fileURLWithPath:path]];
if (!weakSelf.requiredPaths[path]) {

NSError* error = nil;
NSString* sourceText = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];

if (!error && sourceText) {
[currentContext evaluateScript:sourceText withSourceURL:[NSURL fileURLWithPath:path]];
weakSelf.requiredPaths[path] = @(YES);
}
}

return [JSValue valueWithUndefinedInContext:currentContext];

};
}

+ (JSContext*)createJSContext
{
JSContext* context = [[JSContext alloc] init];
[self setUpExceptionLogging:context];
[self setUpConsoleLog:context];
[self setUpTimerFunctionality:context];
[self setUpRequire:context];
return context;
}

@end

0 comments on commit 37dd003

Please sign in to comment.