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