diff --git a/changelog.d/14751-string-payload-accessor.md b/changelog.d/14751-string-payload-accessor.md new file mode 100644 index 00000000000..9cf682405c6 --- /dev/null +++ b/changelog.d/14751-string-payload-accessor.md @@ -0,0 +1 @@ +Eight new open-coded `StringHeader` payload offsets (the HTTP handle prototype's field lookups, the ArrayBuffer/DataView own-property read and four unit tests) read the payload through `string::string_data`, returning the string payload-access ratchet to its baseline. No behaviour change. diff --git a/crates/perry-runtime/src/array/from_primitive_tests.rs b/crates/perry-runtime/src/array/from_primitive_tests.rs index 7d3ea6e5890..bf87b205f50 100644 --- a/crates/perry-runtime/src/array/from_primitive_tests.rs +++ b/crates/perry-runtime/src/array/from_primitive_tests.rs @@ -156,7 +156,7 @@ fn element_bytes(arr: *mut ArrayHeader, index: u32) -> Vec { crate::value::js_get_string_pointer_unified(value) as *const crate::string::StringHeader; unsafe { let len = (*sptr).byte_len as usize; - let data = (sptr as *const u8).add(std::mem::size_of::()); + let data = crate::string::string_data(sptr); std::slice::from_raw_parts(data, len).to_vec() } } diff --git a/crates/perry-runtime/src/buffer/error_object_tests.rs b/crates/perry-runtime/src/buffer/error_object_tests.rs index 46323872413..726d133b8e1 100644 --- a/crates/perry-runtime/src/buffer/error_object_tests.rs +++ b/crates/perry-runtime/src/buffer/error_object_tests.rs @@ -7,7 +7,7 @@ use crate::string::StringHeader; unsafe fn text(s: *const StringHeader) -> String { let len = (*s).byte_len as usize; - let data = (s as *const u8).add(std::mem::size_of::()); + let data = crate::string::string_data(s); String::from_utf8_lossy(std::slice::from_raw_parts(data, len)).into_owned() } diff --git a/crates/perry-runtime/src/object/field_get_set/buffer_own_prop.rs b/crates/perry-runtime/src/object/field_get_set/buffer_own_prop.rs index 348048da864..7a396fba0a8 100644 --- a/crates/perry-runtime/src/object/field_get_set/buffer_own_prop.rs +++ b/crates/perry-runtime/src/object/field_get_set/buffer_own_prop.rs @@ -138,10 +138,7 @@ pub(crate) fn view_get(obj: *const ObjectHeader, key: *const crate::StringHeader let addr = obj as usize; if !key.is_null() { let key_bytes = unsafe { - std::slice::from_raw_parts( - (key as *const u8).add(std::mem::size_of::()), - (*key).byte_len as usize, - ) + std::slice::from_raw_parts(crate::string::string_data(key), (*key).byte_len as usize) }; if let Ok(name) = std::str::from_utf8(key_bytes) { if let Some(v) = crate::buffer::buffer_read_own_prop(addr, name) { diff --git a/crates/perry-runtime/src/object/global_this/declaration_keys_tests.rs b/crates/perry-runtime/src/object/global_this/declaration_keys_tests.rs index 976a3310cbc..1a4bcd2828e 100644 --- a/crates/perry-runtime/src/object/global_this/declaration_keys_tests.rs +++ b/crates/perry-runtime/src/object/global_this/declaration_keys_tests.rs @@ -14,7 +14,7 @@ fn own_string_keys(proto: f64) -> std::collections::BTreeSet { let s = crate::builtins::js_string_coerce(crate::array::js_array_get_f64(names, i)); unsafe { let bytes = std::slice::from_raw_parts( - (s as *const u8).add(std::mem::size_of::()), + crate::string::string_data(s), (*s).byte_len as usize, ); String::from_utf8_lossy(bytes).into_owned() diff --git a/crates/perry-runtime/src/object/http_handle_prototype.rs b/crates/perry-runtime/src/object/http_handle_prototype.rs index 254d0008572..50e22ff4aad 100644 --- a/crates/perry-runtime/src/object/http_handle_prototype.rs +++ b/crates/perry-runtime/src/object/http_handle_prototype.rs @@ -60,10 +60,7 @@ pub(crate) fn inherited_field( let scope = crate::gc::RuntimeHandleScope::new(); let key_root = scope.root_nanbox_f64(crate::value::js_nanbox_string(key as i64)); let name = unsafe { - std::slice::from_raw_parts( - (key as *const u8).add(std::mem::size_of::()), - (*key).byte_len as usize, - ) + std::slice::from_raw_parts(crate::string::string_data(key), (*key).byte_len as usize) } .to_vec(); let mut proto = scope.root_nanbox_f64(of_handle(handle_id)?); @@ -104,17 +101,14 @@ pub(crate) fn field(handle_id: usize, key: *const crate::StringHeader) -> Option let scope = crate::gc::RuntimeHandleScope::new(); let key_root = scope.root_nanbox_f64(crate::value::js_nanbox_string(key as i64)); let name = unsafe { - std::slice::from_raw_parts( - (key as *const u8).add(std::mem::size_of::()), - (*key).byte_len as usize, - ) + std::slice::from_raw_parts(crate::string::string_data(key), (*key).byte_len as usize) }; let name_owned = std::str::from_utf8(name).ok()?.to_owned(); let name = name_owned.as_str(); let native = || { let key = (key_root.get_nanbox_u64() & crate::value::POINTER_MASK) as *const crate::StringHeader; - let ptr = unsafe { (key as *const u8).add(std::mem::size_of::()) }; + let ptr = crate::string::string_data(key); let value = super::class_handles::handle_property_dispatch() .map(|dispatch| unsafe { dispatch(handle_id as i64, ptr, name.len()) }) .unwrap_or_else(|| f64::from_bits(crate::value::TAG_UNDEFINED)); diff --git a/crates/perry-runtime/src/thread/error_stack_clone_tests.rs b/crates/perry-runtime/src/thread/error_stack_clone_tests.rs index 94d31a999ad..56d212ba18d 100644 --- a/crates/perry-runtime/src/thread/error_stack_clone_tests.rs +++ b/crates/perry-runtime/src/thread/error_stack_clone_tests.rs @@ -88,11 +88,7 @@ fn error_stack_clone_reads_a_non_enumerable_getter_once() { let ptr = crate::value::js_get_string_pointer_unified(stack) as *const crate::string::StringHeader; let bytes = unsafe { - std::slice::from_raw_parts( - ptr.cast::() - .add(std::mem::size_of::()), - (*ptr).byte_len as usize, - ) + std::slice::from_raw_parts(crate::string::string_data(ptr), (*ptr).byte_len as usize) }; assert_eq!(bytes, b"getter-stack"); }