Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Fuzzy on memory/sharing objects between Obj-C and Javascript



Hello,

I have a JavaScript function like this:

function callCocoaWithString(functionName, argument) {
  var theBroker = window.myMessageBroker;
  var returnMsg = theBroker.cocoaFunctionWithString(functionName,argument);
}

where myMessageBroker is an NSObject attached in webView:windowScriptObjectAvailable: with a [wso setValue:msgBroker forKey:@"myMessageBroker" ].

I have the following methods in msgBroker.m:

+ (NSString *) webScriptNameForSelector:(SEL)sel
{
    if (sel == @selector(cocoaFunction:withString:))
           return @"cocoaFunctionWithString";
    return nil;
}
+ (BOOL)isSelectorExcludedFromWebScript:(SEL)selector
{
    if (selector == @selector(cocoaFunction:withString:)) {
        return NO;
    }
    return YES;
}

- (NSString *)cocoaFunction:(NSString*)functionName withString:(NSString *)dataString
{
    NSString * someString;
    
    NSString * selName = [ functionName stringByAppendingString:@":" ];
    SEL sel = NSSelectorFromString(selName);
    
    if ( [self respondsToSelector:sel] ) {
        someString = [self performSelector:sel withObject:dataString];
    }
    else {
        NSLog(@"javascript called cocoaFunction: with unknown selector: |%@|", functionName);
        return @"%UNKNOWNFUNCTION";
    }
    
    return someString;
}


- (NSString *) styleAction:(NSString *)dataString
{
    NSString * returnString = [[[NSString alloc] initWithString:dataString] autorelease] ;
    
    return returnString;
}

styleAction: doesn't really do anything yet, obviously, and I'm doing the NSSelectorFromString() thing so that we can add new functions without having to constantly be editing webScriptNameForSelector: and isSelectorExcludedFromWebScript:.

Given a JavaScript
 callCocoaWithString("styleAction","a String")
 
am I doing the right thing with regards to autoreleasing the string returned by styleAction: ? What about the static string I return in cocoaFunction:withString: for unknown selectors?


Thanks
Jeffrey Johnson
Macintosh Development
Wavefunction, Inc.
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Webkitsdk-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/webkitsdk-dev/email@hidden

This email sent to email@hidden



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.