Sunday, July 03, 2011

JSIL: Compile .NET to Javascript

As if compiling LLVM bytecode to Javascript wasn’t crazy enough there is now the JSIL, a project by Mozilla engineer Kevin Gadd. JSIL compiles CIL (.NET’s bytecode format) to readable Javascript.

For instance, after compiling the following C# code using the C# compiler to CIL:

using System;
using System.Collections.Generic;

public static class Program {
  public static void Main (string[] args) {
    var array = new[] { 1, 2, 4, 8, 16 };

    foreach (var i in array)
      Console.WriteLine(i);

    var list = new List<int>(array);

    foreach (var j in list)
      Console.WriteLine(j);
  }
}

JSIL compiles it to the following Javascript (which we had to somewhat awkwardly reformat to fit the width of this site):

JSIL.MakeStaticClass("Program", true);

Program.Main = function (args) {
    var array = JSIL.Array.New(System.Int32,
                      [1, 2, 4, 8, 16]);
    var array2 = array;
__loop0__:
    for (var k = 0; k < array2.length; ++k) {
        var i = array2[k];
        System.Console.WriteLine(i);
    }
    var list = new (System.Collections.Generic
                        .List$b1.Of(System.Int32))
                       (array);


Read more: State of Code
QR: https://chart.googleapis.com/chart?chs=80x80&cht=qr&choe=UTF-8&chl=http://www.stateofcode.com/2011/07/jsil-compile-net-to-javascript/

Posted via email from Jasper-Net