Truly independent web browser
LibJS: Return the string of a PrimitiveString by reference
Andreas Kling32c1703b6259
diff --git a/Libraries/LibJS/Runtime/PrimitiveString.cpp b/Libraries/LibJS/Runtime/PrimitiveString.cppindex 3ba3a692c26..43a2625f9f4 100644--- a/Libraries/LibJS/Runtime/PrimitiveString.cpp+++ b/Libraries/LibJS/Runtime/PrimitiveString.cpp@@ -11,6 +11,9 @@ namespace JS {using namespace EmbeddingABI;+static_assert(sizeof(Utf16String) == sizeof(JSOwnedUtf16String));+static_assert(alignof(Utf16String) == alignof(JSOwnedUtf16String));+static GC::Ref<PrimitiveString> primitive_string_from_abi(JSPrimitiveString* string){VERIFY(string);@@ -37,9 +40,9 @@ GC::Ref<PrimitiveString> PrimitiveString::create_from_unsigned_integer(VM& vm, ureturn primitive_string_from_abi(js_string_create_from_unsigned_integer(vm_to_abi(vm), number));}-Utf16String PrimitiveString::utf16_string() const+Utf16String const& PrimitiveString::utf16_string() const{- return owned_utf16_string_from_abi(js_string_utf16_string(primitive_string_to_abi(*this)));+ return *reinterpret_cast<Utf16String const*>(js_string_utf16_string_address(primitive_string_to_abi(*this)));}Utf16View PrimitiveString::utf16_string_view() constdiff --git a/Libraries/LibJS/Runtime/PrimitiveString.h b/Libraries/LibJS/Runtime/PrimitiveString.hindex dbbff32f126..e57ed6da1e5 100644--- a/Libraries/LibJS/Runtime/PrimitiveString.h+++ b/Libraries/LibJS/Runtime/PrimitiveString.h@@ -27,7 +27,8 @@ public:[[nodiscard]] static GC::Ref<PrimitiveString> create_from_unsigned_integer(VM&, u64);- [[nodiscard]] Utf16String utf16_string() const;+ // The string that holds the code units, which stays where it is, unchanged, as long as this string lives.+ [[nodiscard]] Utf16String const& utf16_string() const;// The code units stay valid as long as the string does.[[nodiscard]] Utf16View utf16_string_view() const;diff --git a/Libraries/LibJS/Rust/src/embedding/string.rs b/Libraries/LibJS/Rust/src/embedding/string.rsindex 7151658ddcb..2e5f5927efc 100644--- a/Libraries/LibJS/Rust/src/embedding/string.rs+++ b/Libraries/LibJS/Rust/src/embedding/string.rs@@ -13,7 +13,7 @@ use ak::Utf16FlyString;use crate::embedding::abi_types::{JSOwnedUtf16String, JSPrimitiveString, JSUtf16View, cell_from_abi, cell_into_abi, owned_utf16_string_from_abi,- owned_utf16_string_into_abi, vm_from_abi,+ vm_from_abi,};use crate::layout::host_class::JSVM;use crate::runtime::primitive_string::PrimitiveString;@@ -84,16 +84,18 @@ pub unsafe extern "C" fn js_string_create_from_unsigned_integer(vm: *mut JSVM, ncell_into_abi(PrimitiveString::create_from_unsigned_integer(vm, number))}-/// The code units of `string` as an AK::Utf16String, which the caller owns. It shares the string's storage, after-/// resolving a string the VM built lazily. Call on the VM's thread.+/// PrimitiveString::utf16_string() as C++ returns it, by reference: the address of the AK::Utf16String that holds the+/// code units of `string`, after resolving a string the VM built lazily. It stays where it is, unchanged, for as long+/// as the string lives. Call on the VM's thread.////// # Safety////// `string` must be a string of the embedder's VM.#[unsafe(no_mangle)]-pub unsafe extern "C" fn js_string_utf16_string(string: *mut JSPrimitiveString) -> JSOwnedUtf16String {+pub unsafe extern "C" fn js_string_utf16_string_address(string: *mut JSPrimitiveString) -> *const JSOwnedUtf16String {// SAFETY: The caller passes a string of the VM.- owned_utf16_string_into_abi(unsafe { cell_from_abi(string) }.utf16_string())+ let string = unsafe { cell_from_abi(string) };+ core::ptr::from_ref(string.utf16_string_in_place()).cast()}/// A view of the code units of `string`, after resolving a string the VM built lazily. The view stays valid for as longdiff --git a/Libraries/LibJS/Rust/src/runtime/primitive_string.rs b/Libraries/LibJS/Rust/src/runtime/primitive_string.rsindex 05408d15e9c..ac069a0a892 100644--- a/Libraries/LibJS/Rust/src/runtime/primitive_string.rs+++ b/Libraries/LibJS/Rust/src/runtime/primitive_string.rs@@ -449,11 +449,16 @@ impl PrimitiveString {}pub fn utf16_string(&self) -> Utf16String {+ self.utf16_string_in_place().clone()+ }++ /// The string's own UTF-16 string, resolving a deferred string first. It stays where it is, unchanged, for as long+ /// as the string lives, since a resolved string never changes.+ pub fn utf16_string_in_place(&self) -> &Utf16String {self.resolve_if_needed();self.resolved_utf16_string().expect("a resolved string has its UTF-16 string")- .clone()}pub fn property_key(&self, vm: &Vm) -> PropertyKey {