Script: Lazily transform the DOMString into Rust String instead of immediately. (#39509)

This implements LazyDOMString (from now on DOMString) as outlined in
https://github.com/servo/servo/issues/39479.
Constructing from a *mut JSString we keep the in a
RootedTraceableBox<Heap<*mut JSString>> and transform
the string into a rust string if necessary via the `make_rust_string`
method.
Methods used in script are implemented on this string. Currently we
transform the string at all times.
But in the future more efficient implementations are possible.

We implement the safety critical sections in a separate module
DOMStringInner which allows simple constructors, `make_rust_string` and
the `bytes` method.
This method returns the new type `EncodedBytes` which contains the
reference to the underlying string in either format.

Testing: WPT tests still seem to work, so this should test this
functionality.

---------

Signed-off-by: Narfinger <Narfinger@users.noreply.github.com>
This commit is contained in:
Narfinger
2025-10-09 20:18:03 +02:00
committed by GitHub
parent ef9f16027b
commit 423800eec4
101 changed files with 1098 additions and 633 deletions

View File

@@ -23,6 +23,7 @@ use net_traits::{
FetchMetadata, FetchResponseListener, Metadata, NetworkError, ResourceFetchTiming,
ResourceTimingType,
};
use script_bindings::domstring::BytesView;
use servo_url::{ImmutableOrigin, ServoUrl};
use style::attr::AttrValue;
use style::str::{HTML_SPACE_CHARACTERS, StaticStringVec};
@@ -77,7 +78,7 @@ impl ScriptSource for ScriptOrigin {
self.unminified_dir.clone()
}
fn extract_bytes(&self) -> &[u8] {
fn extract_bytes(&self) -> BytesView<'_> {
match &self.code {
SourceCode::Text(text) => text.as_bytes(),
SourceCode::Compiled(compiled_source_code) => {
@@ -707,7 +708,7 @@ impl HTMLScriptElement {
global,
element,
InlineCheckType::Script,
text.str(),
&text.str(),
)
{
warn!("Blocking inline script due to CSP");
@@ -751,13 +752,14 @@ impl HTMLScriptElement {
let module_credentials_mode = match script_type {
ScriptType::Classic => CredentialsMode::CredentialsSameOrigin,
ScriptType::Module | ScriptType::ImportMap => reflect_cross_origin_attribute(element)
.map_or(CredentialsMode::CredentialsSameOrigin, |attr| {
match attr.str() {
.map_or(
CredentialsMode::CredentialsSameOrigin,
|attr| match &*attr.str() {
"use-credentials" => CredentialsMode::Include,
"anonymous" => CredentialsMode::CredentialsSameOrigin,
_ => CredentialsMode::CredentialsSameOrigin,
}
}),
},
),
};
// Step 24. Let cryptographic nonce be el's [[CryptographicNonce]] internal slot's value.
@@ -1532,7 +1534,7 @@ impl HTMLScriptElementMethods<crate::DomTypeHolder> for HTMLScriptElement {
fn Supports(_window: &Window, type_: DOMString) -> bool {
// The type argument has to exactly match these values,
// we do not perform an ASCII case-insensitive match.
matches!(type_.str(), "classic" | "module" | "importmap")
matches!(&*type_.str(), "classic" | "module" | "importmap")
}
}