Skip to content

Instantly share code, notes, and snippets.

@akiellor
Created October 24, 2011 23:06
Show Gist options
  • Save akiellor/1310649 to your computer and use it in GitHub Desktop.
Save akiellor/1310649 to your computer and use it in GitHub Desktop.
CoffeeScript Loader for rhino.
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