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:
- Create by hand the minimal part of the objClass
ObjClassand then - Use it to create normally
ObjObjectobjClass and then - 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
- As the
initializeof this first phase of the bootstrap is not easy we give you its code. Note that the definition of the objMethodinitializeis done in the primitive methoddefineManualInitializeMethodIn:.
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.
- In the objMethod
manualObjClassStructure, the instance variable inheritance is simulated. Indeed the instance variable array (that represents the instance variable of the class) contains#classthat should normally be inherited fromObjObjectas we will see in the third phase of the bootstrap. - Note that the class is declared into the class repository using the method
declareClass:. - Note the method
#initializeis method of the metaclassObjClass: when you create a class the initialize method is invoked on a class! TheinitializeobjMethod defined onObjClasshas two aspects: the first one deals with the initialization of the class like any other instance (first line). This behavior is normally done using a super call to invoke theinitializemethod defined inObjObject. The final version of theinitializemethod will do it using perform. The second one deals with the initialization of classes: it performs the instance variable inheritance, then computes the keywords of the newly created class. Note in this final step that the keyword array does not contain the#class:keyword because we do not want to let the user modify the class of an object.
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
- the objMethod
classthat given an objInstance returns its class (the objInstance that represents the class). - the objMethod
isClassthat returns false. - the objMethod
isMetaClassthat returns false. - the objMethod
errorthat takes two arguments the receiver and the selector of the original invocation and raises an error. - the objMethod
getIVthat takes the receiver and an attribute name, aSymbol, and returns its value for the receiver. - the objMethod
setIVthat takes the receiver, an attribute name and a value and sets the value of the given attribute to the given value. - the objMethod
initializethat takes the receiver and an initargs-list and initializes the receiver according to the specification given by the initargs-list. Note that here theinitializemethod only fills the instance according to the specification given by the initargs-list. Compare with theinitializemethod defined onObjClass.
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
- Make sure that you read and execute the test method:
testCreateObjObjectMessage - Make sure that you read and execute the test method:
testCreateObjObjectInstanceMessage
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:
- the primitive
objClassStructurethat creates theObjClassclass by invoking thenewmessage to the classObjClass. Note that during this method theObjClasssymbol refers to two different entities because the new class that is created using the old one is declared in the class dictionary with the same name.
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:
- the objMethod
isClassthat returns true. - the objMethod
isMetaclassthat returns true.
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:#()'.
- the primitive
defineInitializeMethodIn: anObjClassthat adds the objMethodinitializeto the objClass passed as argument. The objMethodinitializetakes the receiver (an objClass) and an initargs-list and initializesthe receiver according to the specification given by the initargs-list. In particular, it should be initialized as any other object, then it should compute its instance variable (i.e., inherited instance variables are computed), the keywords are also computed, the method dictionary should be defined and the class is then declared as an existing one. We provide the following template to help you.
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:
- The locally specified instance variables now are just the instance variables that describe a class. The instance variable
classis inherited fromObjObject. - The
initializemethod now does a super send to invoke the initialization performed byObjObject.
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:
- First just create the class
ObjPoint. - Create an instance of the class
ObjPoint. - Send some messages defined in
ObjObjectto this instance.
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:
- define a metaclass that automatically defines accessors for the specified instances variables.
- avoid that we can change the selector and the arguments when calling a super send.
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:.