js引擎v8源碼怎么解析map對象

js引擎v8源碼怎么解析map對象,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

創(chuàng)新互聯(lián)專注于北侖企業(yè)網(wǎng)站建設,自適應網(wǎng)站建設,成都商城網(wǎng)站開發(fā)。北侖網(wǎng)站建設公司,為北侖等地區(qū)提供建站服務。全流程定制網(wǎng)站,專業(yè)設計,全程項目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務

首先介紹Map類。下面先看類定義

   
     
 
    
   

// All heap objects have a Map that describes their structure.
//  A Map contains information about:
//  - Size information about the object
//  - How to iterate over an object (for garbage collection)
class Map: public HeapObject {
public:
 // instance size.
 inline int instance_size();
 inline void set_instance_size(int value);

 // instance type.
 inline InstanceType instance_type();
 inline void set_instance_type(InstanceType value);

 // tells how many unused property fields are available in the instance.
 // (only used for JSObject in fast mode).
 inline int unused_property_fields();
 inline void set_unused_property_fields(int value);

 // bit field.
 inline byte bit_field();
 inline void set_bit_field(byte value);

 // Tells whether this object has a special lookup behavior.
 void set_special_lookup() {
   set_bit_field(bit_field() | (1 << kHasSpecialLookup));
 }

 bool has_special_lookup() {
   return ((1 << kHasSpecialLookup) & bit_field()) != 0;
 }

 // Tells whether the object in the prototype property will be used
 // for instances created from this function.  If the prototype
 // property is set to a value that is not a JSObject, the prototype
 // property will not be used to create instances of the function.
 // See ECMA-262, 13.2.2.
 inline void set_non_instance_prototype(bool value);
 inline bool has_non_instance_prototype();

 // Tells whether the instance with this map should be ignored by the
 // __proto__ accessor.
 inline void set_is_hidden_prototype() {
   set_bit_field(bit_field() | (1 << kIsHiddenPrototype));
 }

 inline bool is_hidden_prototype() {
   return ((1 << kIsHiddenPrototype) & bit_field()) != 0;
 }

 // Tells whether the instance has a named interceptor.
 inline void set_has_named_interceptor() {
   set_bit_field(bit_field() | (1 << kHasNamedInterceptor));
 }

 inline bool has_named_interceptor() {
   return ((1 << kHasNamedInterceptor) & bit_field()) != 0;
 }

 // Tells whether the instance has a named interceptor.
 inline void set_has_indexed_interceptor() {
   set_bit_field(bit_field() | (1 << kHasIndexedInterceptor));
 }

 inline bool has_indexed_interceptor() {
   return ((1 << kHasIndexedInterceptor) & bit_field()) != 0;
 }

 // Tells whether the instance is undetectable.
 // An undetectable object is a special class of JSObject: 'typeof' operator
 // returns undefined, ToBoolean returns false. Otherwise it behaves like
 // a normal JS object.  It is useful for implementing undetectable
 // document.all in Firefox & Safari.
 // See https://bugzilla.mozilla.org/show_bug.cgi?id=248549.
 inline void set_is_undetectable() {
   set_bit_field(bit_field() | (1 << kIsUndetectable));
 }

 inline bool is_undetectable() {
   return ((1 << kIsUndetectable) & bit_field()) != 0;
 }

 // Tells whether the instance has a call-as-function handler.
 inline void set_has_instance_call_handler() {
   set_bit_field(bit_field() | (1 << kHasInstanceCallHandler));
 }

 inline bool has_instance_call_handler() {
   return ((1 << kHasInstanceCallHandler) & bit_field()) != 0;
 }

 // Tells whether the instance needs security checks when accessing its
 // properties.
 inline void set_needs_access_check() {
   set_bit_field(bit_field() | (1 << kNeedsAccessCheck));
 }

 inline bool needs_access_check() {
   return ((1 << kNeedsAccessCheck) & bit_field()) != 0;
 }

 // [prototype]: implicit prototype object.
 /*
       #define DECL_ACCESSORS(name, type)  \
     inline type* name();                 \
     inline void set_##name(type* value)
     宏展開后變成,定義了讀寫某個屬性的函數(shù)
     Object * prototype();
     void * set_prototype(Object * value);

     屬性的定義如下(宏展開后也是讀寫某個屬性):
       #define ACCESSORS(holder, name, type, offset)                                   \
         type* holder::name() { return type::cast(READ_FIELD(this, offset)); } \
         void holder::set_##name(type* value) {                                \
           WRITE_FIELD(this, offset, value);                                   \
           WRITE_BARRIER(this, offset);                                        \
         }

       // 定義各個類的讀寫某屬性的函數(shù),第三第四個參數(shù)是類型和偏移
       ACCESSORS(Map, instance_descriptors, DescriptorArray,
                 kInstanceDescriptorsOffset)
       ACCESSORS(Map, code_cache, FixedArray, kCodeCacheOffset)
       ACCESSORS(Map, constructor, Object, kConstructorOffset
 */
 DECL_ACCESSORS(prototype, Object)

 // [constructor]: points back to the function responsible for this map.
 DECL_ACCESSORS(constructor, Object)

 // [instance descriptors]: describes the object.
 DECL_ACCESSORS(instance_descriptors, DescriptorArray)

 // [stub cache]: contains stubs compiled for this map.
 DECL_ACCESSORS(code_cache, FixedArray)

 // Returns a copy of the map.
 Object* Copy();

 // Returns the property index for name (only valid for FAST MODE).
 int PropertyIndexFor(String* name);

 // Returns the next free property index (only valid for FAST MODE).
 int NextFreePropertyIndex();

 // Returns the number of properties described in instance_descriptors.
 int NumberOfDescribedProperties();

 // Casting.
 static inline Map* cast(Object* obj);

 // Locate an accessor in the instance descriptor.
 AccessorDescriptor* FindAccessor(String* name);

 // Make sure the instance descriptor has no map transitions
 Object* EnsureNoMapTransitions();

 // Code cache operations.

 // Clears the code cache.
 inline void ClearCodeCache();

 // Update code cache.
 Object* UpdateCodeCache(String* name, Code* code);

 // Returns the found code or undefined if absent.
 Object* FindInCodeCache(String* name, Code::Flags flags);

 // Tells whether code is in the code cache.
 bool IncludedInCodeCache(Code* code);

 // Dispatched behavior.
 void MapIterateBody(ObjectVisitor* v);
#ifdef DEBUG
 void MapPrint();
 void MapVerify();
#endif

 // Layout description.
 static const int kInstanceAttributesOffset = HeapObject::kSize;
 static const int kPrototypeOffset = kInstanceAttributesOffset + kIntSize;
 static const int kConstructorOffset = kPrototypeOffset + kPointerSize;
 static const int kInstanceDescriptorsOffset =
     kConstructorOffset + kPointerSize;
 static const int kCodeCacheOffset = kInstanceDescriptorsOffset + kPointerSize;
 static const int kSize = kCodeCacheOffset + kIntSize;

 // Byte offsets within kInstanceAttributesOffset attributes.
 static const int kInstanceSizeOffset = kInstanceAttributesOffset + 0;
 static const int kInstanceTypeOffset = kInstanceAttributesOffset + 1;
 static const int kUnusedPropertyFieldsOffset = kInstanceAttributesOffset + 2;
 static const int kBitFieldOffset = kInstanceAttributesOffset + 3;

 // kBitFieldOffset對應的一個字節(jié),下面分別是該一個字節(jié)各比特位的標記
 static const int kHasSpecialLookup = 0;
 static const int kHasNonInstancePrototype = 1;
 static const int kIsHiddenPrototype = 2;
 static const int kHasNamedInterceptor = 3;
 static const int kHasIndexedInterceptor = 4;
 static const int kIsUndetectable = 5;
 static const int kHasInstanceCallHandler = 6;
 static const int kNeedsAccessCheck = 7;
private:
 DISALLOW_IMPLICIT_CONSTRUCTORS(Map);
};

             

下面的map的屬性內(nèi)存布局。

js引擎v8源碼怎么解析map對象

我們逐個函數(shù)分析他的實現(xiàn)。首先看objects-inl.h中的實現(xiàn)。

   
     
 
    
   

// 獲取對象某個屬性的地址,p是對象的首地址,offset是偏移,kHeapObjectTag是對象的標記,算地址的時候需要減掉
#define FIELD_ADDR(p, offset) \
 (reinterpret_cast<byte*>(p) + offset - kHeapObjectTag)

// 讀寫一個字節(jié)的內(nèi)容
#define READ_BYTE_FIELD(p, offset) \
 (*reinterpret_cast<byte*>(FIELD_ADDR(p, offset)))

#define WRITE_BYTE_FIELD(p, offset, value) \
 (*reinterpret_cast<byte*>(FIELD_ADDR(p, offset)) = value)


void Map::set_instance_size(int value) {
 ASSERT(0 <= value && value < 256);
 WRITE_BYTE_FIELD(this, kInstanceSizeOffset, static_cast<byte>(value));
}


InstanceType Map::instance_type() {
 return static_cast<InstanceType>(READ_BYTE_FIELD(this, kInstanceTypeOffset));
}


void Map::set_instance_type(InstanceType value) {
 ASSERT(0 <= value && value < 256);
 WRITE_BYTE_FIELD(this, kInstanceTypeOffset, value);
}


int Map::unused_property_fields() {
 return READ_BYTE_FIELD(this, kUnusedPropertyFieldsOffset);
}


void Map::set_unused_property_fields(int value) {
 WRITE_BYTE_FIELD(this, kUnusedPropertyFieldsOffset, Min(value, 255));
}

// 讀寫一個字節(jié)的內(nèi)容,每個比特都記錄著一個標記
byte Map::bit_field() {
 return READ_BYTE_FIELD(this, kBitFieldOffset);
}


void Map::set_bit_field(byte value) {
 WRITE_BYTE_FIELD(this, kBitFieldOffset, value);
}


void Map::set_non_instance_prototype(bool value) {
 if (value) {
   // 設置該位
   set_bit_field(bit_field() | (1 << kHasNonInstancePrototype));
 } else {
   // 清除該位
   set_bit_field(bit_field() & ~(1 << kHasNonInstancePrototype));
 }
}

// 是否設置了某位
bool Map::has_non_instance_prototype() {
 return ((1 << kHasNonInstancePrototype) & bit_field()) != 0;
}

void Map::ClearCodeCache() {
 // No write barrier is needed since empty_fixed_array is not in new space.
 // Please note this function is used during marking:
 //  - MarkCompactCollector::MarkUnmarkedObject
 ASSERT(!Heap::InNewSpace(Heap::empty_fixed_array()));
 WRITE_FIELD(this, kCodeCacheOffset, Heap::empty_fixed_array());
}

             

從上面的代碼中我們知道,只是對某些屬性或標記進行讀寫。

關于js引擎v8源碼怎么解析map對象問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關知識。

網(wǎng)頁題目:js引擎v8源碼怎么解析map對象
文章出自:http://bm7419.com/article26/pcsejg.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、網(wǎng)站收錄、App設計、網(wǎng)站設計、標簽優(yōu)化、定制開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設