V8將指針抽象為Handle模板類對象,Handle模板類對指針進(jìn)行了簡單的封裝,
Handle分為兩類:local和persistent ,Local handles 通常是本地的,短暫的,由HandleScopes類進(jìn)行管理,不需要顯示的釋放。
persistent用于跨多個(gè)獨(dú)立的存儲(chǔ)對象操作,需要顯示的釋放。
HandleScopes類管理local handle。聲明一個(gè)HandleScopes,則后續(xù)所有新建的local handle都由此HandleScopes管理,當(dāng)HandleScopes生命消亡
的時(shí)候,由HandleScopes管理得local handle也就消亡了。
可以看到Local的new的實(shí)現(xiàn),就是通過HandleScope::CreateHandle創(chuàng)建的
template <class T>
Local<T> Local<T>::New(Handle<T> that) {
if (that.IsEmpty()) return Local<T>();
internal::Object** p = reinterpret_cast<internal::Object**>(*that);
return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
}
在已有一個(gè)HandleScopes1的時(shí)候再創(chuàng)建一個(gè)HandleScopes2,則新建的HandleScopes2管理后續(xù)新建的local handle,當(dāng)HandleScopes2
生命周期結(jié)束時(shí),HandleScopes1則管理
HandleScope::HandleScope() : is_closed_(false) {
API_ENTRY_CHECK("HandleScope::HandleScope");
i::HandleScope::Enter(&previous_); //保存前一個(gè)HandleScope
}
HandleScope::~HandleScope() {
if (!is_closed_) {
i::HandleScope::Leave(&previous_); //恢復(fù)前一個(gè)HandleScope為當(dāng)前HandleScope
}
}
posted on 2015-12-13 23:24
merlinfang 閱讀(627)
評論(0) 編輯 收藏 引用 所屬分類:
v8