mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-02-18 11:31:20 -05:00
removing traces of Ruby from coffee-script.coffee, redoing narwhal support to use the new compiler (but untested)
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
(function(){
|
(function(){
|
||||||
var compiler, lexer, parser, path;
|
var lexer, parser, path;
|
||||||
// Set up for both the browser and the server.
|
// Set up for both the browser and the server.
|
||||||
if ((typeof process !== "undefined" && process !== null)) {
|
if ((typeof process !== "undefined" && process !== null)) {
|
||||||
process.mixin(require('nodes'));
|
process.mixin(require('nodes'));
|
||||||
@@ -61,52 +61,4 @@
|
|||||||
}).call(this);
|
}).call(this);
|
||||||
return puts(strings.join(' '));
|
return puts(strings.join(' '));
|
||||||
};
|
};
|
||||||
//---------- Below this line is obsolete, for the Ruby compiler. ----------------
|
|
||||||
// The path to the CoffeeScript executable.
|
|
||||||
compiler = function compiler() {
|
|
||||||
return path.normalize(path.dirname(__filename) + '/../../bin/coffee');
|
|
||||||
};
|
|
||||||
// Compile a string over stdin, with global variables, for the REPL.
|
|
||||||
exports.ruby_compile = function ruby_compile(code, callback) {
|
|
||||||
var coffee, js;
|
|
||||||
js = '';
|
|
||||||
coffee = process.createChildProcess(compiler(), ['--eval', '--no-wrap', '--globals']);
|
|
||||||
coffee.addListener('output', function(results) {
|
|
||||||
if ((typeof results !== "undefined" && results !== null)) {
|
|
||||||
return js += results;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
coffee.addListener('exit', function() {
|
|
||||||
return callback(js);
|
|
||||||
});
|
|
||||||
coffee.write(code);
|
|
||||||
return coffee.close();
|
|
||||||
};
|
|
||||||
// Compile a list of CoffeeScript files on disk.
|
|
||||||
exports.ruby_compile_files = function ruby_compile_files(paths, callback) {
|
|
||||||
var coffee, exit_ran, js;
|
|
||||||
js = '';
|
|
||||||
coffee = process.createChildProcess(compiler(), ['--print'].concat(paths));
|
|
||||||
coffee.addListener('output', function(results) {
|
|
||||||
if ((typeof results !== "undefined" && results !== null)) {
|
|
||||||
return js += results;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
// NB: we have to add a mutex to make sure it doesn't get called twice.
|
|
||||||
exit_ran = false;
|
|
||||||
coffee.addListener('exit', function() {
|
|
||||||
if (exit_ran) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
exit_ran = true;
|
|
||||||
return callback(js);
|
|
||||||
});
|
|
||||||
return coffee.addListener('error', function(message) {
|
|
||||||
if (!(message)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
puts(message);
|
|
||||||
throw new Error("CoffeeScript compile error");
|
|
||||||
});
|
|
||||||
};
|
|
||||||
})();
|
})();
|
||||||
44
lib/narwhal.js
Executable file
44
lib/narwhal.js
Executable file
@@ -0,0 +1,44 @@
|
|||||||
|
(function(){
|
||||||
|
var coffee, factories, file, loader, os, puts;
|
||||||
|
// The Narwhal-compatibility wrapper for CoffeeScript.
|
||||||
|
// Require external dependencies.
|
||||||
|
os = require('os');
|
||||||
|
file = require('file');
|
||||||
|
coffee = require('./coffee-script');
|
||||||
|
// Alias print to "puts", for Node.js compatibility:
|
||||||
|
puts = print;
|
||||||
|
// Compile a string of CoffeeScript into JavaScript.
|
||||||
|
exports.compile = function compile(source) {
|
||||||
|
return coffee.compile(source);
|
||||||
|
};
|
||||||
|
// Compile a given CoffeeScript file into JavaScript.
|
||||||
|
exports.compileFile = function compileFile(path) {
|
||||||
|
return coffee.compile(file.read(path));
|
||||||
|
};
|
||||||
|
// Make a factory for the CoffeeScript environment.
|
||||||
|
exports.makeNarwhalFactory = function makeNarwhalFactory(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 + ")");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// The Narwhal loader for '.coffee' files.
|
||||||
|
factories = {};
|
||||||
|
loader = {};
|
||||||
|
// Reload the coffee-script environment from source.
|
||||||
|
loader.reload = function reload(topId, path) {
|
||||||
|
return factories[topId] = function() {
|
||||||
|
return exports.makeNarwhalFactory(path);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
// Ensure that the coffee-script environment is loaded.
|
||||||
|
loader.load = function load(topId, path) {
|
||||||
|
return factories[topId] = factories[topId] || this.reload(topId, path);
|
||||||
|
};
|
||||||
|
require.loader.loaders.unshift([".coffee", loader]);
|
||||||
|
})();
|
||||||
@@ -1,96 +0,0 @@
|
|||||||
(function(){
|
|
||||||
var File, OS, Readline, checkForErrors, coffeePath, factories, loader, puts;
|
|
||||||
// The Narwhal-compatibility wrapper for CoffeeScript.
|
|
||||||
// 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().dirname().dirname().join('bin', 'coffee');
|
|
||||||
// Our general-purpose error handler.
|
|
||||||
checkForErrors = function checkForErrors(coffeeProcess) {
|
|
||||||
if (coffeeProcess.wait() === 0) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
system.stderr.print(coffeeProcess.stderr.read());
|
|
||||||
throw new Error("CoffeeScript compile error");
|
|
||||||
};
|
|
||||||
// Alias print to "puts", for Node.js compatibility:
|
|
||||||
puts = print;
|
|
||||||
// Run a simple REPL, round-tripping to the CoffeeScript compiler for every
|
|
||||||
// command.
|
|
||||||
exports.run = function run(args) {
|
|
||||||
var __a, __b, i, path, result;
|
|
||||||
if (args.length) {
|
|
||||||
__a = args;
|
|
||||||
for (i = 0; i < __a.length; i++) {
|
|
||||||
path = __a[i];
|
|
||||||
exports.evalCS(File.read(path));
|
|
||||||
delete args[i];
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
__b = [];
|
|
||||||
while (true) {
|
|
||||||
__b.push((function() {
|
|
||||||
try {
|
|
||||||
system.stdout.write('coffee> ').flush();
|
|
||||||
result = exports.evalCS(Readline.readline(), ['--globals']);
|
|
||||||
if (result !== undefined) {
|
|
||||||
return print(result);
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
return print(e);
|
|
||||||
}
|
|
||||||
}).call(this));
|
|
||||||
}
|
|
||||||
return __b;
|
|
||||||
};
|
|
||||||
// Compile a given CoffeeScript file into JavaScript.
|
|
||||||
exports.compileFile = function compileFile(path) {
|
|
||||||
var coffee;
|
|
||||||
coffee = OS.popen([coffeePath, "--print", "--no-wrap", path]);
|
|
||||||
checkForErrors(coffee);
|
|
||||||
return coffee.stdout.read();
|
|
||||||
};
|
|
||||||
// Compile a string of CoffeeScript into JavaScript.
|
|
||||||
exports.compile = function compile(source, flags) {
|
|
||||||
var coffee;
|
|
||||||
coffee = OS.popen([coffeePath, "--eval", "--no-wrap"].concat(flags || []));
|
|
||||||
coffee.stdin.write(source).flush().close();
|
|
||||||
checkForErrors(coffee);
|
|
||||||
return coffee.stdout.read();
|
|
||||||
};
|
|
||||||
// Evaluating a string of CoffeeScript first compiles it externally.
|
|
||||||
exports.evalCS = function evalCS(source, flags) {
|
|
||||||
return eval(exports.compile(source, flags));
|
|
||||||
};
|
|
||||||
// Make a factory for the CoffeeScript environment.
|
|
||||||
exports.makeNarwhalFactory = function makeNarwhalFactory(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 + ")");
|
|
||||||
}
|
|
||||||
};
|
|
||||||
// The Narwhal loader for '.coffee' files.
|
|
||||||
factories = {
|
|
||||||
};
|
|
||||||
loader = {
|
|
||||||
};
|
|
||||||
// Reload the coffee-script environment from source.
|
|
||||||
loader.reload = function reload(topId, path) {
|
|
||||||
return factories[topId] = function() {
|
|
||||||
return exports.makeNarwhalFactory(path);
|
|
||||||
};
|
|
||||||
};
|
|
||||||
// Ensure that the coffee-script environment is loaded.
|
|
||||||
loader.load = function load(topId, path) {
|
|
||||||
return factories[topId] = factories[topId] || this.reload(topId, path);
|
|
||||||
};
|
|
||||||
require.loader.loaders.unshift([".coffee", loader]);
|
|
||||||
})();
|
|
||||||
@@ -1,8 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "coffee-script",
|
"name": "coffee-script",
|
||||||
"lib": "lib/narwhal",
|
|
||||||
"description": "Unfancy JavaScript",
|
"description": "Unfancy JavaScript",
|
||||||
"keywords": ["javascript", "language"],
|
"keywords": ["javascript", "language"],
|
||||||
"author": "Jeremy Ashkenas",
|
"author": "Jeremy Ashkenas",
|
||||||
"version": "0.3.2"
|
"version": "0.5.0"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,45 +47,3 @@ exports.print_tokens: (tokens) ->
|
|||||||
strings: for token in tokens
|
strings: for token in tokens
|
||||||
'[' + token[0] + ' ' + token[1].toString().replace(/\n/, '\\n') + ']'
|
'[' + token[0] + ' ' + token[1].toString().replace(/\n/, '\\n') + ']'
|
||||||
puts strings.join(' ')
|
puts strings.join(' ')
|
||||||
|
|
||||||
|
|
||||||
#---------- Below this line is obsolete, for the Ruby compiler. ----------------
|
|
||||||
|
|
||||||
# The path to the CoffeeScript executable.
|
|
||||||
compiler: ->
|
|
||||||
path.normalize(path.dirname(__filename) + '/../../bin/coffee')
|
|
||||||
|
|
||||||
# Compile a string over stdin, with global variables, for the REPL.
|
|
||||||
exports.ruby_compile: (code, callback) ->
|
|
||||||
js: ''
|
|
||||||
coffee: process.createChildProcess compiler(), ['--eval', '--no-wrap', '--globals']
|
|
||||||
|
|
||||||
coffee.addListener 'output', (results) ->
|
|
||||||
js += results if results?
|
|
||||||
|
|
||||||
coffee.addListener 'exit', ->
|
|
||||||
callback(js)
|
|
||||||
|
|
||||||
coffee.write(code)
|
|
||||||
coffee.close()
|
|
||||||
|
|
||||||
|
|
||||||
# Compile a list of CoffeeScript files on disk.
|
|
||||||
exports.ruby_compile_files: (paths, callback) ->
|
|
||||||
js: ''
|
|
||||||
coffee: process.createChildProcess compiler(), ['--print'].concat(paths)
|
|
||||||
|
|
||||||
coffee.addListener 'output', (results) ->
|
|
||||||
js += results if results?
|
|
||||||
|
|
||||||
# NB: we have to add a mutex to make sure it doesn't get called twice.
|
|
||||||
exit_ran: false
|
|
||||||
coffee.addListener 'exit', ->
|
|
||||||
return if exit_ran
|
|
||||||
exit_ran: true
|
|
||||||
callback(js)
|
|
||||||
|
|
||||||
coffee.addListener 'error', (message) ->
|
|
||||||
return unless message
|
|
||||||
puts message
|
|
||||||
throw new Error "CoffeeScript compile error"
|
|
||||||
|
|||||||
42
src/narwhal.coffee
Normal file
42
src/narwhal.coffee
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
# The Narwhal-compatibility wrapper for CoffeeScript.
|
||||||
|
|
||||||
|
# Require external dependencies.
|
||||||
|
os: require 'os'
|
||||||
|
file: require 'file'
|
||||||
|
coffee: require './coffee-script'
|
||||||
|
|
||||||
|
# Alias print to "puts", for Node.js compatibility:
|
||||||
|
puts: print
|
||||||
|
|
||||||
|
# Compile a string of CoffeeScript into JavaScript.
|
||||||
|
exports.compile: (source) ->
|
||||||
|
coffee.compile source
|
||||||
|
|
||||||
|
# Compile a given CoffeeScript file into JavaScript.
|
||||||
|
exports.compileFile: (path) ->
|
||||||
|
coffee.compile file.read path
|
||||||
|
|
||||||
|
# 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 + ")"
|
||||||
|
|
||||||
|
# The Narwhal loader for '.coffee' files.
|
||||||
|
factories: {}
|
||||||
|
loader: {}
|
||||||
|
|
||||||
|
# Reload the coffee-script environment from source.
|
||||||
|
loader.reload: (topId, path) ->
|
||||||
|
factories[topId]: ->
|
||||||
|
exports.makeNarwhalFactory path
|
||||||
|
|
||||||
|
# Ensure that the coffee-script environment is loaded.
|
||||||
|
loader.load: (topId, path) ->
|
||||||
|
factories[topId] ||= this.reload topId, path
|
||||||
|
|
||||||
|
require.loader.loaders.unshift [".coffee", loader]
|
||||||
@@ -1,77 +0,0 @@
|
|||||||
# The Narwhal-compatibility wrapper for CoffeeScript.
|
|
||||||
|
|
||||||
# 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().dirname().dirname().join('bin', 'coffee')
|
|
||||||
|
|
||||||
# Our general-purpose error handler.
|
|
||||||
checkForErrors: (coffeeProcess) ->
|
|
||||||
return true if coffeeProcess.wait() is 0
|
|
||||||
system.stderr.print coffeeProcess.stderr.read()
|
|
||||||
throw new Error "CoffeeScript compile error"
|
|
||||||
|
|
||||||
# Alias print to "puts", for Node.js compatibility:
|
|
||||||
puts: print
|
|
||||||
|
|
||||||
# Run a simple REPL, round-tripping to the CoffeeScript compiler for every
|
|
||||||
# command.
|
|
||||||
exports.run: (args) ->
|
|
||||||
if args.length
|
|
||||||
for path, i in args
|
|
||||||
exports.evalCS File.read path
|
|
||||||
delete args[i]
|
|
||||||
return true
|
|
||||||
|
|
||||||
while true
|
|
||||||
try
|
|
||||||
system.stdout.write('coffee> ').flush()
|
|
||||||
result: exports.evalCS Readline.readline(), ['--globals']
|
|
||||||
print result if result isnt undefined
|
|
||||||
catch e
|
|
||||||
print e
|
|
||||||
|
|
||||||
# Compile a given CoffeeScript file into JavaScript.
|
|
||||||
exports.compileFile: (path) ->
|
|
||||||
coffee: OS.popen [coffeePath, "--print", "--no-wrap", path]
|
|
||||||
checkForErrors coffee
|
|
||||||
coffee.stdout.read()
|
|
||||||
|
|
||||||
# Compile a string of CoffeeScript into JavaScript.
|
|
||||||
exports.compile: (source, flags) ->
|
|
||||||
coffee: OS.popen [coffeePath, "--eval", "--no-wrap"].concat flags or []
|
|
||||||
coffee.stdin.write(source).flush().close()
|
|
||||||
checkForErrors coffee
|
|
||||||
coffee.stdout.read()
|
|
||||||
|
|
||||||
# Evaluating a string of CoffeeScript first compiles it externally.
|
|
||||||
exports.evalCS: (source, flags) ->
|
|
||||||
eval exports.compile source, flags
|
|
||||||
|
|
||||||
# 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 + ")"
|
|
||||||
|
|
||||||
# The Narwhal loader for '.coffee' files.
|
|
||||||
factories: {}
|
|
||||||
loader: {}
|
|
||||||
|
|
||||||
# Reload the coffee-script environment from source.
|
|
||||||
loader.reload: (topId, path) ->
|
|
||||||
factories[topId]: ->
|
|
||||||
exports.makeNarwhalFactory path
|
|
||||||
|
|
||||||
# Ensure that the coffee-script environment is loaded.
|
|
||||||
loader.load: (topId, path) ->
|
|
||||||
factories[topId] ||= this.reload topId, path
|
|
||||||
|
|
||||||
require.loader.loaders.unshift [".coffee", loader]
|
|
||||||
Reference in New Issue
Block a user