簡體   English   中英

v8 引擎中 DOM 包裝器的令人困惑的解釋

[英]Confusing explanation of DOM wrappers in v8 engine

我試圖了解為什么 V8 API 的結構是這樣的設計,並找到了一個關於隔離、上下文等各種事物之間關系的有用文檔: Link to V8 Binding

在那里,我對某個段落感到困惑:

出於兼容性原因,只要底層 C++ DOM object 存在,我們需要確保將相同的 DOM 包裝器返回到 JavaScript。 我們不應該為同一個 C++ DOM object 返回不同的 DOM 包裝器。

這是一個例子:

var div = document.createElement("div");
div.foo = 1234;  // expando
var p = document.createElement("p");
p.appendChild(div);
div = null;
gc();
console.log(p.firstChild.foo);  // This should be 1234, not undefined

To accomplish the semantics that the same DOM wrapper is returned to JavaScript as long as the underlying C++ DOM object is alive, we need a mapping from the C++ DOM objects to the DOM wrappers. 此外,我們需要對每個世界中的 DOM 包裝器進行沙箱處理。 為了滿足要求,我們讓每個世界都擁有一個 DOM 包裝存儲,該存儲存儲從 C++ DOM 對象到該世界中的 DOM 包裝的映射。

因此,我們在一個隔離中擁有多個 DOM 包裝器存儲。 主世界的映射是用 ScriptWrappable 編寫的。 如果 ScriptWrappable::main_world_wrapper_ 具有非空值,則它是主世界的 C++ DOM object 的 DOM 包裝器。 其他世界的映射寫在 DOMDataStore 中。

我仍在嘗試理解整個段落,但具體的代碼示例和直接的解釋對我來說沒有意義。 我從沒想過代碼片段會打印undefined ,即使它是純粹的 JavaScript API。

我覺得這個例子不正確,但是沒有一個合適的例子,我很難理解這個概念。

屬性foo添加在 JS object 包裝器上,而不是內部 C++ DOM object 上。

因此,當調用gc() ( g arbage c ollection) 時,最初聲明為div的 JS foo將消失(該變量在其屬性之前設置為null )。

If the JS object DOM wrapper was not always the same object, retrieving the C++ DOM object through p.firstChild would return an other, new JS object DOM wrapper, without the foo property.

但是由於它們確保包裝器始終相同,因此無論調用它的上下文如何,該屬性仍然可用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM