|
| 1 | +# V8 DOM Bindings Architecture |
| 2 | + |
| 3 | +This document explains the new V8-based DOM binding implementation in `src/client/script_bindings/dom/` that replaces the N-API implementation. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The V8 DOM bindings provide a direct interface between JavaScript and the native DOM implementation, using V8 APIs instead of N-API for better performance and integration. |
| 8 | + |
| 9 | +## Architecture |
| 10 | + |
| 11 | +### Base Class Pattern |
| 12 | + |
| 13 | +All DOM classes inherit from `scripting_base::ObjectWrap<T, D, B>` where: |
| 14 | +- `T`: The wrapper class (e.g., `Node`, `Element`) |
| 15 | +- `D`: The native DOM class (e.g., `dom::Node`, `dom::Element`) |
| 16 | +- `B`: The base wrapper class for inheritance (e.g., `Element` extends `Node`) |
| 17 | + |
| 18 | +### Inheritance Hierarchy |
| 19 | + |
| 20 | +``` |
| 21 | +Node (wraps dom::Node) |
| 22 | +├── Element (wraps dom::Element, extends Node) |
| 23 | +│ ├── HTMLElement (wraps dom::HTMLElement, extends Element) |
| 24 | +│ │ ├── HTMLDivElement (wraps dom::HTMLDivElement, extends HTMLElement) |
| 25 | +│ │ ├── HTMLSpanElement (wraps dom::HTMLSpanElement, extends HTMLElement) |
| 26 | +│ │ └── ... (other HTML elements) |
| 27 | +│ └── Text (wraps dom::Text, extends Node) |
| 28 | +├── Document (wraps dom::Document, extends Node) |
| 29 | +└── Console (utility class, no DOM equivalent) |
| 30 | +``` |
| 31 | + |
| 32 | +## Key Classes |
| 33 | + |
| 34 | +### Node (`node.hpp`, `node.cpp`) |
| 35 | +- Base class for all DOM nodes |
| 36 | +- Implements EventTarget interface (addEventListener, removeEventListener, dispatchEvent) |
| 37 | +- Properties: nodeName, nodeType, nodeValue, parentNode, etc. |
| 38 | +- Methods: appendChild, removeChild, insertBefore, etc. |
| 39 | + |
| 40 | +### Element (`element.hpp`, `element.cpp`) |
| 41 | +- Base class for all DOM elements |
| 42 | +- Properties: tagName, id, className, innerHTML, outerHTML |
| 43 | +- Methods: getAttribute, setAttribute, hasAttribute, querySelector, etc. |
| 44 | + |
| 45 | +### HTMLElement (`html_element.hpp`, `html_element.cpp`) |
| 46 | +- Base class for all HTML elements |
| 47 | +- Properties: innerText, hidden |
| 48 | +- Methods: click, focus, blur |
| 49 | + |
| 50 | +### Document (`document.hpp`, `document.cpp`) |
| 51 | +- Document root node |
| 52 | +- Properties: documentElement, body, title |
| 53 | +- Methods: createElement, createTextNode, getElementById, querySelector, etc. |
| 54 | + |
| 55 | +### Text (`text.hpp`, `text.cpp`) |
| 56 | +- Text node implementation |
| 57 | +- Properties: data, length, wholeText |
| 58 | +- Methods: substringData, appendData, insertData, deleteData, etc. |
| 59 | + |
| 60 | +### Console (`console.hpp`, `console.cpp`) |
| 61 | +- JavaScript console implementation |
| 62 | +- Methods: log, info, warn, error, debug, trace, assert, clear |
| 63 | +- Creates global `console` object |
| 64 | + |
| 65 | +## Implementation Patterns |
| 66 | + |
| 67 | +### Property Accessors |
| 68 | + |
| 69 | +```cpp |
| 70 | +// Read-only property |
| 71 | +instanceTemplate->SetAccessor( |
| 72 | + String::NewFromUtf8(isolate, "nodeName").ToLocalChecked(), |
| 73 | + NodeNameGetter, |
| 74 | + nullptr, // no setter |
| 75 | + Local<Value>(), |
| 76 | + AccessControl::DEFAULT, |
| 77 | + PropertyAttribute::ReadOnly); |
| 78 | + |
| 79 | +// Read-write property |
| 80 | +instanceTemplate->SetAccessor( |
| 81 | + String::NewFromUtf8(isolate, "nodeValue").ToLocalChecked(), |
| 82 | + NodeValueGetter, |
| 83 | + NodeValueSetter); |
| 84 | +``` |
| 85 | +
|
| 86 | +### Method Binding |
| 87 | +
|
| 88 | +```cpp |
| 89 | +instanceTemplate->Set( |
| 90 | + String::NewFromUtf8(isolate, "appendChild").ToLocalChecked(), |
| 91 | + FunctionTemplate::New(isolate, AppendChild)); |
| 92 | +``` |
| 93 | + |
| 94 | +### Class Initialization |
| 95 | + |
| 96 | +```cpp |
| 97 | +static v8::Local<v8::Function> Initialize(v8::Isolate *isolate) |
| 98 | +{ |
| 99 | + return scripting_base::ObjectWrap<T, D, B>::Initialize(isolate); |
| 100 | +} |
| 101 | +``` |
| 102 | +
|
| 103 | +### Instance Creation |
| 104 | +
|
| 105 | +```cpp |
| 106 | +static v8::Local<v8::Object> NewInstance(v8::Isolate *isolate, std::shared_ptr<D> native) |
| 107 | +{ |
| 108 | + return scripting_base::ObjectWrap<T, D, B>::NewInstance(isolate, native); |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +## Binding Registration |
| 113 | + |
| 114 | +The `binding.cpp` file initializes all DOM classes and registers them with V8: |
| 115 | + |
| 116 | +```cpp |
| 117 | +void Initialize(v8::Isolate *isolate, v8::Local<v8::Object> global) |
| 118 | +{ |
| 119 | + // Initialize constructors |
| 120 | + auto nodeConstructor = Node::Initialize(isolate); |
| 121 | + auto elementConstructor = Element::Initialize(isolate); |
| 122 | + // ... other classes |
| 123 | + |
| 124 | + // Register as globals |
| 125 | + global->Set(context, String::NewFromUtf8(isolate, "Node").ToLocalChecked(), |
| 126 | + nodeConstructor).Check(); |
| 127 | + // ... other registrations |
| 128 | + |
| 129 | + // Create global console object |
| 130 | + auto consoleObject = Console::CreateConsoleObject(isolate); |
| 131 | + global->Set(context, String::NewFromUtf8(isolate, "console").ToLocalChecked(), |
| 132 | + consoleObject).Check(); |
| 133 | +} |
| 134 | +``` |
| 135 | +
|
| 136 | +## Key Differences from N-API Implementation |
| 137 | +
|
| 138 | +### No N-API Dependencies |
| 139 | +- Uses V8 APIs directly (`v8::Object`, `v8::Function`, etc.) |
| 140 | +- No `Napi::` namespace usage |
| 141 | +- No NAPI environment (`Napi::Env`) dependencies |
| 142 | +
|
| 143 | +### Direct V8 Integration |
| 144 | +- Uses `v8::FunctionTemplate` and `v8::ObjectTemplate` |
| 145 | +- Direct V8 property accessors and method callbacks |
| 146 | +- Better performance and integration with V8 engine |
| 147 | +
|
| 148 | +### Simplified Inheritance |
| 149 | +- Uses template parameters for inheritance instead of complex NAPI patterns |
| 150 | +- More type-safe with compile-time inheritance checking |
| 151 | +
|
| 152 | +## Usage |
| 153 | +
|
| 154 | +### Initialization |
| 155 | +```cpp |
| 156 | +#include <client/script_bindings/dom/binding.hpp> |
| 157 | +
|
| 158 | +// In V8 context setup |
| 159 | +script_bindings::dom::Initialize(isolate, global); |
| 160 | +``` |
| 161 | + |
| 162 | +### Creating DOM Objects |
| 163 | +```cpp |
| 164 | +// From native DOM objects |
| 165 | +auto element = document->createElement("div"); |
| 166 | +auto jsElement = Element::NewInstance(isolate, element); |
| 167 | + |
| 168 | +// Or directly use existing JavaScript constructors |
| 169 | +// new Element() will work in JavaScript after initialization |
| 170 | +``` |
| 171 | +
|
| 172 | +## Future Extensions |
| 173 | +
|
| 174 | +To add new DOM classes: |
| 175 | +
|
| 176 | +1. Create header/implementation files following the pattern |
| 177 | +2. Inherit from appropriate base class using ObjectWrap |
| 178 | +3. Implement ConfigureFunctionTemplate, Initialize, and NewInstance |
| 179 | +4. Add to binding.cpp initialization |
| 180 | +
|
| 181 | +## Testing |
| 182 | +
|
| 183 | +The bindings can be tested by: |
| 184 | +1. Creating V8 contexts with the bindings initialized |
| 185 | +2. Executing JavaScript that uses DOM APIs |
| 186 | +3. Verifying proper integration with native DOM backend |
| 187 | +
|
| 188 | +Example JavaScript test: |
| 189 | +```javascript |
| 190 | +const div = document.createElement('div'); |
| 191 | +div.id = 'test'; |
| 192 | +div.innerHTML = '<span>Hello World</span>'; |
| 193 | +document.body.appendChild(div); |
| 194 | +console.log('Element created:', div.tagName); |
| 195 | +``` |
0 commit comments