Skip to content

Commit 5453151

Browse files
authored
Remove use of internal V8 apis (#1710)
Clean up use of v8 internals
1 parent e343a38 commit 5453151

File tree

7 files changed

+72
-276
lines changed

7 files changed

+72
-276
lines changed

src/array_buffer.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ unsafe extern "C" {
5959
deleter: BackingStoreDeleterCallback,
6060
deleter_data: *mut c_void,
6161
) -> *mut BackingStore;
62-
fn v8__BackingStore__EmptyBackingStore(shared: bool) -> *mut BackingStore;
6362

6463
fn v8__BackingStore__Data(this: *const BackingStore) -> *mut c_void;
6564
fn v8__BackingStore__ByteLength(this: *const BackingStore) -> usize;
@@ -450,16 +449,6 @@ impl ArrayBuffer {
450449
.unwrap()
451450
}
452451

453-
/// Create a new, empty ArrayBuffer.
454-
#[inline(always)]
455-
pub fn empty<'s>(scope: &mut HandleScope<'s>) -> Local<'s, ArrayBuffer> {
456-
// SAFETY: This is a v8-provided empty backing store
457-
let backing_store = unsafe {
458-
UniqueRef::from_raw(v8__BackingStore__EmptyBackingStore(false))
459-
};
460-
Self::with_backing_store(scope, &backing_store.make_shared())
461-
}
462-
463452
/// Data length in bytes.
464453
#[inline(always)]
465454
pub fn byte_length(&self) -> usize {
@@ -593,11 +582,6 @@ impl ArrayBuffer {
593582
T: sealed::Rawable,
594583
{
595584
let len = bytes.byte_len();
596-
if len == 0 {
597-
return unsafe {
598-
UniqueRef::from_raw(v8__BackingStore__EmptyBackingStore(false))
599-
};
600-
}
601585

602586
let (ptr, slice) = T::into_raw(bytes);
603587

src/binding.cc

Lines changed: 41 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,24 @@
44
#include <cstddef>
55
#include <cstdint>
66
#include <cstdio>
7-
#include <iostream>
8-
#include <memory>
7+
#include <thread>
98

109
#include "cppgc/allocation.h"
10+
#include "cppgc/persistent.h"
1111
#include "cppgc/platform.h"
12+
#include "libplatform/libplatform.h"
1213
#include "support.h"
1314
#include "unicode/locid.h"
1415
#include "v8-callbacks.h"
15-
#include "v8/include/cppgc/persistent.h"
16-
#include "v8/include/libplatform/libplatform.h"
17-
#include "v8/include/v8-cppgc.h"
18-
#include "v8/include/v8-fast-api-calls.h"
19-
#include "v8/include/v8-inspector.h"
20-
#include "v8/include/v8-internal.h"
21-
#include "v8/include/v8-platform.h"
22-
#include "v8/include/v8-profiler.h"
23-
#include "v8/include/v8.h"
24-
#include "v8/src/api/api-inl.h"
25-
#include "v8/src/api/api.h"
26-
#include "v8/src/base/debug/stack_trace.h"
27-
#include "v8/src/base/sys-info.h"
28-
#include "v8/src/execution/isolate-utils-inl.h"
29-
#include "v8/src/execution/isolate-utils.h"
16+
#include "v8-cppgc.h"
17+
#include "v8-fast-api-calls.h"
18+
#include "v8-inspector.h"
19+
#include "v8-internal.h"
20+
#include "v8-platform.h"
21+
#include "v8-profiler.h"
22+
#include "v8.h"
3023
#include "v8/src/flags/flags.h"
3124
#include "v8/src/libplatform/default-platform.h"
32-
#include "v8/src/objects/objects-inl.h"
33-
#include "v8/src/objects/objects.h"
34-
#include "v8/src/objects/smi.h"
3525

3626
using namespace support;
3727

@@ -125,9 +115,6 @@ static_assert(sizeof(v8::Isolate::DisallowJavascriptExecutionScope) == 12,
125115
#endif
126116

127117
extern "C" {
128-
const extern int v8__internal__Internals__kIsolateEmbedderDataOffset =
129-
v8::internal::Internals::kIsolateEmbedderDataOffset;
130-
131118
void v8__V8__SetFlagsFromCommandLine(int* argc, char** argv,
132119
const char* usage) {
133120
namespace i = v8::internal;
@@ -204,6 +191,14 @@ uint32_t v8__Isolate__GetNumberOfDataSlots(v8::Isolate* isolate) {
204191
return isolate->GetNumberOfDataSlots();
205192
}
206193

194+
void* v8__Isolate__GetData(v8::Isolate* isolate, uint32_t slot) {
195+
return isolate->GetData(slot);
196+
}
197+
198+
void v8__Isolate__SetData(v8::Isolate* isolate, uint32_t slot, void* data) {
199+
isolate->SetData(slot, data);
200+
}
201+
207202
const v8::Data* v8__Isolate__GetDataFromSnapshotOnce(v8::Isolate* isolate,
208203
size_t index) {
209204
return maybe_local_to_ptr(isolate->GetDataFromSnapshotOnce<v8::Data>(index));
@@ -577,17 +572,17 @@ bool v8__Data__EQ(const v8::Data& self, const v8::Data& other) {
577572
}
578573

579574
bool v8__Data__IsBigInt(const v8::Data& self) {
580-
return IsBigInt(*v8::Utils::OpenHandle(&self));
575+
return self.IsValue() && reinterpret_cast<const v8::Value&>(self).IsBigInt();
581576
}
582577

583578
bool v8__Data__IsBoolean(const v8::Data& self) {
584-
return IsBoolean(*v8::Utils::OpenHandle(&self));
579+
return self.IsValue() && reinterpret_cast<const v8::Value&>(self).IsBoolean();
585580
}
586581

587582
bool v8__Data__IsContext(const v8::Data& self) { return self.IsContext(); }
588583

589584
bool v8__Data__IsFixedArray(const v8::Data& self) {
590-
return IsFixedArray(*v8::Utils::OpenHandle(&self));
585+
return self.IsFixedArray();
591586
}
592587

593588
bool v8__Data__IsFunctionTemplate(const v8::Data& self) {
@@ -597,33 +592,34 @@ bool v8__Data__IsFunctionTemplate(const v8::Data& self) {
597592
bool v8__Data__IsModule(const v8::Data& self) { return self.IsModule(); }
598593

599594
bool v8__Data__IsModuleRequest(const v8::Data& self) {
600-
return IsModuleRequest(*v8::Utils::OpenHandle(&self));
595+
return self.IsModuleRequest();
601596
}
602597

603598
bool v8__Data__IsName(const v8::Data& self) {
604-
return IsName(*v8::Utils::OpenHandle(&self));
599+
return self.IsValue() && reinterpret_cast<const v8::Value&>(self).IsName();
605600
}
606601

607602
bool v8__Data__IsNumber(const v8::Data& self) {
608-
return IsNumber(*v8::Utils::OpenHandle(&self));
603+
return self.IsValue() && reinterpret_cast<const v8::Value&>(self).IsNumber();
609604
}
610605

611606
bool v8__Data__IsObjectTemplate(const v8::Data& self) {
612607
return self.IsObjectTemplate();
613608
}
614609

615610
bool v8__Data__IsPrimitive(const v8::Data& self) {
616-
return IsPrimitive(*v8::Utils::OpenHandle(&self)) && !self.IsPrivate();
611+
return self.IsValue() &&
612+
reinterpret_cast<const v8::Value&>(self).IsPrimitive();
617613
}
618614

619615
bool v8__Data__IsPrivate(const v8::Data& self) { return self.IsPrivate(); }
620616

621617
bool v8__Data__IsString(const v8::Data& self) {
622-
return IsString(*v8::Utils::OpenHandle(&self));
618+
return self.IsValue() && reinterpret_cast<const v8::Value&>(self).IsString();
623619
}
624620

625621
bool v8__Data__IsSymbol(const v8::Data& self) {
626-
return IsPublicSymbol(*v8::Utils::OpenHandle(&self));
622+
return self.IsValue() && reinterpret_cast<const v8::Value&>(self).IsSymbol();
627623
}
628624

629625
bool v8__Data__IsValue(const v8::Data& self) { return self.IsValue(); }
@@ -896,6 +892,8 @@ const v8::String* v8__Value__TypeOf(v8::Value& self, v8::Isolate* isolate) {
896892
return local_to_ptr(self.TypeOf(isolate));
897893
}
898894

895+
uint32_t v8__Value__GetHash(v8::Value& self) { return self.GetHash(); }
896+
899897
const v8::Primitive* v8__Null(v8::Isolate* isolate) {
900898
return local_to_ptr(v8::Null(isolate));
901899
}
@@ -954,12 +952,6 @@ two_pointers_t v8__ArrayBuffer__GetBackingStore(const v8::ArrayBuffer& self) {
954952
return make_pod<two_pointers_t>(ptr_to_local(&self)->GetBackingStore());
955953
}
956954

957-
v8::BackingStore* v8__BackingStore__EmptyBackingStore(bool shared) {
958-
std::unique_ptr<i::BackingStoreBase> u = i::BackingStore::EmptyBackingStore(
959-
shared ? i::SharedFlag::kShared : i::SharedFlag::kNotShared);
960-
return static_cast<v8::BackingStore*>(u.release());
961-
}
962-
963955
bool v8__BackingStore__IsResizableByUserJavaScript(
964956
const v8::BackingStore& self) {
965957
return ptr_to_local(&self)->IsResizableByUserJavaScript();
@@ -2361,10 +2353,10 @@ const v8::Object* v8__PropertyCallbackInfo__Holder(
23612353
return local_to_ptr(self.HolderV2());
23622354
}
23632355

2364-
v8::internal::Address* v8__PropertyCallbackInfo__GetReturnValue(
2356+
uintptr_t* v8__PropertyCallbackInfo__GetReturnValue(
23652357
const v8::PropertyCallbackInfo<v8::Value>& self) {
23662358
v8::ReturnValue<v8::Value> rv = self.GetReturnValue();
2367-
return *reinterpret_cast<v8::internal::Address**>(&rv);
2359+
return *reinterpret_cast<uintptr_t**>(&rv);
23682360
}
23692361

23702362
bool v8__PropertyCallbackInfo__ShouldThrowOnError(
@@ -2877,8 +2869,6 @@ class UnprotectedDefaultPlatform : public v8::platform::DefaultPlatform {
28772869
using PriorityMode = v8::platform::PriorityMode;
28782870
using TracingController = v8::TracingController;
28792871

2880-
static constexpr int kMaxThreadPoolSize = 16;
2881-
28822872
public:
28832873
explicit UnprotectedDefaultPlatform(
28842874
int thread_pool_size, IdleTaskSupport idle_task_support,
@@ -2888,28 +2878,8 @@ class UnprotectedDefaultPlatform : public v8::platform::DefaultPlatform {
28882878
std::move(tracing_controller),
28892879
priority_mode) {}
28902880

2891-
static std::unique_ptr<v8::Platform> New(
2892-
int thread_pool_size, IdleTaskSupport idle_task_support,
2893-
InProcessStackDumping in_process_stack_dumping,
2894-
std::unique_ptr<TracingController> tracing_controller = {},
2895-
PriorityMode priority_mode = PriorityMode::kDontApply) {
2896-
// This implementation is semantically equivalent to the implementation of
2897-
// `v8::platform::NewDefaultPlatform()`.
2898-
DCHECK_GE(thread_pool_size, 0);
2899-
if (thread_pool_size < 1) {
2900-
thread_pool_size =
2901-
std::max(v8::base::SysInfo::NumberOfProcessors() - 1, 1);
2902-
}
2903-
thread_pool_size = std::min(thread_pool_size, kMaxThreadPoolSize);
2904-
if (in_process_stack_dumping == InProcessStackDumping::kEnabled) {
2905-
v8::base::debug::EnableInProcessStackDumping();
2906-
}
2907-
return std::make_unique<UnprotectedDefaultPlatform>(
2908-
thread_pool_size, idle_task_support, std::move(tracing_controller),
2909-
priority_mode);
2910-
}
2911-
29122881
v8::ThreadIsolatedAllocator* GetThreadIsolatedAllocator() override {
2882+
// DefaultThreadIsolatedAllocator is PKU protected
29132883
return nullptr;
29142884
}
29152885
};
@@ -2926,11 +2896,14 @@ v8::Platform* v8__Platform__NewDefaultPlatform(int thread_pool_size,
29262896

29272897
v8::Platform* v8__Platform__NewUnprotectedDefaultPlatform(
29282898
int thread_pool_size, bool idle_task_support) {
2929-
return UnprotectedDefaultPlatform::New(
2930-
thread_pool_size,
2931-
idle_task_support ? v8::platform::IdleTaskSupport::kEnabled
2932-
: v8::platform::IdleTaskSupport::kDisabled,
2933-
v8::platform::InProcessStackDumping::kDisabled, nullptr)
2899+
if (thread_pool_size < 1) {
2900+
thread_pool_size = std::thread::hardware_concurrency();
2901+
}
2902+
thread_pool_size = std::max(std::min(thread_pool_size, 16), 1);
2903+
return std::make_unique<UnprotectedDefaultPlatform>(
2904+
thread_pool_size, idle_task_support
2905+
? v8::platform::IdleTaskSupport::kEnabled
2906+
: v8::platform::IdleTaskSupport::kDisabled)
29342907
.release();
29352908
}
29362909

@@ -3422,28 +3395,6 @@ void v8__HeapProfiler__TakeHeapSnapshot(v8::Isolate* isolate,
34223395
const_cast<v8::HeapSnapshot*>(snapshot)->Delete();
34233396
}
34243397

3425-
v8::Isolate* v8__internal__GetIsolateFromHeapObject(const v8::Data& data) {
3426-
namespace i = v8::internal;
3427-
i::Tagged<i::Object> object(reinterpret_cast<const i::Address&>(data));
3428-
i::Isolate* isolate;
3429-
return IsHeapObject(object) &&
3430-
i::GetIsolateFromHeapObject(object.GetHeapObject(), &isolate)
3431-
? reinterpret_cast<v8::Isolate*>(isolate)
3432-
: nullptr;
3433-
}
3434-
3435-
int v8__Value__GetHash(const v8::Value& data) {
3436-
namespace i = v8::internal;
3437-
i::Tagged<i::Object> object(reinterpret_cast<const i::Address&>(data));
3438-
i::Isolate* isolate;
3439-
int hash = IsHeapObject(object) && i::GetIsolateFromHeapObject(
3440-
object.GetHeapObject(), &isolate)
3441-
? i::Object::GetOrCreateHash(object, isolate).value()
3442-
: i::Smi::ToInt(i::Object::GetHash(object));
3443-
assert(hash != 0);
3444-
return hash;
3445-
}
3446-
34473398
void v8__HeapStatistics__CONSTRUCT(uninit_t<v8::HeapStatistics>* buf) {
34483399
// Should be <= than its counterpart in src/isolate.rs
34493400
static_assert(sizeof(v8::HeapStatistics) <= sizeof(uintptr_t[16]),

src/isolate.rs

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -588,11 +588,15 @@ pub type UseCounterCallback =
588588
unsafe extern "C" fn(&mut Isolate, UseCounterFeature);
589589

590590
unsafe extern "C" {
591-
static v8__internal__Internals__kIsolateEmbedderDataOffset: int;
592-
593591
fn v8__Isolate__New(params: *const raw::CreateParams) -> *mut Isolate;
594592
fn v8__Isolate__Dispose(this: *mut Isolate);
595593
fn v8__Isolate__GetNumberOfDataSlots(this: *const Isolate) -> u32;
594+
fn v8__Isolate__GetData(isolate: *const Isolate, slot: u32) -> *mut c_void;
595+
fn v8__Isolate__SetData(
596+
isolate: *const Isolate,
597+
slot: u32,
598+
data: *mut c_void,
599+
);
596600
fn v8__Isolate__Enter(this: *mut Isolate);
597601
fn v8__Isolate__Exit(this: *mut Isolate);
598602
fn v8__Isolate__GetCurrent() -> *mut Isolate;
@@ -782,29 +786,17 @@ unsafe extern "C" {
782786
pub struct Isolate(Opaque);
783787

784788
impl Isolate {
785-
// Total number of isolate data slots provided by V8.
786-
const EMBEDDER_DATA_SLOT_COUNT: u32 = 4;
787-
788-
// Byte offset inside `Isolate` where the isolate data slots are stored. This
789-
// should be the same as the value of `kIsolateEmbedderDataOffset` which is
790-
// defined in `v8-internal.h`.
791-
const EMBEDDER_DATA_OFFSET: usize = size_of::<[*const (); 73]>();
792-
793789
// Isolate data slots used internally by rusty_v8.
794790
const ANNEX_SLOT: u32 = 0;
795791
const CURRENT_SCOPE_DATA_SLOT: u32 = 1;
796792
const INTERNAL_DATA_SLOT_COUNT: u32 = 2;
797793

798794
#[inline(always)]
799795
fn assert_embedder_data_slot_count_and_offset_correct(&self) {
800-
assert_eq!(
801-
unsafe { v8__Isolate__GetNumberOfDataSlots(self) },
802-
Self::EMBEDDER_DATA_SLOT_COUNT
803-
);
804-
assert_eq!(
805-
unsafe { v8__internal__Internals__kIsolateEmbedderDataOffset } as usize,
806-
Self::EMBEDDER_DATA_OFFSET
807-
);
796+
assert!(
797+
unsafe { v8__Isolate__GetNumberOfDataSlots(self) }
798+
>= Self::INTERNAL_DATA_SLOT_COUNT
799+
)
808800
}
809801

810802
fn new_impl(params: CreateParams) -> *mut Isolate {
@@ -977,29 +969,18 @@ impl Isolate {
977969
/// Returns the maximum number of available embedder data slots. Valid slots
978970
/// are in the range of `0 <= n < Isolate::get_number_of_data_slots()`.
979971
pub fn get_number_of_data_slots(&self) -> u32 {
980-
Self::EMBEDDER_DATA_SLOT_COUNT - Self::INTERNAL_DATA_SLOT_COUNT
972+
let n = unsafe { v8__Isolate__GetNumberOfDataSlots(self) };
973+
n - Self::INTERNAL_DATA_SLOT_COUNT
981974
}
982975

983976
#[inline(always)]
984977
pub(crate) fn get_data_internal(&self, slot: u32) -> *mut c_void {
985-
let slots = unsafe {
986-
let p = self as *const Self as *const u8;
987-
let p = p.add(Self::EMBEDDER_DATA_OFFSET);
988-
let p = p as *const [*mut c_void; Self::EMBEDDER_DATA_SLOT_COUNT as _];
989-
&*p
990-
};
991-
slots[slot as usize]
978+
unsafe { v8__Isolate__GetData(self, slot) }
992979
}
993980

994981
#[inline(always)]
995982
pub(crate) fn set_data_internal(&mut self, slot: u32, data: *mut c_void) {
996-
let slots = unsafe {
997-
let p = self as *mut Self as *mut u8;
998-
let p = p.add(Self::EMBEDDER_DATA_OFFSET);
999-
let p = p as *mut [*mut c_void; Self::EMBEDDER_DATA_SLOT_COUNT as _];
1000-
&mut *p
1001-
};
1002-
slots[slot as usize] = data;
983+
unsafe { v8__Isolate__SetData(self, slot, data) }
1003984
}
1004985

1005986
pub(crate) fn init_scope_root(&mut self) {

0 commit comments

Comments
 (0)