Created
October 24, 2011 23:06
-
-
Save akiellor/1310649 to your computer and use it in GitHub Desktop.
CoffeeScript Loader for rhino.
This file contains 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
load("coffee-script.js"); | |
var eval = (function(scope){ | |
var __context__ = Packages.org.mozilla.javascript.Context.getCurrentContext(); | |
function __eval(scope, source, name){ | |
__context__.evaluateString( | |
scope, | |
source, | |
name, | |
0, | |
null | |
); | |
} | |
function eval(source){ | |
__eval(scope, source, 'eval(' + source.substring(0, 100) + ')'); | |
} | |
return { | |
eval: eval | |
} | |
})(this).eval; | |
var loader = (function(){ | |
function isCoffee(file){ | |
return file.isFile() && file.getPath().endsWith(".coffee"); | |
} | |
function coffee(filename){ | |
var file = new java.io.File(filename); | |
if(isCoffee(file)){ | |
var source = readFile(file.getPath()); | |
var compiledSource = CoffeeScript.compile(source, {bare: true}); | |
eval(compiledSource); | |
}else{ | |
throw "LoadError: (" + filename + ") is not coffee"; | |
} | |
} | |
return { | |
coffee: coffee | |
} | |
})(); | |
loader.coffee("somefilename.coffee") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment