majorly cleaned up the CoffeeScript that defines the Narwhal integration

This commit is contained in:
Jeremy Ashkenas
2009-12-24 23:28:01 -08:00
parent 2d57ee693b
commit 7a0de52c96
4 changed files with 83 additions and 90 deletions

View File

@@ -1,6 +1,22 @@
(function(){ (function(){
var File = require('file');
// This (javascript) file is generated from lib/coffee_script/narwhal/coffee-script.cs Executes the `coffee-script` Ruby program to convert from CoffeeScript
// to Javascript. Eventually this will hopefully happen entirely within JS. Require external dependencies.
var OS = require('os'); var OS = require('os');
var File = require('file');
var Readline = require('readline');
// The path to the CoffeeScript Compiler.
var coffeePath = File.path(module.path).dirname().dirname().join('bin', 'coffee-script');
// Our general-purpose error handler.
var checkForErrors = function(coffeeProcess) {
if (coffeeProcess.wait() === 0) {
return true;
}
system.stderr.print(coffeeProcess.stderr.read());
throw new Error("coffee-script compile error");
};
// Run a simple REPL, round-tripping to the CoffeeScript compiler for every
// command.
exports.run = function(args) { exports.run = function(args) {
args.shift(); args.shift();
if (args.length) { if (args.length) {
@@ -9,7 +25,7 @@
while (true) { while (true) {
try { try {
system.stdout.write('cs> ').flush(); system.stdout.write('cs> ').flush();
var result = exports.cs_eval(require('readline').readline()); var result = exports.evalCS(Readline.readline());
if (result !== undefined) { if (result !== undefined) {
print(result); print(result);
} }
@@ -18,34 +34,25 @@
} }
} }
}; };
// executes the coffee-script Ruby program to convert from CoffeeScript to Objective-J. // Compile a given CoffeeScript file into JavaScript.
// eventually this will hopefully be replaced by a JavaScript program.
var coffeePath = File.path(module.path).dirname().dirname().join('bin', 'coffee-script');
exports.compileFile = function(path) { exports.compileFile = function(path) {
var coffee = OS.popen([coffeePath, "--print", "--no-wrap", path]); var coffee = OS.popen([coffeePath, "--print", "--no-wrap", path]);
if (coffee.wait() !== 0) { checkForErrors(coffee);
system.stderr.print(coffee.stderr.read());
throw new Error("coffee-script compile error");
}
return coffee.stdout.read(); return coffee.stdout.read();
}; };
// Compile a string of CoffeeScript into JavaScript.
exports.compile = function(source) { exports.compile = function(source) {
var coffee = OS.popen([coffeePath, "--eval", "--no-wrap"]); var coffee = OS.popen([coffeePath, "--eval", "--no-wrap"]);
coffee.stdin.write(source).flush().close(); coffee.stdin.write(source).flush().close();
if (coffee.wait() !== 0) { checkForErrors(coffee);
system.stderr.print(coffee.stderr.read());
throw new Error("coffee-script compile error");
}
return coffee.stdout.read(); return coffee.stdout.read();
}; };
// these two functions are equivalent to objective-j's objj_eval/make_narwhal_factory. // Evaluating a string of CoffeeScript first compiles it externally.
// implemented as a call to coffee and objj_eval/make_narwhal_factory exports.evalCS = function(source) {
exports.cs_eval = function(source) {
init();
return eval(exports.compile(source)); return eval(exports.compile(source));
}; };
exports.make_narwhal_factory = function(path) { // Make a factory for the CoffeeScript environment.
init(); exports.makeNarwhalFactory = function(path) {
var code = exports.compileFile(path); var code = exports.compileFile(path);
var factoryText = "function(require,exports,module,system,print){" + code + "/**/\n}"; var factoryText = "function(require,exports,module,system,print){" + code + "/**/\n}";
if (system.engine === "rhino") { if (system.engine === "rhino") {
@@ -55,10 +62,4 @@
return eval("(" + factoryText + ")"); return eval("(" + factoryText + ")");
} }
}; };
var init = function() {
// make sure it's only done once
init = function() {
};
return init;
};
})(); })();

View File

@@ -1,23 +1,20 @@
(function(){ (function(){
// This (javascript) file is generated from lib/coffee_script/narwhal/loader.cs
var coffeescript = null; var coffeescript = null;
var CoffeeScriptLoader = function() { var factories = {
var loader = {
};
var factories = {
};
loader.reload = function(topId, path) {
coffeescript = coffeescript || require('coffee-script');
// print("loading objective-j: " + topId + " (" + path + ")");
factories[topId] = coffeescript.make_narwhal_factory(path);
return factories[topId];
};
loader.load = function(topId, path) {
if (!(factories.hasOwnProperty(topId))) {
loader.reload(topId, path);
}
return factories[topId];
};
return loader;
}; };
require.loader.loaders.unshift([".cs", CoffeeScriptLoader()]); var loader = {
// Reload the coffee-script environment from source.
reload: function(topId, path) {
coffeescript = coffeescript || require('coffee-script');
factories[topId] = coffeescript.makeNarwhalFactory(path);
return factories[topId];
},
// Ensure that the coffee-script environment is loaded.
load: function(topId, path) {
return factories[topId] = factories[topId] || this.reload(topId, path);
}
};
require.loader.loaders.unshift([".cs", loader]);
})(); })();

View File

@@ -1,8 +1,24 @@
# This (javascript) file is generated from lib/coffee_script/narwhal/coffee-script.cs # This (javascript) file is generated from lib/coffee_script/narwhal/coffee-script.cs
File: require('file') # Executes the `coffee-script` Ruby program to convert from CoffeeScript
OS: require('os') # to Javascript. Eventually this will hopefully happen entirely within JS.
# Require external dependencies.
OS: require('os')
File: require('file')
Readline: require('readline')
# The path to the CoffeeScript Compiler.
coffeePath: File.path(module.path).dirname().dirname().join('bin', 'coffee-script')
# Our general-purpose error handler.
checkForErrors: coffeeProcess =>
return true if coffeeProcess.wait() is 0
system.stderr.print(coffeeProcess.stderr.read())
throw new Error("coffee-script compile error").
# Run a simple REPL, round-tripping to the CoffeeScript compiler for every
# command.
exports.run: args => exports.run: args =>
args.shift() args.shift()
return require(File.absolute(args[0])) if args.length return require(File.absolute(args[0])) if args.length
@@ -10,54 +26,34 @@ exports.run: args =>
while true while true
try try
system.stdout.write('cs> ').flush() system.stdout.write('cs> ').flush()
result: exports.cs_eval(require('readline').readline()) result: exports.evalCS(Readline.readline())
print(result) if result isnt undefined print(result) if result isnt undefined
catch e catch e
print(e)... print(e)...
# executes the coffee-script Ruby program to convert from CoffeeScript to Objective-J. # Compile a given CoffeeScript file into JavaScript.
# eventually this will hopefully be replaced by a JavaScript program.
coffeePath: File.path(module.path).dirname().dirname().join('bin', 'coffee-script')
exports.compileFile: path => exports.compileFile: path =>
coffee: OS.popen([coffeePath, "--print", "--no-wrap", path]) coffee: OS.popen([coffeePath, "--print", "--no-wrap", path])
checkForErrors(coffee)
if coffee.wait() isnt 0
system.stderr.print(coffee.stderr.read())
throw new Error("coffee-script compile error").
coffee.stdout.read(). coffee.stdout.read().
# Compile a string of CoffeeScript into JavaScript.
exports.compile: source => exports.compile: source =>
coffee: OS.popen([coffeePath, "--eval", "--no-wrap"]) coffee: OS.popen([coffeePath, "--eval", "--no-wrap"])
coffee.stdin.write(source).flush().close()
checkForErrors(coffee)
coffee.stdout.read().
coffee.stdin.write(source).flush().close() # Evaluating a string of CoffeeScript first compiles it externally.
exports.evalCS: source =>
eval(exports.compile(source)).
if coffee.wait() isnt 0 # Make a factory for the CoffeeScript environment.
system.stderr.print(coffee.stderr.read()) exports.makeNarwhalFactory: path =>
throw new Error("coffee-script compile error").
coffee.stdout.read().
# these two functions are equivalent to objective-j's objj_eval/make_narwhal_factory.
# implemented as a call to coffee and objj_eval/make_narwhal_factory
exports.cs_eval: source =>
init()
eval(exports.compile(source)).
exports.make_narwhal_factory: path =>
init()
code: exports.compileFile(path) code: exports.compileFile(path)
factoryText: "function(require,exports,module,system,print){" + code + "/**/\n}" factoryText: "function(require,exports,module,system,print){" + code + "/**/\n}"
if system.engine is "rhino" if system.engine is "rhino"
Packages.org.mozilla.javascript.Context.getCurrentContext().compileFunction(global, factoryText, path, 0, null) Packages.org.mozilla.javascript.Context.getCurrentContext().compileFunction(global, factoryText, path, 0, null)
else else
# eval requires parenthesis, but parenthesis break compileFunction. # eval requires parenthesis, but parenthesis break compileFunction.
eval("(" + factoryText + ")").. eval("(" + factoryText + ")")..
init: =>
# make sure it's only done once
init: => ..

View File

@@ -1,20 +1,19 @@
# This (javascript) file is generated from lib/coffee_script/narwhal/loader.cs # This (javascript) file is generated from lib/coffee_script/narwhal/loader.cs
coffeescript: null coffeescript: null
factories: {}
CoffeeScriptLoader: => loader: {
loader: {}
factories: {}
loader.reload: topId, path => # Reload the coffee-script environment from source.
reload: topId, path =>
coffeescript ||: require('coffee-script') coffeescript ||: require('coffee-script')
# print("loading objective-j: " + topId + " (" + path + ")"); factories[topId]: coffeescript.makeNarwhalFactory(path).
factories[topId]: coffeescript.make_narwhal_factory(path).
loader.load: topId, path => # Ensure that the coffee-script environment is loaded.
loader.reload(topId, path) unless factories.hasOwnProperty(topId) load: topId, path =>
factories[topId]. factories[topId] ||: this.reload(topId, path).
loader. }
require.loader.loaders.unshift([".cs", CoffeeScriptLoader()]) require.loader.loaders.unshift([".cs", loader])