Angel \”Java\” Lopez on Blog

May 20, 2012

March 22, 2012

AjTalk in Javascript (1) First implementations

Some days ago, I moved my AjTalk project from Google Code to GitHub:

https://github.com/ajlopez/AjTalk

Last year, I added the compilation of Smalltalk fileouts to Javascript, into that project. Past Sunday, I created a new project at:

https://github.com/ajlopez/AjTalkJs

as code kata for another approach: implementing an AjTalk virtual machine, from scratch, in JavaScript. The idea is to compile Smalltalk code to methods in bytecodes, and to have an interpreter for those methods. The code is at´

https://github.com/ajlopez/AjTalkJs/blob/master/lib/ajtalk.js

In that implementation, I’m using a sendMessage method and a custom lookup for methods. Excerpt (lib/ajtalk.js):

BaseObject.prototype.sendMessage = function(selector, args)
{
    var method = this.lookup(selector);
    return method.apply(this, args);
};

function BaseClass(name, instvarnames, clsvarnames, supercls) {
    this.name = name;
    this.instvarnames = instvarnames;
    this.clsvarnames = clsvarnames;
    this.supercls = supercls;
    this.methods = {};
};

BaseClass.prototype.__proto__ = BaseObject.prototype;

BaseClass.prototype.defineMethod = function (selector, method)
{
    this.methods[selector] = method;
};

BaseClass.prototype.getInstanceSize = function() {
    var result = this.instvarnames.length;
    if (this.supercls)
        result += this.supercls.getInstanceSize();

    return result;
};

BaseClass.prototype.lookupInstanceMethod = function (selector)
{
    var result = this.methods[selector];
    if (result == null && this.supercls)
        return this.supercls.lookupInstanceMethod(selector);
    return result;
};

At Monday, I had new ideas for implementing the virtual maching. I could use the prototype features of Javascript. Then, I wrote some new code (lib/ajtalknew.js):

function createClass(name, superklass, instvarnames, clsvarnames)
{
    var protoklass = new Function();

    if (superklass)
    {
        // Chain class prototypes
        protoklass.prototype.__proto__ = superklass.proto;
    }
    else
    {
        // First class methods
        protoklass.prototype.basicNew = function()
        {
            var obj = new this.func;
            obj.klass = this;
            return obj;
        }

        protoklass.prototype.defineSubclass = function(name, instvarnames, clsvarnames)
        {
            return createClass(name, this, instvarnames, clsvarnames);
        }

        protoklass.prototype.defineMethod = function(name, method)
        {
            var mthname = name.replace(/:/g, '_');
            if (typeof method == "function")
                this.func.prototype[mthname] = method;
            else
                this.func.prototype[mthname] = method.toFunction();
        }

        protoklass.prototype.defineClassMethod = function(name, method)
        {
            var mthname = name.replace(/:/g, '_');
            if (typeof method == "function")
                this.proto[mthname] = method;
            else
                this.proto[mthname] = method.toFunction();
        }

        // TODO Quick hack. It should inherits from Object prototype
        protoklass.prototype.sendMessage = function(selector, args)
        {
            return this[selector].apply(this, args);
        }
    }

    var klass = new protoklass;

    // Function with prototype of this klass instances
    klass.func = new Function();
    klass.proto = protoklass.prototype;
    klass.name = name;
    klass.super = superklass;
    klass.instvarnames = instvarnames;
    klass.clsvarnames = clsvarnames;

    klass.func.prototype.klass = klass;

    if (superklass)
    {
        // Chaining instances prototypes
        klass.func.prototype.__proto__ = superklass.func.prototype;
    }
    else
    {
        // First instance methods
        klass.func.prototype.sendMessage = function(selector, args)
        {
            return this[selector].apply(this, args);
        }
    }

    Smalltalk[name] = klass;

    return klass;
}

createClass('Object');

Smalltalk.Object.defineClassMethod('compileMethod:', function(text)
    {
        var compiler = new Compiler();
        var method = compiler.compileMethod(text, this);
        this.defineMethod(method.name, method);
        return method;
    });

Smalltalk.Object.defineClassMethod('compileClassMethod:', function(text)
    {
        var compiler = new Compiler();
        var method = compiler.compileMethod(text, this);
        this.defineClassMethod(method.name, method);
        return method;
    });

Ok, it’s a bit tricky, but it works!

Notably, I wrote test using Node builtin assert module (I had no Internet connection, and had no NodeUnit in my machine). The code was nurtured using TDD and it was a good experience for me.

I have a javascript Compiler function/”class” that I can use to compile Smalltalk code to my bytecode version. Only a few bytecodes are implemented:

var ByteCodes = {
    GetValue: 0,
    GetArgument: 1,
    GetLocal: 2,
    GetInstanceVariable: 3,
    GetGlobalVariable: 4,
    GetSelf: 5,
    SetLocal: 10,
    SetInstanceVariable: 11,
    SetGlobalVariable: 12,
    Add: 20,
    Subtract: 21,
    Multiply: 22,
    Divide: 23,
    SendMessage: 40,
    Return: 50
};

Pending work: implement class variables, metaclasses, with TDD. Improve hmtl samples

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

December 13, 2011

AjTalk And Javascript (Part 2) Compiling a Simple Class

Filed under: AjTalk, JavaScript, Open Source Projects, Programming Languages, Smalltalk — ajlopez @ 10:01 am

Previous Post

In this post I will explain with an example how Smalltalk code can be compiled to Javascript using AjTalk.Compiler, named ajtalkc. As usual, the code is in my repo:

http://code.google.com/p/ajtalk/

I filed out a simple Pharo class: Point. Top fragment:

Object subclass: #Point
	instanceVariableNames: 'x y'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Graphics-Primitives'!
!Point commentStamp: '<historical>' prior: 0!
I represent an x-y pair of numbers usually 
designating a location on the screen.!
!Point methodsFor: '*Polymorph-Geometry' stamp: 'gvc 10/31/2006 11:01'!
directionToLineFrom: p1 to: p2
	"Answer the direction of the line from the receiver
	position.
	< 0 => left (receiver to right)
	= => on line
	> 0 => right (receiver to left)."
	^((p2 x - p1 x) * (self y - p1 y)) -
		((self x - p1 x) * (p2 y - p1 y))! !
....

I compiled it using the ajtalkc.exe command line program (see AjTalk.Compiler console project in AjTalk solution; it has a CodeFiles folder with my fileouts):

ajtalkc CodeFiles\PharoCorePoint.st

A new file is created: Program.js. Let's explore how a class is represented in Javascript. These lines define the Point class as a Javascript function, and its class as another Javascript function:

function PointClass()
{
}
function Point()
{
}

Nothing special. The following two lines are:

Point.prototype.__class = PointClass.prototype;
Point.classPrototype = PointClass.prototype;

In this way, every instance of Point class has a variable __class that points to the singleton PointClass.prototype. And the function/”class” Point has a variable classPrototype to point to the same singleton, if it needed.

How to create a new instance of point? Using a PointClass method, in this case defining its basicNew method:

PointClass.prototype['_basicNew'] = function() { return new Point(); };

I added an underscore to avoid name collision with any Javascript common method.

Now, how to assure all instances have two instance variables? Defining them at the propotype:

Point.prototype.x = null;
Point.prototype.y = null;

And the most interesting par: how to translate a method? Let be the min method in Smalltalk:

min
	"Answer a number that is the minimum
	of the x and y of the receiver."
	^self x min: self y! !

Its translation to a method in the Point prototype:

Point.prototype['_min'] = function()
{
    var self = this;
    return send(send(self, '_x'), '_min_', [send(self, '_y')]);
};

Notice that I defined the variable self to be used in the rest of the method (specially in any Javascript closure, where this could not survive). And the use of an utility method send that takes: a target object, the name of the method to invoke, and an array with the arguments.

All this is work in progress. Now, I'm generating additional lines to use Program.js  from Node.js, but if I removed them, I could use the code at client side, from the browser. I should write different compilers for server and client sides. I plan to write a translator to Ruby, too, just as a proof of concept of the power of having an abstract model of the program to execute.

Next topics: compiler options; compiling to browser, to node; how to run in Node.js; using the generated Javascript code; utility functions, inheritance, blocks and closures.

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

October 27, 2011

Smalltalks 2011 Argentina: Your Conference

Filed under: AjTalk, Smalltalk, Software Development — ajlopez @ 9:20 am

A new edition of Smalltalks conference is coming. More info at:

http://www.fast.org.ar/smalltalks2011

There is an active Smalltalk community in my country, and its work is recognized in the Smalltalk world. It’s good to see their renew its effort organizing this year conference.

But you could say that Smalltalk has few relations with your daily work. But the organizer think different. This is the press announce I received:

Did you know that the Smalltalks Conference will be held again this year? (http://www.fast.org.ar/smalltalks2011)
Maybe you’re wondering why you should care about a conference that deals with a language you most certainly don’t use for your job, and of whose existence you may not even be aware. But if you take 5′ to read this I can tell you that you won’t regret it and that after that you will probably be eager to come to the conference :-)

To begin with, this conference isn’t only about a programming language, it’s about a technology and a development culture which still has a wide influence on our profession. For example, last year Gilad Bracha came to the conference. Who is Gilad Bracha? Maybe the name rings a bell… well, that’s because he’s one of the people behind Dart, Google’s new language (http://www.dartlang.org/). And what does that have to do with Smalltalk? Precisely: Gilad Bracha was one of the creators of Strongtalk (http://www.strongtalk.org/), the fastest Samlltalk at that time, which used adaptive compilation, Polimorphic Inline Caching (PIC), optional variable typing, etc. – all of which are being implemented now in Dart. This year one of his closest collaborators will visit us, Vassili Bykov, who implemented the UI of Newspeak, the latest language he has been working on.
But Smalltalk has to do not only with what’s happening with Dart, but with Ruby as well… Have you heard about MagLev? (http://ruby.gemstone.com/) It’s Ruby’s server for transactional and automatically persistent objects. Guess where that comes from… MagLev is the implementation of Ruby running on GemStone/S, a Transactional and Persistent object server for Smalltalk that is more than 25 years old and that has been bought by VMWare because of its great potential as a transactional memory manager for Java. If you didn’t know, read here: http://www.springsource.com/products/data-management/gemfire65
And where does the conference come in here? Martin McClure himself, responsible for MagLev, and GemStone architect Norman Green are coming! The guy in the know! Do you have any doubts about object bases? Now you know where the answers are.

But maybe you’re not interested in any if this… perhaps the way programming languages work or how they are implemented is not your scene, you just make web applications and only want the infrastructure to scale, to be quick at persisting information etc. In that case we also have a place for you. Have you heard of GLASS? (http://seaside.gemstone.com/) It’s the implementation of Seaside, a dynamic framework based on continuations for web applications using GemStone! (http://www.seaside.st/). That’s to say, you develop a web application just like a desktop application, and you get transactionality and persistence at the object level, for free… and what’s better, without relational databases!!! Yes!!!! No more hibernating, no more SQL, no more tables, only objects! It may sound crazy, you may think it doesn’t make sanse… my advice is, don’t draw any conclusions until you come and hear Dale Heinrichs, who is in charge of this product and who will tell us all the details and explain how it is impacting web developments.

Not convinced yet? OK, let me try just a bit more… Do you know Alan Kay? Turing award, “father of the personal computer”, creator of Smalltalk? (http://en.wikipedia.org/wiki/Alan_Kay) No, he’s not coming – yet. But Ian Piumarta and Kim Rose are, two of his closest collaborators in the projects he’s currently working on at his foundation dedicated to minimal programming languages such as OMeta and learning environments like SqueakLand (http://www.vpri.org/index.html). Are you interested in the use of computers for teaching? You can ask Kim. Would you like to know how a good VM is implemented? Ian will be right there to tell you.

If you’re still reading and haven’t scrolled down to the bottom of the page it means I haven’t convinced you yet… hmmm, let’s see what you say about this: MOOSE (http://www.moosetechnology.org), a platform for analyzing your programs – no matter if they are written in Java, C++, C# or Smalltalk, you can visualize your system’s design, not by using those little UML diagrams but by means of graphics specially designed to let you spot at a glance some bugs that may have crept in. Its developer, Tudor Girba, will be there to explain how it works, how it was developed and what you can do with it, because it’s free!
Maybe you’re already tired of reading. I don’t blame, but don’t you blame me either! It’s a great conference! You just can’t miss it! Because this is not all… if you want to find out more about the main Smalltalk development environments, both open source and commercial, you will have the chance to talk to Markus Denker from Pharo (http://www.pharo-project.org/home) and John O’Keefe, architect of VASmalltalk (http://www.instantiations.com/).

A little too much stuff that is industry-oriented? And what about research, do they keep researching on Smalltalk? Well, let me tell you that this will be the second year the conference has a section devoted entirely to research, with an enviable review committee and publication in journals. This way, if you’re doing research on objects and need to present your work at a widely recognized conference, Smalltalks is your place. And I wouldn’t like to forget the university… Smalltalk is still the language used to for teaching objects at almost every university, instead of a merely commercial language.

But I haven’t told you yet about the most important part of this, besides all these people that will be visiting us and with whom we can share our experiences: The Argentine Smalltalk community, one of the most important worldwide concerning this technology. This community has been putting their best efforts during the last five years to organize these conferences, and the last three years some of its members have won the 1st and the 3rd place at the ESUG Technology Award, an international award for the best developments in Smalltalk! And the best part of it is that the winners are from different universities – the UBA, the UAI and the UTN!
The community does not stop there. Did you know that there is an Argentine Smalltalk? A Smalltalk developed by an Argentinian and which is being used all over the world? It’s called CUIS and it was developed by Juan Vuletich (http://www.jvuletich.org/Cuis/Index.html), who is also working on the Morphic 3.0 project, and worked together with Alan Kay developing Squeak. Or did you know that the most widely used layer open source for communicating with relational databases from Pharo or Squeak was developed by an Argentinian too? Or that Fuel, the open source object serialization framework, was also created by an Argentinian? Are you familiar with these names – Mariano Martinez Peck, Guillermo Polito, Martín Dias, Esteban Lorenzano, and others? They are also part of our community and a constant reminder of the excellent technical quality we have in our country. Another Argentinian is the architect of the fastest Smalltalk VM in existence, that of VisualWorks (http://www.cincomsmalltalk.com/main/products/visualworks/), and you can ask him how he was able to speed up the GC about 70% during the last year, and you will have the chance to listen to another Argentinian who has been a Smalltalker for more than 20 years… Can you imagine what your productivity would be if you had been working for 20+ years on the same language? On a language that keeps being productive to our profession? These people are part of this great community and will also be at this wonderful meeting… which is important not only for the people who are coming, but also for those that are already here!
I hope I’ve been able to convince you. I hope you’ve realized that this is not a conference about a programming language, but about a community of developers who want to share with you all they know, and also learn from you. If you want to help this community to keep growing, if you want this to be not just a conference of developers but also for developers, sign up here: http://www.fast.org.ar/smalltalks2011

It’s free, and I can grant you that you won’t regret it. You can see the list of talks at:http://www.fast.org.ar/smalltalks2011/talks

This year it takes place on November 3-5, at the University of Quilmes. And it won’t be restricted to the world of objects: this year we will also have a talk on objects by Fidel (Pablo E. Martínez López), one of the leading Argentinians in the field of Functional Programming, a community that shares the conviction that we are all, after all, programmers! See the response it is already getting:

http://vimeo.com/30529851

We’ll be waiting for you!

FAST
http://www.fast.org.ar

Smalltalks 2010 was a great conference, with high level and interesting presentation. And they accepted my presentation about AjTalk! (Spanish post, Anglish video here thanks to @MartinSalias). This is the schedule of the upcoming conference:

http://www.fast.org.ar/smalltalks2011/schedule

They accepted my talk, again! ;-) This year, I’ll present Smalltalk relations with Javascript (and C#), based on my current work on AjTalk and other implementations.

Related posts:

AjTalk And Javascript (Part 1) Abstract Model
Smalltalk And Javascript
AjTalk: Implementing An Smalltalk-Like Interpreter (Part 1) Object Structure

Keep tuned!

Angel “Java” Lopez
http://www.ajlopez.com
http://twitter.com/ajlopez

October 13, 2011

AjTalk and Javascript (Part 1) Abstract Model

Filed under: AjTalk, JavaScript, Open Source Projects, Programming Languages, Smalltalk — ajlopez @ 9:24 am

Next Post

Three weeks ago I wrote the post:

Smalltalk and Javascript

This year I write a lot of Javascript code, so I’m more confident using it. As I commented in the above post, I’m working to extend my open source Smalltalk-alike VM AjTalk (implemented using C#) to generate Javascript code (yes, me too! ;-) . This first post is dedicated to describe the new abstract model that resides inside AjTalk library project.

AjTalk parses Smalltalk-alike code to memory (methods as bytecodes) and executes it. But now, I have a new abstract Model in memory. This is the current Model folder:

The idea is to have in memory representations of classes, methods, expressions. Some interfaces:

There are many classes that implement IExpression: SetExpression, ConstantExpression, MessageExpression, ArrayExpression, etc. The most interesting is MessageExpression: it represents code that has a receiver, message selector and other expressions as parameters:

a + b. “binary messages”
anObject doSomething. “unary messages”
anObject do: aParameter with: anotherParameter. “keyword messages”

The ModelParser class is in charge of reading an stream of chars and converts it to an abstract model in memory:

Actually, I could parse an file out of Object class from Squeak. My idea is to process all the Squeak kernel classes (I’m a bit lazy to implement them ;-)

The main classes of the obtained model:

CodeModel has elements: each chunk parsed to memory is added to the code model in process. If a chunk represents the definition of a new class, a ClassModel object is created. For each method defined, a MethodModel is created.

Now, I can manipulate this abstract representation in memory. I want to experiment using this model in an interpreter pattern: visit expressions evaluating them, instead of generation of bytecodes. But now, I can use a visitor pattern to traverse the model in memory and translate it to Javascript code:

These are the involved classes:

Notice that I could implement other visitors: maybe to generate other code (Ruby? server side Javascript? Dart? ;-) or, I could execute the parsed code using an Interpreter pattern (i.e., each IExpression could have an Evaluate method).

AjTalk.Compiler is a console application that takes a file as parameter (i.e Object.st) and writes a Program.js in Javascript. The generated code (Work in progress):

You know: I’m addict to Twitter, part of my TweetDeck was captured at right bottom corner ;-) . The above code is an excerpt of Program.js generated from Squeak Object.st. I’m using Node.js to load it (no run yet), without errors. I should implement some Squeak/Smalltalk primitives (notice the commented primitive invocation in the above code). The strategies I’m using to translate Smalltalk construct to Javascript could change in the near future. I’m experimenting with different approaches.

Next topics: how to implements Smalltalk ideas in Javascript.

Keep tuned!

Angel “Java” Lopez
http://www.ajlopez.com
http://twitter.com/ajlopez

September 22, 2011

Smalltalk and Javascript

Filed under: AjTalk, JavaScript, Smalltalk — ajlopez @ 10:11 am

For decades Smalltalk was the flagship of object and classes ideas implemented in a language, environment and live image. But it would be a case of “too much, too early”. Different Smalltalks, companies, no open community, hardware requirements, its “own way IDE”, all conspired to become it a niche tool. Meanwhile, new languages from the 90s, from Javascript to Ruby, take leverage from Smalltalk ideas and are more mainstream tools than the original. In this century, Javascript, Ruby, Python, CLR (.NET with many languages), JVM (Scala, Groovy, Clojure! (look ma.. lisp for the masses!)) are gaining momentum. With the emergence of different devices, online apps, and operating systems, most of these new programs can run everywhere. And they are backed up by a vibrant community, with lot of open source projects contributing to the popularity and adoption of these technologies. An example: look at Node.js, a reactor pattern implemented in Javascript, running over V8 Google Javascript engine. It has lot of modules mounted over its core implementation.

Classic Smalltalkers will prefer their environments, but it’s a reality that Smalltalk code could take advantage of existing programming languages, virtual machines and libraries. One promising path is: compile Smalltalk to Javascript, and reach the “run everywhere” dream. (Note Clojure (special Lisp over JVM and CLR) and its new branch: ClojureScript and its rationale). This post is an enumeration of projects that use some variation of Smalltalk to Javascript original idea.

There is the ST2JS (Smalltalk to Javascript translator):

ST2JS – Smalltalk to Javascript translator
http://www.squeaksource.com/ST2JS/

ST2JS – Traductor de Smalltalk a JavaScript (Parte I) (Spanish)
http://diegogomezdeck.blogspot.com/2006/07/st2js-traductor-de-smalltalk.html

ST2JS – Traductor de Smalltalk a JavaScript (Parte II) (Spanish)
http://diegogomezdeck.blogspot.com/2006/07/st2js-traductor-de-smalltalk_28.html

There is an Smalltalk Web Toolkit based on ST2JS (I couldn’t find any source code, yet):

Smalltalk Web Toolkit (Smalltalks 2009)
http://www.slideshare.net/garduino1/swt-2009

Introducción a Smalltalk Web Toolkit (SWT) « CEIBO
http://ceibo.wordpress.com/2008/01/06/introduccion-a-smalltalk-web-toolkit-swt/ (with detailed mini examples of Smalltak to Javascript translations, like closures, blocks, etc)

Nicolas Petton has Amber (formerly JTalk), than creates Javascript files from Smalltalk code (it has an example running on NodeJs, interesting):

ST 4U 133: Amber
http://www.jarober.com/blog/blogView?entry=3493610269

NicolasPetton/amber – GitHub
https://github.com/NicolasPetton/amber

Peter Fisk (VistaSmalltak, GWT Smalltalk, and now SilverSmalltalk) was written Smalltalk using Silverlight, Google tools, and now, Javascript:

Implementing Smalltalk’s Non-Local Returns in JavaScript « Silver Smalltalk
http://silversmalltalk.wordpress.com/2011/02/02/implementing-smalltalks-non-local-returns-in-javascript/

Smalltalk Classes in JavaScript « Silver Smalltalk
http://silversmalltalk.wordpress.com/2011/02/02/smalltalk-classes-in-javascript/

See the Lively Kernel, Javascript inspired by Smalltalk, and JTalk (now Amber):

InfoQ: Smalltalk IDEs Come to the Browser: Jtalk, tODE, Lively Kernel 2.0
http://www.infoq.com/news/2011/08/smalltalk-in-browser

InfoQ: Dan Ingalls on the History of Smalltalk and the Lively Kernel
http://www.infoq.com/interviews/ingalls-smalltalk

Lively Kernel
http://en.wikipedia.org/wiki/Lively_Kernel

There is an open implementation of Smalltalk to Javascript, named S8, work in progress. See Spanish post:

Blog de Leo De Marco: Lanzamiento de S8 en Smalltalking
http://leodemarco.blogspot.com/2011/09/lanzamiento-de-s8-en-smalltalking.html

And yes! You just guess! There is a Smalltalk to Javascript (and maybe other dynamic language) translator in my pet project AjTalk (WIP) (post is comming). I’m working on returns from inside blocks, and some points, like:

doesNotUnderstand for JavaScript?
http://stackoverflow.com/questions/3918920/doesnotunderstand-for-javascript

Is there an equivalent of the __noSuchMethod__ feature for properties, or a way to implement it in JS?
http://stackoverflow.com/questions/2266789/is-there-an-equivalent-of-the-nosuchmethod-feature-for-properties-or-a-way-t

My links:
http://www.delicious.com/ajlopez/smalltalk+javascript

Thanks to @garduino for his links about ST2JS and Smalltalk Web Toolkit. And to @leodmarco for his post about S8.

Keep tuned, more Smalltalk to Javascript experience (using AjTalk) is comming ;-)

Angel “Java” Lopez
http://www.ajlopez.com
http://twitter.com/ajlopez

May 18, 2011

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

Filed under: .NET, AjTalk, Open Source Projects, Programming Languages, Smalltalk — ajlopez @ 10:59 am

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?:

[Serializable]
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

http://www.ajlopez.com

http://twitter.com/ajlopez

November 15, 2010

Transactional Objects in AjTalk, a quick intro

Filed under: .NET, AjTalk, Open Source Projects, Smalltalk — ajlopez @ 9:29 am

I want to describe a short example, showing the management of transactional objects in AjTalk, my open source Smalltalk-like VM written in C#.

What is a transactional object? It’s an object that, when running inside an in-memory transaction, can manage their instance variable values, monitoring their changes. If the transaction if commited, the new values of the object are visible to the rest of the system. If the transaction if rollbacked, the affected transactional objects restore their original values, at the time of the start of the transaction. Note: these transactions are in memory, and they are not related to transactions in databases.

How to create an AjTalk object transactional? I added a method to Object protocol:

Object methods!

asTransactional
    ^@AjTalk.Transactions.TransactionalObject new: self with: (@AjTalk.Machine current transactionManager)
! !

The object is wrapped by a decorator, a native object of type TransactionalObject. This object receives all the petitions for get and set instance variable values, and manage them for the current transaction, and for any other transaction running in the system, that access the same object. I should write a blog describing the internal implementation. Note: if an instance variable in a transactional object is modified by two transactions, the first wins, and the second fails.

Given a Rectangle class:

Object subclass: #Rectangle instanceVariableNames: ‘width height’
!Rectangle methods!
width
    ^width
!
height
    ^height
!
width: newWidth
    width := newWidth
!
height: newHeight
    height := newHeight
! !

You can create an instance, set some values, and create a transactional decorator:

rect := Rectangle new.
rect width: 500!
rect := rect asTransactional
!

Now, rect variable points to an instance that is transactional. But if there is no open transaction, its behavior is like any other object. How to start a transaction? The current Machine has a new property, holding a TransactionManager object:

tm := @AjTalk.Machine current transactionManager.
console := @System.Console out
!

tm beginTransaction!

The instance rect has a reference pointing to the decorated object, named innerObject. You can check their values:

console write: ‘rect width is ‘.
console writeLine: rect width!

console write: ‘inner rect width is ‘.
console writeLine: rect innerObject width!

The output will be:

rect width is 500
inner rect width is 500

Now, let’s set the width of the transactional object:

rect width: 700!

and check again the values, as above. The ouput will be:

rect width is 700
inner rect width is 500

Usually, you use the instance rect (the decorator) in all the running process. If you check the rect width value in another thread/process (with or without its own transaction), you will get 500. The new value 700 is not published yet: it’s only visible from the current transaction.

You can rollback the transaction:

tm rollbackTransaction!

The new values will be:

rect width is 500
inner rect width is 500

If you were committed the transaction, the result would be:

rect width is 700
inner rect width is 700

You can run the example, from the current trunk. There is a RunTransaction.cmd command in AjTalk.Console project.

Next steps: write a blog describing the implementation of this feature; improve the transactional management to have automatically retries in failed transaction.

Keep tuned!

Angel “Java” Lopez
http://www.ajlopez.com
http://twitter.com/ajlopez

October 28, 2010

Distributed Objects in AjTalk

Filed under: .NET, AjTalk, Programming Languages, Smalltalk — ajlopez @ 8:50 am

I added distributed objects to my AjTalk project (Smalltalk-like VM written in C#). I’m using .NET Remoting to serialize objects from one process to another, and to have proxies to remote objects. You can download the source code and tests from:

http://code.google.com/p/ajtalk/

First, there is a new method for objects:

nil subclass: #Object
!Object methods!
asRemote
    ^@AjTalk.Hosting.RemoteObject new: self
! !

(I’m using AjTalk notation to access .NET types and methods, see AjTalk: Accessing .NET Types And Objects)

The RemoteObject class is a decorator around an IObject (objects in AjTalk), that inherits from MarshalByRefObject. In .NET, such objects are not serialized: instead, a reference is send to the other process. Every call in that proxy, sends the invoke to the original object, still residing in its process:

    public class RemoteObject : MarshalByRefObject, IObject
    {
        private IObject obj;
        private Machine machine;
        public RemoteObject(IObject obj)
        {
            this.obj = obj;
            this.machine = Machine.Current;
        }
    // ...    
    }

There is a host, that can receive calls from other process. Launch a host in process A:

Object subclass: #Host nativeType: @AjTalk.Hosting.RemotingHostServer

!

host := Host new: @AjTalk.Machine !!current with: 20000 with: 'Server2000'

!

From other process, you can connect to this host. Executing in process B:

Object subclass: #RemoteHost nativeType: @AjTalk.Hosting.RemotingHostClient

!RemoteHost methods!

export: aClass

    self execute: (aClass !!toOutputString)

! !

remote := RemoteHost new: 'localhost' with: 20000 with: 'Server2000'

!

Now, remote is an object in process B that can be used to communicate with remote process/machine A.

The export method serialize the string definition of a class. So, if you have a new class in this process, you can send its definition to the remote host. After then, you can create objects in the remote host. Let’s define a new class in process B:

Object subclass: #Rectangle instanceVariableNames: 'width height'

!Rectangle methods!

width

    ^width

!

height

    ^height

!

width: newWidth

    width := newWidth

!

height: newHeight

    height := newHeight

! !

Then, you can export the class to process A:

remote export: Rectangle

!

Now, create a new Rectangle, in process A, executing the following code in process B:

remote execute: 'rect := Rectangle new. rect width: 100. rect height: 20'

!

The execute method sends an string, that is executed in the remote machine. You can receive serialized objects. Again, executing in process B:

rect := remote evaluate: 'rect'.

rect width: 200.

rect height: 30

!

The evaluate method sends an string to process A, evaluate it, and the result (the Rectangle object) is serialized and reified (a copy) in process B. Then, the changes in size are evaluate in the copy at process B. But you have other choice: retrieves a proxy to the rect, that still resides in Process A:

rect2 := remote evaluate: 'rect asRemote'.

rect2 width: 300.

rect2 height: 40

!

asRemote is the key: instead of returning the serialized object, it returns a reference to the remote object residing in process A. The changes to rect2 size are sent and executed in the remote object environment.

Summary, with these classes and methods,  you can:

- Create a host, and connect to it via from other process/machine.

- Send a string to be executed by the remote host

- Export a local class definition to remote host

- Evaluate an expression at remote host and retrieved its serialized result

- Evaluate an expression at remote host and retrieve its result as a remote object accesible via a proxy

I should write a use case of this functionality.

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

October 23, 2010

Agents in AjTalk: Introduction

Filed under: Agents, AjTalk, Open Source Projects, Programming Languages, Smalltalk — ajlopez @ 11:04 am

I extended my AjTalk Smalltalk-like interpreted VM, written in C#, to support the ideas I have implemented for AjSharp:

Agents in AjSharp (Part 1)
Agents in AjSharp (Part 2)
Web Crawler using Agents in AjSharp

and in AjAgents:

AjAgents: a new implementation

Usually, you send a message (its symbol name, arguments) from a sender to a receiver:

The receiver got the message and processes it. Meanwhile, the sender is waiting a result, or the end of processing, at least. But I want to explore another way of doing things: send the message, and leaves it. The sender could continue with its own processing. This way is similar to living systems: a neuron sends a “message” to another neuron, and then this neuron could send or not more “messages” to another neurons. A parallel processing emerges from all this decoupled activity: it’s a form of message passing, a kind of “tell, don’t ask”, or “hey, this is my message, do something good with it, when you have time, but I don’t care of your answer, now”.

Then, I wrote an extension to AjTalk. You can define an agent subclass using:

Object agent: #MyAgent.

You can define methods for the new subclass MyAgent. But when you create and use once of its instances:

myAgent := MyAgent new.
myAgent doSomethingWith: par1 andWith: par2.

the message is sent to the agent, BUT it is not immediately processed. It is put in a queue. Agent process the message from that queue, using another thread (I could implement a message queue to be consumed by a thread pool of fixed size, in order to serve a large number of agents; actually, there is a thread/queue by agent):

Actually, the queue is a blocking one: if it is filled by messages to process, a new arrival message blocks the sender thread. I could implement other strategies for message throttling. The C# classes that implements all this behavior are BaseAgent (overriding the ExecuteMethod method):

public override object ExecuteMethod(IMethod method, object[] arguments)
{
    Message message = new Message(method, arguments);
    this.queue.PostMessage(message);
    return null;    // TODO what to return?
}

and MessageQueue (a blocking queue implementation for message). You can download the current AjTalk source code from trunk at http://code.google.com/p/ajtalk/.

My idea is to write a use case for this kind of agents: my classical web crawler, or something simpler. I have distributed objects, too. I could combine these two features: a distributed web crawler, maybe.

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

Older Posts »

Theme: Shocking Blue Green. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.