Bug 122501

Summary: Dynamically generated JSExport protocols added to a class results in a crash
Product: WebKit Reporter: Axel Katerbau <axel>
Component: JavaScriptCoreAssignee: Nobody <webkit-unassigned>
Status: UNCONFIRMED ---    
Severity: Normal CC: mhahnenberg, mrowe, webkit-bug-importer
Priority: P2 Keywords: InRadar
Version: 528+ (Nightly build)   
Hardware: Mac   
OS: OS X 10.8   
Description Flags
Corresponding Xcode project none

Description Axel Katerbau 2013-10-08 02:21:52 PDT
While the JSExport subprotocol method for exposing methods to JSC works fine when written in Code (as in the examples in Apple's docs), programmatically at runtime generated and added such protocols crash the JSC cocoa bridge.

Run this code to see the crash (I use Xcode 5 release with iOS7 SDK).

@import JavaScriptCore;
@import ObjectiveC;

int main(int argc, char * argv[]) {
    @autoreleasepool {
        JSContext *context = [[JSContext alloc] init];
        Protocol *dynProtocol = objc_allocateProtocol("NSStringJSExport");
        Protocol *jsExportProtocol = @protocol(JSExport);
        protocol_addProtocol(dynProtocol, jsExportProtocol);
        Method method = class_getInstanceMethod([NSString class], @selector(boolValue));
        protocol_addMethodDescription(dynProtocol, @selector(boolValue), method_getTypeEncoding(method), YES, YES);
        //        protocol_addMethodDescription(dynProtocol, @selector(boolValue), "@:", YES, YES); // <-- also crashes
        //        protocol_addMethodDescription(dynProtocol, @selector(boolValue), "B@:", YES, YES); // <-- also crashes
        class_addProtocol([NSString class], dynProtocol);
        context[@"NSString"] = [NSString class]; // <-- crashes here in parseObjCType
        context[@"myString"] = @"YES";
        JSValue *value = [context evaluateScript:@"myResult = myString.boolValue()"];
        NSLog(@"Result = %@", value);
Comment 1 Axel Katerbau 2013-10-08 02:42:37 PDT
Created attachment 213680 [details]
Corresponding Xcode project
Comment 2 Axel Katerbau 2013-10-08 03:05:35 PDT
Version used might be:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9) AppleWebKit/537.71 (KHTML, like Gecko) Version/7.0 Safari/537.71
Comment 3 Radar WebKit Bug Importer 2013-10-09 02:29:44 PDT