Revision [235]

This is an old revision of ObjectiveC made by JohnMcIntosh on 2008-10-17 21:51:45.


This is the third attempt at having an objective-C bridge in Squeak. Historically there is one written by Marcel Weiher, and another by Avi Brant
This plugin does work on the carbon os-x VM (3.18.x) which makes testing easier, however much of the iPhone specific features are
not available under OS-X.

The goal of this plugin was to have it coded entirely in slang and minimize the amount of code that is actually in the plugin. In general
only a handful of people actually build plugins, so the process of updating that code for fixes or enhancements is slow. If the majority
of the code is in Smalltalk then it's easier for anyone to submit a fix or enhancement to the MC server.

On the Smalltalk side a decision was made to use ProtoObject as the super class so we could use Does Not Understand to redirect method invocation to the Objective-C objects. This was a bit troublesome because it required bringing down a large number of methods from Object to make an objective-c class/subclass to work. These methods include:

('printing' longPrintOn:limitedTo:indent: longPrintStringLimitedTo: printOn: printString printStringLimitedTo:)
('smalltalk methods' asExplorerString defaultLabelForInspector error: halt inspect inspectorClass instVarAt: isKindOf: isMorph isNumber isString primitiveFailed smalltalkClass)

The ObjectiveCObject class contains one instance variable externalAddress which is the address of the Objective-C object.
We note at shutdown time if quitting all instances and subinstances of ObjectiveCObject have their externalAddress set to zero,
because they are only valid for the currently running image, not across image restarts.

If the address is zero and an attempt is made to use it as a valid Objective C address then a walkback will occur.

A small number of accessors are available:

descriptionAsString - Invoke description against the Objective-C object and return the result as a smalltalk string
externalAddress - access the address with error check for zero
externalAddressPrivate - PRIVATE access the address with no error check, do not use this
externalAddress: - Set the address of the Objective-C object
objectiveCClass - Return the class of the object, we note asking the object for class will give you the smalltalk class.
objectiveCHash - Return the Objective-C hash
objectiveCSuperClass - Return the Objective-C super class

= - Return false if not the same class, otherwise return true if the same address, or invoke isEqual:to:
hash - Return the objective-C hash, if the object is nil then return hash of the ObjectiveC NSNull singleton


Other methods implemented were:

('testing' ifNil: ifNil:ifNotNil: ifNotNil: ifNotNil:ifNil: isNil isObjectiveCObject)

Which check to see if the object is NULL.
NULL here means is the external Address zero, meaning C language NULL, or is the Objective-C object NSNull?

isObjectiveCObject - is true for class/subclasses of ObjectiveCObject, false otherwise, it's used by the DNU logic.

Class Side methods are:
findClassName: - use to find the class object for the given class name (a smalltalk string)
newWithExternalAddress: - used by the internal logic to build a new instance from an objective-C address

Because there is a limited number of class side and instance side methods likely the Objective-C method you want to
invoke does not exist as a smalltalk method. This means Does Not UnderStand will be triggered, this then uses the
doesNotUnderstand: method to forward the request to the ObjectiveCBridge class for resolution.


The plugin includes an Objective C class squeakSUnitTester to enable testing.
Say we want to create an instance of the class, then invoke the test1unsignedchar: method which returns the
supplied unsigned character we pass in as a value.
classOop := ObjectiveCObject findClassName: 'squeakSUnitTester'.
instance := classOop new.
char := instance test1unsignedchar: $B.
self should: [char = $B].

As you see once we resolve the classOop we just send messages to the instance like we would do with a normal Smalltalk object.
(Keep in mind the few specialized methods for class and superclass)

Note we could have done
instance := classOop alloc init.

We note in these examples the Smalltalk parser may complain that some method you are trying to use does not exist in the image, this of course is true, just confirm your choice and continue. The parser is warning you invoking this method will result in a DNU, but we *are* wanting that...


Subclassing ObjectiveCObject.
The reason for subclassing ObjectiveCObject is to provide accesssor methods that avoid most of the DNU logic. We have one example of this the ObjectiveCNSMethodSignature class which is frequently used to resolve the method signatures to invoke in the DNU logic. Thus we code calls to
methodNumberOfArguments methodReturnLength methodReturnType to invoke the required primitives otherwise the DNU logic would run into a recursive call situation.

There are 3 comments on this page. [Show comments]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki