C#のコードをJavaScriptに変換するScript#

マイクロソフトのWeb Platform and ToolsチームのアーキテクトであるNikhil Kothariの個人ブログC#のコードをJavaScriptに変換するScript#のプロトタイプが公開されているマイクロソフトではAjaxフレームワークとしてATLASを開発しているが、それとは別にNikhilが個人で開発しているのが、このScript#だ。

Script#は一言で言うと、

Script# brings the C# developer experience (programming and tooling) to Javascript/Ajax world.

だ。つまり、C#のプログラミングノウハウやツールをそのまま使い、JavaScriptおよびAjaxを実現するというものだ。

Nikhilによると、このアプローチには次の4つの利点があるという言う。

  • A clean language with the natural constructs. Today with script, you can go a long ways in simulating OOP (classes, interfaces, inheritance etc.), but the simulations are thrown at the face of the developer. Various C# constructs such as properties, events, and syntax for defining inheritance go a long way in making the code readable. Similarly modifier keywords like public vs. protected vs. internal, sealed vs. virtual etc. help define a better object model. All of this can be brought into the scripting world.
  • Easier refactoring and exploration. Script development could benefit immensely from the refactoring, and class browsing support already present in the IDE and in tools such as .NET Reflector. Both lend themselves to having a more manageable code base over time.
  • Ability to generate documentation. Again doc-comments from C# and the existing infrastructure could be leveraged here.
  • Ability to customize the script code easily. For example, I'll show debug vs. release and minimization below. The same idea could apply to building a version of the script that had more error checking or logging built in when you do need to run diagnostics. The approach could also be used to include instrumentation for the purposes of profiling, measuring code coverage, etc. Another interesting aspect of this project is that it will be able to generate script catering to multiple script profiles such as the current Javascript language, as well as Javascript 2 when it appears.

以下、ちょっと乱暴な抄訳だ。

  • クリーンな言語による自然なコンストラクト。今日、OOPをシミュレートしているが、それは開発者の労力を無駄に費やすことになる。
  • リファクタリングと調査の簡易化。IDEや.NET Reflectorなどのようなツールによるリファクタリングやクラスブラウジングの利点を享受できる。
  • ドキュメント作成の簡易化。C#のコメントからドキュメントが容易に作成できる。
  • スクリプトコードのカスタマイズの簡易化。ダイアグのためのエラーチェックやロギング機能の付加が容易。

Nikhilもブログの投稿の中で言っているように、このScript#は一種のC#コンパイラである。CLRのコードを生成する代わりに、JavaScriptのコードを生成する。サーバー側はASP.NET用のモジュールが生成される。

ブログには実際にHelloWorldのAjax版をC#からXMLHTTPRequestを使ったJavaScriptにしたサンプルが紹介されている。デバッグバージョンでは可読性の高いスクリプトが生成され、リリースバージョンではサイズを最小化したスクリプトが生成されるのも実用的なアイデアだ。

ブログからは現在のプロトタイプがダウンロードできるようになっている。また、紹介ビデオも用意されている。興味のある方はどうぞ。