6 Defining the language kernel

Now you have implemented all the primitives we need, you are ready to define the two classes that represent the ObjVLisp kernel: ObjClass and ObjObject. Once such classes will exist we will be able to code using ObjVLisp and not in Pharo anymore. The moment where we go from the low-level world (here Pharo) to the high-level one (here ObjVLisp) is generally called the bootstrap of the language. Indeed after this last phase, our new language exists.

To implement these two classes we could them by manipulating low-level primitives and doing all the plumbing ourselves as what we did in the test setUp methods. But there is something better to do. We can use the high level language to use itself to define itself. This is what we will explain in this chapter.

6.1 About bootstrap

to bootstrap the system: this means creating the kernel consisting of ObjObject and ObjClass classes from themselves. The idea of a smart bootstrap is to be as lazy as possible and to use the system to create itself by creating a fake but working first class with which we will build the rest.

Three steps compose the ObjVlisp bootstrap:

  1. Create by hand the minimal part of the objClass ObjClass and then
  2. Use it to create normally ObjObject objClass and then
  3. Recreate normally and completely ObjClass.

These three steps are described by the following bootstrap method of Obj class. Note the bootstrap is defined as class methods of the class Obj.

Obj class >> bootstrap
      "self bootstrap"

      self initialize.
      self manuallyCreateObjClass.
      self createObjObject.
      self createObjClass.

To help you to implement the functionality of the objClasses ObjClass and ObjObject, we defined another set of tests in the class ObjTestBootstrap. Read them.

6.2 Manually creating ObjClass

The first step is to create manually the class ObjClass. By manually we mean create an array (because we chose an array to represent instances and classes in particular) that represents the objClass ObjClass, then define its methods. You will implement/read this in the primitive manuallyCreateObjClass as shown below:

Obj class >> manuallyCreateObjClass
   "self manuallyCreateObjClass"

   | class |
   class := self manualObjClassStructure.
   Obj declareClass: class.
   self defineManualInitializeMethodIn: class.
   self defineAllocateMethodIn: class.
   self defineNewMethodIn: class.
   ^ class

We will comment some of the methods called in manuallyCreateObjClass.

For this purpose, you have to implement/read all the primitives that compose it.

Your job.

At the class level in the protocol 'bootstrap objClass manual' read or implement: the primitive manualObjClassStructure that returns an objObject that represents the class ObjClass.

Make sure that you execute the test method: testManuallyCreateObjClassStructure

Obj class >> defineManualInitializeMethodIn: class

   class
    addMethod: #initialize
    args: 'initArray'
    withBody:
      '| objsuperclass |
      objself initializeUsing: initArray.  "Initialize a class as an object. In the bootstrapped system will be done via super"
      objsuperclass := Obj giveClassNamed: objself objSuperclassId ifAbsent: [nil].
      objsuperclass isNil
        ifFalse:
          [ objself
               objIVs: (objself computeNewIVFrom: objsuperclass objIVs with: objself objIVs)]
        ifTrue:
          [ objself objIVs: (objself computeNewIVFrom: #(#class) with: objself objIVs)].
      objself
          objKeywords: (objself generateKeywords: (objself objIVs copyWithout: #class)).
      objself objMethodDict: (IdentityDictionary new: 3).
      Obj declareClass: objself.
      objself'

Note that this method works without inheritance since the class ObjObject does not exist yet.

The primitive defineAllocateMethodIn: anObjClass defines in anObjClass passed as argument the objMethod allocate. This allocate method takes only one argument: the class for which a new instance is created as shown below:

defineAllocateMethodIn: class

   class
      addUnaryMethod: #allocate
      withBody: 'objself allocateAnInstance'

Its definition is simple, it just calls the primitive allocateAnInstance.

Following the same principle, define the primitive defineNewMethodIn: anObjClass that defines in anObjClass passed as argument the objMethod new. new takes two arguments: a class and an initargs-list. It invokes the objMethod allocate and initialize.

Your job.

Make sure that you read and execute the test method: testManuallyCreateObjClassAllocate

Remarks

Read carefully the following remarks below and the code.

6.3 Creation of ObjObject

Now you are in the situation where you can create the first real and normal class of the system: the class ObjObject. To do that you send the message new to class ObjClass specifying that the class you are creating is named #ObjObject and only has one instance variable called class. Then you will add the methods defining the behavior shared by all the objects.

Your job: objObjectStructure

Implement/read the following primitive objObjectStructure that creates the ObjObject by invoking the new message to the class ObjClass:

Obj class >> objObjectStructure

   ^ (self giveClassNamed: #ObjClass)
      send: #new
      withArguments: #(#(#name: #ObjObject #iv: #(#class)))

The class ObjObject is named ObjObject, has only one instance variable class, and does not have a superclass because it is the root of the inheritance graph.

Your job: createObjObject

Now implement the primitive createObjObject that calls objObjectStructure to obtain the objObject representing objObject class and define methods in it. To help you we give here the beginning of such a method

Obj class >> createObjObject
   | objObject |
   objObject := self objObjectStructure.
   objObject addUnaryMethod: #class withBody: 'objself objClass'.
   objObject addUnaryMethod: #isClass withBody: 'false'.
   objObject addUnaryMethod: #isMetaclass withBody: 'false'.
   ...
   ...
   ^ objObject

Implement the following methods in ObjObject

Make sure that you read and execute the test method: testCreateObjObjectStructure

In particular, notice that this class does not implement the class method new because it is not a metaclass but does implement the instance method initialize because any object should be initialized.

Your job: run the tests

6.4 Creation of ObjClass

Following the same approach, you can now recreate completely the class ObjClass. The primitive createObjClass is responsible for creating the final class ObjClass. So you will implement it and define all the primitives it needs. Now we only define what is specific to classes, the rest is inherited from the superclass of the class ObjClass, the class ObjObject.

Obj class >> createObjClass
   "self bootstrap"

   | objClass |
   objClass := self objClassStructure.
   self defineAllocateMethodIn: objClass.
   self defineNewMethodIn: objClass.
   self defineInitializeMethodIn: objClass.
   objClass
     addUnaryMethod: #isMetaclass
     withBody: 'objself objIVs includes: #superclass'.
  "an object is a class if is class is a metaclass. cool"

   objClass
     addUnaryMethod: #isClass
     withBody: 'objself objClass send: #isMetaclass withArguments:#()'.

   ^ objClass

To make the method createObjClass work we should implement the method it calls. Implement then:

Your job.

Make sure that you read and execute the test method: testCreateObjClassStructure. Now implement the primitive createObjClass that starts as follow:

Obj class >> createObjClass

   | objClass |
   objClass := self objClassStructure.
   self defineAllocateMethodIn: objClass.
   self defineNewMethodIn: objClass.
   self defineInitializeMethodIn: objClass.
   ...
   ^ objClass

Also define the following methods:

objClass
   addUnaryMethod: #isMetaclass
   withBody: 'objself objIVs includes: #superclass'.

   "an object is a class if is class is a metaclass. cool"
objClass
  addUnaryMethod: #isClass
  withBody: 'objself objClass send: #isMetaclass withArguments:#()'.
Obj class>>defineInitializeMethodIn: objClass

   objClass
     addMethod: #initialize
     args: 'initArray'
     withBody:
        'objself super: #initialize withArguments: {initArray} from: superclassOfClassDefiningTheMethod.
         objself objIVs: (objself
                  computeNewIVFrom:
                        (Obj giveClassNamed: objself objSuperclassId) objIVs
                  with: objself objIVs).
        objself computeAndSetKeywords.
        objself objMethodDict: IdentityDictionary new.
        Obj declareClass: objself.
        objself'
Obj class >> defineInitializeMethodIn: objClass

  objClass
     addMethod: #initialize
     args: 'initArray'
     withBody:
         'objself super: #initialize withArguments: {initArray} from: superclassOfClassDefiningTheMethod.
         objself objIVs: (objself
           computeNewIVFrom: (Obj giveClassNamed: objself objSuperclassId) objIVs
           with: objself objIVs).
         objself computeAndSetKeywords.
         objself objMethodDict: IdentityDictionary new.
         Obj declareClass: objself.
         objself'

Your job.

Make sure that you execute the test method: testCreateObjClassMessage.

Note the following points:

6.5 First User Classes: ObjPoint

Now that ObjVLisp is created and we can start to program some classes. Implement the class ObjPoint and ObjColoredPoint. Here is a possible implementation.

You can choose to implement it at the class level of the class Obj or even better in class named ObjPointTest.

Pay attention that your scenario covers the following aspects:

Define the class ObjPoint so that we can create points as below (create a Pharo method to define it).

ObjClass send: #new
   withArguments: #((#name: #ObjPoint #iv: #(#x y) #superclass: #ObjObject)).
aPoint := pointClass send: #new withArguments: #((#x: 24 #y: 6)).
aPoint send: #getIV withArguments: #(#x).
aPoint send: #setIV withArguments: #(#x 25).
aPoint send: #getIV withArguments: #(#x).

Then add some functionality to the class ObjPoint like the methods x, x:, display which prints the receiver.

Obj ObjPoint
   addUnaryMethod: #givex
   withBody: 'objself valueOfInstanceVariable: #x '.
Obj ObjPoint
   addUnaryMethod: #display
   withBody:
    'Transcript cr;
      show: ''aPoint with x = ''.
    Transcript show: (objself send: #givex withArguments: #()) printString;
   cr'.

Then test these new functionalities.

aPoint send: #x withArguments: #().
aPoint send: #x: withArguments: #(33).
aPoint send: #display withArguments: #().

6.6 First User Classes: ObjColoredPoint

Following the same idea, define the class ObjColored.

Create an instance and send it some basic messages.

aColoredPoint := coloredPointClass
   send: #new
   withArguments: #((#x: 24 #y: 6 #color: #blue)).
aColoredPoint send: #getIV withArguments: #(#x).
aColoredPoint send: #setIV withArguments: #(#x 25).
aColoredPoint send: #getIV withArguments: #(#x).
aColoredPoint send: #getIV withArguments: #(#color).

Your job.

Define some functionality and invoke them: the method color, implement the method display so that it invokes the superclass and adds some information related to the color. Here is an example:

coloredPointClass addUnaryMethod: #display
   withBody:
     'objself super: #display withArguments: #() from: superclassOfClassDefiningTheMethod.
      Transcript cr;
         show: '' with Color = ''.
      Transcript show: (objself send: #giveColor withArguments: #()) printString; cr'.
aColoredPoint send: #x withArguments: #().
aColoredPoint send: #color withArguments: #().
aColoredPoint send: #display withArguments: #()

6.7 A First User Metaclass: ObjAbstract

Now implement the metaclass ObjAbstract that defines instances (classes) that are abstract i.e., that cannot create instances. This class should raise an error when it executes the new message.

Then the following shows you a possible use of this metaclass.

ObjAbstractClass
   send: #new
   withArguments: #(#(#name: #ObjAbstractPoint
            #iv: #()
            #superclass: #ObjPoint)).

ObjAbstractPoint send: #new
   withArguments: #(#(#x: 24 #y: 6))        "should raise an error"

You should redefine the new method. Note that the ObjAbstractClass is an instance of ObjClass because this is a class and inherits from it because this is a metaclass.

6.8 New features that you could implement

You can implement some simple features:

Shared Variables

Note that contrary to the proposition made in the 6th postulate of the original ObjVLisp model, class instance variables are not equivalent to shared variables. According to the 6th postulate, a shared variable will be stored in the instance representing the class and not in an instance variable of the class representing the shared variables. For example, if a workstation has a shared variable named domain. But the domain should not be an extra instance variable of the class of Workstation. Indeed domain has nothing to do with class description.

The correct solution is that domain is a value held into the list of the shared variable of the class Workstation. This means that a class has extra information to describe it: an instance variable sharedVariable holding pairs. So we should be able to write:

Obj Workstation getIV: #sharedVariable
or
Obj Workstation sharedVariableValue: #domain

and get
 #((domain 'inria.fr'))

Introduce shared variables: add a new instance variable in the class ObjClass to hold a dictionary of shared variable bindings (a symbol and a value) that can be queried using specific methods: sharedVariableValue: and sharedVariableValue:put:.