Bug 18717 - SQUIRRELFISH: eval returns the wrong value for a variable declaration statement
Summary: SQUIRRELFISH: eval returns the wrong value for a variable declaration statement
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: 528+ (Nightly build)
Hardware: All All
: P2 Normal
Assignee: Nobody
URL:
Keywords:
Depends on:
Blocks: 18624
  Show dependency treegraph
 
Reported: 2008-04-24 12:41 PDT by Cameron Zwarich (cpst)
Modified: 2008-04-24 16:01 PDT (History)
3 users (show)

See Also:


Attachments
Proposed patch (3.69 KB, patch)
2008-04-24 15:29 PDT, Cameron Zwarich (cpst)
no flags Details | Formatted Diff | Diff
Revised proposed patch (3.98 KB, patch)
2008-04-24 15:43 PDT, Cameron Zwarich (cpst)
oliver: review+
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Cameron Zwarich (cpst) 2008-04-24 12:41:04 PDT
The expression eval("var x = 0;") returns 0 instead of undefined. This causes the JavaScriptCore test ecma_3/Expressions/11.9.6-1.js to fail. Oliver suggested sending something other than dst to the expression in VarStatementNode::emitCode(), but that doesn't work, because then eval("f(); var x= 0;") will return the return value of f(). It seems the best way to fix this is to see if a variable statement is the last statement in a list of statements, in which case you generate code to overwrite its return value with undefined.
Comment 1 Cameron Zwarich (cpst) 2008-04-24 12:49:28 PDT
I should also add that there is nothing in the ECMA spec that suggests it should return undefined, at least as far as I can tell.
Comment 2 Oliver Hunt 2008-04-24 13:56:57 PDT
eval("var x = 0") should return undefined as nothing should ever assign to dst.  However the rule is (afaict from my testing) that var statements are transparent to the result value, so 
eval("someExpr; var x = 0;") 
will return the result of someExpr, assuming someExpr actually has a return value.

I believe therefore the example eval("f(); var x=0;") *should* return the result of f().

Comment 3 Cameron Zwarich (cpst) 2008-04-24 15:29:50 PDT
Created attachment 20802 [details]
Proposed patch

Yeah, Oliver, I accidentally misread the spec. Here is a patch that fixes the problem. It seems to be a 0.2% progression on SunSpider. It fixes ecma_3/Expressions/11.9.6-1.js, but causes js1_5/GetSet/getset-005.js to crash, like many other tests using getters and setters.
Comment 4 Darin Adler 2008-04-24 15:37:03 PDT
Comment on attachment 20802 [details]
Proposed patch

It's better to omit or comment out the argument name "dst" rather than doing "UNUSED_PARAM(dst)" when practical. There are times when due to #ifdef's or ASSERT, for example, that UNUSED_PARAM is unavoidable or better. But I don't think this is one of those times.
Comment 5 Cameron Zwarich (cpst) 2008-04-24 15:43:03 PDT
Created attachment 20803 [details]
Revised proposed patch

Here is a revised version of the patch that avoids the use of UNUSED_PARAM.
Comment 6 Oliver Hunt 2008-04-24 16:01:36 PDT
Committed r32520