<?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>49703</bug_id>
          
          <creation_ts>2010-11-17 18:03:27 -0800</creation_ts>
          <short_desc>HTMLScriptRunner and ScriptElement duplicate code</short_desc>
          <delta_ts>2011-03-01 00:12:09 -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>DOM</component>
          <version>528+ (Nightly build)</version>
          <rep_platform>PC</rep_platform>
          <op_sys>OS X 10.5</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>
          <dependson>49701</dependson>
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Ryosuke Niwa">rniwa</reporter>
          <assigned_to name="Ryosuke Niwa">rniwa</assigned_to>
          <cc>abarth</cc>
    
    <cc>ap</cc>
    
    <cc>darin</cc>
    
    <cc>eric</cc>
    
    <cc>tony</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>310652</commentid>
    <comment_count>0</comment_count>
    <who name="Ryosuke Niwa">rniwa</who>
    <bug_when>2010-11-17 18:03:27 -0800</bug_when>
    <thetext>HTMLScriptRunner and ScriptElement implement very similar features.  We should try to share code between the two as much as possible.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>313431</commentid>
    <comment_count>1</comment_count>
      <attachid>74787</attachid>
    <who name="Ryosuke Niwa">rniwa</who>
    <bug_when>2010-11-24 13:13:07 -0800</bug_when>
    <thetext>Created attachment 74787
work in progress</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>314158</commentid>
    <comment_count>2</comment_count>
      <attachid>74787</attachid>
    <who name="Darin Adler">darin</who>
    <bug_when>2010-11-27 18:17:41 -0800</bug_when>
    <thetext>Comment on attachment 74787
work in progress

View in context: https://bugs.webkit.org/attachment.cgi?id=74787&amp;action=review

&gt; WebCore/dom/ScriptElement.h:48
&gt; +    void evaluateScriptWithEvents(CachedScript*);
&gt; +    void executeScriptWithEvents(const ScriptSourceCode&amp;, bool errorOccurred);

I think these both can be called executeScript, no need to call one evaluate and one execute. C++ overloading will work fine.

The use of boolean for “errorOccurred” is strange, though. A boolean that when true means, “no don’t execute the script at all, just send an error event”, is not good design. Two separate functions would be better even though it’s slightly more code at the call site.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>314165</commentid>
    <comment_count>3</comment_count>
    <who name="Ryosuke Niwa">rniwa</who>
    <bug_when>2010-11-27 20:19:57 -0800</bug_when>
    <thetext>Thanks for the feedback, Darin!

(In reply to comment #2)
&gt; &gt; WebCore/dom/ScriptElement.h:48
&gt; &gt; +    void evaluateScriptWithEvents(CachedScript*);
&gt; &gt; +    void executeScriptWithEvents(const ScriptSourceCode&amp;, bool errorOccurred);
&gt; 
&gt; I think these both can be called executeScript, no need to call one evaluate and one execute. C++ overloading will work fine.

It turned out that ScriptController&apos;s evaluate and execute functions differ slightly.  They have slightly different rules on how to choose DOMWrapperWorld, and while execute may trigger layout, evaluate never does.  I don&apos;t like this ever-slight difference between the two and the fact names &quot;evaluate&quot; and &quot;execute&quot; do not convey any of those differences.  But we should probably resolve that in a separate patch.

&gt; The use of boolean for “errorOccurred” is strange, though. A boolean that when true means, “no don’t execute the script at all, just send an error event”, is not good design. Two separate functions would be better even though it’s slightly more code at the call site.

This is to do with what sourceFromPendingScript does and releaseElementAndClear:
http://trac.webkit.org/browser/trunk/WebCore/html/parser/HTMLScriptRunner.cpp
128	    bool errorOccurred = false;
129	    ScriptSourceCode sourceCode = sourceFromPendingScript(pendingScript, errorOccurred);
130	
131	    // Stop watching loads before executeScript to prevent recursion if the script reloads itself.
132	    if (pendingScript.cachedScript() &amp;&amp; pendingScript.watchingForLoad())
133	        stopWatchingForLoad(pendingScript);
134	
135	    // Clear the pending script before possible rentrancy from executeScript()
136	    RefPtr&lt;Element&gt; element = pendingScript.releaseElementAndClear();
137	    if (ScriptElement* scriptElement = toScriptElement(element.get())) {
138	        NestingLevelIncrementer nestingLevelIncrementer(m_scriptNestingLevel);
139	        IgnoreDestructiveWriteCountIncrementer ignoreDestructiveWriteCountIncrementer(m_document);
140	        if (errorOccurred)
141	            element-&gt;dispatchEvent(createScriptErrorEvent());
142	        else {
143	            ASSERT(isExecutingScript());
144	            scriptElement-&gt;executeScript(sourceCode);
145	            element-&gt;dispatchEvent(createScriptLoadEvent());
146	        }
147	    }
148	    ASSERT(!m_scriptNestingLevel);

We need to call releaseElementAndClear in order to avoid running the same script twice but it&apos;ll clear the script cache (removes itself from the resource loader) so we need to obtain the SourceCode before calling this function.  But then we can&apos;t pass CachedScript to executeScriptWithEvents as in evaluateScriptWithEvents.

I need to spend some time thinking about how to clean up all of these dependencies and legacy structures.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>359698</commentid>
    <comment_count>4</comment_count>
    <who name="Ryosuke Niwa">rniwa</who>
    <bug_when>2011-03-01 00:12:09 -0800</bug_when>
    <thetext>http://trac.webkit.org/changeset/79114 fixed this.</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="1"
              isprivate="0"
          >
            <attachid>74787</attachid>
            <date>2010-11-24 13:13:07 -0800</date>
            <delta_ts>2010-11-27 18:17:41 -0800</delta_ts>
            <desc>work in progress</desc>
            <filename>wip49703</filename>
            <type>text/plain</type>
            <size>3007</size>
            <attacher name="Ryosuke Niwa">rniwa</attacher>
            
              <data encoding="base64">SW5kZXg6IFdlYkNvcmUvZG9tL1NjcmlwdEVsZW1lbnQuY3BwCj09PT09PT09PT09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