Skip to content

Truly independent web browser

LibWeb: Feed the Rust HTML tokenizer its input as AK spans

Andreas Kling5caef9bc2e5e
diff --git a/Libraries/LibWeb/HTML/Parser/HTMLTokenizer.cpp b/Libraries/LibWeb/HTML/Parser/HTMLTokenizer.cpp
index 23c2eb09d2d..6e0c226a6c1 100644
--- a/Libraries/LibWeb/HTML/Parser/HTMLTokenizer.cpp
+++ b/Libraries/LibWeb/HTML/Parser/HTMLTokenizer.cpp
@@ -13,11 +13,16 @@
namespace Web::HTML {
+static ReadonlySpan<u16> utf16_units(Utf16View input)
+{
+ return { reinterpret_cast<u16 const*>(input.utf16_span().data()), input.length_in_code_units() };
+}
+
static RustFfiTokenizerHandle* create_tokenizer_from_utf16(Utf16View input)
{
if (input.has_ascii_storage())
- return rust_html_tokenizer_create_from_ascii(reinterpret_cast<u8 const*>(input.ascii_span().data()), input.length_in_code_units());
- return rust_html_tokenizer_create_from_utf16(reinterpret_cast<u16 const*>(input.utf16_span().data()), input.length_in_code_units());
+ return rust_html_tokenizer_create_from_ascii(input.bytes());
+ return rust_html_tokenizer_create_from_utf16(utf16_units(input));
}
static Utf16FlyString utf16_fly_string_from_ffi(u16 const* ptr, size_t len)
@@ -169,10 +174,10 @@ void HTMLTokenizer::append_to_input_stream(Utf16View input)
return;
if (input.has_ascii_storage()) {
- rust_html_tokenizer_append_ascii_input(m_tokenizer, reinterpret_cast<u8 const*>(input.ascii_span().data()), input.length_in_code_units());
+ rust_html_tokenizer_append_ascii_input(m_tokenizer, input.bytes());
return;
}
- rust_html_tokenizer_append_utf16_input(m_tokenizer, reinterpret_cast<u16 const*>(input.utf16_span().data()), input.length_in_code_units());
+ rust_html_tokenizer_append_utf16_input(m_tokenizer, utf16_units(input));
}
void HTMLTokenizer::close_input_stream()
@@ -184,10 +189,10 @@ void HTMLTokenizer::close_input_stream()
void HTMLTokenizer::insert_input_at_insertion_point(Utf16View input)
{
if (input.has_ascii_storage()) {
- rust_html_tokenizer_insert_ascii_input(m_tokenizer, reinterpret_cast<u8 const*>(input.ascii_span().data()), input.length_in_code_units());
+ rust_html_tokenizer_insert_ascii_input(m_tokenizer, input.bytes());
return;
}
- rust_html_tokenizer_insert_utf16_input(m_tokenizer, reinterpret_cast<u16 const*>(input.utf16_span().data()), input.length_in_code_units());
+ rust_html_tokenizer_insert_utf16_input(m_tokenizer, utf16_units(input));
}
void HTMLTokenizer::insert_eof()
diff --git a/Libraries/LibWeb/HTML/Parser/Rust/cbindgen.toml b/Libraries/LibWeb/HTML/Parser/Rust/cbindgen.toml
index 6b2b44e9572..1e59b277c5b 100644
--- a/Libraries/LibWeb/HTML/Parser/Rust/cbindgen.toml
+++ b/Libraries/LibWeb/HTML/Parser/Rust/cbindgen.toml
@@ -10,6 +10,7 @@ line_length = 120
tab_width = 4
no_includes = true
sys_includes = ["stdint.h", "stddef.h"]
+includes = ["AK/Span.h"]
usize_is_size_t = true
[parse]
@@ -17,3 +18,7 @@ parse_deps = false
[parse.expand]
all_features = false
+
+[export.rename]
+# An ak::Span is laid out as an AK ReadonlySpan, which C++ passes it as.
+"Span" = "AK::ReadonlySpan"
diff --git a/Libraries/LibWeb/HTML/Parser/Rust/src/lib.rs b/Libraries/LibWeb/HTML/Parser/Rust/src/lib.rs
index 7d06f3ef3f2..9f5470cf41a 100644
--- a/Libraries/LibWeb/HTML/Parser/Rust/src/lib.rs
+++ b/Libraries/LibWeb/HTML/Parser/Rust/src/lib.rs
@@ -126,40 +126,17 @@ fn position_to_ffi(pos: &Position) -> (u64, u64) {
}
/// Create a new Rust HTML tokenizer directly from a UTF-16 code unit buffer.
-///
-/// # Safety
-/// `input` must point to `len` valid UTF-16 code units.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_html_tokenizer_create_from_utf16(
- input: *const u16,
- len: usize,
-) -> *mut RustFfiTokenizerHandle {
- let code_units = if input.is_null() || len == 0 {
- Vec::new()
- } else {
- unsafe { ak::slice_from_raw_parts(input, len) }.to_vec()
- };
+pub extern "C" fn rust_html_tokenizer_create_from_utf16(input: ak::Span<'_, u16>) -> *mut RustFfiTokenizerHandle {
+ let code_units = input.as_slice().to_vec();
make_handle(HtmlTokenizer::new(code_units))
}
/// Create a new Rust HTML tokenizer directly from an ASCII byte buffer.
-///
-/// # Safety
-/// `input` must point to `len` ASCII bytes.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_html_tokenizer_create_from_ascii(
- input: *const u8,
- len: usize,
-) -> *mut RustFfiTokenizerHandle {
- let code_units = if input.is_null() || len == 0 {
- Vec::new()
- } else {
- unsafe { ak::slice_from_raw_parts(input, len) }
- .iter()
- .map(|byte| u16::from(*byte))
- .collect()
- };
+pub extern "C" fn rust_html_tokenizer_create_from_ascii(input: ak::Span<'_, u8>) -> *mut RustFfiTokenizerHandle {
+ let code_units = input.as_slice().iter().map(|byte| u16::from(*byte)).collect();
make_handle(HtmlTokenizer::new(code_units))
}
@@ -345,86 +322,54 @@ pub extern "C" fn rust_html_tokenizer_switch_state(handle: Option<&mut RustFfiTo
}
/// Insert UTF-16 input at the current insertion point.
-///
-/// # Safety
-/// `handle` must be valid. `input` must point to `len` valid UTF-16 code units.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_html_tokenizer_insert_utf16_input(
+pub extern "C" fn rust_html_tokenizer_insert_utf16_input(
handle: Option<&mut RustFfiTokenizerHandle>,
- input: *const u16,
- len: usize,
+ input: ak::Span<'_, u16>,
) {
let Some(handle) = handle else {
return;
};
- let code_units = if input.is_null() || len == 0 {
- &[]
- } else {
- unsafe { ak::slice_from_raw_parts(input, len) }
- };
+ let code_units = input.as_slice();
handle.tokenizer.insert_input_at_insertion_point(code_units);
}
/// Insert ASCII input at the current insertion point.
-///
-/// # Safety
-/// `handle` must be valid. `input` must point to `len` ASCII bytes.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_html_tokenizer_insert_ascii_input(
+pub extern "C" fn rust_html_tokenizer_insert_ascii_input(
handle: Option<&mut RustFfiTokenizerHandle>,
- input: *const u8,
- len: usize,
+ input: ak::Span<'_, u8>,
) {
let Some(handle) = handle else {
return;
};
- let input = if input.is_null() || len == 0 {
- &[]
- } else {
- unsafe { ak::slice_from_raw_parts(input, len) }
- };
+ let input = input.as_slice();
handle.tokenizer.insert_ascii_input_at_insertion_point(input);
}
/// Append UTF-16 input to the tokenizer input stream.
-///
-/// # Safety
-/// `handle` must be valid. `input` must point to `len` valid UTF-16 code units.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_html_tokenizer_append_utf16_input(
+pub extern "C" fn rust_html_tokenizer_append_utf16_input(
handle: Option<&mut RustFfiTokenizerHandle>,
- input: *const u16,
- len: usize,
+ input: ak::Span<'_, u16>,
) {
let Some(handle) = handle else {
return;
};
- let code_units = if input.is_null() || len == 0 {
- &[]
- } else {
- unsafe { ak::slice_from_raw_parts(input, len) }
- };
+ let code_units = input.as_slice();
handle.tokenizer.append_input(code_units);
}
/// Append ASCII input to the tokenizer input stream.
-///
-/// # Safety
-/// `handle` must be valid. `input` must point to `len` ASCII bytes.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_html_tokenizer_append_ascii_input(
+pub extern "C" fn rust_html_tokenizer_append_ascii_input(
handle: Option<&mut RustFfiTokenizerHandle>,
- input: *const u8,
- len: usize,
+ input: ak::Span<'_, u8>,
) {
let Some(handle) = handle else {
return;
};
- let input = if input.is_null() || len == 0 {
- &[]
- } else {
- unsafe { ak::slice_from_raw_parts(input, len) }
- };
+ let input = input.as_slice();
handle.tokenizer.append_ascii_input(input);
}