From 8baec0f34fe32a212cc561a5ce97dc5fbd51da72 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Mon, 11 Nov 2024 15:32:02 +0100 Subject: [PATCH] to_rust_cow_lossy --- src/binding.cc | 16 ---------------- src/string.rs | 28 ++++++++++++++++++++-------- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/binding.cc b/src/binding.cc index 0f8c791f..a3a5afce 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -1228,22 +1228,6 @@ void v8__String__ValueView__DESTRUCT(v8::String::ValueView* self) { self->~ValueView(); } -bool v8__String__ValueView__is_one_byte(const v8::String::ValueView& self) { - return self.is_one_byte(); -} - -const void* v8__String__ValueView__data(const v8::String::ValueView& self) { - if (self.is_one_byte()) { - return reinterpret_cast(self.data8()); - } else { - return reinterpret_cast(self.data16()); - } -} - -int v8__String__ValueView__length(const v8::String::ValueView& self) { - return self.length(); -} - const v8::Symbol* v8__Symbol__New(v8::Isolate* isolate, const v8::String* description) { return local_to_ptr(v8::Symbol::New(isolate, ptr_to_local(description))); diff --git a/src/string.rs b/src/string.rs index 4b60960f..91b49df4 100644 --- a/src/string.rs +++ b/src/string.rs @@ -10,6 +10,7 @@ use crate::String; use std::borrow::Cow; use std::convert::TryInto; use std::default::Default; +use std::ffi::c_int; use std::ffi::c_void; use std::hint::unreachable_unchecked; use std::marker::PhantomData; @@ -124,9 +125,14 @@ extern "C" { string: *const String, ); fn v8__String__ValueView__DESTRUCT(this: *mut ValueView); - fn v8__String__ValueView__is_one_byte(this: *const ValueView) -> bool; - fn v8__String__ValueView__data(this: *const ValueView) -> *const c_void; - fn v8__String__ValueView__length(this: *const ValueView) -> int; +} + +#[repr(C)] +struct ValueViewRaw { + flat_str: Local<'static, String>, + data: *const c_void, + length: c_int, + is_one_byte: bool, } #[derive(PartialEq, Debug)] @@ -1170,12 +1176,18 @@ impl<'s> ValueView<'s> { #[inline(always)] pub fn data(&self) -> ValueViewData<'_> { unsafe { - let data = v8__String__ValueView__data(self); - let length = v8__String__ValueView__length(self) as usize; - if v8__String__ValueView__is_one_byte(self) { - ValueViewData::OneByte(std::slice::from_raw_parts(data as _, length)) + let this = &*(self as *const _ as *const ValueViewRaw); + let length = this.length as usize; + if this.is_one_byte { + ValueViewData::OneByte(std::slice::from_raw_parts( + this.data as _, + length, + )) } else { - ValueViewData::TwoByte(std::slice::from_raw_parts(data as _, length)) + ValueViewData::TwoByte(std::slice::from_raw_parts( + this.data as _, + length, + )) } } }