The Coffeescript Approach
- make everything a function call / message send - no property access
- give every object a reference to it's current context - no strange hoisting
- allow for multiple parents - no single prototype
- implement a undirected resend - no directed resend required for super calls
- hide all internal state - no assignment
One of the key bits to making this real will be implementing the algorithm for Self message sends. This is going to involve creating a "primative" extension to Object.prototype which will do the message send calls. The Self selectors can be safely stored in object properties, and all of the accessor methods can be modeled via variable argument functions with hidden state as Phos already does. Also static slot assignments can also be mapped to functions which return a value. Removing assignment from the language can simply be achieved by using Self syntax, and that translates easily. As a result most objects will simply consist of slots whose keys can not be called via dot notation, and all calls made via a special _send method on Object.prototype.
Since the value of each property will itself be a function, I can also attach annotations to each, by making the function have an .annotation property. I will probably add a method to Function.prototype to manage these as well. Finally, the isParent predicate can exploit the fact that object keys may end in * as in Self, and produce a simple lookup mechanism. The scoping rules will ultimately also require implementing Blocks as a Self object with the block traits, and the nested method. In reality, I can get away by adding some primitives to Function.prototype, and create an invoke method that passes in the function object itself as the Block context. It's properties can be used to capture block state.
In the end, the hardest bit is not implementing Self, but building out a rather massive library of useful objects. All of the JS produced by this compiler will be straight forward JS. But the techniques employed will probably break optimization. That said, since we can use it to compile JS and then post to a KV store like Riak or CouchDB, I can always come back to any code and recompile later to a more optimized form. Mixed in with an environment like Chromeless, this might be a viable way to move Self into the browser.