My favorites | Sign in
Project Logo
                
Search
for
Updated Nov 21, 2008 by parmanoir
SampleBridgeEvaluation  

How does the bridge evaluate a Javascript expression and calls back ObjC, bridging results back and forth ? See also HowJSCocoaWorks, JSCocoaInternals

Calling NSWorkspace.sharedWorkspace.activeApplication.valueForKey('NSApplicationName')

  • 1 Finding no variable named NSWorkspace, Javascript queries our global object for NSWorkspace via its getProperty callbackOSXObject_getProperty. This method will retrieve an ObjC class or query BridgeSupport for C functions, structures, and enums.
  • 2 OSXObject_getProperty found an ObjC class. It packages it in a Javascript Object via boxing — this javascript object holds private data in the form of an instance of JSCocoaPrivateObject containing a reference to the class, NSWorkspace
  • 3 The boxed object goes back to Javascript
  • 4 Javascript queries it for a property name sharedWorkspace. As the returned object belongs to us, it has some callbacks — Javascript calls jsCocoaObject_getProperty
  • 5 jsCocoaObject_getProperty unboxes the Javascript object it receives to discover the packaged class NSWorkspace (packaged in step2)
  • 6 jsCocoaObject_getProperty calls respondsToSelector on the packaged class with the given property name, sharedWorkspace — respondsToSelector returns true
  • 7 sharedWorkspace is a zero argument method — jsCocoaObject_getProperty now calls the method and packages the result back to Javascript (like step2)
  • 8 steps 4-7 happen in the same way
  • 9 Javascript calls againjsCocoaObject_getProperty asking for valueForKey. No zero argument method is found, we package back to Javascript a JSCocoaPrivateObject holding type and method name
  • 10 Javascript calls jsCocoaObject_callAsFunction with the step9 packaged method object. JSCocoa checks if method is a SplitCall. If not, it checks if the object responds to the given method name BUT with one argument added, calling respondsToSelector with valueForKey:(note the semi colon). The object responds.
  • 11 jsCocoaObject_callAsFunction converts arguments to native ObjC data with JSCocoaFFIArguments and calls the method with libffi
  • 12 jsCocoaObject_callAsFunction packages the result back to Javascript
  • 13 Not knowing how to handle that object, Javascript calls valueOf on it — a callback that will convert native data into Javascript strings and numbers. Javascript will then have a Javascript string containing the application name

Calling myWindow.title = 'hello'

  • 1 Javascript calls the setter callback jsCocoaObject_getProperty with propertyName = 'title' and value = 'hello'
  • 2 jsCocoaObject_getProperty checks if propertyName can be set by converting it from title to setTitle: and checking if the object responds to that selector
  • 3 If the object responds, jsCocoaObject_getProperty convert the argument 'hello' to a native ObjC string with JSCocoaFFIArgument and calls setTitle: via libffi

Sign in to add a comment
Hosted by Google Code