<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>14237</bug_id>
          
          <creation_ts>2007-06-19 16:28:00 -0700</creation_ts>
          <short_desc>Javascript &quot;var&quot; statement interprets initialization in the topmost function scope</short_desc>
          <delta_ts>2007-07-14 10:10:12 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>JavaScriptCore</component>
          <version>523.x (Safari 3)</version>
          <rep_platform>Mac</rep_platform>
          <op_sys>OS X 10.4</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Kris Kowal">kkowal</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>ap</cc>
    
    <cc>brendan</cc>
    
    <cc>kkowal</cc>
    
    <cc>mrowe</cc>
    
    <cc>zwarich</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>7062</commentid>
    <comment_count>0</comment_count>
    <who name="Kris Kowal">kkowal</who>
    <bug_when>2007-06-19 16:28:00 -0700</bug_when>
    <thetext>Javascript &quot;var&quot; statement interprets initialization in the topmost function scope.  While the declaration must be interpreted in the topmost function scope, the initialization should be interpreted in the topmost scope.  For example, the statement, {{{ var name = value; }}}, can be conceptually split into {{{ var name; }}} and {{{ name = value; }}}.  The former statement is interpreted in the topmost function scope or the global scope if there are no function scopes on the scope chain.  According to the ECMA 262 Edition 3 specification, the latter statement is interpreted in the topmost scope.  The distinction is only apparent when a variable is declared inside a {with} block that has the same name as a variable in the topmost context.  For example: {{{ with ({&apos;a&apos;: 10}) { var a = 20 } }}}.

This code exposes the flaw:
{{{
if (this.alert) print = alert;
var a = 10;
print(a); /* should be 10; not an issue */
var object = {&apos;a&apos;: 20};
with (object) {
    print(a); /* should be 20; so far no controversy */
    var a = 30; /* &quot;var a&quot; is evaluated in the function scope and ignored, but &quot;a = 30&quot; gets evaluated in the topmost scope*/
    print(object.a) /* should be 30, fails in Safari with an output of 20 */
    print(a); /* should be 30, fails in Safari with an output of 20 */
}
print(a); /* should be 10, fails in Safari with an output of 30 */
}}}

Expected Results:
    10
    20
    30
    30
    10

Actual Results:
    10
    20
    20
    20
    30

NOTES:

This bug is closely related to an _invalid_ bug I filed against Firefox&apos;s Javascript implementation wherein Brendan Eich settled several of my misconceptions about the &quot;var&quot; statement: https://bugzilla.mozilla.org/show_bug.cgi?id=383558</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>7057</commentid>
    <comment_count>1</comment_count>
      <attachid>15130</attachid>
    <who name="Mark Rowe (bdash)">mrowe</who>
    <bug_when>2007-06-19 17:27:27 -0700</bug_when>
    <thetext>Created attachment 15130
Test case

Test case.  It passes in Camino 1.5, but not in Safari 3 beta.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>4917</commentid>
    <comment_count>2</comment_count>
    <who name="Cameron Zwarich (cpst)">zwarich</who>
    <bug_when>2007-07-12 19:08:56 -0700</bug_when>
    <thetext>This is caused by the same issue as bug 13517 and is fixed by the proposed patch there. See bug 13517, comment #5 for an explanation.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>4768</commentid>
    <comment_count>3</comment_count>
    <who name="Mark Rowe (bdash)">mrowe</who>
    <bug_when>2007-07-14 10:10:12 -0700</bug_when>
    <thetext>Bug 13517 was fixed in r24287, which means that this is also fixed.</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="0"
              isprivate="0"
          >
            <attachid>15130</attachid>
            <date>2007-06-19 17:27:27 -0700</date>
            <delta_ts>2007-06-19 17:27:27 -0700</delta_ts>
            <desc>Test case</desc>
            <filename>bug-14237.htm</filename>
            <type>text/html</type>
            <size>1028</size>
            <attacher name="Mark Rowe (bdash)">mrowe</attacher>
            
              <data encoding="base64">PGh0bWw+CjxoZWFkPgo8L2hlYWQ+Cjxib2R5PgogICAgPGRpdiBpZD0nY29uc29sZSc+PC9kaXY+
CjxzY3JpcHQgbGFuZ3VhZ2U9IkphdmFTY3JpcHQiPgpmdW5jdGlvbiBsb2codmFsdWUpIHsKICAg
IHZhciBjb25zb2xlID0gZG9jdW1lbnQuZ2V0RWxlbWVudEJ5SWQoJ2NvbnNvbGUnKTsKICAgIHZh
ciBwID0gZG9jdW1lbnQuY3JlYXRlRWxlbWVudCgncCcpOwogICAgcC5hcHBlbmRDaGlsZChkb2N1
bWVudC5jcmVhdGVUZXh0Tm9kZSh2YWx1ZSkpOwogICAgY29uc29sZS5hcHBlbmRDaGlsZChwKTsK
fQoKZnVuY3Rpb24gc2hvdWxkQmUoZXhwZWN0ZWQsIGFjdHVhbCkgewogICAgaWYgKGV4cGVjdGVk
ICE9IGFjdHVhbCkKICAgICAgICBsb2coJ0V4cGVjdGVkICcgKyBleHBlY3RlZCArICcsIGdvdCAn
ICsgYWN0dWFsICsgJy4nKTsKICAgIGVsc2UKICAgICAgICBsb2coJ0V4cGVjdGVkIGFuZCBnb3Qg
JyArIGV4cGVjdGVkKTsKfQoKdmFyIGEgPSAxMDsKc2hvdWxkQmUoMTAsIGEpOyAvKiBzaG91bGQg
YmUgMTA7IG5vdCBhbiBpc3N1ZSAqLwp2YXIgb2JqZWN0ID0geydhJzogMjB9Owp3aXRoIChvYmpl
Y3QpIHsKICAgIHNob3VsZEJlKDIwLCBhKTsgLyogc2hvdWxkIGJlIDIwOyBzbyBmYXIgbm8gY29u
dHJvdmVyc3kgKi8KICAgIHZhciBhID0gMzA7IC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