1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

Better fixes for problem mentioned in last 3 commits. Also adds _

special value (as in node's REPL) that always holds return value of last
executed expression. Finally, fixes #1109
This commit is contained in:
Michael Ficarra 2011-04-29 13:59:59 -04:00
parent 0bdf9538d0
commit 63ce244359
4 changed files with 104 additions and 43 deletions

View file

@ -1,7 +1,8 @@
(function() { (function() {
var Lexer, RESERVED, compile, fs, lexer, parser, path, _ref; var Lexer, RESERVED, compile, fs, lexer, parser, path, vm, _ref;
fs = require('fs'); fs = require('fs');
path = require('path'); path = require('path');
vm = require('vm');
_ref = require('./lexer'), Lexer = _ref.Lexer, RESERVED = _ref.RESERVED; _ref = require('./lexer'), Lexer = _ref.Lexer, RESERVED = _ref.RESERVED;
parser = require('./parser').parser; parser = require('./parser').parser;
if (require.extensions) { if (require.extensions) {
@ -64,11 +65,30 @@
} }
}; };
exports.eval = function(code, options) { exports.eval = function(code, options) {
global.__filename = module.filename = process.argv[1] = options.filename; var g, js, sandbox;
global.__dirname = path.dirname(__filename); if (options == null) {
global.require = require; options = {};
global.module = module; }
return (0, eval)(compile(code, options)); sandbox = options.sandbox;
if (!sandbox) {
sandbox = {
require: require,
module: {
exports: {}
},
global: {}
};
for (g in global) {
sandbox[g] = global[g];
}
sandbox.global.global = sandbox.global;
sandbox.global.root = sandbox.global;
sandbox.global.GLOBAL = sandbox.global;
}
sandbox.__filename = options.filename || 'eval';
sandbox.__dirname = path.dirname(sandbox.__filename);
js = compile("_=(" + (code.trim()) + ")", options);
return vm.runInNewContext(js, sandbox, sandbox.__filename);
}; };
lexer = new Lexer; lexer = new Lexer;
parser.lexer = { parser.lexer = {

View file

@ -15,27 +15,44 @@
return stdout.write((err.stack || err.toString()) + '\n\n'); return stdout.write((err.stack || err.toString()) + '\n\n');
}; };
backlog = ''; backlog = '';
run = function(buffer) { run = (function() {
var code, val; var g, sandbox;
code = backlog += '\n' + buffer.toString(); sandbox = {
if (code[code.length - 1] === '\\') { require: require,
return backlog = backlog.slice(0, backlog.length - 1); module: {
exports: {}
},
global: {}
};
for (g in global) {
sandbox[g] = global[g];
} }
backlog = ''; sandbox.global.global = sandbox.global;
try { sandbox.global.root = sandbox.global;
val = CoffeeScript.eval(code, { sandbox.global.GLOBAL = sandbox.global;
bare: true, return function(buffer) {
globals: true, var code, val;
filename: 'repl' code = backlog += '\n' + buffer.toString();
}); if (code[code.length - 1] === '\\') {
if (val !== void 0) { return backlog = backlog.slice(0, backlog.length - 1);
process.stdout.write(inspect(val, false, 2, enableColours) + '\n');
} }
} catch (err) { backlog = '';
error(err); try {
} val = CoffeeScript.eval(code, {
return repl.prompt(); sandbox: sandbox,
}; bare: true,
globals: true,
filename: 'repl'
});
if (val !== void 0) {
process.stdout.write(inspect(val, false, 2, enableColours) + '\n');
}
} catch (err) {
error(err);
}
return repl.prompt();
};
})();
ACCESSOR = /\s*([\w\.]+)(?:\.(\w*))$/; ACCESSOR = /\s*([\w\.]+)(?:\.(\w*))$/;
SIMPLEVAR = /\s*(\w*)$/i; SIMPLEVAR = /\s*(\w*)$/i;
autocomplete = function(text) { autocomplete = function(text) {

View file

@ -8,6 +8,7 @@
fs = require 'fs' fs = require 'fs'
path = require 'path' path = require 'path'
vm = require 'vm'
{Lexer,RESERVED} = require './lexer' {Lexer,RESERVED} = require './lexer'
{parser} = require './parser' {parser} = require './parser'
@ -77,12 +78,21 @@ exports.run = (code, options) ->
# Compile and evaluate a string of CoffeeScript (in a Node.js-like environment). # Compile and evaluate a string of CoffeeScript (in a Node.js-like environment).
# The CoffeeScript REPL uses this to run the input. # The CoffeeScript REPL uses this to run the input.
exports.eval = (code, options) -> exports.eval = (code, options = {}) ->
global.__filename = module.filename = process.argv[1] = options.filename sandbox = options.sandbox
global.__dirname = path.dirname __filename unless sandbox
global.require = require sandbox =
global.module = module require: require
(0;eval) compile code, options module : { exports: {} }
global : {}
sandbox[g] = global[g] for g of global
sandbox.global.global = sandbox.global
sandbox.global.root = sandbox.global
sandbox.global.GLOBAL = sandbox.global
sandbox.__filename = options.filename || 'eval'
sandbox.__dirname = path.dirname sandbox.__filename
js = compile "_=(#{code.trim()})", options
vm.runInNewContext js, sandbox, sandbox.__filename
# Instantiate a Lexer for our use here. # Instantiate a Lexer for our use here.
lexer = new Lexer lexer = new Lexer

View file

@ -31,18 +31,32 @@ backlog = ''
# The main REPL function. **run** is called every time a line of code is entered. # The main REPL function. **run** is called every time a line of code is entered.
# Attempt to evaluate the command. If there's an exception, print it out instead # Attempt to evaluate the command. If there's an exception, print it out instead
# of exiting. # of exiting.
run = (buffer) -> run = do ->
code = backlog += '\n' + buffer.toString() sandbox =
if code[code.length - 1] is '\\' require: require
return backlog = backlog[0...backlog.length - 1] module : { exports: {} }
backlog = '' global : {}
try sandbox[g] = global[g] for g of global
val = CoffeeScript.eval code, bare: on, globals: on, filename: 'repl' sandbox.global.global = sandbox.global
unless val is undefined sandbox.global.root = sandbox.global
process.stdout.write inspect(val, no, 2, enableColours) + '\n' sandbox.global.GLOBAL = sandbox.global
catch err (buffer) ->
error err code = backlog += '\n' + buffer.toString()
repl.prompt() if code[code.length - 1] is '\\'
return backlog = backlog[0...backlog.length - 1]
backlog = ''
try
val = CoffeeScript.eval code, {
sandbox,
bare: on,
globals: on,
filename: 'repl'
}
unless val is undefined
process.stdout.write inspect(val, no, 2, enableColours) + '\n'
catch err
error err
repl.prompt()
## Autocompletion ## Autocompletion