2010-07-24 15:31:43 +00:00
|
|
|
(function() {
|
2011-07-11 16:55:29 +00:00
|
|
|
var ACCESSOR, CoffeeScript, Module, REPL_PROMPT, REPL_PROMPT_CONTINUATION, SIMPLEVAR, Script, autocomplete, backlog, completeAttribute, completeVariable, enableColours, error, g, getCompletions, inspect, nonContextGlobals, readline, repl, run, sandbox, stdin, stdout, _i, _len;
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-03-16 03:39:46 +00:00
|
|
|
CoffeeScript = require('./coffee-script');
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-06-11 22:36:18 +00:00
|
|
|
readline = require('readline');
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2011-04-06 00:40:54 +00:00
|
|
|
inspect = require('util').inspect;
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2011-03-16 10:47:58 +00:00
|
|
|
Script = require('vm').Script;
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2011-07-06 07:54:36 +00:00
|
|
|
Module = require('module');
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2011-07-06 20:31:48 +00:00
|
|
|
REPL_PROMPT = 'coffee> ';
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2011-07-06 20:31:48 +00:00
|
|
|
REPL_PROMPT_CONTINUATION = '......> ';
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2011-04-06 00:51:28 +00:00
|
|
|
enableColours = false;
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2011-08-08 16:55:22 +00:00
|
|
|
if (process.platform !== 'win32') {
|
|
|
|
enableColours = !process.env.NODE_DISABLE_COLORS;
|
|
|
|
}
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2011-01-22 10:40:25 +00:00
|
|
|
stdin = process.openStdin();
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2011-01-22 10:40:25 +00:00
|
|
|
stdout = process.stdout;
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-12-05 20:46:04 +00:00
|
|
|
error = function(err) {
|
2011-09-19 06:18:42 +00:00
|
|
|
return stdout.write((err.stack || err.toString()) + '\n');
|
2010-12-05 20:46:04 +00:00
|
|
|
};
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2011-07-06 21:00:13 +00:00
|
|
|
backlog = '';
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2011-07-10 18:26:31 +00:00
|
|
|
sandbox = Script.createContext();
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2011-07-11 17:08:47 +00:00
|
|
|
nonContextGlobals = ['Buffer', 'console', 'process', 'setInterval', 'clearInterval', 'setTimeout', 'clearTimeout'];
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2011-07-11 16:55:29 +00:00
|
|
|
for (_i = 0, _len = nonContextGlobals.length; _i < _len; _i++) {
|
|
|
|
g = nonContextGlobals[_i];
|
|
|
|
sandbox[g] = global[g];
|
|
|
|
}
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2011-07-10 18:26:31 +00:00
|
|
|
sandbox.global = sandbox.root = sandbox.GLOBAL = sandbox;
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2011-07-10 18:26:31 +00:00
|
|
|
run = function(buffer) {
|
|
|
|
var code, returnValue, _;
|
|
|
|
if (!buffer.toString().trim() && !backlog) {
|
|
|
|
repl.prompt();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
code = backlog += buffer;
|
|
|
|
if (code[code.length - 1] === '\\') {
|
|
|
|
backlog = "" + backlog.slice(0, -1) + "\n";
|
|
|
|
repl.setPrompt(REPL_PROMPT_CONTINUATION);
|
|
|
|
repl.prompt();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
repl.setPrompt(REPL_PROMPT);
|
|
|
|
backlog = '';
|
|
|
|
try {
|
|
|
|
_ = sandbox._;
|
|
|
|
returnValue = CoffeeScript.eval("_=(" + code + "\n)", {
|
|
|
|
sandbox: sandbox,
|
|
|
|
filename: 'repl',
|
|
|
|
modulename: 'repl'
|
|
|
|
});
|
|
|
|
if (returnValue === void 0) {
|
|
|
|
sandbox._ = _;
|
|
|
|
} else {
|
|
|
|
process.stdout.write(inspect(returnValue, false, 2, enableColours) + '\n');
|
2011-04-29 17:59:59 +00:00
|
|
|
}
|
2011-07-10 18:26:31 +00:00
|
|
|
} catch (err) {
|
|
|
|
error(err);
|
|
|
|
}
|
|
|
|
return repl.prompt();
|
|
|
|
};
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2011-01-15 19:53:07 +00:00
|
|
|
ACCESSOR = /\s*([\w\.]+)(?:\.(\w*))$/;
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2011-01-15 19:53:07 +00:00
|
|
|
SIMPLEVAR = /\s*(\w*)$/i;
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2011-01-15 19:53:07 +00:00
|
|
|
autocomplete = function(text) {
|
|
|
|
return completeAttribute(text) || completeVariable(text) || [[], text];
|
|
|
|
};
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2011-01-15 19:53:07 +00:00
|
|
|
completeAttribute = function(text) {
|
|
|
|
var all, completions, match, obj, prefix, val;
|
|
|
|
if (match = text.match(ACCESSOR)) {
|
|
|
|
all = match[0], obj = match[1], prefix = match[2];
|
|
|
|
try {
|
2011-07-10 18:26:31 +00:00
|
|
|
val = Script.runInContext(obj, sandbox);
|
2011-01-15 19:53:07 +00:00
|
|
|
} catch (error) {
|
2011-07-10 18:26:31 +00:00
|
|
|
return;
|
2011-01-15 19:53:07 +00:00
|
|
|
}
|
2011-07-10 18:26:31 +00:00
|
|
|
completions = getCompletions(prefix, Object.getOwnPropertyNames(val));
|
2011-01-15 19:53:07 +00:00
|
|
|
return [completions, prefix];
|
|
|
|
}
|
|
|
|
};
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2011-01-15 19:53:07 +00:00
|
|
|
completeVariable = function(text) {
|
2011-08-08 22:22:22 +00:00
|
|
|
var completions, free, keywords, possibilities, r, vars, _ref;
|
|
|
|
free = (_ref = text.match(SIMPLEVAR)) != null ? _ref[1] : void 0;
|
|
|
|
if (free != null) {
|
2011-07-10 18:26:31 +00:00
|
|
|
vars = Script.runInContext('Object.getOwnPropertyNames(this)', sandbox);
|
2011-08-08 22:22:22 +00:00
|
|
|
keywords = (function() {
|
|
|
|
var _j, _len2, _ref2, _results;
|
|
|
|
_ref2 = CoffeeScript.RESERVED;
|
|
|
|
_results = [];
|
|
|
|
for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) {
|
|
|
|
r = _ref2[_j];
|
|
|
|
if (r.slice(0, 2) !== '__') _results.push(r);
|
|
|
|
}
|
|
|
|
return _results;
|
|
|
|
})();
|
|
|
|
possibilities = vars.concat(keywords);
|
2011-07-10 18:26:31 +00:00
|
|
|
completions = getCompletions(free, possibilities);
|
2011-01-15 19:53:07 +00:00
|
|
|
return [completions, free];
|
|
|
|
}
|
|
|
|
};
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2011-01-15 19:53:07 +00:00
|
|
|
getCompletions = function(prefix, candidates) {
|
2011-07-11 16:55:29 +00:00
|
|
|
var el, _j, _len2, _results;
|
2011-01-15 19:53:07 +00:00
|
|
|
_results = [];
|
2011-07-11 16:55:29 +00:00
|
|
|
for (_j = 0, _len2 = candidates.length; _j < _len2; _j++) {
|
|
|
|
el = candidates[_j];
|
2011-08-07 06:59:37 +00:00
|
|
|
if (el.indexOf(prefix) === 0) _results.push(el);
|
2011-01-15 19:53:07 +00:00
|
|
|
}
|
|
|
|
return _results;
|
|
|
|
};
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-12-05 20:46:04 +00:00
|
|
|
process.on('uncaughtException', error);
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2011-01-24 06:57:31 +00:00
|
|
|
if (readline.createInterface.length < 3) {
|
2011-01-22 11:30:22 +00:00
|
|
|
repl = readline.createInterface(stdin, autocomplete);
|
|
|
|
stdin.on('data', function(buffer) {
|
|
|
|
return repl.write(buffer);
|
|
|
|
});
|
2011-01-22 11:54:43 +00:00
|
|
|
} else {
|
|
|
|
repl = readline.createInterface(stdin, stdout, autocomplete);
|
2011-01-22 11:30:22 +00:00
|
|
|
}
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2011-07-06 21:00:13 +00:00
|
|
|
repl.on('attemptClose', function() {
|
|
|
|
if (backlog) {
|
|
|
|
backlog = '';
|
|
|
|
process.stdout.write('\n');
|
|
|
|
repl.setPrompt(REPL_PROMPT);
|
|
|
|
return repl.prompt();
|
|
|
|
} else {
|
|
|
|
return repl.close();
|
|
|
|
}
|
|
|
|
});
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-07-18 11:54:44 +00:00
|
|
|
repl.on('close', function() {
|
2011-07-06 17:05:07 +00:00
|
|
|
process.stdout.write('\n');
|
2011-01-22 10:40:25 +00:00
|
|
|
return stdin.destroy();
|
2010-06-11 22:36:18 +00:00
|
|
|
});
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-07-18 11:54:44 +00:00
|
|
|
repl.on('line', run);
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2011-07-06 21:00:13 +00:00
|
|
|
repl.setPrompt(REPL_PROMPT);
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-06-11 22:36:18 +00:00
|
|
|
repl.prompt();
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-09-21 07:53:58 +00:00
|
|
|
}).call(this);
|