mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 19:12:43 +00:00 
			
		
		
		
	LibWeb: Make TreeWalker GC-allocated
This commit is contained in:
		
							parent
							
								
									bd629c45b5
								
							
						
					
					
						commit
						a4ddb0ef87
					
				
					 7 changed files with 41 additions and 30 deletions
				
			
		|  | @ -1750,7 +1750,7 @@ JS::NonnullGCPtr<NodeIterator> Document::create_node_iterator(Node& root, unsign | |||
| } | ||||
| 
 | ||||
| // https://dom.spec.whatwg.org/#dom-document-createtreewalker
 | ||||
| NonnullRefPtr<TreeWalker> Document::create_tree_walker(Node& root, unsigned what_to_show, NodeFilter* filter) | ||||
| JS::NonnullGCPtr<TreeWalker> Document::create_tree_walker(Node& root, unsigned what_to_show, JS::GCPtr<NodeFilter> filter) | ||||
| { | ||||
|     return TreeWalker::create(root, what_to_show, filter); | ||||
| } | ||||
|  |  | |||
|  | @ -351,7 +351,7 @@ public: | |||
|     static ExceptionOr<PrefixAndTagName> validate_qualified_name(String const& qualified_name); | ||||
| 
 | ||||
|     JS::NonnullGCPtr<NodeIterator> create_node_iterator(Node& root, unsigned what_to_show, JS::GCPtr<NodeFilter>); | ||||
|     NonnullRefPtr<TreeWalker> create_tree_walker(Node& root, unsigned what_to_show, NodeFilter*); | ||||
|     JS::NonnullGCPtr<TreeWalker> create_tree_walker(Node& root, unsigned what_to_show, JS::GCPtr<NodeFilter>); | ||||
| 
 | ||||
|     void register_node_iterator(Badge<NodeIterator>, NodeIterator&); | ||||
|     void unregister_node_iterator(Badge<NodeIterator>, NodeIterator&); | ||||
|  |  | |||
|  | @ -8,6 +8,7 @@ | |||
| #include <LibWeb/Bindings/IDLAbstractOperations.h> | ||||
| #include <LibWeb/Bindings/NodeWrapper.h> | ||||
| #include <LibWeb/Bindings/NodeWrapperFactory.h> | ||||
| #include <LibWeb/Bindings/TreeWalkerPrototype.h> | ||||
| #include <LibWeb/Bindings/Wrapper.h> | ||||
| #include <LibWeb/DOM/DOMException.h> | ||||
| #include <LibWeb/DOM/Node.h> | ||||
|  | @ -17,26 +18,36 @@ | |||
| namespace Web::DOM { | ||||
| 
 | ||||
| TreeWalker::TreeWalker(Node& root) | ||||
|     : m_root(root) | ||||
|     : PlatformObject(root.document().preferred_window_object().ensure_web_prototype<Bindings::TreeWalkerPrototype>("TreeWalker")) | ||||
|     , m_root(root) | ||||
|     , m_current(root) | ||||
| { | ||||
| } | ||||
| 
 | ||||
| TreeWalker::~TreeWalker() = default; | ||||
| 
 | ||||
| void TreeWalker::visit_edges(Cell::Visitor& visitor) | ||||
| { | ||||
|     Base::visit_edges(visitor); | ||||
|     visitor.visit(m_filter.ptr()); | ||||
| } | ||||
| 
 | ||||
| // https://dom.spec.whatwg.org/#dom-document-createtreewalker
 | ||||
| NonnullRefPtr<TreeWalker> TreeWalker::create(Node& root, unsigned what_to_show, NodeFilter* filter) | ||||
| JS::NonnullGCPtr<TreeWalker> TreeWalker::create(Node& root, unsigned what_to_show, JS::GCPtr<NodeFilter> filter) | ||||
| { | ||||
|     // 1. Let walker be a new TreeWalker object.
 | ||||
|     // 2. Set walker’s root and walker’s current to root.
 | ||||
|     auto walker = adopt_ref(*new TreeWalker(root)); | ||||
|     auto& window_object = root.document().preferred_window_object(); | ||||
|     auto* walker = window_object.heap().allocate<TreeWalker>(window_object.realm(), root); | ||||
| 
 | ||||
|     // 3. Set walker’s whatToShow to whatToShow.
 | ||||
|     walker->m_what_to_show = what_to_show; | ||||
| 
 | ||||
|     // 4. Set walker’s filter to filter.
 | ||||
|     walker->m_filter = JS::make_handle(filter); | ||||
|     walker->m_filter = filter; | ||||
| 
 | ||||
|     // 5. Return walker.
 | ||||
|     return walker; | ||||
|     return *walker; | ||||
| } | ||||
| 
 | ||||
| // https://dom.spec.whatwg.org/#dom-treewalker-currentnode
 | ||||
|  | @ -220,13 +231,9 @@ JS::ThrowCompletionOr<RefPtr<Node>> TreeWalker::next_node() | |||
| // https://dom.spec.whatwg.org/#concept-node-filter
 | ||||
| JS::ThrowCompletionOr<NodeFilter::Result> TreeWalker::filter(Node& node) | ||||
| { | ||||
|     VERIFY(wrapper()); | ||||
|     auto& vm = wrapper()->vm(); | ||||
|     auto& realm = *vm.current_realm(); | ||||
| 
 | ||||
|     // 1. If traverser’s active flag is set, then throw an "InvalidStateError" DOMException.
 | ||||
|     if (m_active) | ||||
|         return JS::throw_completion(wrap(realm, InvalidStateError::create("NodeIterator is already active"))); | ||||
|         return JS::throw_completion(wrap(shape().realm(), InvalidStateError::create("NodeIterator is already active"))); | ||||
| 
 | ||||
|     // 2. Let n be node’s nodeType attribute value − 1.
 | ||||
|     auto n = node.node_type() - 1; | ||||
|  | @ -236,7 +243,7 @@ JS::ThrowCompletionOr<NodeFilter::Result> TreeWalker::filter(Node& node) | |||
|         return NodeFilter::FILTER_SKIP; | ||||
| 
 | ||||
|     // 4. If traverser’s filter is null, then return FILTER_ACCEPT.
 | ||||
|     if (!m_filter.cell()) | ||||
|     if (!m_filter) | ||||
|         return NodeFilter::FILTER_ACCEPT; | ||||
| 
 | ||||
|     // 5. Set traverser’s active flag.
 | ||||
|  | @ -244,7 +251,7 @@ JS::ThrowCompletionOr<NodeFilter::Result> TreeWalker::filter(Node& node) | |||
| 
 | ||||
|     // 6. Let result be the return value of call a user object’s operation with traverser’s filter, "acceptNode", and « node ».
 | ||||
|     //    If this throws an exception, then unset traverser’s active flag and rethrow the exception.
 | ||||
|     auto result = Bindings::IDL::call_user_object_operation(m_filter->callback(), "acceptNode", {}, wrap(realm, node)); | ||||
|     auto result = Bindings::IDL::call_user_object_operation(m_filter->callback(), "acceptNode", {}, wrap(shape().realm(), node)); | ||||
|     if (result.is_abrupt()) { | ||||
|         m_active = false; | ||||
|         return result; | ||||
|  | @ -254,7 +261,7 @@ JS::ThrowCompletionOr<NodeFilter::Result> TreeWalker::filter(Node& node) | |||
|     m_active = false; | ||||
| 
 | ||||
|     // 8. Return result.
 | ||||
|     auto result_value = TRY(result.value()->to_i32(vm)); | ||||
|     auto result_value = TRY(result.value()->to_i32(vm())); | ||||
|     return static_cast<NodeFilter::Result>(result_value); | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -6,21 +6,21 @@ | |||
| 
 | ||||
| #pragma once | ||||
| 
 | ||||
| #include <AK/RefCounted.h> | ||||
| #include <LibWeb/Bindings/Wrappable.h> | ||||
| #include <LibWeb/DOM/NodeFilter.h> | ||||
| 
 | ||||
| namespace Web::DOM { | ||||
| 
 | ||||
| // https://dom.spec.whatwg.org/#treewalker
 | ||||
| class TreeWalker | ||||
|     : public RefCounted<TreeWalker> | ||||
|     , public Bindings::Wrappable { | ||||
| public: | ||||
|     using WrapperType = Bindings::TreeWalkerWrapper; | ||||
| class TreeWalker final : public Bindings::PlatformObject { | ||||
|     JS_OBJECT(TreeWalker, JS::Object); | ||||
| 
 | ||||
|     static NonnullRefPtr<TreeWalker> create(Node& root, unsigned what_to_show, NodeFilter*); | ||||
|     virtual ~TreeWalker() override = default; | ||||
| public: | ||||
|     static JS::NonnullGCPtr<TreeWalker> create(Node& root, unsigned what_to_show, JS::GCPtr<NodeFilter>); | ||||
| 
 | ||||
|     explicit TreeWalker(Node& root); | ||||
|     virtual ~TreeWalker() override; | ||||
| 
 | ||||
|     TreeWalker& impl() { return *this; } | ||||
| 
 | ||||
|     NonnullRefPtr<Node> current_node() const; | ||||
|     void set_current_node(Node&); | ||||
|  | @ -35,12 +35,12 @@ public: | |||
| 
 | ||||
|     NonnullRefPtr<Node> root() { return m_root; } | ||||
| 
 | ||||
|     NodeFilter* filter() { return m_filter.cell(); } | ||||
|     NodeFilter* filter() { return m_filter.ptr(); } | ||||
| 
 | ||||
|     unsigned what_to_show() const { return m_what_to_show; } | ||||
| 
 | ||||
| private: | ||||
|     TreeWalker(Node& root); | ||||
|     virtual void visit_edges(Cell::Visitor&) override; | ||||
| 
 | ||||
|     enum class ChildTraversalType { | ||||
|         First, | ||||
|  | @ -66,10 +66,15 @@ private: | |||
|     unsigned m_what_to_show { 0 }; | ||||
| 
 | ||||
|     // https://dom.spec.whatwg.org/#concept-traversal-filter
 | ||||
|     JS::Handle<DOM::NodeFilter> m_filter; | ||||
|     JS::GCPtr<DOM::NodeFilter> m_filter; | ||||
| 
 | ||||
|     // https://dom.spec.whatwg.org/#concept-traversal-active
 | ||||
|     bool m_active { false }; | ||||
| }; | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| namespace Web::Bindings { | ||||
| inline JS::Object* wrap(JS::Realm&, Web::DOM::TreeWalker& object) { return &object; } | ||||
| using TreeWalkerWrapper = Web::DOM::TreeWalker; | ||||
| } | ||||
|  |  | |||
|  | @ -1,7 +1,7 @@ | |||
| #import <DOM/Node.idl> | ||||
| #import <DOM/NodeFilter.idl> | ||||
| 
 | ||||
| [Exposed=Window] | ||||
| [Exposed=Window, NoInstanceWrapper] | ||||
| interface TreeWalker { | ||||
| 
 | ||||
|     [SameObject] readonly attribute Node root; | ||||
|  |  | |||
|  | @ -604,7 +604,6 @@ class TextDecoderWrapper; | |||
| class TextEncoderWrapper; | ||||
| class TextMetricsWrapper; | ||||
| class TextWrapper; | ||||
| class TreeWalkerWrapper; | ||||
| class UIEventWrapper; | ||||
| class URLConstructor; | ||||
| class URLPrototype; | ||||
|  |  | |||
|  | @ -49,7 +49,7 @@ libweb_js_wrapper(DOM/Range) | |||
| libweb_js_wrapper(DOM/ShadowRoot) | ||||
| libweb_js_wrapper(DOM/StaticRange) | ||||
| libweb_js_wrapper(DOM/Text) | ||||
| libweb_js_wrapper(DOM/TreeWalker) | ||||
| libweb_js_wrapper(DOM/TreeWalker NO_INSTANCE) | ||||
| libweb_js_wrapper(DOMParsing/XMLSerializer) | ||||
| libweb_js_wrapper(Encoding/TextDecoder) | ||||
| libweb_js_wrapper(Encoding/TextEncoder) | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Andreas Kling
						Andreas Kling