Skip to content

Commit 4acdb5e

Browse files
jeremyromanCommit bot
authored andcommitted
Give v8::Eternal a direct reference to the handle.
This makes it more similar to other handle types (like PersistentBase), by simply storing an i::Object** cast to T*. This means that it is not necessary to look up the handle in the eternal handles table to access the underlying value. Like the built-in roots (null, etc.), an eternal handle can never be destroyed, so we don't even need to allocate a separate local handle. Instead, the Local<T> can point directly at the eternal reference. This makes Eternal<T>::Get trivial. Review-Url: https://codereview.chromium.org/2751263003 Cr-Commit-Position: refs/heads/master@{#43912}
1 parent 7bd0c1d commit 4acdb5e

2 files changed

Lines changed: 16 additions & 21 deletions

File tree

include/v8.h

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -360,19 +360,18 @@ class MaybeLocal {
360360
// Eternal handles are set-once handles that live for the life of the isolate.
361361
template <class T> class Eternal {
362362
public:
363-
V8_INLINE Eternal() : index_(kInitialValue) { }
364-
template<class S>
365-
V8_INLINE Eternal(Isolate* isolate, Local<S> handle) : index_(kInitialValue) {
363+
V8_INLINE Eternal() : val_(nullptr) {}
364+
template <class S>
365+
V8_INLINE Eternal(Isolate* isolate, Local<S> handle) : val_(nullptr) {
366366
Set(isolate, handle);
367367
}
368368
// Can only be safely called if already set.
369369
V8_INLINE Local<T> Get(Isolate* isolate) const;
370-
V8_INLINE bool IsEmpty() const { return index_ == kInitialValue; }
370+
V8_INLINE bool IsEmpty() const { return val_ == nullptr; }
371371
template<class S> V8_INLINE void Set(Isolate* isolate, Local<S> handle);
372372

373373
private:
374-
static const int kInitialValue = -1;
375-
int index_;
374+
T* val_;
376375
};
377376

378377

@@ -7673,10 +7672,7 @@ class V8_EXPORT V8 {
76737672
WeakCallbackInfo<void>::Callback weak_callback);
76747673
static void MakeWeak(internal::Object*** location_addr);
76757674
static void* ClearWeak(internal::Object** location);
7676-
static void Eternalize(Isolate* isolate,
7677-
Value* handle,
7678-
int* index);
7679-
static Local<Value> GetEternal(Isolate* isolate, int index);
7675+
static Value* Eternalize(Isolate* isolate, Value* handle);
76807676

76817677
static void RegisterExternallyReferencedObject(internal::Object** object,
76827678
internal::Isolate* isolate);
@@ -8641,12 +8637,15 @@ template<class T>
86418637
template<class S>
86428638
void Eternal<T>::Set(Isolate* isolate, Local<S> handle) {
86438639
TYPE_CHECK(T, S);
8644-
V8::Eternalize(isolate, reinterpret_cast<Value*>(*handle), &this->index_);
8640+
val_ = reinterpret_cast<T*>(
8641+
V8::Eternalize(isolate, reinterpret_cast<Value*>(*handle)));
86458642
}
86468643

86478644
template <class T>
86488645
Local<T> Eternal<T>::Get(Isolate* isolate) const {
8649-
return Local<T>(reinterpret_cast<T*>(*V8::GetEternal(isolate, index_)));
8646+
// The eternal handle will never go away, so as with the roots, we don't even
8647+
// need to open a handle.
8648+
return Local<T>(val_);
86508649
}
86518650

86528651

src/api.cc

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -948,17 +948,13 @@ void V8::DisposeGlobal(i::Object** location) {
948948
i::GlobalHandles::Destroy(location);
949949
}
950950

951-
952-
void V8::Eternalize(Isolate* v8_isolate, Value* value, int* index) {
951+
Value* V8::Eternalize(Isolate* v8_isolate, Value* value) {
953952
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
954953
i::Object* object = *Utils::OpenHandle(value);
955-
isolate->eternal_handles()->Create(isolate, object, index);
956-
}
957-
958-
959-
Local<Value> V8::GetEternal(Isolate* v8_isolate, int index) {
960-
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
961-
return Utils::ToLocal(isolate->eternal_handles()->Get(index));
954+
int index = -1;
955+
isolate->eternal_handles()->Create(isolate, object, &index);
956+
return reinterpret_cast<Value*>(
957+
isolate->eternal_handles()->Get(index).location());
962958
}
963959

964960

0 commit comments

Comments
 (0)