LibWeb+LibGC: Import GC swift module into LibWeb and an initial user

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.
This commit is contained in:
Andrew Kaster 2025-03-22 19:12:30 -06:00 committed by Andrew Kaster
parent 8554ee386e
commit 9ee2473aa4
Notes: github-actions[bot] 2025-04-03 22:49:20 +00:00
5 changed files with 70 additions and 4 deletions

View file

@ -0,0 +1,60 @@
/*
* 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)
}
}