fixing up narwhal integration (again)

This commit is contained in:
Jeremy Ashkenas
2009-12-30 15:05:05 -05:00
parent 622ddea343
commit 96859e749b
4 changed files with 40 additions and 9 deletions

View File

@@ -22,6 +22,7 @@ checkForErrors: coffeeProcess =>
exports.run: args =>
if args.length
exports.evalCS(File.read(path)) for path in args
delete args[i] for path, i in args
return true
while true
@@ -48,3 +49,13 @@ exports.compile: source =>
# Evaluating a string of CoffeeScript first compiles it externally.
exports.evalCS: source =>
eval(exports.compile(source))
# Make a factory for the CoffeeScript environment.
exports.makeNarwhalFactory: path =>
code: exports.compileFile(path)
factoryText: "function(require,exports,module,system,print){" + code + "/**/\n}"
if system.engine is "rhino"
Packages.org.mozilla.javascript.Context.getCurrentContext().compileFunction(global, factoryText, path, 0, null)
else
# eval requires parentheses, but parentheses break compileFunction.
eval("(" + factoryText + ")")

View File

@@ -18,7 +18,7 @@
// Run a simple REPL, round-tripping to the CoffeeScript compiler for every
// command.
exports.run = function(args) {
var __a, __b, __c, __d, path, result;
var __a, __b, __c, __d, __e, __f, __g, __h, i, path, result;
if (args.length) {
__a = args;
__d = [];
@@ -27,6 +27,14 @@
__d[__b] = exports.evalCS(File.read(path));
}
__d;
__e = args;
__h = [];
for (__f=0, __g=__e.length; __f<__g; __f++) {
path = __e[__f];
i = __f;
__h[__f] = delete args[i];
}
__h;
return true;
}
while (true) {
@@ -60,4 +68,16 @@
exports.evalCS = function(source) {
return eval(exports.compile(source));
};
// Make a factory for the CoffeeScript environment.
exports.makeNarwhalFactory = function(path) {
var code, factoryText;
code = exports.compileFile(path);
factoryText = "function(require,exports,module,system,print){" + code + "/**/\n}";
if (system.engine === "rhino") {
return Packages.org.mozilla.javascript.Context.getCurrentContext().compileFunction(global, factoryText, path, 0, null);
} else {
// eval requires parentheses, but parentheses break compileFunction.
return eval("(" + factoryText + ")");
}
};
})();

View File

@@ -6,15 +6,15 @@
};
loader = {
// Reload the coffee-script environment from source.
reload: function(topId) {
reload: function(topId, path) {
coffeescript = coffeescript || require('coffee-script');
return (factories[topId] = function() {
return coffeescript;
return coffeescript.makeNarwhalFactory(path);
});
},
// Ensure that the coffee-script environment is loaded.
load: function(topId) {
return factories[topId] = factories[topId] || this.reload(topId);
load: function(topId, path) {
return factories[topId] = factories[topId] || this.reload(topId, path);
}
};
require.loader.loaders.unshift([".coffee", loader]);

View File

@@ -6,13 +6,13 @@ factories: {}
loader: {
# Reload the coffee-script environment from source.
reload: topId =>
reload: topId, path =>
coffeescript ||= require('coffee-script')
factories[topId]: => coffeescript
factories[topId]: => coffeescript.makeNarwhalFactory(path)
# Ensure that the coffee-script environment is loaded.
load: topId =>
factories[topId] ||= this.reload(topId)
load: topId, path =>
factories[topId] ||= this.reload(topId, path)
}