$30 off During Our Annual Pro Sale. View Details »

NodObjC - JSConf.ar 2012

NodObjC - JSConf.ar 2012

The slides for my talk from JSConf.ar 2012. I introduce NodObjC and explain what my purposes for creating it were.

tootallnate

May 21, 2012
Tweet

More Decks by tootallnate

Other Decks in Technology

Transcript

  1. NODOBJC
    Bringing Objective-C to Node.js
    By Nathan Rajlich
    JSConf Argentina
    May 2012

    View Slide

  2. Hola JSConf Argentina!

    View Slide

  3. ABOUT ME
    • My name is Nathan Rajlich
    • I have been loving JavaScript for about 5 years
    • I have been playing with Node.js for a little over 2 years now
    • Have written a slew of useful and not-so-useful modules for
    Node that are available through npm
    • One of the Node.js core committers

    View Slide

  4. ABOUT ME
    • Lucky enough to be one of the engineers on
    the awesome Learnboost team

    View Slide

  5. OBJECTIVE-C
    So who here has written an OS X or iOS app?

    View Slide

  6. OBJECTIVE-C
    • A “strict superset” of C
    • Object-oriented
    • Has a “message-sending” syntax influenced by SmallTalk
    WHAT IS IT?

    View Slide

  7. OBJECTIVE-C
    WHAT IS IT?
    bracket-syntax
    =
    sending messages
    C functions are (still)
    first-class citizens
    Objective-C “objects”
    are standard C
    pointers
    Method names and
    arguments are
    intertwined

    View Slide

  8. OBJECTIVE-C
    • Originally gained traction through Steve Jobs’ former
    company: NeXT
    • When NeXT got acquired by Apple in 1996, Objective-C was
    adapted to create applications for Mac OS X
    • Has gained more popularity in the last few years because of
    iOS and the App Store
    HISTORY

    View Slide

  9. OBJECTIVE-C
    TODAY
    Objective-C == Apple

    View Slide

  10. OBJECTIVE-C
    WTF?
    This is JSConf!
    Why am I talking about Objective-C?

    View Slide

  11. STORY TIME!
    Let’s go back to August 2010...
    • I was still new to Node.js
    • I was still in the mode where I try to do everything with Node
    • So naturally, I wanted to interact with iTunes using Node.js
    • The ultimate goal being to write a web app that would stream
    audio/video from an iTunes installation to a regular web
    browser using the and tags

    View Slide

  12. STORY TIME!
    +
    iTunes (on OS X) offers 2 APIs for interacting with it:
    • AppleScript
    • Objective-C’s “ScriptingBridge” framework

    View Slide

  13. ATTEMPT #1:
    NODE-APPLESCRIPT
    gh://TooTallNate/node-applescript

    View Slide

  14. NODE-APPLESCRIPT
    The AppleScript command
    we want to execute
    The callback function to
    invoke when the
    command completes
    AppleScript can return all kinds
    of values:
    Array, Object, Number, String,
    Date, Buffer, etc.

    View Slide

  15. NODE-APPLESCRIPT
    • You have to manually construct Strings for the commands
    • Internally uses a child process for every command
    • Who wants to write English to create programs anyway?
    It Works!
    But... it’s not so great after all...

    View Slide

  16. WE CAN DO BETTER!
    So I thought to myself...

    View Slide

  17. • A native Node.js binding to the “iTunes.h” header file
    • Has the opportunity to massage into a nice JavaScript API
    • Should be a lot faster than using “node-applescript” to
    communicate with iTunes
    ATTEMPT #2:
    NODE-ITUNES
    gh://TooTallNate/node-iTunes

    View Slide

  18. NODE-ITUNES
    You create a
    connection to
    iTunes first
    Every function is
    asynchronous and
    expects a callback
    function
    The API attempts
    to map to the
    “iTunes.h” file as
    best as possible

    View Slide

  19. • Quickly got out-of-control in terms of files and LOC
    • Every exposed API from “iTunes.h” needs accompanying
    bridge code to expose it to JavaScript
    • Worse yet, it can only interact with one “iTunes object” at a
    time (a list of objects requires separate thread pool calls)
    NODE-ITUNES
    Too much code!!!!

    View Slide

  20. So then I thought to myself...
    THERE’S GOT TO BE
    ANOTHER WAY!

    View Slide

  21. MACRUBY
    Soon, I was introduced to MacRuby
    I WANT THIS, BUT IN NODE

    View Slide

  22. View Slide

  23. INTRODUCING...
    NodObjC
    The NodeJS ⁵ Objective-C bridge
    gh://TooTallNate/NodObjC

    View Slide

  24. NODOBJC
    • MacRuby
    • JSCocoa
    • PyObjC
    A few other “bridges” to Objective-C:
    NodObjC is conceptually very similar to these
    other bridges, except that it runs in Node.js

    View Slide

  25. NODOBJC
    • Started developing it in July 2010
    • Still a relatively young project, and not yet feature-
    complete
    • Already at the point where you can create full-fledged
    iOS or OS X applications using nothing but JavaScript
    and Node.js

    View Slide

  26. NODOBJC
    Module-mode Global-mode
    vs.
    NodObjC has 2 modes of operation:
    Use “module-mode” when
    creating a module that
    depends on NodObjC
    Use “global-mode” when
    creating an application, or
    some other “end product”

    View Slide

  27. NODOBJC
    Importing Frameworks
    Relative Lookup:
    Absolute Lookup:
    Importing a custom framework?
    See: man gen_bridge_metadata

    View Slide

  28. NODOBJC
    Message-sending syntax
    In Objective-C:
    In NodObjC:

    View Slide

  29. [ do Node REPL live demo... ]

    View Slide

  30. FUTURE PLANS
    • NodObjC is at an interesting stage of its development
    • It is very usable, but there are some edge-case features that
    may or may not be show-stoppers for some people
    • C Blocks are only partially implemented
    • Needs some better handling of BridgeSupport files
    • Needs support for vararg functions, like “NSLog()”

    View Slide

  31. FUTURE PLANS
    • node-ffi, NodObjC’s core dependency, is currently finishing up
    a big refactor for its v1.0 release.
    • NodObjC will have to be refactored a little to use node-ffi 1.0
    • NodObjC’s API will likely remain unchanged after the refactor

    View Slide

  32. MUCHAS GRACIAS!
    Thanks for listening!
    Questions?
    Comments?

    View Slide