nested functions do not have access to variables defined in their parent function

VERIFIED WORKSFORME

Status

()

VERIFIED WORKSFORME
18 years ago
17 years ago

People

(Reporter: rginda, Assigned: rogerl)

Tracking

({js1.5})

Trunk
x86
All
js1.5
Points:
---

Firefox Tracking Flags

(Not tracked)

Details

(Reporter)

Description

18 years ago
Here is most of the text from an email I got from Steve Newman,
snewman@macromedia.com:

...nested functions do not seem to have access
to variables
defined in their parent function, at least in some cases.
Here is a
test case that reproduces the bug:

    function test()
    {
       var param = 5;
       // var foo = test.__call__;

       (function() {alert(param*param);}) ();
    }

    test();

When I execute this script, I get an undefined-identifier
error
for the use of "param" in the anonymous function.  However,
if I
enable the declaration of "var foo" (by removing the comment
slashes),
the code executes properly.

As far as I can tell, the problem is that in the code for
"case JSOP_ANONFUNOBJ:" in jsinterp.c, the inner function's
parent
chain is set to fp->varobj, but fp->varobj is null.
Referencing
test.__call__ causes js_GetCallObject to be called, which
has the
side effect of filling in fp->varobj with the call object
for the
test() function, and so the ANONFUNOBJ code then sets up the
parent
chain correctly.
(Reporter)

Updated

18 years ago
Keywords: js1.5
This like so worksforme, and works even better with the brendan/shaver patch in
bug 65308.  Unless someone removed the logic to set JSFUN_HEAVYWEIGHT for the
outer function, I don't see how it could have broken.  I also believe (hope!)
that cases like this are covered by our testsuite.

What version of the engine is Steve using?

/be
Status: NEW → RESOLVED
Last Resolved: 18 years ago
Resolution: --- → WORKSFORME

Comment 2

18 years ago
Verifying WORKSFORME on WinNT. Steve's testcase and variations that I've 
tried seem to work just fine. I will add all this to the JS test suite - 
Status: RESOLVED → VERIFIED
You need to log in before you can comment on or make changes to this bug.