v8 handlescope的机制

HandleScope

成员

HandleScope类重要的3个成员

internal::Isolate* isolate_;

intieral::Object** prev_next_;

internal::Object** prev_limit_;

HandleScope::CreateHandle
Object** HandleScope::CreateHandle(Isolate* isolate, Object* value) {
  DCHECK(AllowHandleAllocation::IsAllowed());
  HandleScopeData* data = isolate->handle_scope_data();

  Object** result = data->next;
  if (result == data->limit) result = Extend(isolate);
  // Update the current next field, set the value in the created
  // handle, and return the result.
  DCHECK(result < data->limit);
  data->next = result + 1;                                                                                                                                                                                                               

  *result = value;
  return result;
}

HandleScopeData对象,是一个struct

                                                                                                                                                                                                                                         
struct HandleScopeData final {                                                                                      
  Object** next;                                                                                                    
  Object** limit;                                                                                                   
  int level;                                                                                                        
  int sealed_level;                                                                                                 
  CanonicalHandleScope* canonical_scope;                                                                            
                                                                                                                    
  void Initialize() {                                                                                               
    next = limit = nullptr;                                                                                         
    sealed_level = level = 0;                                                                                       
    canonical_scope = nullptr;                                                                                      
  }                                                                                                                 
}; 

data的next保存者被管理的对象。

HandleScopeData对象是被internal::Isolate的handle_scope_data_对象获取的。

HandleScope是管理对象的引用,不是保存对象的内容。

结构图:
v8 handlescope的机制_第1张图片

Isolate管理了一个Object** 的block数组。每个Block组占用(4/8)KB的大小(一个或者两个页面),这是每次不够用时的扩展一个block。当前的block可能是最后一个block或者倒数第二block。这样做的目的是为了避免过多的block没有被释放。
Isolate的HandleScopeData对象,是指向当前Block组内的当前的下个free的slot,以及当前block的limit。这样,当next == limit的时候,就会请求一个新的block。每次请求都是取block数组的最后一个block。如果最后一个block就是当前的block,那么就会分配新的block,并加入到数组中。
HandleScope是为了记录上次HandleScopeData所在的next和limit指针。这里是模拟了堆栈。HandleScope相当于保存了上个函数的堆栈指针。
当HandleScope析构的时候,会调用HandleScope::CloseScope方法,将当前的slot都丢弃掉(这样相当于删除了本地引用,GC就会认为这些对象不再被引用了),也会删除那些没有任何有效数据的block。
HandleScope会形成一个链表,直到最上层的一个scope。

EscopeHandleScope

EscopeHandleScope继承自HandleScope。

它只能Escope一个value值。它的原理是这样的:

  • 在初始化HandleScope(它的父类)数据之前,先获取一个escape_slot_,这个solt是建立在它的上层的HandleScope上的。
  • 然后他建立一个新的HandleScope
  • 最后调用Escape的时候,将需要 escape的值填充到escape_slot_内,即把这个值放在上层HandleScope上。
    其余的Local 对象都被丢弃了。

EscopeHandleScope显然是一个类似语法糖的存在。目的是为了能够调用一个C++函数,将临时分配的对象统统扔掉,只保留关键的对象。

你可能感兴趣的:(v8)