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 objc_registerProtocol(dynProtocol); 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); } }
Created attachment 213680 [details] Corresponding Xcode project
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
<rdar://problem/15184814>