views:

297

answers:

6

I want to write a HttpHandler that compiles CoffeeScript on-the-fly and sends the resulting javascript. I have tried Ms Jscript and IronJS without success. I don't want to use Rhino because the java dependency would make it too difficult to distribute.

Has anyone compiled CoffeeScript from .NET or have any idea how it could be done?

UPDATE:

when using the MS Jscript interpreter an error is thrown in:

Lexer.prototype.extension_token = function () {
        var _d, _e, _f, extension;
        _e = Lexer.extensions;
        for (_d = 0, _f = _e.length; _d < _f; _d++) {
            extension = _e[_d];
            if (extension.call(this)) {
                return true
            }
        }
        return false
    };

The error is: Error: Object doesn't support this property or method and occurs on the line:

_e = Lexer.extensions;
A: 

I don't have a direct answer, (I hope you find one) but mayby take a look at the following to see how it might be done.

  • Jint - javascript Interpreter for .NET
  • Using IKVM to compile Rhino would get rid of the Java runtime requirement.
  • jcoffeescript. I haven't looked at jcoffeescript but I think it depends on jRuby and Rhino. You could possibly IKVM this as well.
Jafin
+2  A: 

Since the CoffeeScript compiler now runs on Internet Explorer, after a couple recent tweaks, it should be good to go within other MS-flavors of JS as well. Try including extras/coffee-script.js from the latest version, and you should be good to go with CoffeeScript.compile(code).

jashkenas
I have tried that with the version that works in IE and it still crashes JScript and IronJS.
liammclennan
Crashes how? And feel free to pop into #coffeescript on FreeNode if you'd like to discuss it or ask for help.
jashkenas
I have updated the question with more detail. JScript gives an error, IronJS crashes the process. I got no response of #coffeescript.
liammclennan
That doesn't make too much sense ... Lexer.extensions is an empty array (unless you've extended CoffeeScript, which you haven't). I don't see why JScript wouldn't be able to declare the "_e" variable, especially when Internet Explorer has no trouble with it.I'd be glad to investigate, if you can tell me how to get your setup setup (ha) on a Windows VM.
jashkenas
To track the error to a line number I used js beautifier. I think it introduced the error I described.
liammclennan
+1  A: 

I tried running the bundled extras/coffee-script.js through Windows Based Script Host (or just wscript) and it didn't report any issues. I then added this line:

WScript.Echo(CoffeeScript.compile('a: 1'));

at the end of the file and run it through wscript again and it printed the resulting JavaScript correctly.

Are you using COM objects? Can you share some more of the code responsible for initialising the MScript object reference?

StanAngeloff
I tried this and it works fine with WScript, but the same script won't run with Microsoft.JScript.Vsa.VsaEngine. So the problem is I can't run it from code.
liammclennan
Wasn't `Microsoft.JScript.Vsa` deprecated?Anything useful in `System.CodeDom.Compiler`?
StanAngeloff
+1  A: 

I have implemented a solution, but I am not satisfied with it. It works by shelling out to CScript to execute the compiler. I'd love some help implementing a better solution.

http://code.google.com/p/coffeescripthandler/

liammclennan
Also doesn't work correctly. This snippet causes it to fail. http://gist.github.com/566951
kouPhax
A: 

Just to help this question along... I tried a few other .NET javascript engines - which also didn't work.

PandaWood
+1  A: 

I've managed to compile CoffeeScript from .NET using IKVM, jcoffeescript and Rhino. It was straightforward, except that the JCoffeeScriptCompiler constructor overload without parameters didn't work. It ran OK with a java.util.Collections.EMPTY_LIST as parameter.

This is how I did it:

  1. Download IKVM, jcoffeescript and Rhino.
  2. Run ikvmc against js.jar, creating js.dll.
  3. Run ikvmc against the jcoffeescript jar.
  4. Add a reference to the jcoffeescript dll in Visual Studio. More references may be needed, but you will be warned about those.
  5. Run new org.jcoffeescript.JCoffeeScriptCompiler(java.util.Collections.EMPTY_LIST).compile() in your code.

The next step would be to create a build task and/or an HTTP handler.

Hans-Christian Holm