merging @TrevorBurnham's pull request #1314, enhancing `CS.eval`; closes #1314

This commit is contained in:
Michael Ficarra 2011-05-25 03:53:51 -04:00
parent bbf37e5229
commit e4f47a05f6
3 changed files with 14 additions and 3 deletions

View File

@ -60,7 +60,7 @@
}
};
exports.eval = function(code, options) {
var g, js, sandbox, _i, _len, _ref2;
var g, js, k, o, sandbox, v, _i, _len, _ref2;
if (options == null) {
options = {};
}
@ -82,7 +82,13 @@
}
sandbox.__filename = options.filename || 'eval';
sandbox.__dirname = path.dirname(sandbox.__filename);
js = compile("_=(" + (code.trim()) + ")", options);
o = {};
for (k in options) {
v = options[k];
o[k] = v;
}
o.bare = true;
js = compile("_=(" + (code.trim()) + ")", o);
return vm.runInNewContext(js, sandbox, sandbox.__filename);
};
lexer = new Lexer;

View File

@ -87,7 +87,9 @@ exports.eval = (code, options = {}) ->
sandbox.global.global = sandbox.global.root = sandbox.global.GLOBAL = sandbox
sandbox.__filename = options.filename || 'eval'
sandbox.__dirname = path.dirname sandbox.__filename
js = compile "_=(#{code.trim()})", options
o = {}; o[k] = v for k, v of options
o.bare = on # ensure return value
js = compile "_=(#{code.trim()})", o
vm.runInNewContext js, sandbox, sandbox.__filename
# Instantiate a Lexer for our use here.

View File

@ -59,3 +59,6 @@ test "#1106: __proto__ compilation", ->
object = eq
@["__proto__"] = true
ok __proto__
test "eval returns result when bare is false and filename is not set", ->
ok 'passed' is CoffeeScript.eval '"passed"'