HTMLDocumentParser 中的HTMLTokenizer在解析到<script>開始標(biāo)簽時(shí),創(chuàng)建HTMLScriptElement對(duì)象,在解析</script>時(shí),開始解析JS代碼啊,如果<script src=””>中含有src,那么發(fā)出一個(gè)異步請(qǐng)求(在異步請(qǐng)求過程中,parser會(huì)中斷,等待JS的解析結(jié)果,期間Webkit會(huì)來做一些 DNS預(yù)取,資源預(yù)解析等工作,總之WebKit是不會(huì)浪費(fèi)這些寶貴時(shí)間的),等src資源返回后開始解析。
比如鼠標(biāo)點(diǎn)擊一個(gè)按鈕
onload等函數(shù)
WebKit中所有的JS執(zhí)行都是通過Frame中的ScriptController m_script;來完成,也就是說,所有的每個(gè)Frame只有一個(gè)JS入口。
ScriptController中含有一個(gè)V8Proxy的V8代理,所有執(zhí)行過程都是通過V8Proxy來完成的。
如何把DOM接口的相關(guān)接口暴露給V8的?
要把DOM傳遞給V8調(diào)用就要解決兩個(gè)問題,DOM對(duì)象怎么傳遞給V8?
DOM對(duì)象的中的方法怎么傳遞給V8?
還有諸如其他的Screen等對(duì)象是如何傳遞給V8的?
一個(gè)Frame中的V8運(yùn)行環(huán)境,也就是上面文章所提到的Context,
W3C—WEB IDL
由該工作草案定義的接口定義語言(interface definition language)叫作Web IDL,它可被用來描述要在Web瀏覽器里實(shí)現(xiàn)的接口。Web IDL是一種IDL的變體,它所具有的很多特性使之能夠更容易地對(duì)Web平臺(tái)里的常用腳本對(duì)象的行為進(jìn)行規(guī)定。為了支持過去只能以文字描述的常用功能,該 IDL在很多方面得到了擴(kuò)展。另外,它還為ECMAScript第3版和Java給出了精確的語言綁定。
回歸WebKit核心代碼WebCore中的源代碼,可以發(fā)現(xiàn)有許多xxx.idl文件,這些文件就是需要暴露給JS解析引擎的接口。
以WebKitWebCorepageDOMWindow.h 為列,改文件主要在C++層次描述了一個(gè)Web頁面的所有特性,包括 DOM,screen,History等等相關(guān)信息。這些信息都是通過WebKitWebCorepageDOMwindows.idl描述文件來描述的,DOMwindows.idl主要分為兩個(gè)部分,屬性和方法,DOMwindows.idl的代碼太多了,就不在這里列舉IDL文件內(nèi)容了。其實(shí) xxx.idl文件在C++運(yùn)行時(shí)并不起任何作用,真正起作用的是編譯器利用 xxx.idl文件來生成的對(duì)應(yīng)的C++源文件,由于通過xxx.idl接口描述的屬性和功能規(guī)則比較簡單,所以通過編譯器通過perl語言,然后按照一定規(guī)則來生成對(duì)應(yīng)的供JS引擎調(diào)用的C++源文件。
Chromium生成的V8接口對(duì)應(yīng)的目錄在srcchromeDebugobjglobal_intermediatewebkitbindings或者
srcchromeReleaseobjglobal_intermediatewebkitbindings.
通過分析這些動(dòng)態(tài)生成的文件可以發(fā)現(xiàn),他們的組成結(jié)構(gòu)非常簡單,
namespace WebCore {
class V8DOMWindow {
public:
static bool HasInstance(v8::Handle value);
static v8::Persistent GetRawTemplate();
static v8::Persistent GetTemplate();
static DOMWindow* toNative(v8::Handle);
static v8::Handle wrap(DOMWindow*);
static void derefObject(void*);
static WrapperTypeInfo info;
…忽略了下面的部分代碼
};
v8::Handle toV8(DOMWindow*);
v8::Handle toV8(PassRefPtr);
}
然后CPP文件中是這樣實(shí)現(xiàn)的
static const BatchedAttribute shadowAttrs[] = {
// Attribute ‘location’ (Type: ‘a(chǎn)ttribute’ ExtAttr: ‘V8DisallowShadowing DoNotCheckDomainSecurity CPPCustom V8CustomSetter JSCCustom’)
{“l(fā)ocation”, DOMWindowInternal::locationAttrGetter, V8DOMWindow::locationAccessorSetter, 0 , static_cast(v8::ALL_CAN_READ | v8::ALL_CAN_WRITE | v8::PROHIBITS_OVERWRITING), static_cast(v8::None | v8::DontDelete), 0 },
// Attribute ‘window’ (Type: ‘readonly attribute’ ExtAttr: ‘V8DisallowShadowing DoNotCheckDomainSecurity’)
{“window”, DOMWindowInternal::windowAttrGetter, 0, 0 , static_cast(v8::ALL_CAN_READ | v8::PROHIBITS_OVERWRITING), static_cast(v8::None | v8::DontDelete), 0 },
// Attribute ‘top’ (Type: ‘a(chǎn)ttribute’ ExtAttr: ‘V8DisallowShadowing DoNotCheckDomainSecurityOnGet Replaceable V8ReadOnly’)
{“top”, DOMWindowInternal::topAttrGetter, 0, 0 , static_cast(v8::ALL_CAN_READ | v8::PROHIBITS_OVERWRITING), static_cast(v8::None | v8::DontDelete), 0 },
};
static const BatchedAttribute DOMWindowAttrs[] = {
// Attribute ‘screen’ (Type: ‘readonly attribute’ ExtAttr: ”)
{“screen”, DOMWindowInternal::screenAttrGetter, 0, 0 , static_cast(v8::DEFAULT), static_cast(v8::None), 0 },
// Attribute ‘history’ (Type: ‘readonly attribute’ ExtAttr: ‘JSCCustomGetter DoNotCheckDomainSecurity’)
{“history”, DOMWindowInternal::historyAttrGetter, 0, 0 , static_cast(v8::ALL_CAN_READ), static_cast(v8::None), 0 }
……下面的代碼忽略,
}
static const BatchedCallback DOMWindowCallbacks[] = {
{“getSelection”, DOMWindowInternal::getSelectionCallback},
{“print”, DOMWindowInternal::printCallback},
{“stop”, DOMWindowInternal::stopCallback},
{“open”, V8DOMWindow::openCallback},
{“showModalDialog”, V8DOMWindow::showModalDialogCallback},
{“alert”, DOMWindowInternal::alertCallback},
{“confirm”, DOMWindowInternal::confirmCallback},
{“prompt”, DOMWindowInternal::promptCallback},
{“find”, DOMWindowInternal::findCallback},
{“scrollBy”, DOMWindowInternal::scrollByCallback},
{“scrollTo”, DOMWindowInternal::scrollToCallback},
{“scroll”, DOMWindowInternal::scrollCallback},
{“moveBy”, DOMWindowInternal::moveByCallback},
{“moveTo”, DOMWindowInternal::moveToCallback},
{“resizeBy”, DOMWindowInternal::resizeByCallback},
{“resizeTo”, DOMWindowInternal::resizeToCallback},
{“getMatchedCSSRules”, DOMWindowInternal::getMatchedCSSRulesCallback},
{“setTimeout”, V8DOMWindow::setTimeoutCallback},
{“clearTimeout”, DOMWindowInternal::clearTimeoutCallback},
{“setInterval”, V8DOMWindow::setIntervalCallback},
{“clearInterval”, DOMWindowInternal::clearIntervalCallback},
{“atob”, DOMWindowInternal::atobCallback},
{“btoa”, DOMWindowInternal::btoaCallback},
{“addEventListener”, V8DOMWindow::addEventListenerCallback},
{“removeEventListener”, V8DOMWindow::removeEventListenerCallback},
{“captureEvents”, V8DOMWindow::captureEventsCallback},
{“releaseEvents”, V8DOMWindow::releaseEventsCallback},
};
注意到這兩部分分別定義了JS所能調(diào)用的 屬性和所使用的方法。
然后再把這些屬性和方法注冊(cè)到V8執(zhí)行時(shí)所需要的Context中,其流程大致是這樣的。
ScriptController::executeScript
ScriptController::evaluate
v8::Handle v8Context = V8Proxy::mainWorldContext(m_proxy->frame());
proxy->mainWorldContext();
V8DOMWindowShell::initContextIfNeeded
V8DOMWindowShell::createNewContext
V8DOMWindow::GetShadowObjectTemplate
static v8::Persistent ConfigureShadowObjectTemplate
void batchConfigureAttributes
void configureAttribute
(attribute.onProto ? proto : instance)->SetAccessor(v8::String::New(attribute.name),
attribute.getter,
attribute.setter,
v8::External::Wrap(attribute.data),
attribute.settings,
attribute.attribute);
注冊(cè)函數(shù)的執(zhí)行流程和上面這個(gè)流程類似。
這樣V8就可以在指定的Context中調(diào)用所有WEB IDL中所規(guī)則的屬性和方法了。
聯(lián)系客服