Skip to content

Commit 79c94a9

Browse files
authored
feat: Bring back deprecated v8::String API (#1694)
Brings back String APIs removed in #1692 and renamed the new APIs to have _v2 suffix.
1 parent 45edd18 commit 79c94a9

File tree

3 files changed

+207
-23
lines changed

3 files changed

+207
-23
lines changed

src/binding.cc

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434

3535
using namespace support;
3636

37+
// TODO(bartlomieju): ideally we could ignore only some of the deprecated APIs
38+
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
39+
3740
template <typename T>
3841
constexpr size_t align_to(size_t size) {
3942
return (size + sizeof(T) - 1) & ~(sizeof(T) - 1);
@@ -1082,20 +1085,37 @@ int v8__String__Utf8Length(const v8::String& self, v8::Isolate* isolate) {
10821085
return self.Utf8LengthV2(isolate);
10831086
}
10841087

1085-
void v8__String__Write(const v8::String& self, v8::Isolate* isolate,
1086-
uint32_t offset, uint32_t length, uint16_t* buffer,
1087-
int flags) {
1088+
int v8__String__Write(const v8::String& self, v8::Isolate* isolate,
1089+
uint16_t* buffer, int start, int length, int options) {
1090+
return self.Write(isolate, buffer, start, length, options);
1091+
}
1092+
1093+
void v8__String__Write_v2(const v8::String& self, v8::Isolate* isolate,
1094+
uint32_t offset, uint32_t length, uint16_t* buffer,
1095+
int flags) {
10881096
return self.WriteV2(isolate, offset, length, buffer, flags);
10891097
}
10901098

1091-
void v8__String__WriteOneByte(const v8::String& self, v8::Isolate* isolate,
1092-
uint32_t offset, uint32_t length, uint8_t* buffer,
1093-
int flags) {
1099+
int v8__String__WriteOneByte(const v8::String& self, v8::Isolate* isolate,
1100+
uint8_t* buffer, int start, int length,
1101+
int options) {
1102+
return self.WriteOneByte(isolate, buffer, start, length, options);
1103+
}
1104+
1105+
void v8__String__WriteOneByte_v2(const v8::String& self, v8::Isolate* isolate,
1106+
uint32_t offset, uint32_t length,
1107+
uint8_t* buffer, int flags) {
10941108
return self.WriteOneByteV2(isolate, offset, length, buffer, flags);
10951109
}
10961110

1097-
size_t v8__String__WriteUtf8(const v8::String& self, v8::Isolate* isolate,
1098-
char* buffer, size_t capacity, int flags) {
1111+
int v8__String__WriteUtf8(const v8::String& self, v8::Isolate* isolate,
1112+
char* buffer, int length, int* nchars_ref,
1113+
int options) {
1114+
return self.WriteUtf8(isolate, buffer, length, nchars_ref, options);
1115+
}
1116+
1117+
size_t v8__String__WriteUtf8_v2(const v8::String& self, v8::Isolate* isolate,
1118+
char* buffer, size_t capacity, int flags) {
10991119
return self.WriteUtf8V2(isolate, buffer, capacity, flags);
11001120
}
11011121

src/string.rs

Lines changed: 176 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ extern "C" {
4545
fn v8__String__Utf8Length(this: *const String, isolate: *mut Isolate) -> int;
4646

4747
fn v8__String__Write(
48+
this: *const String,
49+
isolate: *mut Isolate,
50+
buffer: *mut u16,
51+
start: int,
52+
length: int,
53+
options: WriteOptions,
54+
) -> int;
55+
56+
fn v8__String__Write_v2(
4857
this: *const String,
4958
isolate: *mut Isolate,
5059
offset: u32,
@@ -54,6 +63,15 @@ extern "C" {
5463
);
5564

5665
fn v8__String__WriteOneByte(
66+
this: *const String,
67+
isolate: *mut Isolate,
68+
buffer: *mut u8,
69+
start: int,
70+
length: int,
71+
options: WriteOptions,
72+
) -> int;
73+
74+
fn v8__String__WriteOneByte_v2(
5775
this: *const String,
5876
isolate: *mut Isolate,
5977
offset: u32,
@@ -63,6 +81,15 @@ extern "C" {
6381
);
6482

6583
fn v8__String__WriteUtf8(
84+
this: *const String,
85+
isolate: *mut Isolate,
86+
buffer: *mut char,
87+
length: int,
88+
nchars_ref: *mut int,
89+
options: WriteOptions,
90+
) -> int;
91+
92+
fn v8__String__WriteUtf8_v2(
6693
this: *const String,
6794
isolate: *mut Isolate,
6895
buffer: *mut char,
@@ -325,6 +352,21 @@ pub enum NewStringType {
325352
Internalized,
326353
}
327354

355+
bitflags! {
356+
#[derive(Clone, Copy, Default)]
357+
#[repr(transparent)]
358+
pub struct WriteOptions: int {
359+
const NO_OPTIONS = 0;
360+
const HINT_MANY_WRITES_EXPECTED = 1;
361+
const NO_NULL_TERMINATION = 2;
362+
const PRESERVE_ONE_BYTE_NULL = 4;
363+
// Used by WriteUtf8 to replace orphan surrogate code units with the
364+
// unicode replacement character. Needs to be set to guarantee valid UTF-8
365+
// output.
366+
const REPLACE_INVALID_UTF8 = 8;
367+
}
368+
}
369+
328370
bitflags! {
329371
#[derive(Clone, Copy, Default)]
330372
#[repr(transparent)]
@@ -430,15 +472,38 @@ impl String {
430472
/// Writes the contents of the string to an external buffer, as 16-bit
431473
/// (UTF-16) character codes.
432474
#[inline(always)]
475+
#[deprecated = "Use `v8::String::write_v2` instead"]
433476
pub fn write(
477+
&self,
478+
scope: &mut Isolate,
479+
buffer: &mut [u16],
480+
start: usize,
481+
options: WriteOptions,
482+
) -> usize {
483+
unsafe {
484+
v8__String__Write(
485+
self,
486+
scope,
487+
buffer.as_mut_ptr(),
488+
start.try_into().unwrap_or(int::MAX),
489+
buffer.len().try_into().unwrap_or(int::MAX),
490+
options,
491+
) as usize
492+
}
493+
}
494+
495+
/// Writes the contents of the string to an external buffer, as 16-bit
496+
/// (UTF-16) character codes.
497+
#[inline(always)]
498+
pub fn write_v2(
434499
&self,
435500
scope: &mut Isolate,
436501
offset: u32,
437502
buffer: &mut [u16],
438503
flags: WriteFlags,
439504
) {
440505
unsafe {
441-
v8__String__Write(
506+
v8__String__Write_v2(
442507
self,
443508
scope,
444509
offset,
@@ -452,15 +517,38 @@ impl String {
452517
/// Writes the contents of the string to an external buffer, as one-byte
453518
/// (Latin-1) characters.
454519
#[inline(always)]
520+
#[deprecated = "Use `v8::String::write_one_byte_v2` instead."]
455521
pub fn write_one_byte(
522+
&self,
523+
scope: &mut Isolate,
524+
buffer: &mut [u8],
525+
start: usize,
526+
options: WriteOptions,
527+
) -> usize {
528+
unsafe {
529+
v8__String__WriteOneByte(
530+
self,
531+
scope,
532+
buffer.as_mut_ptr(),
533+
start.try_into().unwrap_or(int::MAX),
534+
buffer.len().try_into().unwrap_or(int::MAX),
535+
options,
536+
) as usize
537+
}
538+
}
539+
540+
/// Writes the contents of the string to an external buffer, as one-byte
541+
/// (Latin-1) characters.
542+
#[inline(always)]
543+
pub fn write_one_byte_v2(
456544
&self,
457545
scope: &mut Isolate,
458546
offset: u32,
459547
buffer: &mut [u8],
460548
flags: WriteFlags,
461549
) {
462550
unsafe {
463-
v8__String__WriteOneByte(
551+
v8__String__WriteOneByte_v2(
464552
self,
465553
scope,
466554
offset,
@@ -474,15 +562,38 @@ impl String {
474562
/// Writes the contents of the string to an external [`MaybeUninit`] buffer, as one-byte
475563
/// (Latin-1) characters.
476564
#[inline(always)]
565+
#[deprecated = "Use `v8::String::write_one_byte_uninit_v2` instead."]
477566
pub fn write_one_byte_uninit(
567+
&self,
568+
scope: &mut Isolate,
569+
buffer: &mut [MaybeUninit<u8>],
570+
start: usize,
571+
options: WriteOptions,
572+
) -> usize {
573+
unsafe {
574+
v8__String__WriteOneByte(
575+
self,
576+
scope,
577+
buffer.as_mut_ptr() as *mut u8,
578+
start.try_into().unwrap_or(int::MAX),
579+
buffer.len().try_into().unwrap_or(int::MAX),
580+
options,
581+
) as usize
582+
}
583+
}
584+
585+
/// Writes the contents of the string to an external [`MaybeUninit`] buffer, as one-byte
586+
/// (Latin-1) characters.
587+
#[inline(always)]
588+
pub fn write_one_byte_uninit_v2(
478589
&self,
479590
scope: &mut Isolate,
480591
offset: u32,
481592
buffer: &mut [MaybeUninit<u8>],
482593
flags: WriteFlags,
483594
) {
484595
unsafe {
485-
v8__String__WriteOneByte(
596+
v8__String__WriteOneByte_v2(
486597
self,
487598
scope,
488599
offset,
@@ -495,7 +606,31 @@ impl String {
495606

496607
/// Writes the contents of the string to an external buffer, as UTF-8.
497608
#[inline(always)]
609+
#[deprecated = "Use `v8::String::write_utf8_v2` instead."]
498610
pub fn write_utf8(
611+
&self,
612+
scope: &mut Isolate,
613+
buffer: &mut [u8],
614+
nchars_ref: Option<&mut usize>,
615+
options: WriteOptions,
616+
) -> usize {
617+
unsafe {
618+
// SAFETY:
619+
// We assume that v8 will overwrite the buffer without de-initializing any byte in it.
620+
// So the type casting of the buffer is safe.
621+
let buffer = {
622+
let len = buffer.len();
623+
let data = buffer.as_mut_ptr().cast();
624+
slice::from_raw_parts_mut(data, len)
625+
};
626+
#[allow(deprecated)]
627+
self.write_utf8_uninit(scope, buffer, nchars_ref, options)
628+
}
629+
}
630+
631+
/// Writes the contents of the string to an external buffer, as UTF-8.
632+
#[inline(always)]
633+
pub fn write_utf8_v2(
499634
&self,
500635
scope: &mut Isolate,
501636
buffer: &mut [u8],
@@ -511,19 +646,45 @@ impl String {
511646
let data = buffer.as_mut_ptr().cast();
512647
slice::from_raw_parts_mut(data, len)
513648
};
514-
self.write_utf8_uninit(scope, buffer, flags)
649+
self.write_utf8_uninit_v2(scope, buffer, flags)
515650
}
516651
}
517652

518653
/// Writes the contents of the string to an external [`MaybeUninit`] buffer, as UTF-8.
654+
#[deprecated = "Use `v8::String::write_utf8_uninit_v2` instead."]
519655
pub fn write_utf8_uninit(
520656
&self,
521657
scope: &mut Isolate,
522658
buffer: &mut [MaybeUninit<u8>],
523-
flags: WriteFlags,
659+
nchars_ref: Option<&mut usize>,
660+
options: WriteOptions,
524661
) -> usize {
662+
let mut nchars_ref_int: int = 0;
525663
let bytes = unsafe {
526664
v8__String__WriteUtf8(
665+
self,
666+
scope,
667+
buffer.as_mut_ptr() as *mut char,
668+
buffer.len().try_into().unwrap_or(int::MAX),
669+
&mut nchars_ref_int,
670+
options,
671+
)
672+
};
673+
if let Some(r) = nchars_ref {
674+
*r = nchars_ref_int as usize;
675+
}
676+
bytes as usize
677+
}
678+
679+
/// Writes the contents of the string to an external [`MaybeUninit`] buffer, as UTF-8.
680+
pub fn write_utf8_uninit_v2(
681+
&self,
682+
scope: &mut Isolate,
683+
buffer: &mut [MaybeUninit<u8>],
684+
flags: WriteFlags,
685+
) -> usize {
686+
let bytes = unsafe {
687+
v8__String__WriteUtf8_v2(
527688
self,
528689
scope,
529690
buffer.as_mut_ptr() as _,
@@ -791,7 +952,7 @@ impl String {
791952
let buffer = std::ptr::slice_from_raw_parts_mut(data, len_utf16);
792953

793954
// Write to this MaybeUninit buffer, assuming we're going to fill this entire buffer
794-
self.write_one_byte_uninit(
955+
self.write_one_byte_uninit_v2(
795956
scope,
796957
0,
797958
&mut *buffer,
@@ -816,7 +977,7 @@ impl String {
816977
let buffer = std::ptr::slice_from_raw_parts_mut(data, len_utf8);
817978

818979
// Write to this MaybeUninit buffer, assuming we're going to fill this entire buffer
819-
let length = self.write_utf8_uninit(
980+
let length = self.write_utf8_uninit_v2(
820981
scope,
821982
&mut *buffer,
822983
WriteFlags::kReplaceInvalidUtf8,
@@ -850,7 +1011,7 @@ impl String {
8501011
// string is 100% 7-bit ASCII.
8511012
if self.is_onebyte() && len_utf8 == len_utf16 {
8521013
if len_utf16 <= N {
853-
self.write_one_byte_uninit(scope, 0, buffer, WriteFlags::empty());
1014+
self.write_one_byte_uninit_v2(scope, 0, buffer, WriteFlags::empty());
8541015
unsafe {
8551016
// Get a slice of &[u8] of what we know is initialized now
8561017
let buffer = &mut buffer[..len_utf16];
@@ -869,7 +1030,7 @@ impl String {
8691030
let buffer = std::ptr::slice_from_raw_parts_mut(data, len_utf16);
8701031

8711032
// Write to this MaybeUninit buffer, assuming we're going to fill this entire buffer
872-
self.write_one_byte_uninit(
1033+
self.write_one_byte_uninit_v2(
8731034
scope,
8741035
0,
8751036
&mut *buffer,
@@ -886,8 +1047,11 @@ impl String {
8861047

8871048
if len_utf8 <= N {
8881049
// No malloc path
889-
let length =
890-
self.write_utf8_uninit(scope, buffer, WriteFlags::kReplaceInvalidUtf8);
1050+
let length = self.write_utf8_uninit_v2(
1051+
scope,
1052+
buffer,
1053+
WriteFlags::kReplaceInvalidUtf8,
1054+
);
8911055
debug_assert!(length == len_utf8);
8921056

8931057
// SAFETY: We know that we wrote `length` UTF-8 bytes. See `slice_assume_init_mut` for additional guarantee information.
@@ -911,7 +1075,7 @@ impl String {
9111075
let buffer = std::ptr::slice_from_raw_parts_mut(data, len_utf8);
9121076

9131077
// Write to this MaybeUninit buffer, assuming we're going to fill this entire buffer
914-
let length = self.write_utf8_uninit(
1078+
let length = self.write_utf8_uninit_v2(
9151079
scope,
9161080
&mut *buffer,
9171081
WriteFlags::kReplaceInvalidUtf8,

0 commit comments

Comments
 (0)