Issue 2311 in v8: Inconsistent closure behaviour in "with" statement.

8 views
Skip to first unread message

codesite...@google.com

unread,
Aug 30, 2012, 2:22:20 AM8/30/12
to v8-...@googlegroups.com
Status: New
Owner: ----

New issue 2311 by srikuma...@gmail.com: Inconsistent closure behaviour
in "with" statement.
http://code.google.com/p/v8/issues/detail?id=2311

Named functions within "with" statements are (incorrectly?) lifted out of
the "with" scope and therefore don't capture the scope object in their
lexical environments. Closures, otoh, capture the scope object correctly.
Not sure whether this is according to spec, but it seems inconsistent. It
is also inconsistent with Firefox's implementation in which both forms of
functions capture the scope object correctly.

Browsers: Chrome Canary 23.0.1249.0, Firefox 12.0

Test case: The following code snippet fails with "ReferenceError" in V8,
but runs as expected in Firefox.

{{{
var scope = {msg: "hello world"};
var f;
with (scope) {
function show() {
console.log(msg);
}
f = show;
}
f();
}}}

The following code works in V8, though -

{{{
var scope = {msg: "hello world"};
var f;
with (scope) {
f = function () {
console.log(msg);
};
}
f();
}}}



codesite...@google.com

unread,
Oct 9, 2012, 5:26:07 AM10/9/12
to v8-...@googlegroups.com
Updates:
Status: WorkingAsIntended
Owner: rossb...@chromium.org

Comment #1 on issue 2311 by rossb...@chromium.org: Inconsistent closure
The first snippet is not actually legal ES5, because function declarations
are only allowed at global or function scope. Existing implementations
allow local function declarations, but wildly differ in their behaviour --
V8 usually follows Safari in such cases.

Because ES5 lacks block scoping and thus requires hoisting of declarations,
there is no reasonable semantics that can be given to local function
declarations. This will be fixed with the upcoming ES6, which introduces
proper scoping and legalizes local function declarations based on that.


Reply all
Reply to author
Forward
0 new messages