Go here to download ready-to-run and source distributions of Jangaroo. more...

Tuesday, December 6, 2011

Simulating ActionScript in JavaScript: Private Members Cont'd

In the first blog post about simulating private members in ActionScript, I compared different solutions to represent private members in JavaScript. The solution implemented by Jangaroo is to rename private members, so that they do not name-clash with private members of the same class, defined on a different inheritance level.
Bernd Paradies asked how Jangaroo solves untyped access to private members. Since the answer is rather extensive, I'll dedicate this follow-up post to the topic.

The short answer is: it's the nature of the beast. Untyped access to private members cannot be detected at compile time. However, potential access to private members can be detected at compile time and generate code that repeats the check at runtime, although Jangaroo does not implement this at the moment.

Typed Private Member Access
Let's recall the typical way to access private members, which can easily be detected by the Jangaroo compiler:

 1 public class Foo {
 2   private var foo:String = "bar";
 3   public function getFoo():String {
 4     return foo;
 5   }
 6 }

As foo in line 4 is resolved to a field of the class, and there is no implicit this access in JavaScript, the Jangaroo compiler adds this. before foo. Also, as field foo is declared private, the compiler renames it to foo$1 (see previous blog post) and thus generates the following JavaScript code for the body of method getFoo():

 4     return$1;

Where is My Type?
Now consider the following code (which does not really make sense, but illustrates the point):

 1 public class Foo {
 2   private var foo:String = "bar";
 3   public function getFoo():String {
 4     var untypedThis:Object = this;
 5     return;
 6   }
 7 }

Of course, in this simple example it would still be possible to determine the runtime type of untypedThis statically, but it is easy to imagine a situation where this is not possible. In the current Jangaroo implementation, the following JavaScript code would be generated for the body of method getFoo():

 4     var untypedThis = this;
 5     return;

As you can see, the compiler fails to detect that foo actually refers to the private member, does not rename the access to foo$1, and thus the undefined value of would be returned.

A (partial) solution is to let the compiler detect any expression of the form untyped.private-member and generate code that takes into account the runtime type of untyped, like so:

 5     return untyped[untyped instanceof Foo ? 'foo$1' : 'foo'];

Trusting today's JavaScript JIT compilers' optimizations, this code should be moved to a utility function to avoid double evaluation of the untyped expression (which could have side effects):

 5     return Foo.$class.get(untyped, 'foo');
where Foo.$class provides access to Jangaroo's "meta class" of Foo, and get() would be implemented there like this:

  public function get(object:Object, property:String):* {
    return object[object instanceof this.publicConstructor ?
      property + this.inheritanceLevel : property];

The Weak and the Wicked
Looking at ActionScript carefully, you'll notice that the identifier or expression left of the dot does not even have to be untyped, but may just be typed with a more general type, and the same scenario may apply. For example, assume our class Foo is a subclass of dynamic class Bar, we could replace Object by Bar, and the example would still work! Even if Bar is not dynamic, we could access the private member foo through a local variable of type Bar using square brackets, like so:

public class Foo extends Bar {
  private var foo:String = "bar";
  public function getFoo():String {
    var weaklyTypedThis:Bar = this;
    return weaklyTypedThis['foo'];

The Flex compiler does not complain, and at runtime, in Flash, indeed the private member is accessed. Compare this to the following example, where we try to access the private member of a superclass:

public class Baz extends Foo {
  public function Baz() {
    var superThis:Foo = this;
    trace(superThis['foo']); // is undefined, not "bar"!

Maybe you'll be surprised that the result is not any kind of access violation, but simply undefined—the private member of the superclass is simply not visible for the subclass! This is well emulated by Jangaroo through renaming private members. The Jangaroo compiler just has to take care to detect every correct access of a private member and rename the property upon access.

Turing Strikes Again
Things get even nastier when the property is a dynamic expression. Consider

  return this[veryComplicatedComputation()];

Here, too, we'd have to generate

  return Foo.$class.get(this, veryComplicatedComputation());

and extend the utility function by a runtime check whether the property is actually a private member name:

  public function get(object:Object, property:String):* {
    return object[object instanceof this.publicConstructor &&
      this.getMemberDeclaration("private", property) ?
        property + this.inheritanceLevel : property];

Taken together, the important criterion when to generate code for possible private member access is not whether the left-hand expression of the dot is untyped, but the compiler has to check whether the complete property access is not typed. This is the case exactly when the property is not a compile-time constant or when the property cannot be resolved statically within the type of the expression, and in any case, the type of the expression must be a supertype of the current class or the current class itself.

To wrap up, the compiler would have to generate a call to the dynamic access function if and only if
  1. the left-hand expression could have a runtime type compatible to the current class and 
  2. the property expression could be one of the current class's private members.
Here, could means that it may be, but is not certain at compile time. If both conditions are certain at compile time, the compiler knows for sure that the code represents access to a private member, and can simply use the renamed property.

One more thing: I have been talking about read access of private members. The same strategy would have to be applied when writing members, resulting in another utility function set(object, property, value).

Of course, we could optimize runtime performance by using specific utility functions for the following three different cases:
  1. The property is definitely a private member, but the left-hand expression may or may not be of the current class: check instanceof only.
  2. The left-hand expression is definitely of the current class, but the property may or may not be a private member: check the property only.
  3. Both the left-hand expression may or may not be of the current class, and the property may or may not be a private member: check instanceof and the property.
However, my guess is that this optimization is not necessary. Firstly, checking instanceof and whether some string is contained in a fixed and not very large set are cheap operations. Secondly, when using ActionScript (and not untyped JavaScript), you should avoid untyped access of properties whenever possible. Thus, the situation is a rare case, and would only be implemented for full ActionScript semantic compatibility. If the developer wants better performance, she can either insert a type cast to convert the property access to a typed one, or move dynamic properties to a dedicated object whose type is statically incompatible with the current class (e.g. use Dictionary), and thus no runtime check would be generated.
If this solution proves to inflict significant runtime overhead, the compiler should issue a warning when it has to generate runtime checks.

Wednesday, November 30, 2011

Simulating ActionScript in JavaScript: Private Members

Inspired by Adobe's Bernd Paradies, member of the FalconJS / FlashRT team, blogging about his ideas on "Compiling ActionScript to JavaScript", I thought it was about time to give more details about how Jangaroo implements ActionScript language features. Because there are so many of them, blogging about them one at a time seems like a good idea. Let's start with "private members".

The Early Days
When people started simulating class-based OOP in JavaScript, many thought that it is not possible to define private members that support the principle of information hiding, because in JavaScript, all properties of an object are publicly accessible. So in many cases, the convention was used to let private members start with an underscore ("_"), meaning "don't use this member, best assume it is not existing at all".

The Reference Solution
As we all know in the meantime, it is possible to implement real information hiding in JavaScript, namely by using closures / lexical scoping. Douglas Crockford (who else?) has written a nice summary of this approach back in 2001: Private Members in JavaScript
So it looks like all we have to do to simulate ActionScript's private members in JavaScript is to use this pattern, right? Of course, it is not that easy, as in practice, there are two problems with private members à la Crockford:
  1. Runtime overhead
  2. Source-code placement
While the first is a general problem, the second is Jangaroo-specific.
The runtime overhead of closures results from the fact that in Crockford's solution, private members and privileged methods (methods that use private members) have to be defined inside the constructor. This means that the corresponding function objects are created for every instance of the class! This results in a performance penalty as well as in increased memory usage. Of course, this is not an issue for classes with few instances (extreme: singletons), but for classes like events, where you create many, many instances, this can quickly become a major problem.
The second problem, source-code placement, results from a specific Jangaroo feature. In debug mode, the Jangaroo compiler generates JavaScript code that keeps every piece of executable code at exactly the same line as in the ActionScript source file. This is a central feature of Jangaroo and very important for source-level debugging with a free choice of JavaScript debugger. So how can we place private and privileged methods inside the constructor, when in the ActionScript source code, they are not?

The Super-Secret Private This
For solving the source-code placement issue, I came up with the idea of a "shared private this" object. It turned out that this approach makes things a bit more complicated and only mitigates (but not removes) the runtime overhead, so it did not actually make it into Jangaroo, but still I'd like to present it here.
The basic idea is that private members, as opposed to being local variables in constructor scope, are properties of a single object that is defined inside the constructor. To be shared, this "private this" object has to be "injected" into, i.e. put in the surrounding lexical scope of every privileged method. Consider the following class, which is the ActionScript version of the example used by Douglas Crockford:

 1 public class Container {
 2   public var member:String;
 3   private var secret:int;

 5   public function Container(param:String) {
 6     member = param;

 7     secret = 3;
 8   }
10   public function stamp(string:String):String {
11     return member + string;
12   }
14   private function dec():Boolean {
15     if (secret > 0) {
16       secret -= 1;
17       return true;
18     } else {
19       return false;
20     }
21   }
23   public function service():String {
24     return dec() ? member : null;
25   }
26 }
To inject the "private this" into all privileged methods, we wrap these inside functions. What Douglas calls that, we'll call this$, and all other helper idenfiers are suffixed with $. Here is the resulting JavaScript code:

 1 Container = (function() {
 2   //public var member:String;
 3   //private var secret:int;
 5   var Container = function(param) { var this$ = init$(this);
 6     this.member = param;
 7     this$.secret = 3;
 8   };
10   Container.prototype.stamp = function(string) {
11     return this.member + string;
12   };
14   var dec = function() {
15     if (this.secret > 0) {
16       this.secret -= 1;
17       return true;
18     } else {
19       return false;
20     }
21   };
23   var service$ = function(this$){return function(){
24     return this$.dec() ? this.member : null;
25   };};
27   var init$ = function(o) {
28     var this$ =
29       dec: dec
30     };

31     o.service = service$(this$);
32     return this$;
33   }
35   return Container;
36 })();
Note how all executable code stays in exactly the same line, and how similar it can be expressed in JavaScript!
The following variants of Douglas' patterns were used:
  • Private methods are only defined once (reduces runtime overhead!), and are supposed to be called on the "private this". If they need to access public members, too, we could easily extend the "private this" by a reference to the "public this".
  • Privileged methods are wrapped in a function that receives the "private this", so that the current "private this" is in lexical scope. 
  • To keep all source code at its original location, all additional initialization is moved to a generated method init$. It creates the "private this" this$ with all private members, creates instances of privileged methods handing in this$, and assigns these to the "public this".

The Pragmatic Solution
As said above, this solution still introduces runtime overhead and a bit complexity, so we looked for a more efficient and simpler alternative. I mentioned the naive approach to use a simple naming convention. The real flaw in this approach is not that you can access "private" members when you should not be able to. When compiling ActionScript to JavaScript, we can check access rights on the ActionScript source, so this is not an issue. But what this approach breaks is that private members are also used to avoid name clashes between subclass and superclass!
Image you define a framework, and provide some base class that is supposed to be extended by clients of the framework. This base class usually provides members with different visibility. The private members (and the "internal" ones, which we neglect for now) can not be seen by a subclass. A client's subclass could define its own private members any way they like, as long as they don't name-clash with public or protected members of the superclass.
Now you update the framework and add a private method, as you think this should not change the framework API. But if we use the naive naming convention of prefixing all private members with an underscore (or the like), there is now the chance that the new private member of the superclass name-clashes with an existing private member of the client's subclass!
Jangaroo's pragmatic solution to separate private members of different inheritance levels within one class is to suffix private member names with a $ followed by the numeric inheritance level. Object has inheritance level 0, and a class extending X has the inheritance level of X plus one. This effectively prevents name-clashes between private members of the same name, but defined on different inheritance levels, and thus solves the framework update problem described above.
We used to compute the inheritance level of a class at runtime, but this made class loading and initialization more complex. Thus, we later decided to let the compiler compute the inheritance level. Of course this reduces "binary" compatibility, meaning that when you refactor a framework class e.g. by introducing an intermediate class in the inheritance hierarchy, clients will have to recompile their code, or the inheritance level of their subclass will be incorrect. But recompiling (without changing the source code) after updating a framework should not really be a problem.
To give a concrete example, here is the simplified generated JavaScript code for the example above. It is not exactly what Jangaroo would produce, as there are many other features covered by the Jangaroo Runtime, which I'll elaborate on in upcoming blog posts. Also, it does not illustrate the inheritance issue, but just imagine a subclass that also defines a private field secret, which would then be renamed secret$2.
 1 Container = (function() {
 2   //public var member:String;
 3   //private var secret:int;
 5   var Container = function(param) {
 6     this.member = param;
 7     this.secret$1 = 3;
 8   };
10   Container.prototype.stamp = function(string) {
11     return this.member + string;
12   };
14   Container.prototype.dec$1 = function() {
15     if (this.secret$1 > 0) {
16       this.secret$1 -= 1;
17       return true;
18     } else {
19       return false;
20     }
21   };
23   Container.prototype.service = function() {
24     return this.dec$1() ? this.member : null;
25   };
return Container;
28 })();
Let me conclude with an overview of the four solutions for private members in JavaScript discussed here.
naive naming convention private members
(D. Crockford)
private this inheritance level suffix
(used in Jangaroo)
information hiding
avoiding name-clashes
no runtime overhead
keep source lines
As you can see in the table, there is no perfect solution (no column with checkmarks in every row), so for Jangaroo, we chose the pragmatic one that ensures good performance, while having some drawbacks on information hiding.

Since performance is the only argument against the "private this" solution, we should investigate in performance analysis of today's JavaScript engines to quantify the time and space overhead introduced by the appoach. Maybe it is not that bad after all.
With all modern browsers supporting the JavaScript API Object.defineProperty(),we can improve Jangaroo's pragmatic solution's information hiding by defining all private members to not be enumerable. Taking a closer look at ActionScript semantics, actually, all class members are not enumerable, i.e. they are all not visible in a for ... in loop (only dynamic properties are).
There are several possible improvements to approximate ActionScript semantics more closely when relying on features of a modern JavaScript engine (ECMAScript 5). I'll come to these when discussing further ActionScript language features.

Thursday, November 10, 2011

Enterprise JavaScript with Jangaroo at PLASTIC / SPLASH 2011

While almost one year ago, we talked about "Jangaroo - AS3 w/o FlashPlayer", we thought it was about time to stress the other story, namely using Jangaroo for creating large-scale JavaScript applications. Because that's what we do very successfully at CoreMedia.
Several people (thanks Florian, Martin!) pointed us at a very fitting call for papers of the PLASTIC workshop at the SPLASH 2011. It is about Programming Languages And Systems Technologies for Internet Clients, with a focus on "compilation and runtime techniques for Internet client programming languages". Andreas kicked off the paper, Olaf and Dennis supported with proof-reading, LaTeX support and helpful comments, and I fleshed out the text and extended the parts of my expertise, namely the runtime, libraries, and build process. The paper, entitled Enterprise JavaScript with Jangaroo --
Using ActionScript 3 for JavaScript ”Programming in the Large”
, was accepted and can be downloaded here.
On 24th of October, I presented the corresponding talk at the PLASTIC workshop in Portland, Oregon. To not bore the part of the audience who already read the paper (maybe the empty set?), the slides show a third category of "how to execute another programming language in the browser", since providing a client-side interpreter seems to be a hot topic (mostly because of Google swiffy, but there are others, too). But have a look at the slides for yourself.
Before starting with the slides, I showed off the demos Box2D and Open Flash Charts, which I admit are a bit off-topic, since they rather belong to the "port Flash applications to HTML5" story than to "Enterprise JavaScript", but hey, they are always good to get people's attention ;-).
I hope I could get across what Jangaroo is able to, and got some interesting feedback. Adam Welc (Co-Chair of the workshop, in his other life working on Flash at Adobe) pointed me to another Flash-related Open Source projects, lightspark, which might help in getting on with Jangaroo's Flash re-implementation JooFlash.
People liked the way Jangaroo keeps the generated code close to the source, even keeping line numbers to allow source-level debugging.
The most motivating part when visiting a workshop or conference is of course about meeting people. I remembered Google's Mark Miller from the Ajax Experience 2009 in Boston, who here at PLASTIC co-authored a paper about traits.js: recommended read! Christian Hammer, Andreas Gampe, Aiman Erbad and several others also presented and we had interesting discussions. Was nice getting to know you guys!
Another JavaScript guru I also met first on Ajax Experience is Rik Arends, then at, who now does a wonderful job over at Cloud9IDE. I tell you he's one of the good guys (even if we disagree on where to use typed languages and compilers)!
Finally, I loved the key note of David Ungar, Everything You Know (about Parallel Programming) Is Wrong! A Wild Screed about the Future; he really is a great presenter! Using Romeo and Juliet as an example of parallel actions that went wrong (and might have ended up "hunky-dory" in a different sequentialization) is really funky...