advertisement

Article:
  Browsing Cocoa with F-Script
Subject:   Some more remarks
Date:   2001-12-26 05:50:53
From:   pmougin
Response to: remarks on topology

A key point is that the structure described is actually present, in memory, for each Objective-C program during run-time. The "is instance of" and "inherits from" relationship are represented by mere pointers. This structure is created and managed automatically by the Objective-C run-time.



The main purpose of the in-memory representation of each class is to host a correlation table, which associates method names (in the form of selectors) with pointers to the actual compiled code of the methods. This is the basic information the messaging system needs in order to work.



This table is of course based on the information that was given to the compiler in the source code. In particular, instance methods (which are syntactically marked by "-" at the beginning of their declarations in Objective-C source code) are referenced in the correlation table of the corresponding class. Class methods (which are marked with "+") are referenced in the correlation table of the corresponding meta-class.



When a message is sent to an object, the run-time system exploit this structure in order to find the actual implementation of the method (the compiled code to execute). The first step is to traverse, just once, the "is instance of" relationship, from the receiver to its class. This allows the messaging system to get to the class of the object. Then, the messaging system looks into the correlation table hosted inside the class object in order to find the method that was invoked. If the method is not present in the table, the system follow the "inherits from" relationship, and try to find the method in the table of the super class. It goes upper in the hierarchy like this until it find the method or reach the root class.



Since classes are themselves objects, one can send them messages. The process presented below is applied exactly in the same way. Since NSObject is the root class for not only the class hierarchy but also for the meta-class hierarchy, instances methods defined in NSObject can also be invoked on classes.



Phil