fixin up narwhal factory and adding more implicit blocks to the lexer

This commit is contained in:
Jeremy Ashkenas
2009-12-30 14:32:59 -05:00
parent ea58be2838
commit f93e552cb3
7 changed files with 35 additions and 46 deletions

View File

@@ -20,7 +20,9 @@ checkForErrors: coffeeProcess =>
# Run a simple REPL, round-tripping to the CoffeeScript compiler for every
# command.
exports.run: args =>
return true if args.length
if args.length
exports.evalCS(File.read(path)) for path in args
return true
while true
try
@@ -46,13 +48,3 @@ 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,8 +18,15 @@
// Run a simple REPL, round-tripping to the CoffeeScript compiler for every
// command.
exports.run = function(args) {
var result;
var __a, __b, __c, __d, path, result;
if (args.length) {
__a = args;
__d = [];
for (__b=0, __c=__a.length; __b<__c; __b++) {
path = __a[__b];
__d[__b] = exports.evalCS(File.read(path));
}
__d;
return true;
}
while (true) {
@@ -53,16 +60,4 @@
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,13 +6,15 @@
};
loader = {
// Reload the coffee-script environment from source.
reload: function(topId, path) {
reload: function(topId) {
coffeescript = coffeescript || require('coffee-script');
return (factories[topId] = coffeescript.makeNarwhalFactory(path));
return (factories[topId] = function() {
return coffeescript;
});
},
// Ensure that the coffee-script environment is loaded.
load: function(topId, path) {
return factories[topId] = factories[topId] || this.reload(topId, path);
load: function(topId) {
return factories[topId] = factories[topId] || this.reload(topId);
}
};
require.loader.loaders.unshift([".coffee", loader]);

View File

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