mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-07 09:31:53 +00:00
Start work on a speculative HTML Parser in Swift. This component will walk ahead of the normal HTML parser looking for fetch() requests to make while the normal parser is blocked. This work exposed many holes in the Swift C++ interop component, which have been reported upstream.
60 lines
1.6 KiB
Swift
60 lines
1.6 KiB
Swift
/*
|
|
* Copyright (c) 2025, Andrew Kaster <andrew@ladybird.org>>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
import AK
|
|
import Collections
|
|
import Foundation
|
|
import GC
|
|
@_exported import WebCxx
|
|
|
|
// Workaround for https://github.com/swiftlang/swift/issues/80231
|
|
// If any line of this changes, the whole thing breaks though
|
|
extension GC.Cell.Visitor {
|
|
public func visit(_ parser: Web.HTML.HTMLParserGCPtr) {
|
|
if let parser = parser.ptr() {
|
|
let cell: GC.Cell = cxxCast(parser)
|
|
visit(cell)
|
|
}
|
|
}
|
|
}
|
|
|
|
struct SpeculativeMockElement {
|
|
let name: Swift.String
|
|
let localName: Swift.String
|
|
let attributes: [HTMLToken.Attribute]
|
|
var children: [SpeculativeMockElement]
|
|
|
|
init(name: Swift.String, localName: Swift.String, attributes: [HTMLToken.Attribute]) {
|
|
self.name = name
|
|
self.localName = localName
|
|
self.attributes = attributes
|
|
self.children = []
|
|
}
|
|
|
|
mutating func appendChild(_ child: consuming SpeculativeMockElement) {
|
|
children.append(child)
|
|
}
|
|
}
|
|
|
|
public final class SpeculativeHTMLParser: HeapAllocatable {
|
|
var parser = Web.HTML.HTMLParserGCPtr() // FIXME: Want HTMLParserGCRef here, but how to initialize it?
|
|
|
|
public init(cell: GC.Cell) {
|
|
self.cell = cell
|
|
}
|
|
public var cell: GC.Cell
|
|
|
|
public static func create(on heap: GC.Heap, `for` parser: Web.HTML.HTMLParserGCPtr) -> GC.Cell {
|
|
precondition(heap.is_gc_deferred())
|
|
let _self = allocate(on: heap)
|
|
_self.pointee.parser = parser
|
|
return _self.pointee.cell
|
|
}
|
|
|
|
public func visitEdges(_ visitor: GC.Cell.Visitor) {
|
|
visitor.visit(parser)
|
|
}
|
|
}
|