Created
May 2, 2011 20:32
-
-
Save anaisbetts/952296 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add the browser version of coffee-script.js as an Embedded Resource to use this