NODOBJC
Bringing Objective-C to Node.js
By Nathan Rajlich
JSConf Argentina
May 2012
Slide 2
Slide 2 text
Hola JSConf Argentina!
Slide 3
Slide 3 text
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
Slide 4
Slide 4 text
ABOUT ME
• Lucky enough to be one of the engineers on
the awesome Learnboost team
Slide 5
Slide 5 text
OBJECTIVE-C
So who here has written an OS X or iOS app?
Slide 6
Slide 6 text
OBJECTIVE-C
• A “strict superset” of C
• Object-oriented
• Has a “message-sending” syntax influenced by SmallTalk
WHAT IS IT?
Slide 7
Slide 7 text
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
Slide 8
Slide 8 text
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
Slide 9
Slide 9 text
OBJECTIVE-C
TODAY
Objective-C == Apple
Slide 10
Slide 10 text
OBJECTIVE-C
WTF?
This is JSConf!
Why am I talking about Objective-C?
Slide 11
Slide 11 text
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
Slide 12
Slide 12 text
STORY TIME!
+
iTunes (on OS X) offers 2 APIs for interacting with it:
• AppleScript
• Objective-C’s “ScriptingBridge” framework
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.
Slide 15
Slide 15 text
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...
Slide 16
Slide 16 text
WE CAN DO BETTER!
So I thought to myself...
Slide 17
Slide 17 text
• 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
Slide 18
Slide 18 text
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
Slide 19
Slide 19 text
• 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!!!!
Slide 20
Slide 20 text
So then I thought to myself...
THERE’S GOT TO BE
ANOTHER WAY!
Slide 21
Slide 21 text
MACRUBY
Soon, I was introduced to MacRuby
I WANT THIS, BUT IN NODE
Slide 22
Slide 22 text
No content
Slide 23
Slide 23 text
INTRODUCING...
NodObjC
The NodeJS ⁵ Objective-C bridge
gh://TooTallNate/NodObjC
Slide 24
Slide 24 text
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
Slide 25
Slide 25 text
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
Slide 26
Slide 26 text
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”
Slide 27
Slide 27 text
NODOBJC
Importing Frameworks
Relative Lookup:
Absolute Lookup:
Importing a custom framework?
See: man gen_bridge_metadata
Slide 28
Slide 28 text
NODOBJC
Message-sending syntax
In Objective-C:
In NodObjC:
Slide 29
Slide 29 text
[ do Node REPL live demo... ]
Slide 30
Slide 30 text
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()”
Slide 31
Slide 31 text
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
Slide 32
Slide 32 text
MUCHAS GRACIAS!
Thanks for listening!
Questions?
Comments?