mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-02-18 11:31:20 -05:00
majorly cleaned up the CoffeeScript that defines the Narwhal integration
This commit is contained in:
@@ -1,6 +1,22 @@
|
||||
(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 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) {
|
||||
args.shift();
|
||||
if (args.length) {
|
||||
@@ -9,7 +25,7 @@
|
||||
while (true) {
|
||||
try {
|
||||
system.stdout.write('cs> ').flush();
|
||||
var result = exports.cs_eval(require('readline').readline());
|
||||
var result = exports.evalCS(Readline.readline());
|
||||
if (result !== undefined) {
|
||||
print(result);
|
||||
}
|
||||
@@ -18,34 +34,25 @@
|
||||
}
|
||||
}
|
||||
};
|
||||
// executes the coffee-script Ruby program to convert from CoffeeScript to Objective-J.
|
||||
// eventually this will hopefully be replaced by a JavaScript program.
|
||||
var coffeePath = File.path(module.path).dirname().dirname().join('bin', 'coffee-script');
|
||||
// Compile a given CoffeeScript file into JavaScript.
|
||||
exports.compileFile = function(path) {
|
||||
var coffee = OS.popen([coffeePath, "--print", "--no-wrap", path]);
|
||||
if (coffee.wait() !== 0) {
|
||||
system.stderr.print(coffee.stderr.read());
|
||||
throw new Error("coffee-script compile error");
|
||||
}
|
||||
checkForErrors(coffee);
|
||||
return coffee.stdout.read();
|
||||
};
|
||||
// Compile a string of CoffeeScript into JavaScript.
|
||||
exports.compile = function(source) {
|
||||
var coffee = OS.popen([coffeePath, "--eval", "--no-wrap"]);
|
||||
coffee.stdin.write(source).flush().close();
|
||||
if (coffee.wait() !== 0) {
|
||||
system.stderr.print(coffee.stderr.read());
|
||||
throw new Error("coffee-script compile error");
|
||||
}
|
||||
checkForErrors(coffee);
|
||||
return 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 = function(source) {
|
||||
init();
|
||||
// Evaluating a string of CoffeeScript first compiles it externally.
|
||||
exports.evalCS = function(source) {
|
||||
return eval(exports.compile(source));
|
||||
};
|
||||
exports.make_narwhal_factory = function(path) {
|
||||
init();
|
||||
// Make a factory for the CoffeeScript environment.
|
||||
exports.makeNarwhalFactory = function(path) {
|
||||
var code = exports.compileFile(path);
|
||||
var factoryText = "function(require,exports,module,system,print){" + code + "/**/\n}";
|
||||
if (system.engine === "rhino") {
|
||||
@@ -55,10 +62,4 @@
|
||||
return eval("(" + factoryText + ")");
|
||||
}
|
||||
};
|
||||
var init = function() {
|
||||
// make sure it's only done once
|
||||
init = function() {
|
||||
};
|
||||
return init;
|
||||
};
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user