Skip to content

Commit b3d793e

Browse files
jeisingerCommit bot
authored andcommitted
[api] Introduce ReturnValue::Get
This is a convenience API that an embedder can use to do final checks on the return value. Note that this creates a new handle and thus defeats the performance optimization done for ReturnValue - an embedder should only use this in non-performance critical code paths. BUG= [email protected] Review URL: https://codereview.chromium.org/1875263003 Cr-Commit-Position: refs/heads/master@{#35409}
1 parent 0f0d349 commit b3d793e

3 files changed

Lines changed: 28 additions & 5 deletions

File tree

include/v8.h

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@ class Local {
330330
template <class F1, class F2, class F3>
331331
friend class PersistentValueMapBase;
332332
template<class F1, class F2> friend class PersistentValueVector;
333+
template <class F>
334+
friend class ReturnValue;
333335

334336
explicit V8_INLINE Local(T* that) : val_(that) {}
335337
V8_INLINE static Local<T> New(Isolate* isolate, T* that);
@@ -3127,12 +3129,17 @@ class ReturnValue {
31273129
V8_INLINE void SetUndefined();
31283130
V8_INLINE void SetEmptyString();
31293131
// Convenience getter for Isolate
3130-
V8_INLINE Isolate* GetIsolate();
3132+
V8_INLINE Isolate* GetIsolate() const;
31313133

31323134
// Pointer setter: Uncompilable to prevent inadvertent misuse.
31333135
template <typename S>
31343136
V8_INLINE void Set(S* whatever);
31353137

3138+
// Getter. Creates a new Local<> so it comes with a certain performance
3139+
// hit. If the ReturnValue was not yet set, this will return the undefined
3140+
// value.
3141+
V8_INLINE Local<Value> Get() const;
3142+
31363143
private:
31373144
template<class F> friend class ReturnValue;
31383145
template<class F> friend class FunctionCallbackInfo;
@@ -7334,6 +7341,7 @@ class Internals {
73347341
kAmountOfExternalAllocatedMemoryAtLastGlobalGCOffset + kApiInt64Size +
73357342
kApiPointerSize;
73367343
static const int kUndefinedValueRootIndex = 4;
7344+
static const int kTheHoleValueRootIndex = 5;
73377345
static const int kNullValueRootIndex = 6;
73387346
static const int kTrueValueRootIndex = 7;
73397347
static const int kFalseValueRootIndex = 8;
@@ -7813,14 +7821,22 @@ void ReturnValue<T>::SetEmptyString() {
78137821
*value_ = *I::GetRoot(GetIsolate(), I::kEmptyStringRootIndex);
78147822
}
78157823

7816-
template<typename T>
7817-
Isolate* ReturnValue<T>::GetIsolate() {
7824+
template <typename T>
7825+
Isolate* ReturnValue<T>::GetIsolate() const {
78187826
// Isolate is always the pointer below the default value on the stack.
78197827
return *reinterpret_cast<Isolate**>(&value_[-2]);
78207828
}
78217829

7822-
template<typename T>
7823-
template<typename S>
7830+
template <typename T>
7831+
Local<Value> ReturnValue<T>::Get() const {
7832+
typedef internal::Internals I;
7833+
if (*value_ == *I::GetRoot(GetIsolate(), I::kTheHoleValueRootIndex))
7834+
return Local<Value>(*Undefined(GetIsolate()));
7835+
return Local<Value>::New(GetIsolate(), reinterpret_cast<Value*>(value_));
7836+
}
7837+
7838+
template <typename T>
7839+
template <typename S>
78247840
void ReturnValue<T>::Set(S* whatever) {
78257841
// Uncompilable to prevent inadvertent misuse.
78267842
TYPE_CHECK(S*, Primitive);

src/heap/heap.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@ class Heap {
549549

550550
STATIC_ASSERT(kUndefinedValueRootIndex ==
551551
Internals::kUndefinedValueRootIndex);
552+
STATIC_ASSERT(kTheHoleValueRootIndex == Internals::kTheHoleValueRootIndex);
552553
STATIC_ASSERT(kNullValueRootIndex == Internals::kNullValueRootIndex);
553554
STATIC_ASSERT(kTrueValueRootIndex == Internals::kTrueValueRootIndex);
554555
STATIC_ASSERT(kFalseValueRootIndex == Internals::kFalseValueRootIndex);

test/cctest/test-api.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ static void CheckReturnValue(const T& t, i::Address callback) {
1818
CHECK((*o)->IsTheHole() || (*o)->IsUndefined());
1919
// Verify reset
2020
bool is_runtime = (*o)->IsTheHole();
21+
if (is_runtime) {
22+
CHECK(rv.Get()->IsUndefined());
23+
} else {
24+
i::Handle<i::Object> v = v8::Utils::OpenHandle(*rv.Get());
25+
CHECK_EQ(*v, *o);
26+
}
2127
rv.Set(true);
2228
CHECK(!(*o)->IsTheHole() && !(*o)->IsUndefined());
2329
rv.Set(v8::Local<v8::Object>());

0 commit comments

Comments
 (0)