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() {
var Lexer, RESERVED, compile, fs, lexer, parser, path, _ref;
var Lexer, RESERVED, compile, fs, lexer, parser, path, vm, _ref;
fs = require('fs');
path = require('path');
vm = require('vm');
_ref = require('./lexer'), Lexer = _ref.Lexer, RESERVED = _ref.RESERVED;
parser = require('./parser').parser;
if (require.extensions) {
@ -64,11 +65,30 @@
}
};
exports.eval = function(code, options) {
global.__filename = module.filename = process.argv[1] = options.filename;
global.__dirname = path.dirname(__filename);
global.require = require;
global.module = module;
return (0, eval)(compile(code, options));
var g, js, sandbox;
if (options == null) {
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;
parser.lexer = {

View file

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

View file

@ -8,6 +8,7 @@
fs = require 'fs'
path = require 'path'
vm = require 'vm'
{Lexer,RESERVED} = require './lexer'
{parser} = require './parser'
@ -77,12 +78,21 @@ exports.run = (code, options) ->
# Compile and evaluate a string of CoffeeScript (in a Node.js-like environment).
# The CoffeeScript REPL uses this to run the input.
exports.eval = (code, options) ->
global.__filename = module.filename = process.argv[1] = options.filename
global.__dirname = path.dirname __filename
global.require = require
global.module = module
(0;eval) compile code, options
exports.eval = (code, options = {}) ->
sandbox = options.sandbox
unless sandbox
sandbox =
require: require
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.
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.
# Attempt to evaluate the command. If there's an exception, print it out instead
# of exiting.
run = (buffer) ->
code = backlog += '\n' + buffer.toString()
if code[code.length - 1] is '\\'
return backlog = backlog[0...backlog.length - 1]
backlog = ''
try
val = CoffeeScript.eval code, 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()
run = do ->
sandbox =
require: require
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
(buffer) ->
code = backlog += '\n' + buffer.toString()
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