Add String::get_external_string_resource_base and get_external_string_resource (#1396)

This commit is contained in:
Divy Srivastava 2024-02-13 19:48:34 +05:30 committed by GitHub
parent eb48d037c9
commit e04dc7baf4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 57 additions and 0 deletions

3
.gn
View File

@ -70,6 +70,9 @@ default_args = {
# fails to compile.
v8_enable_maglev = false
# Enable V8 object print for debugging.
# v8_enable_object_print = true
# Enable Deno-specific extra bindings
deno_enable_extras = true
}

View File

@ -1033,6 +1033,16 @@ int v8__String__WriteUtf8(const v8::String& self, v8::Isolate* isolate,
return self.WriteUtf8(isolate, buffer, length, nchars_ref, options);
}
const v8::String::ExternalStringResource* v8__String__GetExternalStringResource(
const v8::String& self) {
return self.GetExternalStringResource();
}
const v8::String::ExternalStringResourceBase* v8__String__GetExternalStringResourceBase(
const v8::String& self, v8::String::Encoding* encoding_out) {
return self.GetExternalStringResourceBase(encoding_out);
}
class ExternalStaticOneByteStringResource
: public v8::String::ExternalOneByteStringResource {
public:

View File

@ -2,11 +2,13 @@ use std::borrow::Cow;
use std::convert::TryInto;
use std::default::Default;
use std::mem::MaybeUninit;
use std::ptr::NonNull;
use std::slice;
use crate::support::char;
use crate::support::int;
use crate::support::size_t;
use crate::support::Opaque;
use crate::HandleScope;
use crate::Isolate;
use crate::Local;
@ -69,6 +71,14 @@ extern "C" {
options: WriteOptions,
) -> int;
fn v8__String__GetExternalStringResource(
this: *const String,
) -> *mut ExternalStringResource;
fn v8__String__GetExternalStringResourceBase(
this: *const String,
encoding: *mut Encoding,
) -> *mut ExternalOneByteStringResourceBase;
fn v8__String__NewExternalOneByte(
isolate: *mut Isolate,
onebyte_const: *const OneByteConst,
@ -95,6 +105,19 @@ extern "C" {
fn v8__String__ContainsOnlyOneByte(this: *const String) -> bool;
}
#[repr(C)]
pub enum Encoding {
Unknown = 0,
OneByte = 1,
TwoByte = 2,
}
#[repr(C)]
pub struct ExternalStringResource(Opaque);
#[repr(C)]
pub struct ExternalOneByteStringResourceBase(Opaque);
#[repr(C)]
#[derive(Debug)]
pub struct OneByteConst {
@ -500,6 +523,27 @@ impl String {
}
}
// Get the ExternalStringResource for an external string.
//
// Returns None if is_external() doesn't return true.
pub fn get_external_string_resource(
&self,
) -> Option<NonNull<ExternalStringResource>> {
NonNull::new(unsafe { v8__String__GetExternalStringResource(self) })
}
pub fn get_external_string_resource_base(
&self,
) -> (Option<NonNull<ExternalOneByteStringResourceBase>>, Encoding) {
let mut encoding = Encoding::Unknown;
(
NonNull::new(unsafe {
v8__String__GetExternalStringResourceBase(self, &mut encoding)
}),
encoding,
)
}
/// True if string is external
#[inline(always)]
pub fn is_external(&self) -> bool {