Skip to content

Instantly share code, notes, and snippets.

@anaisbetts
Created May 2, 2011 20:32
Show Gist options
  • Save anaisbetts/952296 to your computer and use it in GitHub Desktop.
Save anaisbetts/952296 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using Jurassic;
using Jurassic.Library;
namespace NotActuallyTheDlrCoffee
{
public class CoffeeScriptCompiler
{
[ThreadStatic] ScriptEngine _engine;
ScriptSource _coffeeScriptCode;
public string Compile(string coffeeScriptCode)
{
if (_engine == null) {
_engine = initializeCoffeeScriptEngine();
}
return _engine.CallGlobalFunction<string>("compilify", coffeeScriptCode);
}
ScriptEngine initializeCoffeeScriptEngine()
{
if (_coffeeScriptCode == null) {
var ms = new MemoryStream();
Assembly.GetExecutingAssembly().GetManifestResourceStream("NotActuallyTheDlrCoffee.lib.coffee-script.js").CopyTo(ms);
var str = Encoding.ASCII.GetString(ms.GetBuffer());
_coffeeScriptCode = new StringScriptSource(str.Replace('\0', ' '));
}
var ret = new ScriptEngine();
ret.Execute(_coffeeScriptCode);
const string compilifyFunction =
@"function compilify(code) { return CoffeeScript.compile(code, {bare: true}); }";
ret.Execute(compilifyFunction);
return ret;
}
}
}
@anaisbetts
Copy link
Author

Add the browser version of coffee-script.js as an Embedded Resource to use this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment