Bug 701886 - eval() incorrectly sets variable to undefined when redeclaring it with var
Summary: eval() incorrectly sets variable to undefined when redeclaring it with var
Status: RESOLVED FIXED
Alias: None
Product: MuJS
Classification: Unclassified
Component: general (show other bugs)
Version: 1.0.6
Hardware: PC Linux
: P4 major
Assignee: Tor Andersson
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-11-08 15:59 UTC by Arto Pekkanen
Modified: 2021-03-04 11:22 UTC (History)
0 users

See Also:
Customer:
Word Size: ---


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Arto Pekkanen 2019-11-08 15:59:59 UTC
var foo = {name: "foo"};
foo.name;
var foo;
foo.name;
eval("var foo;");
foo.name;

When you run the forementioned code, the first two foo.name statements execute fine just as expected; even after foo has been initialized in the first line, the second "var foo" statement doesn't unset the variable.

However, the third foo.name statement causes an exception:
Uncaught exception - TypeError: cannot convert undefined to object

When you inspect the variable foo after the eval() statement, you find out that it has been unset (set to undefined), which in my view is incorrect behaviour. All variable declarations and assignments inside eval() should behave as if they were executed in the outside of eval(). Or does ECMA-262 specify otherwise? Seems unlikely.

This bug is present in 1.6.0 and master branch, and thus likely in all versions.
Comment 1 Tor Andersson 2020-01-10 10:00:17 UTC
commit 3d3f473c399186d229bc0313d3f4efaef0cc5bdb
Author: Tor Andersson <tor.andersson@artifex.com>
Date:   Mon Nov 11 11:02:54 2019 +0100

    Bug 701887: Create arguments if eval is present.
    
    We can't know at compile time that the 'arguments' object will not be used
    from the eval statement, so err on the side of caution and always create
    the arguments object if eval can be called.
Comment 2 Tor Andersson 2020-01-10 10:01:09 UTC
commit d248b0ce1800a1ebf2c853f205c1947642185c6a
Author: Tor Andersson <tor.andersson@artifex.com>
Date:   Mon Nov 11 11:34:43 2019 +0100

    Bug 701886: Always create new scope for eval().
    
    Distinguish eval code from script code.
Comment 3 Tor Andersson 2021-03-04 11:22:23 UTC
The fix in d248b0ce1800a1ebf2c853f205c1947642185c6a is wrong.

Fixed in a better way in:

commit a34fdf2af87cc13b1d85cd19812c4d0b722f3e3a
Author: Tor Andersson <tor.andersson@artifex.com>
Date:   Thu Mar 4 12:20:46 2021 +0100

    Bug 701886: Don't redefine/reset existing vars in script code.
    
    If a var is already declared in the same scope, don't redeclare it.
    Should fix issues with "var" used in eval() code.