<?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>7751</bug_id>
          
          <creation_ts>2006-03-13 05:10:01 -0800</creation_ts>
          <short_desc>Scope is broken with nested evals inside of functions.</short_desc>
          <delta_ts>2007-07-14 12:42:47 -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>420+</version>
          <rep_platform>Mac</rep_platform>
          <op_sys>OS X 10.4</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>INVALID</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="Francisco Tolmasky">tolmasky</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>gavin.sharp</cc>
    
    <cc>ian</cc>
    
    <cc>zwarich</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>36097</commentid>
    <comment_count>0</comment_count>
    <who name="Francisco Tolmasky">tolmasky</who>
    <bug_when>2006-03-13 05:10:01 -0800</bug_when>
    <thetext>When I nest evals, even though I do eval.apply(window, ...) the this object is kind of window and kind of not.  Doing alert(this==window) alerts true, however doing function a() { } will not put a into this, or window for that matter.  Check the attachment for a clearer example.  Note, this works fine in Firefox.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>36098</commentid>
    <comment_count>1</comment_count>
      <attachid>7046</attachid>
    <who name="Francisco Tolmasky">tolmasky</who>
    <bug_when>2006-03-13 05:13:48 -0800</bug_when>
    <thetext>Created attachment 7046
Breaks scope.

We should be seeing this alerted:

yup!
[Object Window]
function a() ...
function a() ...
function a() ...
true
Bluebird of happiness

(run it through firefox, it will gives this result).
However, we instead get this:

yup!
[Obejct Window]
undefined
function a()...
undefined
true
Bluebird of happiness

The strange thing is that &quot;this&quot; really thinks its window, but it doesn&apos;t behave like it since a() doesnt get registered into window.

If you comment out the r function, so that everything that happens is global, like this:

&lt;script&gt;

alert(&quot;yup!&quot;);

//function r()
//{
    var predefined_js= &quot;alert(this);function a(){alert(&apos;bluebird of happiness&apos;);}alert(this.a);alert(a);alert(window.a);alert(this===window);&quot;;
    var main_js= &quot;eval.apply(window, [predefined_js]); main= function() { a(); return 0; }&quot;;

    eval.apply(window, [main_js]);
//}

//r();

main();

&lt;/script&gt;

Now it works as it should.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>36762</commentid>
    <comment_count>2</comment_count>
      <attachid>7136</attachid>
    <who name="Francisco Tolmasky">tolmasky</who>
    <bug_when>2006-03-17 11:06:52 -0800</bug_when>
    <thetext>Created attachment 7136
Reduced Test Case

The function internals should be outputted.
I believe what&apos;s happening is that if the scope is not 100% &quot;shallow&quot; it refuses to allow declarations of type function [name] { } to be added to the global object.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>36892</commentid>
    <comment_count>3</comment_count>
      <attachid>7168</attachid>
    <who name="Francisco Tolmasky">tolmasky</who>
    <bug_when>2006-03-18 20:58:24 -0800</bug_when>
    <thetext>Created attachment 7168
Fixed Reduced Test Case</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>5026</commentid>
    <comment_count>4</comment_count>
      <attachid>15472</attachid>
    <who name="Cameron Zwarich (cpst)">zwarich</who>
    <bug_when>2007-07-11 03:03:30 -0700</bug_when>
    <thetext>Created attachment 15472
Further reduced test case

(In reply to comment #2)

&gt; I believe what&apos;s happening is that if the scope is not 100% &quot;shallow&quot; it
&gt; refuses to allow declarations of type function [name] { } to be added to the
&gt; global object.

It also happens for other declarations, and for single evals, not just nested evals. I simplified your original example a bit to get the attached test case. In Firefox and Opera it outputs &quot;bluebird of happiness&quot; three times, but in WebKit it outputs &quot;undefined&quot; instead of the first.

However, I don&apos;t think this is actually a bug. I think Safari has the correct behaviour here, judging by the ECMA spec. I will explain my argument.

In the spec (chapter 10, although most of what I am saying comes from section 10.2), there are a few important pieces of context-sensitive information. The two that affect the situation in this bug report are the variable instantiation context and the value of &quot;this&quot;.

According to 10.2.3, when you call a function without specifying the &quot;this&quot; value, it is inherited from the caller. Thus, in my example, the value of &quot;this&quot; in the function body of f() the only time it is called is the global object. The variable instantiation context is, of course, local to the function and is not the same as the global variable instantiation context.

According to 10.2.2, for code run by using eval, the variable instantiation context and the &quot;this&quot; value are inherited from the caller. If you take my attached example and change &quot;eval.call(this, &quot; to simply &quot;eval(&quot;, all three major Mac browsers agree (I couldn&apos;t test IE) that it should output &quot;undefined&quot; and then &quot;bluebird of happiness&quot; two times.

So, what is different when you use eval.call (or eval.apply) that causes the other browsers to behave differently? First, note that 10.2.2 seems to imply that evaluated code always has a &quot;this&quot; object of its calling context, although that isn&apos;t the issue here, as all of the browsers agree on what the &quot;this&quot; object should be. However, they disagree on what the variable instantiation context should be. Firefox and Opera think that by passing &quot;this&quot; as the first argument of call, the variable instantiation context of the evaluated code changes to the top level. But 10.2.2 says that the variable instantiation context is always that of the caller and is not dependent on the value of &quot;this&quot;. And according to 15.3.4.4, passing something as the first argument of call could only possibly change the value of &quot;this&quot;. Therefore, WebKit&apos;s behaviour seems to be correct.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>4983</commentid>
    <comment_count>5</comment_count>
    <who name="David Kilzer (:ddkilzer)">ddkilzer</who>
    <bug_when>2007-07-11 21:33:57 -0700</bug_when>
    <thetext>See also: http://bugs.webkit.org/show_bug.cgi?id=12912#c6</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>4973</commentid>
    <comment_count>6</comment_count>
    <who name="Cameron Zwarich (cpst)">zwarich</who>
    <bug_when>2007-07-12 00:17:18 -0700</bug_when>
    <thetext>I went to file this as a bug in the Mozilla Core Bugzilla. But first I figured I&apos;d have the etiquette to test it on a newer build, so I downloaded Firefox 3.0a7pre. Surprisingly, it now has the same behaviour as Webkit. I searched the Bugzilla for a bug report and I couldn&apos;t find one. If I am really bored, I will hunt down the change in Firefox, but I am not that familiar with their codebase.

The Firefox 3.0 alpha also affects the behaviour mentioned in bug 11399, making it the same as Webkit. I will make a separate comment there.

Perhaps someone should review this bug and mark it INVALID?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>4777</commentid>
    <comment_count>7</comment_count>
    <who name="David Kilzer (:ddkilzer)">ddkilzer</who>
    <bug_when>2007-07-14 12:42:47 -0700</bug_when>
    <thetext>Marking as RESOLVED/INVALID per Comment #4 and Comment #6.

</thetext>
  </long_desc>
      
          <attachment
              isobsolete="1"
              ispatch="0"
              isprivate="0"
          >
            <attachid>7046</attachid>
            <date>2006-03-13 05:13:48 -0800</date>
            <delta_ts>2007-07-10 22:25:03 -0700</delta_ts>
            <desc>Breaks scope.</desc>
            <filename>scopetest.html</filename>
            <type>text/html</type>
            <size>341</size>
            <attacher name="Francisco Tolmasky">tolmasky</attacher>
            
              <data encoding="base64">PHNjcmlwdD4KCmFsZXJ0KCJ5dXAhIik7CgpmdW5jdGlvbiByKCkKewogICAgdmFyIHByZWRlZmlu
ZWRfanM9ICJhbGVydCh0aGlzKTtmdW5jdGlvbiBhKCl7YWxlcnQoJ2JsdWViaXJkIG9mIGhhcHBp
bmVzcycpO31hbGVydCh0aGlzLmEpO2FsZXJ0KGEpO2FsZXJ0KHdpbmRvdy5hKTthbGVydCh0aGlz
PT09d2luZG93KTsiOwogICAgdmFyIG1haW5fanM9ICJldmFsLmFwcGx5KHdpbmRvdywgW3ByZWRl
ZmluZWRfanNdKTsgbWFpbj0gZnVuY3Rpb24oKSB7IGEoKTsgcmV0dXJuIDA7IH0iOwoKICAgIGV2
YWwuYXBwbHkod2luZG93LCBbbWFpbl9qc10pOwp9CgpyKCk7CgptYWluKCk7Cgo8L3NjcmlwdD4=
</data>

          </attachment>
          <attachment
              isobsolete="1"
              ispatch="0"
              isprivate="0"
          >
            <attachid>7136</attachid>
            <date>2006-03-17 11:06:52 -0800</date>
            <delta_ts>2007-07-10 22:25:20 -0700</delta_ts>
            <desc>Reduced Test Case</desc>
            <filename>tester.html</filename>
            <type>text/html</type>
            <size>117</size>
            <attacher name="Francisco Tolmasky">tolmasky</attacher>
            
              <data encoding="base64">PHNjcmlwdD4KYT0ge307CgpmdW5jdGlvbiByKCkKewogICAgZXZhbC5hcHBseSh3aW5kb3csIFsi
ZnVuY3Rpb24geCgpIHsgYWxlcnQoXCJ5dXBcIik7IH0iXSk7Cn0KCmFsZXJ0KHgpOwoKPC9zY3Jp
cHQ+
</data>

          </attachment>
          <attachment
              isobsolete="0"
              ispatch="0"
              isprivate="0"
          >
            <attachid>7168</attachid>
            <date>2006-03-18 20:58:24 -0800</date>
            <delta_ts>2006-03-18 20:58:24 -0800</delta_ts>
            <desc>Fixed Reduced Test Case</desc>
            <filename>tester.html</filename>
            <type>text/html</type>
            <size>115</size>
            <attacher name="Francisco Tolmasky">tolmasky</attacher>
            
              <data encoding="base64">PHNjcmlwdD4KZnVuY3Rpb24gcigpCnsKICAgIGV2YWwuYXBwbHkod2luZG93LCBbImZ1bmN0aW9u
IHgoKSB7IGFsZXJ0KFwieXVwXCIpOyB9Il0pOwp9CgpyKCk7CgphbGVydCh4KTsKCjwvc2NyaXB0
Pg==
</data>

          </attachment>
          <attachment
              isobsolete="0"
              ispatch="0"
              isprivate="0"
          >
            <attachid>15472</attachid>
            <date>2007-07-11 03:03:30 -0700</date>
            <delta_ts>2007-07-11 03:03:30 -0700</delta_ts>
            <desc>Further reduced test case</desc>
            <filename>scopetest3.html</filename>
            <type>text/html</type>
            <size>138</size>
            <attacher name="Cameron Zwarich (cpst)">zwarich</attacher>
            
              <data encoding="base64">PHNjcmlwdD4KZnVuY3Rpb24gZigpCnsKICAgIGV2YWwuY2FsbCh0aGlzLCAidmFyIGEgPSAnYmx1
ZWJpcmQgb2YgaGFwcGluZXNzJzsgYWxlcnQodGhpcy5hKTsgYWxlcnQoYSk7Iik7CgogICAgYWxl
cnQoYSk7Cn0KCmYoKTsKPC9zY3JpcHQ+
</data>

          </attachment>
      

    </bug>

</bugzilla>