Skip to content

Commit

Permalink
to_rust_cow_lossy
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacasonato committed Nov 11, 2024
1 parent ac7fadc commit 8baec0f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 24 deletions.
16 changes: 0 additions & 16 deletions src/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<const void*>(self.data8());
} else {
return reinterpret_cast<const void*>(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)));
Expand Down
28 changes: 20 additions & 8 deletions src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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,
))
}
}
}
Expand Down

0 comments on commit 8baec0f

Please sign in to comment.