Pro Yearly is on sale from $80 to $50! »

JavaScriptCore.frameworkでできること

 JavaScriptCore.frameworkでできること

JavaScriptCore.frameworkの解説と、できることの限界を探ります。

9bf923e39671cde83584e3e926296c13?s=128

Kishikawa Katsumi

February 02, 2014
Tweet

Transcript

  1. +BWB4DSJQU$PSFGSBNFXPSLͰ Ͱ͖Δ͜ͱ

  2. ؛઒ࠀݾ http://kishikawakatsumi.com Twitter @k_katsumi 24/7 twenty-four seven http://d.hatena.ne.jp/KishikawaKatsumi/

  3. ΞδΣϯμ w +BWB4DSJQU$PSFGSBNFXPSLͷ֓ཁͱ࢖͍ํ w 0CKFDUJWF$3VOUJNF"1*ͷ׆༻ํ๏ w +BWB4DSJQU#SJEHFͷ঺հ

  4. ؔ࿈هࣄ w +BWB4DSJQU͚ͩͰJ04ͷ6*Λॻ͍ͯΈΔ w IUUQEIBUFOBOFKQ,JTIJLBXB,BUTVNJ w +BWB4DSJQUͰJ04ΞϓϦ͕ॻ͚ΔϥΠϒϥϦ+BWB4DSJQU#SJEHFΛ ެ։͠·ͨ͠ w IUUQEIBUFOBOFKQ,JTIJLBXB,BUTVNJ

  5. αϯϓϧίʔυ w IUUQTHJUIVCDPNLJTIJLBXBLBUTVNJEPXOMPBEBCMFJPTBQQT w IUUQTHJUIVCDPNLJTIJLBXBLBUTVNJ+BWB4DSJQU#SJEHF

  6. +BWB4DSJQU$PSFGSBNFXPSL

  7. 0CKFDUJWF$+BWB4DSJQUϒϦοδ w 0CKFDUJWF$͔Β+BWB4DSJQUΛ࣮ߦ͢Δ w +BWB4DSJQU͔Β0CKFDUJWF$Λ࣮ߦ͢Δ w 0CKFDUJWF$ͱ+BWB4DSJQUͷΦϒδΣΫτͷ ૬ޓม׵

  8. ֓ཁ

  9. ! JSContext *context = [[JSContext alloc] init]; ! JSValue *result

    = [context evaluateScript:@"2 + 8"]; NSLog(@"%d", result.toInt32);
  10. JSContext *context = [[JSContext alloc] init]; ! context[@"factorial"] = ^(int

    x) { int factorial = 1; for (; x > 1; x--) { factorial *= x; } return factorial; }; ! JSValue *result = [context evaluateScript:@"factorial(5);"]; NSLog(@"%d", result.toInt32);
  11. JSContext *context = [[JSContext alloc] init]; ! context[@"factorial"] = ^(int

    x) { int factorial = 1; for (; x > 1; x--) { factorial *= x; } return factorial; }; ! JSValue *function = context[@“factorial"]; ! JSValue *result = [function callWithArguments:@[@(5)]]; NSLog(@"%d", result.toInt32);
  12. JSContext *context = [[JSContext alloc] init]; [context evaluateScript: @"function sum(a,

    b) { return a + b; }" ]; ! JSValue *function = context[@“sum"]; ! JSValue *result = [function callWithArguments:@[@(2), @(3)]] NSLog(@"%d", result.toInt32);
  13. w +47JSUVBM.BDIJOF w +4$POUFYU w +47BMVF w +4.BOBHFE7BMVF w +4&YQPSU

  14. w +47JSUVBM.BDIJOF  γϯάϧεϨουͷ+4Ծ૝Ϛγϯ w +4$POUFYU  +4ͷ࣮ߦίϯςΩετʢΦϒδΣΫτͷ؅ཧʣ w +47BMVF

     +4ΦϒδΣΫτͷϥούʔ
  15. +47JSUVBM.BDIJOF +4$POUFYU +47BMVF +47JSUVBM.BDIJOF +47JSUVBM.BDIJOF +47BMVF +4$POUFYU +47BMVF +47BMVF

  16. ࢖͍Ͳ͜Ζ

  17. 0CKFDUJWF$+BWB4DSJQU

  18. 0CKFDUJWF$+BWB4DSJQU w Ұ෦ͷϩδοΫΛ+BWB4DSJQUͰهड़  8FC΍"OESPJEͳͲผͷϓϥοτϑΥʔϜͱڞ௨Խ͢Δͱ͔ w +BWB4DSJQUͰॻ͔ΕͨϥΠϒϥϦΛ࢖͏

  19. w ͸ͯͳه๏1BSTFS  UFYUIBUFOBKT  IUUQUFDIOJUPZPODPNKBWBTDSJQUBQQMJDBUJPOUFYUIBUFOBEPXOMPBEIUNM w 5FYUJMF1BSTFS  UFYUJMFKT

     IUUQTHJUIVCDPNCPSHBSUFYUJMFKT w +40/ͷѹॖ  +40/)  IUUQTHJUIVCDPN8FC3FqFDUJPO+40/) +BWB4DSJQUͰॻ͔ΕͨϥΠϒϥϦΛ࢖͏
  20. ͸ͯͳه๏1BSTFS

  21. NSBundle *bundle = [NSBundle mainBundle]; NSString *path = [bundle pathForResource:@"text-hatena"

    ofType:@"js"]; NSString *script = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; ! JSContext *context = [[JSContext alloc] init]; [context evaluateScript:script]; [context evaluateScript:@"var parser = new TextHatena();"]; ! JSValue *parser = context[@"parser"]; ! NSString *text = [NSString stringWithContentsOfFile: [bundle pathForResource:@“sample” ofType:@“txt"] encoding:NSUTF8StringEncoding error:nil]; ! NSLog(@"%@", [parser invokeMethod:@"parse" withArguments:@[text]]); ! return YES;
  22. *1384756611*[Objective-C][iOS]iOSΞϓϦέʔγϣϯͰΩʔϘʔυγϣʔτΧοτʹରԠ͢Δ ↓ ΑΓஸೡͳهࣄ͸ͪ͜ΒͰެ։͍ͯ͠·͢ɻ - [http://mobiletou.ch/2013/11/ ios%E3%82%A2%E3%83%97%E3%83%AA%E3%81%A7%E5%A4%96%E9%83%A8%E3%82%AD%E3%83%BC%E3%83%9C%E3 %83%BC%E3%83%89%E3%81%8B%E3%82%89%E3%81%AE%E3%82%B7%E3%83%A7%E3%83%BC%E3%83%88%E3%82%AB %E3%83%83%E3%83%88:title=iOSΞϓϦͰ֎෦ΩʔϘʔυ͔ΒͷγϣʔτΧοτʹରԠ͢Δํ๏ - iOSΞϓϦ։

    ൃ͜΅Ε࿩] ! ! iOS 7ͷSafari΍ϝʔϧͰ͸֎෦ΩʔϘʔυΛ࢖༻ͨ͠ࡍʹར༻Ͱ͖ΔͰ͖ΔγϣʔτΧοτ͕[http:// www.appbank.net/2013/11/08/iphone-news/696998.php:title=ҎલΑΓॆ࣮ͨ͜͠ͱ͕࿩୊ʹͳΓ·͠ ͨɻ] ! ! iOS 7Ͱ͸ΩʔϘʔυγϣʔτΧοτΛ࣮૷͢ΔͨΊͷAPI͕௥Ճ͞Ε͍ͯΔͷͰɺαʔυύʔςΟͷΞϓϦέʔγϣ ϯ΋ΩʔϘʔυγϣʔτΧοτʹରԠ͢Δ͜ͱ͕Ͱ͖·͢ɻ ! ! ಛఆͷΩʔϘʔυγϣʔτΧοτʹԠ౴͢Δʹ͸ԼهͷϓϩύςΟΛ࣮૷͠·͢ɻ >|objc| @property(nonatomic, readonly) NSArray *keyCommands ||<
  23. <div class="section"> <h3><a href="#1384756611" name="1384756611"><span class="sanchor">o-</span></a> [<a class="sectioncategory" href="searchdiary?word=*[Objective-C]">Objective-C</a>][<a class="sectioncategory"

    href="searchdiary?word=*[iOS]">iOS</a>]iOSΞϓϦέʔγϣϯͰΩʔϘʔυγϣʔτ ΧοτʹରԠ͢Δ</h3> <p>↓ ΑΓஸೡͳهࣄ͸ͪ͜ΒͰެ։͍ͯ͠·͢ɻ</p> <ul> <li> [http://mobiletou.ch/2013/11/ ios%E3%82%A2%E3%83%97%E3%83%AA%E3%81%A7%E5%A4%96%E9%83%A8%E3%82%AD%E3%83%BC%E3%83%9C%E3%83%BC%E3%8 3%89%E3%81%8B%E3%82%89%E3%81%AE%E3%82%B7%E3%83%A7%E3%83%BC%E3%83%88%E3%82%AB%E3%83%83%E3%83%88:tit le=iOSΞϓϦͰ֎෦ΩʔϘʔυ͔ΒͷγϣʔτΧοτʹରԠ͢Δํ๏ - iOSΞϓϦ։ൃ͜΅Ε࿩]</li> </ul> <br> <p>iOS 7ͷSafari΍ϝʔϧͰ͸֎෦ΩʔϘʔυΛ࢖༻ͨ͠ࡍʹར༻Ͱ͖ΔͰ͖ΔγϣʔτΧοτ͕[http:// www.appbank.net/2013/11/08/iphone-news/696998.php:title=ҎલΑΓॆ࣮ͨ͜͠ͱ͕࿩୊ʹͳΓ·ͨ͠ɻ]</p> <br> <p>iOS 7Ͱ͸ΩʔϘʔυγϣʔτΧοτΛ࣮૷͢ΔͨΊͷAPI͕௥Ճ͞Ε͍ͯΔͷͰɺαʔυύʔςΟͷΞϓϦέʔγϣϯ΋ ΩʔϘʔυγϣʔτΧοτʹରԠ͢Δ͜ͱ͕Ͱ͖·͢ɻ</p> <br> <p>ಛఆͷΩʔϘʔυγϣʔτΧοτʹԠ౴͢Δʹ͸ԼهͷϓϩύςΟΛ࣮૷͠·͢ɻ</p> <pre class="syntax-highlight prettyprint lang-objc"> @property(nonatomic, readonly) NSArray *keyCommands </pre>
  24. 5FYUJMF1BSTFS

  25. NSBundle *bundle = [NSBundle mainBundle]; NSString *path = [bundle pathForResource:@"textile"

    ofType:@"js"]; NSString *script = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; ! JSContext *context = [[JSContext alloc] init]; [context evaluateScript:script]; ! JSValue *func = context[@"textile"]; ! NSString *text = [NSString stringWithContentsOfFile: [bundle pathForResource:@"sample" ofType:@“textile"] encoding:NSUTF8StringEncoding error:nil]; ! NSLog(@"%@", [func callWithArguments:@[text]]);
  26. ࠓճ͸ɺεϖγϟϧηογϣϯͱͯ͠Evernoteຊ͔ࣾΒMac൛EvernoteͷUIͷϦχϡʔΞϧͷࢦشΛ͞ Ε·ͨ͠["Jack Hirsch":https://twitter.com/Jackolicious]͞Μʹ͖͍͖ͯͨͩɺഎܠ΍։ ൃख๏ͳͲΛ͓࿩͍͖ͨͩ·͢ɻ ͢΂ͯͷ։ൃऀʹͱͬͯඇৗʹ༗ҙٛͳ࣌ؒʹͳΔͱࢥ͍·͢ͷͰͥͻ͓ӽ͍ͩ͘͠͞ɻ ! ! h3. ελοϑืू !

    ౰೔ɺձ৔ͷ४උ΍ड෇ͳͲΛख఻ͬͯͩ͘͞Δ͔ͨΛืू͍ͨ͠·͢ɻख఻ͬͯ΋͍͍Αɺͱ͍͏͔ͨ ͕͍Βͬ͠Ό͍·ͨ͠Β͝࿈བྷ͍ͩ͘͞·ͤɻ ! ["lesamoureuses":https://twitter.com/lesamoureuses] ["huin":https://twitter.com/huin] ["myb":https://twitter.com/myb] ["nun_":https://twitter.com/nun_] ["KohsakuNishida":https://twitter.com/KohsakuNishida] ["৿ౢେथ":https://www.facebook.com/hiroki.with.omnia] ! ! *։৔ 12:30* **৔ॴ** גࣜձࣾVOYAGE GROUP (౦ژ౎ौ୩۠ਆઘொ8-16 ौ୩ϑΝʔετϓϨΠε8F)
  27. <p>ࠓճ͸ɺεϖγϟϧηογϣϯͱͯ͠Evernoteຊ͔ࣾΒMac൛EvernoteͷUIͷϦχϡʔΞ ϧͷࢦشΛ͞Ε·ͨ͠<a href="https://twitter.com/Jackolicious">Jack Hirsch</a>͞Μʹ͖͍͖ͯͨͩɺഎܠ΍։ൃख๏ͳͲΛ͓࿩͍͖ͨͩ·͢ɻ<br /> ͢΂ͯͷ։ൃऀʹͱͬͯඇৗʹ༗ҙٛͳ࣌ؒʹͳΔͱࢥ͍·͢ͷͰͥͻ͓ӽ͍ͩ͘͠͞ɻ</p> <h3>ελοϑืू</h3> <p>౰೔ɺձ৔ͷ४උ΍ड෇ͳͲΛख఻ͬͯͩ͘͞Δ͔ͨΛืू͍ͨ͠·͢ɻख఻ͬͯ΋͍͍ Αɺͱ͍͏͔͕͍ͨΒͬ͠Ό͍·ͨ͠Β͝࿈བྷ͍ͩ͘͞·ͤɻ</p> <p><a

    href="https://twitter.com/lesamoureuses">lesamoureuses</a><br /> <a href="https://twitter.com/huin">huin</a><br /> <a href="https://twitter.com/myb">myb</a><br /> <a href="https://twitter.com/nun_">nun_</a><br /> <a href="https://twitter.com/KohsakuNishida">KohsakuNishida</a><br /> <a href="https://www.facebook.com/hiroki.with.omnia">৿ౢେथ</a> </p> <p><strong>։৔ 12:30</strong><br /> <b>৔ॴ</b><br /> גࣜձࣾ<span class="caps">VOYAGE</span> <span class="caps">GROUP</ span><br /> (౦ژ౎ौ୩۠ਆઘொ8-16 ौ୩ϑΝʔετϓϨΠε8F)</p>
  28. +BWB4DSJQU0CKFDUJWF$

  29. +4&YQPSU

  30. +BWB4DSJQUʹެ։͢Δ 0CKFDUJWF$ͷΠϯλʔϑΣʔε

  31. @protocol JSUIWindow <JSExport> ! @property (nonatomic) CGRect frame; @property (nonatomic)

    UIColor *backgroundColor; ! + (id)new; - (void)makeKeyAndVisible; ! @end
  32. @interface JSUIWindow : UIWindow <JSUIWindow> ! @end ! @implementation JSUIWindow

    ! @end
  33. JSContext *context = [[JSContext alloc] init]; context[@"JSUIWindow"] = [JSUIWindow class];

    JSContext *context = [[JSContext alloc] init]; context[@"JSUIWindow"] = [JSUIWindow class]; ! [context evaluateScript:@"var window = JSUIWindow.new();"]; ! JSValue *value = context[@"window"]; NSLog(@"%@", value.toObject);
  34. طଘͷΫϥεΛ+4&YQPSUʹ ద߹ͤ͞Δ

  35. ! class_addProtocol([UIWindow class], @protocol(JSUIWindow)); JSContext *context = [[JSContext alloc] init];

    context[@"UIWindow"] = [UIWindow class]; [context evaluateScript:@"var window = UIWindow.new();"]; ! JSValue *value = context[@"window"]; NSLog(@"%@", value.toObject);
  36. ಈతʹ΍ͬͯΈΔ ʢͰ͖ͳ͍ʣ

  37. static void setup(JSContext *context) { const char *prefix = "JSUI";

    unsigned int numberOfClasses; Class *classes = objc_copyClassList(&numberOfClasses); for (unsigned int i = 0; i < numberOfClasses; i++) { Class cls = classes[i]; const char *className = class_getName(cls); NSString *name = @(className); char protocolName[512]; snprintf(protocolName, sizeof(protocolName), "%s%s", prefix, className); Protocol *proto = objc_allocateProtocol(protocolName); traverce_class(proto, cls); protocol_addProtocol(proto, @protocol(JSExport)); objc_registerProtocol(proto); class_addProtocol(cls, proto); context[name] = cls; } free(classes); } 4*("#35
  38. +BWB4DSJQU#SJEHF

  39. None
  40. @protocol JSBUIView; ! @protocol JSBUIAlertView <JSExport, JSBUIView> ! @property (nonatomic,

    readonly) NSInteger numberOfButtons; @property (nonatomic, readonly, getter = isVisible) BOOL visible; @property (nonatomic) NSInteger cancelButtonIndex; @property (nonatomic, copy) NSString *title; @property (nonatomic, copy) NSString *message; @property (nonatomic, assign) id delegate; @property (nonatomic, assign) UIAlertViewStyle alertViewStyle; @property (nonatomic, readonly) NSInteger firstOtherButtonIndex; ! - (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles , ...; - (NSInteger)addButtonWithTitle:(NSString *)title; - (NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex; - (void)show; - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated: (BOOL)animated; - (UITextField *)textFieldAtIndex:(NSInteger)textFieldIndex; ! #pragma clang diagnostic pop ! @end
  41. @protocol JSBUIView <JSExport, JSBUIResponder> ! @property (nonatomic, getter = isOpaque)

    BOOL opaque; @property (nonatomic) CGRect frame; @property (nonatomic, getter = isHidden) BOOL hidden; @property (nonatomic) BOOL autoresizesSubviews; @property (nonatomic, readonly, copy) NSArray *subviews; ! ! + (Class)layerClass; + (void)beginAnimations:(NSString *)animationID context:(void *)context; + (void)commitAnimations;
  42. @protocol JSBNSObject <JSExport, NSObject> ! - (BOOL)isEqual:(id)object; - (NSUInteger)hash; !

    - (Class)superclass; - (Class)class; ! - (BOOL)isProxy; ! - (BOOL)isKindOfClass:(Class)aClass; - (BOOL)isMemberOfClass:(Class)aClass; - (BOOL)conformsToProtocol:(Protocol *)aProtocol; ! - (NSString *)description; - (NSString *)debugDescription; ! + (void)load; ! + (void)initialize; - (id)init; ! + (id)new; + (id)allocWithZone:(struct _NSZone *)zone;
  43. JSContext *context = [JSBScriptingSupport globalContext]; ! [context addScriptingSupport:@“AssetsLibrary"]; [context addScriptingSupport:@"Accounts"];

    [context addScriptingSupport:@"Social"]; [context addScriptingSupport:@“MapKit"];
  44. [context evaluateScript: @"var window = UIWindow.alloc().initWithFrame(UIScreen.mainScreen().bounds);" @"window.backgroundColor = UIColor.whiteColor();" @""

    @"var navigationController = UINavigationController.new();" @"" @"var tableViewController = UITableViewController.new();" @"tableViewController.navigationItem.title = 'JavaScriptBridge';" @"navigationController.viewControllers = [tableViewController];" @"" @"window.rootViewController = navigationController;" @"" @"window.makeKeyAndVisible();" @"" @"var alertView = UIAlertView.new();" @"alertView.message = 'Hello JavaScriptBridge!';" @"alertView.addButtonWithTitle('OK');" @"alertView.show();" ];
  45. UISlider *slider = [[UISlider alloc] initWithFrame:frame]; slider.backgroundColor = [UIColor clearColor];

    slider.minimumValue = 0.0; slider.maximumValue = 100.0; slider.continuous = YES; slider.value = 50.0; var slider = UISlider.alloc().initWithFrame(frame); slider.backgroundColor = UIColor.clearColor(); slider.minimumValue = 0.0; slider.maximumValue = 100.0; slider.continuous = true; slider.value = 50.0; ϓϩύςΟΞΫηε
  46. UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; var window

    = UIWindow.alloc().initWithFrame(UIScreen.mainScreen().bounds); ϝιουݺͼग़͠
  47. UIView *view = [UIView new]; view.frame = CGRectMake(20, 80, 280,

    80); ! CGFloat x = view.frame.origin.x; CGFloat width = view.frame.size.width; var view = UIView.new(); view.frame = {x: 20, y: 80, width: 280, height: 80}; ! var x = view.frame.x; var width = view.frame.width; ߏ଄ମ
  48. ಠࣗͷ֦ு

  49. Ϋϥεఆٛ

  50. var MainViewController = JSB.defineClass('MainViewController : UITableViewController', { // Instance Method

    Definitions viewDidLoad: function() { self.navigationItem.title = 'UICatalog'; }, viewWillAppear: function(animated) { self.tableView.reloadData(); } }, { // Class Method Definitions attemptRotationToDeviceOrientation: function() { ... } });
  51. Class cls = objc_allocateClassPair(NSClassFromString(parentClassName), className.UTF8String, 0); objc_registerClassPair(cls); ! Class superClass

    = class_getSuperclass(cls); if (superClass) { setupForwardingImplementations(cls, superClass, instanceMembers, staticMembers); } ! NSString *types; BOOL result; ! Class metaClass = objc_getMetaClass(className.UTF8String); ! types = [NSString stringWithFormat: @"%s%s%s%s", @encode(NSMethodSignature), @encode(id), @encode(SEL), @encode(SEL)]; result = class_addMethod(cls, @selector(methodSignatureForSelector:), (IMP)methodSignatureForSelector, types.UTF8String); result = class_addMethod(metaClass, @selector(methodSignatureForSelector:), (IMP)methodSignatureForSelector, types.UTF8String); ! types = [NSString stringWithFormat: @"%s%s%s%s", @encode(void), @encode(id), @encode(SEL), @encode(NSInvocation)]; result = class_addMethod(cls, @selector(forwardInvocation:), (IMP)forwardInvocation, types.UTF8String); result = class_addMethod(metaClass, @selector(forwardInvocation:), (IMP)forwardInvocation, types.UTF8String); ! types = [NSString stringWithFormat: @"%s%s%s%s", @encode(BOOL), @encode(id), @encode(SEL), @encode(SEL)]; result = class_addMethod(cls, @selector(respondsToSelector:), (IMP)respondsToSelector, types.UTF8String); result = class_addMethod(metaClass, @selector(respondsToSelector:), (IMP)respondsToSelector, types.UTF8String); ! for (NSString *protocol in [protocols componentsSeparatedByString:@","]) { class_addProtocol(cls, NSProtocolFromString([protocol stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]])); } ! class_addProtocol(cls, @protocol(JSBNSObject)); ! NSString *key = mangledNameFromClass(cls); globalContext[key] = cls; globalContext[key][JSBInstanceMembersKey] = instanceMembers; globalContext[key][JSBStaticMembersKey] = staticMembers; ! return cls;
  52. Class cls = objc_allocateClassPair(NSClassFromString(parentClassName), className.UTF8String, 0); objc_registerClassPair(cls); ΫϥεΛಈతʹఆٛ

  53. NSString *types; BOOL result; ! types = [NSString stringWithFormat: @“%s%s%s%s",

    @encode(NSMethodSignature), @encode(id), @encode(SEL), @encode(SEL)]; ! result = class_addMethod(cls, @selector(methodSignatureForSelector:), (IMP)methodSignatureForSelector, types.UTF8String); ΫϥεʹϝιουΛ௥Ճ ʢΠϯελϯεϝιουʣ
  54. NFUIPE4JHOBUVSF'PS4FMFDUPS GPSXBSE*OWPDBUJPO SFTQPOET5P4FMFDUPS ϝιουݺͼग़͠Λసૹ

  55. NSMethodSignature *methodSignatureForSelector(id self, SEL _cmd, SEL selector) { NSMethodSignature *methodSignature

    = nil; Class cls = object_getClass(self); if (class_isMetaClass(cls)) { methodSignature = [cls instanceMethodSignatureForSelector:selector]; if (methodSignature) { return methodSignature; } } else { methodSignature = [cls instanceMethodSignatureForSelector:selector]; if (methodSignature) { return methodSignature; } } NSUInteger numberOfArguments = [[NSStringFromSelector(selector) componentsSeparatedByString:@":"] count] - 1; return [NSMethodSignature signatureWithObjCTypes:[[@"@@:" stringByPaddingToLength:numberOfArguments + 3 withString:@"@" startingAtIndex:0] UTF8String]]; } ϝιουݺͼग़͠Λసૹ
  56. BOOL respondsToSelector(id self, SEL _cmd, SEL selector) { NSString *propertyName

    = propertyNameFromSelector(selector); JSValue *function = propertyForObject(self, propertyName); return !function.isUndefined; } ϝιουݺͼग़͠Λసૹ
  57. void forwardInvocation(id self, SEL _cmd, NSInvocation *invocation) { JSContext *context

    = [JSBScriptingSupport globalContext]; if ([[self superclass] instancesRespondToSelector:invocation.selector]) { invokeSuper(invocation); } id currentSelf = context[@"self"]; context[@"self"] = self; NSString *propertyName = propertyNameFromSelector(invocation.selector); JSValue *function = propertyForObject(self, propertyName); if (!function.isUndefined) { NSArray *arguments = extractArguments(invocation); JSValue *returnValue = [function callWithArguments:arguments]; setReturnValue(returnValue, invocation); } context[@"self"] = currentSelf; } ϝιουݺͼग़͠Λసૹ
  58. NSString *types; BOOL result; ! Class metaClass = objc_getMetaClass(className.UTF8String); !

    types = [NSString stringWithFormat: @"%s%s%s%s", @encode(NSMethodSignature), @encode(id), @encode(SEL), @encode(SEL)]; ! result = class_addMethod(metaClass, @selector(methodSignatureForSelector:), (IMP)methodSignatureForSelector, types.UTF8String); ΫϥεʹϝιουΛ௥Ճ ʢΫϥεϝιουʣ
  59. JSValue *propertyForObject(id obj, NSString *propertyName) { JSContext *context = [JSBScriptingSupport

    globalContext]; JSValue *properties = nil; Class cls = object_getClass(obj); if (class_isMetaClass(cls)) { properties = context[mangledNameFromClass(obj)][JSBStaticMembersKey]; } else { properties = context[mangledNameFromClass(cls)][JSBInstanceMembersKey]; } return properties[propertyName]; } JEܕͷΦϒδΣΫτ͕ ΫϥεΦϒδΣΫτ͔Ͳ͏͔
  60. unsigned int numberOfInstanceMethods = 0; Method *instanceMethods = class_copyMethodList(cls, &numberOfInstanceMethods);

    for (unsigned int i = 0; i < numberOfInstanceMethods; i++) { Method method = instanceMethods[i]; struct objc_method_description *description = method_getDescription(method); NSString *propertyName = propertyNameFromSelector(description->name); JSValue *function = instanceFunctions[propertyName]; if (!function.isUndefined) { class_addMethod(targetClass, description->name, _objc_msgForward, description->types); } } if (instanceMethods) { free(instanceMethods); } ΫϥεʹϝιουΛ௥Ճ ʢΦʔόʔϥΠυʣ
  61. class_addMethod(targetClass, description->name, _objc_msgForward, description->types); ϝιουݺͼग़͠Λసૹ

  62. @PCKD@NTH'PSXBSE

  63. ! OBJC_EXPORT id _objc_msgForward(id receiver, SEL sel, ...) __OSX_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);

  64. void forwardInvocation(id self, SEL _cmd, NSInvocation *invocation) { JSContext *context

    = [JSBScriptingSupport globalContext]; if ([[self superclass] instancesRespondToSelector:invocation.selector]) { invokeSuper(invocation); } id currentSelf = context[@"self"]; context[@"self"] = self; NSString *propertyName = propertyNameFromSelector(invocation.selector); JSValue *function = propertyForObject(self, propertyName); if (!function.isUndefined) { NSArray *arguments = extractArguments(invocation); JSValue *returnValue = [function callWithArguments:arguments]; setReturnValue(returnValue, invocation); } context[@"self"] = currentSelf; } ʢͳΜͱͳ͘ʣ+4ͱ0CK$͕ର࿩Ͱ͖Δ
  65. Ϟδϡʔϧ ʢͳΜͪΌͬͯʣ

  66. var ButtonsViewController = JSB.require('buttonsViewController'); var ControlsViewController = JSB.require('controlsViewController'); var WebViewController

    = JSB.require('webViewController'); var MapViewController = JSB.require('mapViewController'); ! var MainViewController = JSB.defineClass('MainViewController : UITableViewController', { viewDidLoad: function() { self.navigationItem.title = 'UICatalog'; ... }); JSB.exports = MainViewController;
  67. w 8FCΤϯδχΞ w J04ΤϯδχΞ w σβΠφʔ ! w ϢϏϨδΛࢧ͑Δٕज़ w

    IUUQBLJTVUFDPNCMPHQPTUIUNM w ब׆೔ه  ϢϏϨδ๚໰ w IUUQMBJTPIBUFOBCMPHDPNFOUSZ&##&###&"&"@ @&"&&"$&#&"""&'