//https://github.com/v8/v8/blob/master/src/objects/js-objects.h
// 我们先来看JS中object的定义,继承自JSReceiver
// Line 278
class JSObject : public JSReceiver {
//省略...
}
// Line 26
// 接下来再看,JSReceiver继承自HeapObject,并且有几个重要属性
// JSReceiver includes types on which properties can be defined, i.e.,
// JSObject and JSProxy.
class JSReceiver : public HeapObject {
public:
NEVER_READ_ONLY_SPACE
// Returns true if there is no slow (ie, dictionary) backing store.
// 是否有快速属性模式
inline bool HasFastProperties() const;
// Returns the properties array backing store if it exists.
// Otherwise, returns an empty_property_array when there's a Smi (hash code) or an empty_fixed_array for a fast properties map.
// 属性数组
inline PropertyArray property_array() const;
// Gets slow properties for non-global objects.
// 字典属性
inline NameDictionary property_dictionary() const;
// Sets the properties backing store and makes sure any existing hash is moved
// to the new properties store. To clear out the properties store, pass in the
// empty_fixed_array(), the hash will be maintained in this case as well.
void SetProperties(HeapObject properties);
// There are five possible values for the properties offset.
// 1) EmptyFixedArray/EmptyPropertyDictionary - This is the standard
// placeholder.
//
// 2) Smi - This is the hash code of the object.
//
// 3) PropertyArray - This is similar to a FixedArray but stores
// the hash code of the object in its length field. This is a fast
// backing store.
//
// 4) NameDictionary - This is the dictionary-mode backing store.
//
// 4) GlobalDictionary - This is the backing store for the
// GlobalObject.
// 初始化属性
inline void initialize_properties();
由上可知对象有快速属性和字典属性两种模式,快速属性由数组存储,字典属性采用hash表存储。
快速属性这里不深入,我们接下来看看NameDictionary的底层结构
// https://github.com/v8/v8/blob/master/src/objects/dictionary.h
// 我们先来看看继承链
// Line 202
class V8_EXPORT_PRIVATE NameDictionary : public BaseNameDictionary{}
// Line 128
class EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE) BaseNameDictionary : public Dictionary {}
// Line 26
class EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE) Dictionary : public HashTable {}
// 由上可知继承自HashTable,我们来看HashTable的定义
// https://github.com/v8/v8/blob/master/src/objects/hash-table.h
// 并且在文件开头的注释已经很详细了
// HashTable is a subclass of FixedArray that implements a hash table that uses open addressing and quadratic probing.
*重要: hash表使用数组为基础数据,并在其上实现了开放寻址和二次探测
// In order for the quadratic probing to work, elements that have not yet been used and elements that have been deleted are distinguished.
// Probing continues when deleted elements are encountered and stops when unused elements are encountered.
* 为了使二次探测工作正常,未使用/被删除的元素将被标记删除而不是直接删除
// - Elements with key == undefined have not been used yet.
// - Elements with key == the_hole have been deleted.
// 以下会被使用的hash表派生类
// Line 292
template
class EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE) ObjectHashTableBase
: public HashTable {}
接下来我们看看V8在实现hash表时的几个重要行为和参数
// https://github.com/v8/v8/blob/master/src/objects/objects.cc
1. 扩容
// Line 7590
// 下面源代码是新增一个元素后的逻辑
ObjectHashTableBase::Put(Isolate* isolate, Handle table, Handle
Web高级 JavaScript中的数据结构
标签:javascrip hub 初始化 case flags 执行环境 tcap fill https