2 Diving into the kernel

In this chapter, we will describe one by one all the aspects of the kernel. We will cover instance shapes, methods, instance and inheritance relationship interplay, object allocation, object and class initialization...

2.1 Instances

In this kernel, there is only one instantiation link; it is applied at all levels as shown by Figure 2.1:

Chain of instantiation: classes are objects, too.
Figure 2.1 Chain of instantiation: classes are objects, too.

In our diagrams, we represent objects (mainly terminal instances) as rounded rectangles with a list of instance variable values. Since classes are objects, when we want to stress that classes are objects we use the same graphical convention as shown in Figure 2.4.

Handling infinite recursion

A class is an object. Thus it is an instance of another class, its metaclass. This metaclass is an object, too, an instance of a metametaclass that is an object, too, an instance of another metametametaclass, etc. To stop this potential infinite recursion, ObjVlisp is similar to solutions proposed in many meta-circular systems: one instance (e.g., Class) is an instance of itself.

In ObjVLisp:

We will see later the implication of this self-instantiation at the level of the class structure itself.

2.2 Understanding metaclasses

The model unifies classes and instances. It follows from the instance-related postulates of the kernel that:

At the implementation level, there is only one kind of entity: objects. There is no special treatment for classes. Classes are instantiated following the same process as terminal instances. They are sent messages in the same way that other objects are sent messages.

This unification between instances and classes does not mean that objects and classes have the same distinction. Indeed not all the objects are classes. In particular, the sole difference between a class and an instance is the ability to respond to the creation message: new. Only a class knows how to respond to it. Then, metaclasses are just classes whose instances are classes as shown in Figure 2.2.

Everything is an object. Classes are just objects that can create other objects, and metaclasses are just classes whose instances are classes.
Figure 2.2 Everything is an object. Classes are just objects that can create other objects, and metaclasses are just classes whose instances are classes.

2.3 Instance structure

The model does not really bring anything new about instance structure when compared with languages such as Pharo or Java.

Instance variables are represented as a list of instance variable defined by a class. Such instance variables are shared by all instances. The values of such instance variables are specific to each instance. Figure 2.3 shows that instances of Workstation have two values: a name and a next node.

Instances of  Workstation  have two values: their names and their next node.
Figure 2.3 Instances of Workstation have two values: their names and their next node.

In addition, we note that an object has a pointer to its class. As we will see when we discuss inheritance later on, every object possesses an instance variable class (inherited from Object) that points to its class.

Note that this management of a class instance variable defined in Object is specific to the model. In Pharo for example, the class identification is not managed as a declared instance variable, but as an element part of any object. It is an index in a class table.

2.4 About behavior

Let us continue with basic instance behavior. As in modern class-based languages, this kernel has to represent how methods are stored and looked up.

Methods belong to a class. They define the behavior of all the instances of the class. They are stored in a method dictionary that associates a key (the method selector) and the method body.

Since methods are stored in a class, the method dictionary should be described in the metaclass. Therefore, the method dictionary of a class is the value of the instance variable methodDict defined on the metaclass Class. Each class will have its own method dictionary.

2.5 Class as an object

Here is the minimal information that a class should have:

This minimal state is similar to that of Pharo: the Pharo Behavior class has a format (compact description of instance variables), a method dictionary, and a superclass link.

In ObjVLisp, we have a name to identify the class. As an instance factory, the metaclass Class possesses four instance variables that describe a class:

Since a class is an object, a class has the instance variable class inherited from Object that refers to its class as any object.

Point  class as an object.
Figure 2.4 Point class as an object.

Example 1: class Point

Figure 2.4 shows the instance variable values for the class Point as declared by the programmer and before class initialization and inheritance take place.

Class  as an object.
Figure 2.5 Class as an object.

Example 2: class Class

Figure 2.5 describes the class Class itself. Indeed it is also an object.

Through the prism of objects.
Figure 2.6 Through the prism of objects.

Everything is an object

Figure 2.6 describes a typical situation of terminal instances, classes, and metaclasses when viewed from an object perspective. We see three levels of instances: terminal objects (mac1 and mac2 which are instances of Workstation), class objects (Workstation and Point which are instances of Class) and the metaclass (Class which is an instance of itself).

Sending a message is a two-step process: method lookup and execution.
Figure 2.7 Sending a message is a two-step process: method lookup and execution.

2.6 Sending a message

In this kernel, the second postulate states that the only way to perform computation is via message passing.

Sending a message is a two-step process as shown by Figure 2.7:

  1. Method lookup: the method corresponding to the selector is looked up in the class of the receiver and its superclasses.
  2. Method execution: the method is applied to the receiver. This means that self or this in the method will be bound to the receiver.

Conceptually, sending a message can be described by the following function composition:

sending a message (receiver argument)
	 return apply (lookup (selector classof(receiver) receiver) receiver arguments)

Method lookup

Now the lookup process is conceptually defined as follows:

  1. The lookup starts in the class of the receiver.
  2. If the method is defined in that class (i.e., if the method is defined in the method dictionary), it is returned.
  3. Otherwise the search continues in the superclass of the currently explored class.
  4. If no method is found and there is no superclass to explore (if we are in the class Object), this is an error (i.e., the method is not defined).

Looking for a method is a two-step process: first, go to the class of receiver then follow inheritance.
Figure 2.8 Looking for a method is a two-step process: first, go to the class of receiver then follow inheritance.

The method lookup walks through the inheritance graph one class at a time using the superclass link. Here is a possible description of the lookup algorithm that will be used for both instance and class methods.

lookup (selector class receiver):
   if the method is found in class
      then return it
      else if class == Object
           then send the message error to the receiver
           else lookup (selector superclass(class) receiver)

When a message is not found, another message is sent to the receiver supporting reflective operation.
Figure 2.9 When a message is not found, another message is sent to the receiver supporting reflective operation.

2.7 Handling unknown messages

When the method is not found, the message error is sent as shown in Figure 2.9. Sending a message instead of simply reporting an error using a trace or an exception is a key design decision. In Pharo, this is done via the doesNotUnderstand: message, and it is an important reflective hook. Indeed classes can define their own implementation of the method error and perform specific actions to the case of messages that are not understood. For example, it is possible to implement proxies (objects representing other remote objects) or compile code on the fly by redefining such a message locally.

Now it should be noted that the previous algorithm has a limitation when a missing method has an arbitrary number of arguments. They are not passed to the error message. A better way to handle this is to decompose the algorithm differently as follows:

lookup (selector class):
   if the method is found in class
      then return it
      else if class == Object
           then return nil
           else lookup (selector superclass(class))

And then we redefined sending a message as follows:

sending a message (receiver argument)
   methodOrNil = lookup (selector classof(receiver)).
   if methodOrNil is nil
      then send the message error to the receiver
      else return apply(methodOrNil receiver arguments)

Remarks

This lookup is conceptually the same as in Pharo where all methods are public and virtual. There are no statically bound methods; even class methods are looked up dynamically. This allows for defining very elegant and dynamic registration mechanisms.

While the lookup happens at runtime, it is often cached. Languages usually have several systems of caches, e.g., global (class, selector), one per call site, etc.

2.8 Inheritance

In this kernel, there are two aspects of inheritance to consider:

Let's look at these two aspects.

Instance variable inheritance

Instance variable inheritance is done at class creation time. From that perspective, it is static and performed once. When a class C is created, its instance variables are the union of the instance variables of its superclass and the instance variables defined locally in class C. Each language defines the exact semantics of instance variable inheritance, for example, if they accept instance variables with the same name or not. In our model, we decide to use the simplest way: there should be no duplicate names.

instance-variables(aClass) =
	union (instance-variables(superclass(aClass)), local-instance-variables(aClass))

A word about the union of instance variables: when the implementation of the language is based on offsets to access instance variables, the union should make sure that the locations of inherited instance variables are kept ordered compared to the superclass. In general, we want to be able to apply methods of the superclass to subclasses without copying them down and recompiling them. Indeed if a method uses a variable at a given position in the instance variable lists, applying this method to instances of subclasses should work. In the implementation proposed next chapter, we will use accessors and will not support direct access to instance variables from a method body.

Method lookup

As previously described in Section 2.6, methods are looked up at runtime. Methods defined in superclasses are reused and applied to instances of subclasses. Contrary to instance variable inheritance, this part of inheritance is dynamic, i.e., it happens during program execution.

2.9 Object: defining the minimal behavior of any object

Object represents the minimal behavior that any object should understand, e.g., returning the class of the object, being able to handle errors, and initializing the object. This is why Object is the root of the hierarchy. Depending on the language, Object can be complex. In our kernel it is kept minimal as we will show in the implementation chapter.

Figure 2.10 shows the inheritance graph without the presence of instantiation. A Workstation is an object (i.e., it should at least understand the minimal behavior), so the class Workstation inherits directly or indirectly from the class Object. A class is also an object (i.e., it should understand the minimal behavior) so the class Class inherits from the class Object. In particular, the class (note the lowercase) instance variable is inherited from Object class.

Full inheritance graph: Every class ultimately inherits from  Object .
Figure 2.10 Full inheritance graph: Every class ultimately inherits from Object.

Remark

In Pharo, the class Object is not the root of inheritance. The root is in fact ProtoObject, and Object inherits from it. Most of the classes still inherit from Object. The design goal of ProtoObject is special: It supports the maximum generation of errors. Such errors can be captured via redefinition of the message doesNotUnderstand: and can support different scenarios such as implementing proxies.

2.10 Inheritance and instantiation together

Now that we have seen the instantiation and the inheritance graphs, we can examine the complete picture. Figure 2.11 shows the graphs and in particular how such graphs are used during message resolution:

This process is the same when we send messages to the classes themselves. There is no difference between sending a message to an object or a class. The system always performs the same steps.

Kernel with instantiation and inheritance link.
Figure 2.11 Kernel with instantiation and inheritance link.

2.11 Review of self and super semantics

Since our experience showed us that even some book authors got the essential semantics of object-oriented programming wrong, we review here some facts that programmers familiar with object-oriented programming should master. For further readings refer to Pharo By Example or the Pharo Mooc available at http://mooc.pharo.org.

As explained in Section 2.6, sending a message to an object always starts the lookup the corresponding method in the class of the receiver.

Now we should distinguish two cases: self and super. In the body of a method, both self (also called this in languages like Java) and super always represent the receiver of the message. Yes you read it well, both self and super always represent the receiver!

The difference lies in the class from where the lookup starts:

When a message is sent to super, the lookup starts in the superclass of the method's class.

This distinction between self and super is required to handle the case where a method is redefined locally in a class but that you need to invoke the behavior defined in its superclasses. Note that the superclass method may be defined not in a direct superclass but one of the class ancestor, so there is a need for a method lookup and this method lookup should start above the method redefined it (here the method containing the super expression). Hence the name super.

Note that the lookup of a method in the case of super does not look in the superclass of the class of the receiver, since this would mean that it may loop forever in the case of inheritance tree with three classes.

Looking at Figure 2.12 we see that the key point is that B new bar returns 50 since the method is dynamically looked up and self represents the receiver, i.e., the instance of the class B. What is important to see is that sendings of self act as a hook and that subclasses' code can be injected in superclass code.

A new foo
>>> 10
B new foo
>>> 50
A new bar
>>> 10
B new bar
>>> 50

self always represents the receiver.
Figure 2.12 self always represents the receiver.

Sequence diagram of self always represents the receiver.
Figure 2.13 Sequence diagram of self always represents the receiver.

For super, the situation depicted in Figure 2.14 shows that super represents the receiver, but that when super is the receiver of a message, the method is looked up differently (starting from the superclass of the class using super) hence R new bar returns 100, but neither 20 nor 60.

super represents the receiver, but the method lookup starts in the superclass of the class of the method using super.
Figure 2.14 super represents the receiver, but the method lookup starts in the superclass of the class of the method using super.

Q new bar
>>> 20
R new bar
>>> 100

As a conclusion, we can say that self is dynamic and super static. Let us explain this view:

2.12 Object creation

Now we are ready to understand the creation of objects. In this model, there is only one way to create instances: we should send the message new to the class with a specification of the instance variable values as arguments.

2.13 Creation of instances of the class Point

The following examples show several point instantiations. What we see is that the model inherits from the Lisp tradition of passing arguments using keys and values, and that the order of arguments is not important.

Point new :x 24 :y 6
>>> aPoint (24 6)
Point new :y 6 :x 24
>>> aPoint (24 6)

When there is no value specified, the value of an instance variable is initialized to nil. It is worth to see that in CLOS (Common Lisp Object System) Stee90a provides the notion of default values for instance variable initialization. It can be added to ObjVlisp as an exercise and does not bring conceptual difficulties.

Point new
>>> aPoint (nil nil)

When the same argument is passed multiple times, then the implementation takes the first occurrence.

Point new :y 10 :y 15
>>> aPoint (nil 10)

We should not worry too much about such details: The point is that we can pass multiple arguments with a tag to identify them.

2.14 Creation of the class Point instance of Class

Since the class Point is an instance of the class Class, to create it, we should send the message new to the class as follows:

Class new
   :name 'Point'
   :superclass 'Object'
   :iv #(x y)
>>> aClass

What is interesting to see here is that we use exactly the same way to create an instance of the class Point as the class itself. Note that the possibility of having the same way to create objects or classes is also due to the fact that the arguments are specified using a list of pairs.

An implementation could have two different messages to create instances and classes. As soon as the same new, allocate, or initialize methods are involved, the essence of the object creation is similar and uniform.

Instance creation: Role of the metaclass

The following diagram (Figure 2.15) shows that despite what one might expect when we create a terminal instance the metaclass Class is involved in the process. Indeed, we send the message new to the class, to resolve this message, the system will look for the method in the class of the receiver (here Workstation) which is the metaclass Class. The method new is found in the metaclass and applied to the receiver, the class Workstation. Its effect is to create an instance of the class Workstation.

Metaclass role during instance creation: Applying plain message resolution. Right: situation before - Left: situation after the instance creation.
Figure 2.15 Metaclass role during instance creation: Applying plain message resolution. Right: situation before - Left: situation after the instance creation.

The same happens when creating a class. Figure 2.16 shows the process. We send a message, now this time, to the class Class. The system makes no exception and to resolve the message, it looks for the method in the class of the receiver. The class of the receiver is itself, so the method new found in Class is applied to Class (the receiver of the message), and a new class is created.

Metaclass role during class creation: Applying plain message resolution - the self instantiation link is followed.
Figure 2.16 Metaclass role during class creation: Applying plain message resolution - the self instantiation link is followed.

new = allocate and initialize

Creating an instance is the composition of two actions: a memory allocation allocate message and an object initialization message initialize.

In Pharo syntax, it means:

aClass new: args = (aClass allocate) initialize: args

We should see the following:

Object allocation: the message allocate

Allocating an object means allocating enough space to the object state but there's more: instances should be marked with their class name or id. There is an invariant in this model and in general in object-oriented programming models. Every single object must have an identifier to its class, else the system will break when trying to resolve a message.

Object allocation should return a newly created instance with:

In the ObjVLisp model, the marking of an object as an instance of a class is performed by setting the value of the instance variable class inherited from Object. In Pharo, this information is not recorded as an instance variable but encoded in the internal object representation in the virtual machine.

The allocate method is defined on the metaclass Class. Here are some examples of allocation.

Point allocate
>>> #(Point nil nil)

A point allocation allocates three slots: one for the class and two for x and y values.

Class allocate
>>> #(Class nil nil nil nil nil)

The allocation for an object representing a class allocates six slots: one for class and one for each of the class instance variables: name, super, iv, keywords (see below), and methodDict.

Object initialization

Object initialization is the process of passing arguments as key/value pairs and assigning the value(s) to the corresponding instance variable(s).

This is illustrated in the following snippet. An instance of class Point is created and the key/value pairs (:y 6) and (:x 24) are specified. In the following snippet :y and :x are keywords in ObjVLisp parlance. They are built automatically from the instance variables and are used to uniquely identify values.

In the following snippet, the instance is created and it receives the initialize: message with the key/value pairs. The initialize: method is responsible for setting the corresponding variables in the receiver.

Point new :y 6  :x 24
>>> #(Point nil nil) initialize: (:y 6 :x 24)]
>>> #(Point 24 6)

When an object is initialized as a terminal instance, two actions are performed:

Class initialization

During its initialization, a class should perform several steps:

2.15 The Class class

Now we get a better understanding of what is the class Class:

In particular, metaclasses define three messages related to instance creation.

There is a difference between the method initialize: executed on any instance creation and the class initialize: method only executed when the created instance is a class.

2.16 Conclusion

At this stage you saw all the concepts of this minimal object-oriented kernel where classes are themselves instances of other classes. In the following chapter, we explore more metaclasses and we encourage you to read it because it will shed an interesting light on the model.