Skip to content

Commit

Permalink
Switched __jsc__ var to noretain, this way the context dies normally …
Browse files Browse the repository at this point in the history
…(previously, had a retain count of 2) (This fixed a bug where the context was not deallocated, therefore jsClasses were not either, and tests were calling in to dead code
  • Loading branch information
parmanoir committed Aug 3, 2011
1 parent 965cc43 commit ebee9c2
Show file tree
Hide file tree
Showing 21 changed files with 27,351 additions and 20,440 deletions.
36 changes: 28 additions & 8 deletions JSCocoa/JSCocoaController.m
Expand Up @@ -259,6 +259,13 @@ - (id)initWithGlobalContext:(JSGlobalContextRef)_ctx
#endif
// Create a reference to ourselves, and make it read only, don't enum, don't delete
[self setObject:self withName:@"__jsc__" attributes:kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontEnum|kJSPropertyAttributeDontDelete];
// Get reference back and set it to no retain
JSValueRef jsSelf = [self evalJSString:@"__jsc__"];
JSCocoaPrivateObject* private = JSObjectGetPrivate(JSValueToObject(ctx, jsSelf, NULL));
// Overwrite settings
[private setObjectNoRetain:self];
// And discard private's retain
[self release];


// Load class kit
Expand Down Expand Up @@ -325,23 +332,25 @@ - (id)init
//
- (void)cleanUp
{
NSLog(@"JSCocoa : %p dying", self);
[boxedObjects release];
NSLog(@"JSCocoa : %p dying (ownsContext=%d)", self, ownsContext);
[self setUseSafeDealloc:NO];

// Cleanup if we created the JavascriptCore context.
// If not, let user do it. In a WebView, this method will be called during JS GC,
// and trying to execute more JS code will fail.
// User must clean up manually by calling unlinkAllReferences then destroying the webView


if (ownsContext) {
[self unlinkAllReferences];
JSGarbageCollect(ctx);
JSGlobalContextRelease(ctx);
}
[self logBoxedObjects];

controllerCount--;
if (controllerCount == 0)
{
NSLog(@"OUT");
if (OSXObjectClass) {
JSClassRelease(OSXObjectClass);
JSClassRelease(jsCocoaObjectClass);
Expand Down Expand Up @@ -379,19 +388,28 @@ - (void)cleanUp


// Remove classes : go backwards to remove child classes first
for (id class in [jsClasses reverseObjectEnumerator])
objc_disposeClassPair([class pointerValue]);
/*
NSInteger c = [jsClasses count]-1;
NSLog(@"CLASSCOUNT %d", c);
while (c >= 0)
{
id class = [[jsClasses objectAtIndex:c] pointerValue];
NSLog(@"DEALLOC CLASS %@", class);
objc_disposeClassPair(class);
c--;
}

*/
[jsClasses release];
jsClasses = nil;
}

NSLog(@"DESTROY context");
if (ownsContext)
JSGlobalContextRelease(ctx);

NSLog(@"DESTROY boxedObjects");
[boxedObjects release];
}

- (oneway void)release
Expand Down Expand Up @@ -1994,7 +2012,7 @@ - (int)runTests:(NSString*)path withSelector:(SEL)sel {

for (id file in files) {
id filePath = [NSString stringWithFormat:@"%@/%@", path, file];
// NSLog(@">>>evaling %@", filePath);
NSLog(@">>>evaling %@", filePath);
// BOOL evaled = [self evalJSFile:filePath];

id evaled = nil;
Expand Down Expand Up @@ -2051,7 +2069,8 @@ - (void)unlinkAllReferences
// [self evalJSString:@"for (var i in this) { log('DELETE ' + i); this[i] = null; delete this[i]; }"];
// [self evalJSString:@"for (var i in this) { this[i] = null; delete this[i]; }"];

id del = @"var keys = Object.keys(this); var c = keys.length; for (var i=0; i<c; i++) { try { this[keys[i]] = null } catch(e) {} }";
// id del = @"var keys = Object.keys(this); var c = keys.length; for (var i=0; i<c; i++) { try { this[keys[i]] = null } catch(e) {} }";
id del = @"for (var i in this) { this[i] = null; delete this[i]; }";
[self evalJSString:del];
// Everything is now collectable !
}
Expand Down Expand Up @@ -3259,9 +3278,9 @@ static void jsCocoaObject_finalize(JSObjectRef object)

// Clean up the object now as WebKit calls us twice while cleaning __jsc__ (20110730)
JSObjectSetPrivate(object, NULL);

id jsc = nil;
JSContextRef ctx = [private ctx];

if (!ctx) {
NSLog(@"+++++++NO CONTEXT, NO DELETE");
} else {
Expand All @@ -3270,6 +3289,7 @@ static void jsCocoaObject_finalize(JSObjectRef object)
if (!jsc) {
NSLog(@"*******NO CONTEXT+++++++++++");
}

//
// If a boxed object is being destroyed, remove it from the cache
//
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions TestsRunner/ApplicationController.m
Expand Up @@ -22,6 +22,7 @@ - (id)init {
return nil;

test_unit = test_delegate = test_webview = test_autocall = YES;
test_webview = NO;

return self;
}
Expand Down Expand Up @@ -239,6 +240,8 @@ - (IBAction)runJSTests:(id)sender
id r = [o performSelector:[@"だけを追加する:" UTF8String] withObject:[NSNumber numberWithInt:7]];
NSLog(@"r=%@", r);
*/
NSLog(@"DOUBLE CYCLE");
// [self cycleContext];
}


Expand Down

0 comments on commit ebee9c2

Please sign in to comment.