Skip to content

Commit 45277e2

Browse files
danbevjasnell
authored andcommitted
src: add incr/decr operators for Reference
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]>
1 parent cca12ee commit 45277e2

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed

src/aliased_buffer.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,22 @@ class AliasedBuffer {
141141
return aliased_buffer_->GetValue(index_);
142142
}
143143

144+
template <typename T>
145+
inline Reference& operator+=(const T& val) {
146+
const T current = aliased_buffer_->GetValue(index_);
147+
aliased_buffer_->SetValue(index_, current + val);
148+
return *this;
149+
}
150+
151+
inline Reference& operator+=(const Reference& val) {
152+
return this->operator+=(static_cast<NativeT>(val));
153+
}
154+
155+
template <typename T>
156+
inline Reference& operator-=(const T& val) {
157+
return this->operator+=(-val);
158+
}
159+
144160
private:
145161
AliasedBuffer<NativeT, V8T>* aliased_buffer_;
146162
size_t index_;

src/env-inl.h

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ inline v8::Local<v8::String> Environment::AsyncHooks::provider_string(int idx) {
112112
}
113113

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

119118
inline Environment* Environment::AsyncHooks::env() {
@@ -135,7 +134,7 @@ inline void Environment::AsyncHooks::push_async_ids(double async_id,
135134
grow_async_ids_stack();
136135
async_ids_stack_[2 * offset] = async_id_fields_[kExecutionAsyncId];
137136
async_ids_stack_[2 * offset + 1] = async_id_fields_[kTriggerAsyncId];
138-
fields_[kStackLength] = fields_[kStackLength] + 1;
137+
fields_[kStackLength] += 1;
139138
async_id_fields_[kExecutionAsyncId] = async_id;
140139
async_id_fields_[kTriggerAsyncId] = trigger_async_id;
141140
}
@@ -240,19 +239,19 @@ inline bool Environment::ImmediateInfo::has_outstanding() const {
240239
}
241240

242241
inline void Environment::ImmediateInfo::count_inc(uint32_t increment) {
243-
fields_[kCount] = fields_[kCount] + increment;
242+
fields_[kCount] += increment;
244243
}
245244

246245
inline void Environment::ImmediateInfo::count_dec(uint32_t decrement) {
247-
fields_[kCount] = fields_[kCount] - decrement;
246+
fields_[kCount] -= decrement;
248247
}
249248

250249
inline void Environment::ImmediateInfo::ref_count_inc(uint32_t increment) {
251-
fields_[kRefCount] = fields_[kRefCount] + increment;
250+
fields_[kRefCount] += increment;
252251
}
253252

254253
inline void Environment::ImmediateInfo::ref_count_dec(uint32_t decrement) {
255-
fields_[kRefCount] = fields_[kRefCount] - decrement;
254+
fields_[kRefCount] -= decrement;
256255
}
257256

258257
inline Environment::TickInfo::TickInfo(v8::Isolate* isolate)
@@ -478,8 +477,7 @@ inline std::vector<double>* Environment::destroy_async_id_list() {
478477
}
479478

480479
inline double Environment::new_async_id() {
481-
async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter] =
482-
async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter] + 1;
480+
async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter] += 1;
483481
return async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter];
484482
}
485483

test/cctest/test_aliased_buffer.cc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,33 @@ TEST_F(AliasBufferTest, SharedArrayBuffer4) {
207207
int8_t, v8::Int8Array,
208208
int32_t, v8::Int32Array>(isolate_, 1, 3, 1);
209209
}
210+
211+
TEST_F(AliasBufferTest, OperatorOverloads) {
212+
v8::Isolate::Scope isolate_scope(isolate_);
213+
v8::HandleScope handle_scope(isolate_);
214+
v8::Local<v8::Context> context = v8::Context::New(isolate_);
215+
v8::Context::Scope context_scope(context);
216+
const size_t size = 10;
217+
AliasedBuffer<uint32_t, v8::Uint32Array> ab{isolate_, size};
218+
219+
EXPECT_EQ(static_cast<uint32_t>(1), ab[0] = 1);
220+
EXPECT_EQ(static_cast<uint32_t>(4), ab[0] += 3);
221+
EXPECT_EQ(static_cast<uint32_t>(2), ab[0] -= 2);
222+
EXPECT_EQ(static_cast<uint32_t>(-2), -ab[0]);
223+
}
224+
225+
TEST_F(AliasBufferTest, OperatorOverloadsRefs) {
226+
v8::Isolate::Scope isolate_scope(isolate_);
227+
v8::HandleScope handle_scope(isolate_);
228+
v8::Local<v8::Context> context = v8::Context::New(isolate_);
229+
v8::Context::Scope context_scope(context);
230+
AliasedBuffer<uint32_t, v8::Uint32Array> ab{isolate_, 2};
231+
using Reference = AliasedBuffer<uint32_t, v8::Uint32Array>::Reference;
232+
Reference ref = ab[0];
233+
Reference ref_value = ab[1] = 2;
234+
235+
EXPECT_EQ(static_cast<uint32_t>(2), ref = ref_value);
236+
EXPECT_EQ(static_cast<uint32_t>(4), ref += ref_value);
237+
EXPECT_EQ(static_cast<uint32_t>(2), ref -= ref_value);
238+
EXPECT_EQ(static_cast<uint32_t>(-2), -ref);
239+
}

0 commit comments

Comments
 (0)