wiki:DOMInJavaScript

Version 5 (modified by abarth@webkit.org, 9 years ago) (diff)

--

DOM in JavaScript

Overview

In this proposal, we upload the four basic DOM pointers (firstChild, lastChild, nextSibling, previousSibling) into the JavaScript engine so that they can be accessed more quickly from JavaScript. We then provide C++ an optimized code path for reading and writing these properties.

JavaScript

The main change in this proposal is to move the four basic DOM pointers from C++ pointers to JavaScript properties. When accessing nextSibling, for example, from JavaScript, the JavaScript engine will simply read the nextSibling property as usual (and apply all its usual optimizations). The DOM structure, then, is stored in JavaScript.

C++

After moving the DOM pointers into the JavaScript VM, we'll still need to provide a fast path for C++ code to access the pointers. Today, C++ simply reads a pointer from a fixed offset in the Node object. In this proposal, C++ code will need to take a slightly more indirect route:

Node* Node::nextSibling()
{
    m_wrapper->fastGetProperty(kNextSiblingIndex)->impl();
}

void Note::setNextSibling(Node* node)
{
    m_wrapper->fastSetProperty(kNextSiblingIndex, node->m_wrapper);
}

In order to get or set the nextSibling property, the C++ code needs to consult its JavaScript wrapper. (Note: This implies that we'll need to eagerly creata JavaScript wrappers for DOM nodes.) In this approach, the JavaScript wrapper stores the four DOM pointers at fixed offsets in memory, letting the C++ code read or write the property directly rather than having to do a hash table lookup.

My understanding is that fastGetProperty is similar to JSC::JSObject::getDirectOffset, the key is to create JavaScript wrappers for DOM nodes in such a way that initial offsets are know to correspond to particular properties (e.g., the four basic DOM pointers.)