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

Whoops, mergefail; corrected

This commit is contained in:
Trevor Burnham 2011-10-23 22:45:32 -04:00
parent cf32ba0149
commit 3f4daaf2ad
2 changed files with 25 additions and 33 deletions

View file

@ -1,5 +1,5 @@
(function() { (function() {
var Lexer, RESERVED, compile, fs, lexer, parser, path, _ref; var Lexer, RESERVED, compile, fs, lexer, parser, path, vm, _ref;
var __hasProp = Object.prototype.hasOwnProperty; var __hasProp = Object.prototype.hasOwnProperty;
fs = require('fs'); fs = require('fs');
@ -10,6 +10,8 @@
parser = require('./parser').parser; parser = require('./parser').parser;
vm = require('vm');
if (require.extensions) { if (require.extensions) {
require.extensions['.coffee'] = function(module, filename) { require.extensions['.coffee'] = function(module, filename) {
var content; var content;
@ -74,14 +76,13 @@
var Module, Script, js, k, o, r, sandbox, v, _i, _len, _module, _ref2, _ref3, _require; var Module, Script, js, k, o, r, sandbox, v, _i, _len, _module, _ref2, _ref3, _require;
if (options == null) options = {}; if (options == null) options = {};
if (!(code = code.trim())) return; if (!(code = code.trim())) return;
Script = require('vm').Script; Script = vm.Script;
if (Script) { if (Script) {
sandbox = Script.createContext();
sandbox.global = sandbox.root = sandbox.GLOBAL = sandbox;
if (options.sandbox != null) { if (options.sandbox != null) {
if (options.sandbox instanceof sandbox.constructor) { if (options.sandbox instanceof Script.createContext().constructor) {
sandbox = options.sandbox; sandbox = options.sandbox;
} else { } else {
sandbox = Script.createContext();
_ref2 = options.sandbox; _ref2 = options.sandbox;
for (k in _ref2) { for (k in _ref2) {
if (!__hasProp.call(_ref2, k)) continue; if (!__hasProp.call(_ref2, k)) continue;
@ -89,14 +90,17 @@
sandbox[k] = v; sandbox[k] = v;
} }
} }
sandbox.global = sandbox.root = sandbox.GLOBAL = sandbox;
} else {
sandbox = global;
} }
sandbox.__filename = options.filename || 'eval'; sandbox.__filename = options.filename || 'eval';
sandbox.__dirname = path.dirname(sandbox.__filename); sandbox.__dirname = path.dirname(sandbox.__filename);
if (!(sandbox.module || sandbox.require)) { if (!(sandbox !== global || sandbox.module || sandbox.require)) {
Module = require('module'); Module = require('module');
sandbox.module = _module = new Module(options.modulename || 'eval'); sandbox.module = _module = new Module(options.modulename || 'eval');
sandbox.require = _require = function(path) { sandbox.require = _require = function(path) {
return Module._load(path, _module); return Module._load(path, _module, true);
}; };
_module.filename = sandbox.__filename; _module.filename = sandbox.__filename;
_ref3 = Object.getOwnPropertyNames(require); _ref3 = Object.getOwnPropertyNames(require);
@ -118,10 +122,10 @@
} }
o.bare = true; o.bare = true;
js = compile(code, o); js = compile(code, o);
if (Script) { if (sandbox === global) {
return Script.runInContext(js, sandbox); return vm.runInThisContext(js);
} else { } else {
return eval(js); return vm.runInContext(js, sandbox);
} }
}; };

View file

@ -1,5 +1,5 @@
(function() { (function() {
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; var ACCESSOR, CoffeeScript, Module, REPL_PROMPT, REPL_PROMPT_CONTINUATION, SIMPLEVAR, Script, autocomplete, backlog, completeAttribute, completeVariable, enableColours, error, getCompletions, inspect, readline, repl, run, stdin, stdout;
CoffeeScript = require('./coffee-script'); CoffeeScript = require('./coffee-script');
@ -31,17 +31,6 @@
backlog = ''; backlog = '';
sandbox = Script.createContext();
nonContextGlobals = ['Buffer', 'console', 'process', 'setInterval', 'clearInterval', 'setTimeout', 'clearTimeout'];
for (_i = 0, _len = nonContextGlobals.length; _i < _len; _i++) {
g = nonContextGlobals[_i];
sandbox[g] = global[g];
}
sandbox.global = sandbox.root = sandbox.GLOBAL = sandbox;
run = function(buffer) { run = function(buffer) {
var code, returnValue, _; var code, returnValue, _;
if (!buffer.toString().trim() && !backlog) { if (!buffer.toString().trim() && !backlog) {
@ -58,14 +47,13 @@
repl.setPrompt(REPL_PROMPT); repl.setPrompt(REPL_PROMPT);
backlog = ''; backlog = '';
try { try {
_ = sandbox._; _ = global._;
returnValue = CoffeeScript.eval("_=(" + code + "\n)", { returnValue = CoffeeScript.eval("_=(" + code + "\n)", {
sandbox: sandbox,
filename: 'repl', filename: 'repl',
modulename: 'repl' modulename: 'repl'
}); });
if (returnValue === void 0) { if (returnValue === void 0) {
sandbox._ = _; global._ = _;
} else { } else {
process.stdout.write(inspect(returnValue, false, 2, enableColours) + '\n'); process.stdout.write(inspect(returnValue, false, 2, enableColours) + '\n');
} }
@ -88,7 +76,7 @@
if (match = text.match(ACCESSOR)) { if (match = text.match(ACCESSOR)) {
all = match[0], obj = match[1], prefix = match[2]; all = match[0], obj = match[1], prefix = match[2];
try { try {
val = Script.runInContext(obj, sandbox); val = Script.runInThisContext(obj);
} catch (error) { } catch (error) {
return; return;
} }
@ -101,13 +89,13 @@
var completions, free, keywords, possibilities, r, vars, _ref; var completions, free, keywords, possibilities, r, vars, _ref;
free = (_ref = text.match(SIMPLEVAR)) != null ? _ref[1] : void 0; free = (_ref = text.match(SIMPLEVAR)) != null ? _ref[1] : void 0;
if (free != null) { if (free != null) {
vars = Script.runInContext('Object.getOwnPropertyNames(this)', sandbox); vars = Script.runInThisContext('Object.getOwnPropertyNames(this)');
keywords = (function() { keywords = (function() {
var _j, _len2, _ref2, _results; var _i, _len, _ref2, _results;
_ref2 = CoffeeScript.RESERVED; _ref2 = CoffeeScript.RESERVED;
_results = []; _results = [];
for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) { for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
r = _ref2[_j]; r = _ref2[_i];
if (r.slice(0, 2) !== '__') _results.push(r); if (r.slice(0, 2) !== '__') _results.push(r);
} }
return _results; return _results;
@ -119,10 +107,10 @@
}; };
getCompletions = function(prefix, candidates) { getCompletions = function(prefix, candidates) {
var el, _j, _len2, _results; var el, _i, _len, _results;
_results = []; _results = [];
for (_j = 0, _len2 = candidates.length; _j < _len2; _j++) { for (_i = 0, _len = candidates.length; _i < _len; _i++) {
el = candidates[_j]; el = candidates[_i];
if (el.indexOf(prefix) === 0) _results.push(el); if (el.indexOf(prefix) === 0) _results.push(el);
} }
return _results; return _results;