Skip to content

Commit

Permalink
src: add incr/decr operators for Reference
Browse files Browse the repository at this point in the history
This commit adds operator overloads for increment/decrement to
AliasedBuffer::Reference.
The motivation for doing this is to hopefully make code that needs
to increment/decrement a little simpler.

PR-URL: #19083
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
  • Loading branch information
danbev authored and MylesBorins committed Mar 20, 2018
1 parent a04e4ae commit 27754c5
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
16 changes: 16 additions & 0 deletions src/aliased_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,22 @@ class AliasedBuffer {
return aliased_buffer_->GetValue(index_);
}

template <typename T>
inline Reference& operator+=(const T& val) {
const T current = aliased_buffer_->GetValue(index_);
aliased_buffer_->SetValue(index_, current + val);
return *this;
}

inline Reference& operator+=(const Reference& val) {
return this->operator+=(static_cast<NativeT>(val));
}

template <typename T>
inline Reference& operator-=(const T& val) {
return this->operator+=(-val);
}

private:
AliasedBuffer<NativeT, V8T>* aliased_buffer_;
size_t index_;
Expand Down
16 changes: 7 additions & 9 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ inline v8::Local<v8::String> Environment::AsyncHooks::provider_string(int idx) {
}

inline void Environment::AsyncHooks::no_force_checks() {
// fields_ does not have the -= operator defined
fields_[kCheck] = fields_[kCheck] - 1;
fields_[kCheck] -= 1;
}

inline Environment* Environment::AsyncHooks::env() {
Expand All @@ -134,7 +133,7 @@ inline void Environment::AsyncHooks::push_async_ids(double async_id,
grow_async_ids_stack();
async_ids_stack_[2 * offset] = async_id_fields_[kExecutionAsyncId];
async_ids_stack_[2 * offset + 1] = async_id_fields_[kTriggerAsyncId];
fields_[kStackLength] = fields_[kStackLength] + 1;
fields_[kStackLength] += 1;
async_id_fields_[kExecutionAsyncId] = async_id;
async_id_fields_[kTriggerAsyncId] = trigger_async_id;
}
Expand Down Expand Up @@ -238,19 +237,19 @@ inline bool Environment::ImmediateInfo::has_outstanding() const {
}

inline void Environment::ImmediateInfo::count_inc(uint32_t increment) {
fields_[kCount] = fields_[kCount] + increment;
fields_[kCount] += increment;
}

inline void Environment::ImmediateInfo::count_dec(uint32_t decrement) {
fields_[kCount] = fields_[kCount] - decrement;
fields_[kCount] -= decrement;
}

inline void Environment::ImmediateInfo::ref_count_inc(uint32_t increment) {
fields_[kRefCount] = fields_[kRefCount] + increment;
fields_[kRefCount] += increment;
}

inline void Environment::ImmediateInfo::ref_count_dec(uint32_t decrement) {
fields_[kRefCount] = fields_[kRefCount] - decrement;
fields_[kRefCount] -= decrement;
}

inline Environment::TickInfo::TickInfo(v8::Isolate* isolate)
Expand Down Expand Up @@ -461,8 +460,7 @@ inline std::vector<double>* Environment::destroy_async_id_list() {
}

inline double Environment::new_async_id() {
async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter] =
async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter] + 1;
async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter] += 1;
return async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter];
}

Expand Down
30 changes: 30 additions & 0 deletions test/cctest/test_aliased_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,33 @@ TEST_F(AliasBufferTest, SharedArrayBuffer4) {
int8_t, v8::Int8Array,
int32_t, v8::Int32Array>(isolate_, 1, 3, 1);
}

TEST_F(AliasBufferTest, OperatorOverloads) {
v8::Isolate::Scope isolate_scope(isolate_);
v8::HandleScope handle_scope(isolate_);
v8::Local<v8::Context> context = v8::Context::New(isolate_);
v8::Context::Scope context_scope(context);
const size_t size = 10;
AliasedBuffer<uint32_t, v8::Uint32Array> ab{isolate_, size};

EXPECT_EQ(static_cast<uint32_t>(1), ab[0] = 1);
EXPECT_EQ(static_cast<uint32_t>(4), ab[0] += 3);
EXPECT_EQ(static_cast<uint32_t>(2), ab[0] -= 2);
EXPECT_EQ(static_cast<uint32_t>(-2), -ab[0]);
}

TEST_F(AliasBufferTest, OperatorOverloadsRefs) {
v8::Isolate::Scope isolate_scope(isolate_);
v8::HandleScope handle_scope(isolate_);
v8::Local<v8::Context> context = v8::Context::New(isolate_);
v8::Context::Scope context_scope(context);
AliasedBuffer<uint32_t, v8::Uint32Array> ab{isolate_, 2};
using Reference = AliasedBuffer<uint32_t, v8::Uint32Array>::Reference;
Reference ref = ab[0];
Reference ref_value = ab[1] = 2;

EXPECT_EQ(static_cast<uint32_t>(2), ref = ref_value);
EXPECT_EQ(static_cast<uint32_t>(4), ref += ref_value);
EXPECT_EQ(static_cast<uint32_t>(2), ref -= ref_value);
EXPECT_EQ(static_cast<uint32_t>(-2), -ref);
}

0 comments on commit 27754c5

Please sign in to comment.