Thursday, May 25, 2006

Script#: C# to Javascript compiler

Nikhil Kothari has a post on a C# to Javascript compiler. Similar in approach to Google's Web Toolkit it allows you to program your client side web code in C# and compile it to Javascript. From this:
sing System;
using ScriptFX;
using ScriptFX.UI;

namespace HelloWorld {

public class HelloWorldScriptlet : IScriptlet {

private Button _okButton;
private TextBox _nameTextBox;
private Label _helloLabel;

private XMLHttpRequest _request;

public void Start() {
_okButton = new Button(Document.GetElementById("okButton"));
_nameTextBox = new TextBox(Document.GetElementById("nameTextBox"));
_helloLabel = new Label(Document.GetElementById("helloLabel"));

_okButton.Click += new EventHandler(OnOKButtonClick);
}

private void OnOKButtonClick(object sender, EventArgs e) {
Callback completedCallback = new Callback(this.OnRequestComplete);

_request = new XMLHttpRequest();
_request.Onreadystatechange = Delegate.Unwrap(completedCallback);
_request.Open("GET", "Hello.axd?name=" + _nameTextBox.Text, /* async */ true);
_request.Send(null);
}

private void OnRequestComplete() {
if (_request.ReadyState == 4) {
_request.Onreadystatechange = null;

string greeting = _request.ResponseText;
_helloLabel.Text = greeting;
}
}
}
}
It generates:
HelloWorld.HelloWorldScriptlet = function Scenarios_HelloWorldScriptlet() {
}
HelloWorld.HelloWorldScriptlet.prototype = {
_okButton: null,
_nameTextBox: null,
_helloLabel: null,
_request: null,

start: function Scenarios_HelloWorldScriptlet$start() {
this._okButton = new ScriptFX.UI.Button(document.getElementById('okButton'));
this._nameTextBox = new ScriptFX.UI.TextBox(document.getElementById('nameTextBox'));
this._helloLabel = new ScriptFX.UI.Label(document.getElementById('helloLabel'));
this._okButton.add_click(new Delegate(this, this._onOKButtonClick));
},

_onOKButtonClick: function Scenarios_HelloWorldScriptlet$_onOKButtonClick(sender, e) {
var completedCallback = new Delegate(this, this._onRequestComplete);
this._request = new XMLHttpRequest();
this._request.onreadystatechange = Delegate.unwrap(completedCallback);
this._request.open('GET', 'Hello.axd?name=' + this._nameTextBox.get_text(), true);
this._request.send(null);
},

_onRequestComplete: function Scenarios_HelloWorldScriptlet$_onRequestComplete() {
if (this._request.readyState == 4) {
this._request.onreadystatechange = null;
var greeting = this._request.responseText;
this._helloLabel.set_text(greeting);
}
}
}

HelloWorld.HelloWorldScriptlet.registerClass('HelloWorld.HelloWorldScriptlet', null, ScriptFX.IScriptlet);

Generating Javascript from other languages seems to be getting popular. Another system that does this is Morfik. They have some compelling demos and appear to compile C#, Pascal, Basic and Java into Javascript.

Categories: ,

2 Comments:

Anonymous simon said...

Here is a Python port of GWT, albeit in a very early stage.

10:49 PM  
Blogger zproxy said...

visit jsc project as it targets js java and php, oh and leave feedback! :)

6:05 AM  

Post a Comment

<< Home