<?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>141184</bug_id>
          
          <creation_ts>2015-02-02 19:05:33 -0800</creation_ts>
          <short_desc>REGRESSION: Function declaration inside a function and using the same name as an argument to the containing function no longer takes precedence over the argument</short_desc>
          <delta_ts>2015-02-09 09:52:50 -0800</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>528+ (Nightly build)</version>
          <rep_platform>Unspecified</rep_platform>
          <op_sys>OS X 10.10</op_sys>
          <bug_status>NEW</bug_status>
          <resolution></resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords>InRadar</keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Rob Brackett">rob</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>fpizlo</cc>
    
    <cc>ggaren</cc>
    
    <cc>webkit-bug-importer</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1066320</commentid>
    <comment_count>0</comment_count>
    <who name="Rob Brackett">rob</who>
    <bug_when>2015-02-02 19:05:33 -0800</bug_when>
    <thetext>Using the latest WebKit Nightly (r179474), I ran into a problem using code like the following:

```
(function(Thing) {
	Thing.whatever = &quot;This breaks.&quot;;
	function Thing() {
		return this;
	}
})()
```

An exception is thrown on line 2 above, when trying to assign to `Thing.whatever`. The exception is “TypeError: undefined is not an object (evaluating &apos;Thing.whatever = &quot;This breaks.&quot;&apos;)“ It seems that, on line 2, `Thing` evaluates to the first argument of the containing function (which is undefined) instead of the function defined in line 3. This differs from the currently shipping version of Safari, where `Thing` on line 2 evaluates to the function declaration instead of to `undefined`.

A quick perusal of section “10.5 Declaration Binding Instantiation” in the ECMA-262 spec _seems_ like the new behavior is correct, though I’m honestly not sure I’m reading it right. However, this behavior differs from previous builds of JSC and from many other JS engines (V8, Spidermonkey). It also breaks code existing in the wild — I encountered this while browsing a site built using Instapaper’s web app (http://instapaper.com), which I believe is using Browserify, which creates code like the above example.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1066324</commentid>
    <comment_count>1</comment_count>
    <who name="Rob Brackett">rob</who>
    <bug_when>2015-02-02 19:20:53 -0800</bug_when>
    <thetext>Well, in the process of writing a test for this, it seems like this only fails when the inspector is open, so this might not actually be JSC, but instead something about the web inspector.

Still not sure why Instapaper doesn’t work in WebKit Nightly; assuming it’s unrelated to this (and hard to determine since the inspector can’t be open!) :(</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1066391</commentid>
    <comment_count>2</comment_count>
    <who name="Rob Brackett">rob</who>
    <bug_when>2015-02-03 01:41:39 -0800</bug_when>
    <thetext>OK! This took a fair amount of poking before I finally narrowed it down. The cause of the problem I was seeing on Instapaper was actually code in the form:

```
(function(Thing) {
  Thing.whatever = &quot;This breaks.&quot;;
  function Thing() {
    // referencing `Thing` in this inner scope here is what does it
    Thing;
  }
})()
```

It seems like referencing `Thing` inside the `Thing` function inside a function with `Thing` as an argument (wow) causes `Thing` in the outer function to reference the argument instead of the function declaration.

As far as what I was seeing with the inspector open, I have no idea whether it was related or completely different. Whether or not the cause is the same, the symptoms are.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1066392</commentid>
    <comment_count>3</comment_count>
      <attachid>245929</attachid>
    <who name="Rob Brackett">rob</who>
    <bug_when>2015-02-03 01:48:00 -0800</bug_when>
    <thetext>Created attachment 245929
Simple test case for function declaration names vs. argument names</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1066501</commentid>
    <comment_count>4</comment_count>
    <who name="Radar WebKit Bug Importer">webkit-bug-importer</who>
    <bug_when>2015-02-03 11:05:37 -0800</bug_when>
    <thetext>&lt;rdar://problem/19702564&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1067838</commentid>
    <comment_count>5</comment_count>
    <who name="Rob Brackett">rob</who>
    <bug_when>2015-02-09 09:52:50 -0800</bug_when>
    <thetext>FWIW, I have encountered this issue on several more sites in regular browsing. It may be an odd construction, but it’s definitely a real-world problem.</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="0"
              isprivate="0"
          >
            <attachid>245929</attachid>
            <date>2015-02-03 01:48:00 -0800</date>
            <delta_ts>2015-02-03 01:48:00 -0800</delta_ts>
            <desc>Simple test case for function declaration names vs. argument names</desc>
            <filename>function-declaration-vs-argument-name-resolution.html</filename>
            <type>text/html</type>
            <size>1549</size>
            <attacher name="Rob Brackett">rob</attacher>
            
              <data encoding="base64">PCFET0NUWVBFIGh0bWw+CjxodG1sPgogIDxoZWFkPgogICAgPG1ldGEgY2hhcnNldD0idXRmLTgi
IC8+CiAgICA8dGl0bGU+RnVuY3Rpb24gZGVjbGFyYXRpb25zIHNob3VsZCB0YWtlIHByZWNlZGVu
Y2Ugb3ZlciBmb3JtYWwgYXJndW1lbnRzIGluIHRoZSBzYW1lIHNjb3BlLjwvdGl0bGU+CiAgICA8
c3R5bGUgdHlwZT0idGV4dC9jc3MiPgogICAgICAucGFzcyB7CiAgICAgICAgYmFja2dyb3VuZDog
Z3JlZW47CiAgICAgICAgY29sb3I6IHdoaXRlOwogICAgICB9CiAgICAgIC5mYWlsIHsKICAgICAg
ICBiYWNrZ3JvdW5kOiByZWQ7CiAgICAgICAgY29sb3I6IHdoaXRlOwogICAgICB9CiAgICA8L3N0
eWxlPgogIDwvaGVhZD4KICA8Ym9keT4KICAgCiAgICA8cD4KICAgICAgV2hlbiBhIGZ1bmN0aW9u
IGlzIDxlbT5kZWNsYXJlZ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WUpOwogICAgICAKICAgICAgdmFyIHBhc3NGYWlsID0gZG9jdW1lbnQu
Z2V0RWxlbWVudEJ5SWQoInBhc3MtZmFpbCIpOwogICAgICBpZiAoYXJndW1lbnRWYWx1ZS53aGF0
ZXZlciA9PT0gIlRoaXMgYnJlYWtzLiIpIHsKICAgICAgICBwYXNzRmFpbC5pbm5lckhUTUwgPSAi
RkFJTCI7CiAgICAgICAgcGFzc0ZhaWwuY2xhc3NOYW1lID0gImZhaWwiOwogICAgICB9CiAgICAg
IGVsc2UgewogICAgICAgIHBhc3NGYWlsLmlubmVySFRNTCA9ICJQQVNTIjsKICAgICAgICBwYXNz
RmFpbC5jbGFzc05hbWUgPSAicGFzcyI7CiAgICAgIH0KICAgIDwvc2NyaXB0PgogCiAgPC9ib2R5
Pgo8L2h0bWw+Cg==
</data>

          </attachment>
      

    </bug>

</bugzilla>