Webkit对Javascipt的执行优先级

长平狐 发布于 2013/04/25 10:57
阅读 65
收藏 0

原创!转载请注明出自http://blog.csdn.net/lichwei1983


生成DOM Tree的过程中,HTMLTreeBuilder会经常检查看看自己是否已经暂停,以便执行脚本,比如:

HTMLTreeBuilder处理token的时候,如果发现有<script>标签--token.name() == scriptTag,就会停下解析token的流程,转而运行这个脚本--通过runScriptsForPausedTreeBuilder


HTMLTreeBuilder.cpp:

HTMLTreeBuilder::processEndTag(AtomicHTMLToken& token){

   case TextMode:

       if (token.name() == scriptTag) {

           // Pause ourselves so that parsing stops until the script can be processed by the caller.

           m_isPaused = true;

   .......

}


bool HTMLDocumentParser::canTakeNextToken(SynchronousMode mode, PumpSession& session)

{

      if(m_treeBuilder->isPaused()) {

       // If we're paused waiting for a script, we try to execute scripts before continuing.

       bool shouldContinueParsing = runScriptsForPausedTreeBuilder();

       m_treeBuilder->setPaused(!shouldContinueParsing);

       if (!shouldContinueParsing || isStopped())

           return false;

     }

 .........

   }

。。。

}

又比如加载cachedResource时候也需要检查:是否导致HTMLTreeBuilder process token的流程中断。

bool shouldContinueParsing = m_scriptRunner->executeScriptsWaitingForLoad(cachedResource);

m_treeBuilder->setPaused(!shouldContinueParsing);


或者StyeSheet的加载需要中断:

void HTMLDocumentParser::executeScriptsWaitingForStylesheets()

{

   m_treeBuilder->setPaused(false);

   bool shouldContinueParsing = m_scriptRunner->executeScriptsWaitingForStylesheets();

   m_treeBuilder->setPaused(!shouldContinueParsing);

}



原文链接:http://blog.csdn.net/lichwei1983/article/details/8806353
加载中
返回顶部
顶部