Skip to content

Commit

Permalink
Massive commit while wheels spin
Browse files Browse the repository at this point in the history
  • Loading branch information
Blain Hamon committed Mar 8, 2011
1 parent 1e4bedf commit a348739
Show file tree
Hide file tree
Showing 8 changed files with 329 additions and 148 deletions.
3 changes: 3 additions & 0 deletions iphone/Classes/KrollBridge.h
Expand Up @@ -49,5 +49,8 @@
- (id)preloadForKey:(id)key name:(id)name;
- (KrollContext*)krollContext;

+ (NSArray *)krollBridgesUsingProxy:(id)proxy;
-(void)enqueueEvent:(NSString*)type forProxy:(TiProxy *)proxy withObject:(id)obj withSource:(id)source;

@end

74 changes: 73 additions & 1 deletion iphone/Classes/KrollBridge.mm
Expand Up @@ -13,6 +13,7 @@
#import "TiUtils.h"
#import "TiApp.h"
#import "ApplicationMods.h"
#import <libkern/OSAtomic.h>

#ifdef DEBUGGER_ENABLED
#import "TiDebuggerContext.h"
Expand Down Expand Up @@ -146,9 +147,22 @@ -(TiModule*)moduleNamed:(NSString*)name context:(id<TiEvaluator>)context

@end

OSSpinLock krollBridgeRegistryLock = OS_SPINLOCK_INIT;
CFMutableSetRef krollBridgeRegistry = nil;

@implementation KrollBridge

+(void)initialize
{
if (krollBridgeRegistry == nil)
{
CFSetCallBacks doNotRetain = kCFTypeSetCallBacks;
doNotRetain.retain = NULL;
doNotRetain.release = NULL;
krollBridgeRegistry = CFSetCreateMutable(NULL, 3, &doNotRetain);
}
}

-(id)init
{
if (self = [super init])
Expand All @@ -163,6 +177,9 @@ -(id)init
object:nil];

proxyLock = [[NSRecursiveLock alloc] init];
OSSpinLockLock(&krollBridgeRegistryLock);
CFSetAddValue(krollBridgeRegistry, self);
OSSpinLockUnlock(&krollBridgeRegistryLock);
}
return self;
}
Expand Down Expand Up @@ -238,6 +255,9 @@ -(void)dealloc
RELEASE_TO_NIL(titanium);
RELEASE_TO_NIL(modules);
RELEASE_TO_NIL(proxyLock);
OSSpinLockLock(&krollBridgeRegistryLock);
CFSetRemoveValue(krollBridgeRegistry, self);
OSSpinLockUnlock(&krollBridgeRegistryLock);
[super dealloc];
}

Expand Down Expand Up @@ -406,6 +426,20 @@ - (void)fireEvent:(id)listener withObject:(id)obj remove:(BOOL)yn thisObject:(Ti

}

-(void)enqueueEvent:(NSString*)type forProxy:(TiProxy *)proxy withObject:(id)obj withSource:(id)source
{
KrollObject * eventKrollObject = [self krollObjectForProxy:proxy];
KrollObject * sourceObject = [self krollObjectForProxy:source];
if (sourceObject == nil)
{
sourceObject = eventKrollObject;
}
KrollEvent * newEvent = [[KrollEvent alloc] initWithType:type ForKrollObject:eventKrollObject
eventObject:obj thisObject:sourceObject];
[context enqueue:newEvent];
[newEvent release];
}

-(void)injectPatches
{
// called to inject any Titanium patches in JS before a context is loaded... nice for
Expand Down Expand Up @@ -547,7 +581,10 @@ - (void)registerProxy:(id)proxy
//NOTE: Do NOT treat registeredProxies like a mutableDictionary; mutable dictionaries copy keys,
//CFMutableDictionaryRefs only retain keys, which lets them work with proxies properly.
KrollObject * ourKrollObject = [[KrollObject alloc] initWithTarget:proxy context:context];
// CFDictionaryAddValue(registeredProxies, proxy, ourKrollObject);

VerboseLog(@"%@ is adding %@ %X for %@ %X",self,proxy,proxy,ourKrollObject,ourKrollObject);
CFDictionaryAddValue(registeredProxies, proxy, ourKrollObject);
[ourKrollObject release];

[proxyLock unlock];
}
Expand All @@ -565,6 +602,7 @@ - (void)unregisterProxy:(id)proxy
}
if (registeredProxies != NULL)
{
VerboseLog(@"%@ is removing %@ %X for",self,proxy,proxy);
CFDictionaryRemoveValue(registeredProxies, proxy);
//Don't bother with removing the empty registry. It's small and leaves on dealloc anyways.
}
Expand All @@ -573,6 +611,10 @@ - (void)unregisterProxy:(id)proxy

- (BOOL)usesProxy:(id)proxy
{
if (proxy == nil)
{
return NO;
}
BOOL result=NO;
[proxyLock lock];
if (registeredProxies != NULL)
Expand Down Expand Up @@ -704,4 +746,34 @@ -(id)require:(KrollContext*)kroll path:(NSString*)path
@throw [NSException exceptionWithName:@"org.appcelerator.kroll" reason:[NSString stringWithFormat:@"Couldn't find module: %@",path] userInfo:nil];
}

+ (NSArray *)krollBridgesUsingProxy:(id)proxy
{
NSMutableArray * results = nil;

OSSpinLockLock(&krollBridgeRegistryLock);
int bridgeCount = CFSetGetCount(krollBridgeRegistry);
KrollBridge * registryObjects[bridgeCount];
CFSetGetValues(krollBridgeRegistry, (const void **)registryObjects);

for (int currentBridgeIndex = 0; currentBridgeIndex < bridgeCount; currentBridgeIndex++)
{
KrollBridge * currentBridge = registryObjects[currentBridgeIndex];
if (![currentBridge usesProxy:proxy])
{
continue;
}
if (results == nil)
{
results = [NSMutableArray arrayWithObject:currentBridge];
continue;
}
[results addObject:currentBridge];
}

//Why do we wait so long? In case someone tries to dealloc the krollBridge while we're looking at it.
//registryObjects nor the registry does a retain here!
OSSpinLockUnlock(&krollBridgeRegistryLock);
return results;
}

@end
11 changes: 8 additions & 3 deletions iphone/Classes/KrollContext.h
Expand Up @@ -67,6 +67,9 @@
-(void)invokeOnThread:(id)callback_ method:(SEL)method_ withObject:(id)obj callback:(id)callback selector:(SEL)selector_;
-(void)evalJS:(NSString*)code;
-(id)evalJSAndWait:(NSString*)code;

-(void)enqueue:(id)obj;

-(void)invokeEvent:(KrollCallback*)callback_ args:(NSArray*)args_ thisObject:(id)thisObject_;
-(void)registerTimer:(id)timer timerId:(double)timerId;
-(void)unregisterTimer:(double)timerId;
Expand Down Expand Up @@ -98,13 +101,15 @@
-(id)invokeWithResult:(KrollContext*)context;
@end

@class KrollObject;
@interface KrollEvent : NSObject {
@private
KrollCallback *callback;
NSArray *args;
NSString * type;
KrollObject * callbackObject;
NSDictionary *eventObject;
id thisObject;
}
-(id)initWithCallback:(KrollCallback*)callback_ args:(NSArray*)args_ thisObject:(id)thisObject_;
-(id)initWithType:(NSString *)newType ForKrollObject:(KrollObject*)newCallbackObject eventObject:(NSDictionary*)newEventObject thisObject:(id)newThisObject;
-(void)invoke:(KrollContext*)context;
@end

Expand Down
23 changes: 15 additions & 8 deletions iphone/Classes/KrollContext.mm
Expand Up @@ -523,26 +523,28 @@ -(id)invokeWithResult:(KrollContext*)context

@implementation KrollEvent

-(id)initWithCallback:(KrollCallback*)callback_ args:(NSArray*)args_ thisObject:(id)thisObject_
-(id)initWithType:(NSString *)newType ForKrollObject:(KrollObject*)newCallbackObject eventObject:(NSDictionary*)newEventObject thisObject:(id)newThisObject;
{
if (self = [super init])
{
callback = [callback_ retain];
args = [args_ retain];
thisObject = [thisObject_ retain];
type = [newType copy];
callbackObject = [newCallbackObject retain];
eventObject = [newEventObject retain];
thisObject = [newThisObject retain];
}
return self;
}
-(void)dealloc
{
[type release];
[thisObject release];
[callback release];
[args release];
[callbackObject release];
[eventObject release];
[super dealloc];
}
-(void)invoke:(KrollContext*)context
{
[callback call:args thisObject:thisObject];
[callbackObject triggerEvent:type withObject:eventObject thisObject:thisObject];
}
@end

Expand Down Expand Up @@ -823,6 +825,7 @@ -(void)invokeOnThread:(id)callback_ method:(SEL)method_ withObject:(id)obj callb

-(void)invokeEvent:(KrollCallback*)callback_ args:(NSArray*)args_ thisObject:(id)thisObject_
{
NSLog(@"We shouldn't be here!");
KrollEvent *event = [[KrollEvent alloc] initWithCallback:callback_ args:args_ thisObject:thisObject_];
[self enqueue:event];
[event release];
Expand Down Expand Up @@ -1016,12 +1019,14 @@ -(void)main
// we have a pending GC request to try and reclaim memory
if (gcrequest)
{
NSAutoreleasePool * garbagePool = [[NSAutoreleasePool alloc] init];
#if CONTEXT_DEBUG == 1
NSLog(@"CONTEXT<%@>: forced garbage collection requested",self);
#endif
TiGarbageCollect(context);
loopCount = 0;
gcrequest = NO;
[garbagePool drain];
}

BOOL stuff_in_queue = YES;
Expand Down Expand Up @@ -1079,11 +1084,13 @@ -(void)main
// TODO: experiment, attempt to collect more often than usual given our environment
if (loopCount == GC_LOOP_COUNT)
{
NSAutoreleasePool * garbagePool = [[NSAutoreleasePool alloc] init];
#if CONTEXT_DEBUG == 1
NSLog(@"CONTEXT<%@>: garbage collecting after loop count of %d exceeded (count=%d)",self,loopCount,KrollContextCount);
#endif
TiGarbageCollect(context);
loopCount = 0;
[garbagePool drain];
}

// check to see if we're already stopped and in the flush queue state, in which case,
Expand All @@ -1107,7 +1114,7 @@ -(void)main
{
// wait only 10 seconds and then loop, this will allow us to garbage
// collect every so often
[condition waitUntilDate:[NSDate dateWithTimeIntervalSinceNow:10]];
[condition waitUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
}
[condition unlock];

Expand Down
7 changes: 6 additions & 1 deletion iphone/Classes/KrollObject.h
Expand Up @@ -8,7 +8,7 @@
#import "TiCore.h"
#import "TiBase.h"

@class KrollContext;
@class KrollContext, KrollCallback;
extern TiClassRef KrollObjectClassRef;

void KrollFinalizer(TiObjectRef ref);
Expand Down Expand Up @@ -54,5 +54,10 @@ bool KrollDeleteProperty(TiContextRef ctx, TiObjectRef object, TiStringRef prope
-(id)target;
-(TiObjectRef)jsobject;


-(void)storeCallback:(KrollCallback *)eventCallback forEvent:(NSString *)eventName;
-(void)removeCallback:(KrollCallback *)eventCallback forEvent:(NSString *)eventName;
-(void)triggerEvent:(NSString *)eventName withObject:(NSDictionary *)eventData thisObject:(KrollObject *)thisObject;

@end

0 comments on commit a348739

Please sign in to comment.