Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REPL changes context on each evaluation #1444

Closed
TrevorBurnham opened this issue Jun 18, 2011 · 8 comments
Closed

REPL changes context on each evaluation #1444

TrevorBurnham opened this issue Jun 18, 2011 · 8 comments
Assignees

Comments

@TrevorBurnham
Copy link
Collaborator

A helpful reader of my book just reported an interesting issue. Michael, maybe you can figure this one out?

coffee> this is global
true  # as expected
coffee> foo = -> this is global
coffee> foo()
false  # surprising...
coffee> foo.call global
false  # mind-blowing

And it's not that functions can't be run in any context; it seems they just can't be run in global:

coffee> bar = {}
coffee> baz = -> this is bar
coffee> baz.call bar
true

Here's another rub: If you define and run the function all in one line, then it does run in the global context by default:

coffee> (-> this is global)()
true
coffee> (-> this is global).call global
true
coffee> do testFunc = -> this is global
true

Tested under CoffeeScript 1.1.1 and current master. Any ideas?

Btw, tested under the Node REPL; no issues there:

node> var foo = function() {console.log(this === global);}
node> foo()
true
@michaelficarra
Copy link
Collaborator

Yeah, the REPL has some issues with global objects currently, likely all my fault. See #1425 and #1035 for the others. These three are on the top of my TODO list, but it could be up to a week before I get enough time to actually sit down and figure these out.

edit: Actually, I'll probably get to them tomorrow.

edit: Actually, probably not... ugh.

@telemachus
Copy link

For what it's worth, I'm getting varied and inconsistent results for coffee, node and v8 (using the REPL and interpreter for all three): https://gist.github.com/1033081

@TrevorBurnham
Copy link
Collaborator Author

@telemachus I posted an answer to your Stack Overflow question: http://stackoverflow.com/questions/6396467/a-puzzle-about-this-in-javascript-coffeescript/6397029#6397029

The inconsistency is because this in the outermost scope is global under v8 and the REPL, but exports under coffee and node when a file is being run. That's all as it should be; I need to explain it better in the book.

@satyr
Copy link
Collaborator

satyr commented Jun 19, 2011

Renamed title. ("Function context can't be global in REPL" was inaccurate.)

@ghost ghost assigned michaelficarra Jun 24, 2011
@michaelficarra
Copy link
Collaborator

Check this out:

self = do -> this
self is global # true!

@TrevorBurnham: After playing around with this for a while, I'm pretty sure you've found a bug with node's vm.runInNewContext.

vm = require('vm');
sandbox = {};
sandbox.global = sandbox;
vm.runInNewContext('global === this', sandbox); // true, as expected
vm.runInNewContext('(function(){ return global === this }())', sandbox); // true, as expected
vm.runInNewContext('var fn = function(){ return global === this }', sandbox);
vm.runInNewContext('fn()', sandbox); // false, unexpectedly

I'll post back here with the issue number as soon as I file it.

update: See node issue #1280.

michaelficarra added a commit that referenced this issue Jul 6, 2011
CoffeeScript.eval. Instead of writing about all the changes and why I
made those decisions, I'll just answer any questions in the commit
comments, so add a commit comment if you want to question anything.
Thanks to @TrevorBurnham and @satyr for their help/contributions. Also,
closes #1487. And still no REPL tests...
@michaelficarra
Copy link
Collaborator

Fixed by fff4c9c, which doesn't use vm.runInNewContext

michaelficarra added a commit that referenced this issue Jul 7, 2011
CoffeeScript.eval. Instead of writing about all the changes and why I
made those decisions, I'll just answer any questions in the commit
comments, so add a commit comment if you want to question anything.
Thanks to @TrevorBurnham and @satyr for their help/contributions. Also,
closes #1487. And still no REPL tests...
@satyr
Copy link
Collaborator

satyr commented Jul 7, 2011

Should probably fix tab completion as well.

$ node
> Array.foo = 0
0
> Array.f

Hitting tab here completes to .foo as expected. Not the case for coffee REPL.

@michaelficarra
Copy link
Collaborator

@satyr: Fixed tab completion in af1cf34

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants