JSCocoa uses 4 basic building blocks :
- JavascriptCore, WebKit's Javascript engine. JavascriptCore lets you define your own classes and setup callbacks for each event on your objects : get a property, set a property, call, convert to type
- BridgeSupport lives in every framework of Leopard. It's an xml file describing each class, C function, structure, and constant that each framework handles
- dlopen and dlsym : two Dynamic Library functions. dlopen loads a framework dynamically, dlsym retrieves a symbol (a pointer to a callable function or NSString constant)
- ObjCRuntime is a set of functions to list and create ObjC classes and their methods
We can now make our own Javascript objects, understand the structure of frameworks, get pointers to call C functions, and understand and create ObjC classes and methods.
From there, we can call ObjC (but not C). But we're still only guests of ObjCRuntime, as we can't create new classes and write their methods in Javascript. We need something to make us first class citizens, a piece of code that will make our Javascript callable by ObjCRuntime :
- libffi : a library that lets us create callable C functions of any MethodEncoding. ObjC methods are C functions with two extra parameters : the current instance (self) and its selector. libffi calls these FFIclosures. libffi also lets us call these pointers, which mean we can now call C functions !
And that's it ! With this, we're fully bridged. We can call pretty much anything from any framework and give to ObjCRuntime our own Javascript code to be executed.
Want to know even more ?
- JSCocoaInternals details classes in the project.
- SampleBridgeEvaluation details how Javascript code gets bridged to ObjC, what objects are created, how variables are converted between C/ObjC and JSCocoa.