Changeset 3450

Show
Ignore:
Timestamp:
12/10/06 13:26:55 (2 years ago)
Author:
timothy
Message:

Code clean up for JavaScript? exception reporting. Fixes a leak also.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/Models/JVChatEvent.m

    r3345 r3450  
    246246 
    247247- (id) init { 
    248         if( ( self = [super init] ) ) _doc = NULL; 
    249         return self; 
    250 
    251  
    252 - (id) initWithName:(NSString *) name andMessage:(id) message { 
    253         if( ( self = [self init] ) ) { 
     248        if( ( self = [super init] ) ) { 
    254249                _loadedMessage = YES; 
    255250                _loadedAttributes = YES; 
    256251                _loadedSmall = YES; 
    257252                [self setDate:[NSDate date]]; 
     253                [self setEventIdentifier:[NSString locallyUniqueString]]; 
     254        } 
     255 
     256        return self; 
     257} 
     258 
     259- (id) initWithName:(NSString *) name andMessage:(id) message { 
     260        if( ( self = [self init] ) ) { 
    258261                [self setName:name]; 
    259262                [self setMessage:message]; 
    260                 [self setEventIdentifier:[NSString locallyUniqueString]]; 
    261263        } 
    262264 
  • trunk/Models/JVChatMessage.m

    r3259 r3450  
    382382#pragma mark - 
    383383 
    384 - (id) initWithText:(id) body sender:(id) sender
    385         if( ( self = [self init] ) ) { 
     384- (id) init
     385        if( ( self = [super init] ) ) { 
    386386                _loaded = YES; 
    387387                _bodyLoaded = YES; 
    388388                _senderLoaded = YES; 
    389389                [self setDate:[NSDate date]]; 
     390                [self setMessageIdentifier:[NSString locallyUniqueString]]; 
     391        } 
     392 
     393        return self; 
     394} 
     395 
     396- (id) initWithText:(id) body sender:(id) sender { 
     397        if( ( self = [self init] ) ) { 
    390398                [self setBody:body]; 
    391399                [self setSender:sender]; 
    392                 [self setMessageIdentifier:[NSString locallyUniqueString]]; 
    393400        } 
    394401 
  • trunk/Plug-Ins/JavaScript Support/JVJavaScriptChatPlugin.h

    r3439 r3450  
    33extern NSString *JVJavaScriptErrorDomain; 
    44 
     5@class WebScriptCallFrame; 
    56@class WebView; 
    67 
     
    1112        WebView *_webview; 
    1213        NSString *_currentFunction; 
     14        id _currentException; 
    1315        BOOL _loading; 
    1416} 
     
    2123- (void) setupScriptGlobalsForWebView:(WebView *) webView; 
    2224 
     25- (void) reportErrorForCallFrame:(WebScriptCallFrame *) frame lineNumber:(unsigned int) line; 
    2326- (void) reportError:(NSDictionary *) error inFunction:(NSString *) functionName whileLoading:(BOOL) whileLoading; 
    2427- (id) callScriptFunctionNamed:(NSString *) functionName withArguments:(NSArray *) arguments forSelector:(SEL) selector; 
  • trunk/Plug-Ins/JavaScript Support/JVJavaScriptChatPlugin.m

    r3449 r3450  
    216216} 
    217217 
     218- (void) webView:(WebView *) sender didEnterCallFrame:(WebScriptCallFrame *) frame sourceId:(int) sid line:(int) line forWebFrame:(WebFrame *) webFrame { 
     219        [_currentException release]; 
     220        _currentException = nil; 
     221} 
     222 
    218223- (void) webView:(WebView *) sender willLeaveCallFrame:(WebScriptCallFrame *) frame sourceId:(int) sid line:(int) line forWebFrame:(WebFrame *) webFrame { 
    219         if( ! [[frame userInfo] isEqual:@"handled"] && [frame exception] ) { 
    220                 id exception = [frame exception]; 
    221                 NSString *sourceURL = nil; 
    222                 @try { 
    223                         sourceURL = [exception valueForKey:@"sourceURL"]; 
    224                 } @catch( NSException *e ) { 
    225                         sourceURL = nil; 
    226                 } 
    227  
    228                 NSDictionary *error = [[NSDictionary allocWithZone:nil] initWithObjectsAndKeys:[exception valueForKey:@"message"], @"message", [exception valueForKey:@"line"], @"lineNumber", sourceURL, @"sourceURL", nil]; 
    229                 [self reportError:error inFunction:_currentFunction whileLoading:_loading]; 
    230                 [error release]; 
    231         } 
     224        if( [frame exception] ) [self reportErrorForCallFrame:frame lineNumber:line]; 
    232225} 
    233226 
    234227- (void) webView:(WebView *) sender exceptionWasRaised:(WebScriptCallFrame *) frame sourceId:(int) sid line:(int) line forWebFrame:(WebFrame *) webFrame { 
    235         [frame setUserInfo:@"handled"]; 
    236  
    237         id exception = [frame exception]; 
    238         NSString *sourceURL = nil; 
    239         @try { 
    240                 sourceURL = [exception valueForKey:@"sourceURL"]; 
    241         } @catch( NSException *e ) { 
    242                 sourceURL = nil; 
    243         } 
    244  
    245         NSDictionary *error = [[NSDictionary allocWithZone:nil] initWithObjectsAndKeys:[exception valueForKey:@"message"], @"message", [exception valueForKey:@"line"], @"lineNumber", sourceURL, @"sourceURL", nil]; 
    246         [self reportError:error inFunction:_currentFunction whileLoading:_loading]; 
    247         [error release]; 
     228        if( [frame exception] ) [self reportErrorForCallFrame:frame lineNumber:line]; 
    248229} 
    249230 
     
    264245 
    265246- (id) allocInstance:(NSString *) class { 
    266         return [NSClassFromString(class) allocWithZone:nil]; 
     247        return [[NSClassFromString(class) allocWithZone:nil] autorelease]; 
    267248} 
    268249 
     
    324305#pragma mark - 
    325306 
     307- (void) reportErrorForCallFrame:(WebScriptCallFrame *) frame lineNumber:(unsigned int) line { 
     308        id handled = nil; 
     309        id exception = [frame exception]; 
     310        @try { handled = [exception valueForKey:@"handled"]; } @catch( NSException *e ) { handled = nil; } 
     311 
     312        if( exception && ! handled && ! [_currentException isEqual:exception] ) { 
     313                [_currentException release]; 
     314                _currentException = [exception retain]; 
     315 
     316                NSNumber *lineNumber = ( line ? [NSNumber numberWithUnsignedInt:line] : nil ); 
     317                NSString *sourceURL = nil; 
     318                NSString *message = exception; 
     319 
     320                if( [exception isKindOfClass:[WebScriptObject class]] ) { 
     321                        [exception setValue:[NSNumber numberWithBool:YES] forKey:@"handled"]; 
     322 
     323                        @try { lineNumber = [exception valueForKey:@"line"]; } @catch( NSException *e ) { lineNumber = nil; } 
     324                        @try { sourceURL = [exception valueForKey:@"sourceURL"]; } @catch( NSException *e ) { sourceURL = nil; } 
     325                        @try { message = [exception valueForKey:@"message"]; } @catch( NSException *e ) { message = exception; } 
     326                } 
     327 
     328                NSDictionary *error = [[NSDictionary allocWithZone:nil] initWithObjectsAndKeys:message, @"message", lineNumber, @"lineNumber", sourceURL, @"sourceURL", nil]; 
     329                [self reportError:error inFunction:_currentFunction whileLoading:_loading]; 
     330                [error release]; 
     331        } 
     332} 
     333 
    326334- (void) reportError:(NSDictionary *) error inFunction:(NSString *) functionName whileLoading:(BOOL) whileLoading { 
    327335        NSMutableString *errorDesc = [[NSMutableString alloc] initWithCapacity:64]; 
  • trunk/Plug-Ins/JavaScript Support/plugin.html

    r3449 r3450  
    55<meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
    66<script> 
     7String.prototype.fourCharCode = function() { 
     8        var result = 0; 
     9        var spaceCode = ' '.charCodeAt(0); 
     10 
     11        if( this.length >= 1 ) result |= ( this.charCodeAt(0) & 0x00ff ) << 24; 
     12        else result |= spaceCode << 24; 
     13 
     14        if( this.length >= 2 ) result |= ( this.charCodeAt(1) & 0x00ff ) << 16; 
     15        else result |= spaceCode << 16; 
     16 
     17        if( this.length >= 3 ) result |= ( this.charCodeAt(2) & 0x00ff ) << 8; 
     18        else result |= spaceCode << 8; 
     19 
     20        if( this.length >= 4 ) result |= ( this.charCodeAt(3) & 0x00ff ); 
     21        else result |= spaceCode; 
     22 
     23        return result; 
     24} 
     25 
     26JVChatMessageNormalType = 'noMt'.fourCharCode(); 
     27JVChatMessageNoticeType = 'nTMt'.fourCharCode(); 
     28 
     29function JVChatMessage() { 
     30        throw "Can't create a JVChatMessage directly, use JVMutableChatMessage."; 
     31} 
     32 
    733function JVMutableChatMessage(text, sender) { 
    834        var obj = Plugin.allocInstance('JVMutableChatMessage'); 
    9         return obj.initWithText(text, sender); 
     35        if( text !== undefined && sender !== undefined ) 
     36                return obj.initWithText(text, sender); 
     37        return obj.init(); 
     38
     39 
     40function JVChatEvent() { 
     41        throw "Can't create a JVChatEvent directly, use JVMutableChatEvent."; 
     42
     43 
     44function JVMutableChatEvent(name, message) { 
     45        var obj = Plugin.allocInstance('JVMutableChatEvent'); 
     46        if( name !== undefined && message !== undefined ) 
     47                return obj.initWithName(name, message); 
     48        return obj.init(); 
    1049} 
    1150</script>