AjTalk: Implementing an Smalltalk-like interpreter (Part 1) Object structure

I start writing about the design decisions I had to take to build AjTalk, my open source Smalltalk-like interpreter. There is a long tradition in building Smalltalk virtual machines; let see what I could do and how.

In this first post I want to explore how to implement the core concept of any compiler or interpreter that tries to use an Smalltalk-like object.
An object in classic Smalltalk has an internal state, represented by instance variables (leave aside the indexed variables). A first view:

In many implementations (including the early ones, like the classic Smalltalk-80), each object contains values ??of one type: either it is an object containing single-byte or single words, or just pointers to other objects:

It is also usual that some values??, rather than pointers, ??are "primitive" values as an integer. To get the difference between a pointer to another object and an integer value, some bits of the word that stores the pointer / integer value are used to distinguish between the two kinds. A pointer can points to another object:

Another implementation style, is to use an object table instead of a direct pointer:

A table of objects has, per cell, representing object information, such as type (an object of bytes? of pointers?), its class, and the location of its internal state (instance variable values). The object table strategy simplifies operations like “become:” and the garbage collection. It can be used to remove the object from memory and retrieve it when a message is send to it. The cell had the persistence information.

How do I implemented all these features in AjTalk? From the beginning I wanted AjTalk could access and manage .NET native objects. So I chose to have two types of objects: the natives .NET objects (strings, arrays, integers, reals, any framework object…), and the AjTalk proper objects. So instead of handling objects that contain only bytes or chars, or pointers, I decided that AjTalk objects have instance variables whose values ??point to any object (.NET or AjTalk).

Internally, an AjTalk object keep an array of object values?:

public class BaseObject : IObject, ISerializable
    private IBehavior behavior;
    private object[] variables; // pointers to objects
    // ...

An element of the array can point to an integer, a char, ie, value objects, or to objects in memory. .NET achieves this using boxing, the price to pay to get this flexibility. My implementation use the native mechanism of boxing/unboxing. Fortunately, the implementation details are directly managed by. NET. From the point of view of my C # code, all this management is totally transparent. This solution delegates the management of garbage collection (freeing the memory of unreferenced objects, unreachable from the active object graph) to the .NET implementation.

Next steps: explore how to implement message and their invocation, methods implementation, classes and inheritance, and more.

Keep tuned!

Angel “Java” Lopez



One thought on “AjTalk: Implementing an Smalltalk-like interpreter (Part 1) Object structure

  1. Pingback: Smalltalks 2011 Argentina: Your Conference « Angel “Java” Lopez on Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s