Skip to content

Commit 27754c5

Browse files
danbevMylesBorins
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 a04e4ae commit 27754c5

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed

src/aliased_buffer.h

+16
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

+7-9
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ inline v8::Local<v8::String> Environment::AsyncHooks::provider_string(int idx) {
111111
}
112112

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

118117
inline Environment* Environment::AsyncHooks::env() {
@@ -134,7 +133,7 @@ inline void Environment::AsyncHooks::push_async_ids(double async_id,
134133
grow_async_ids_stack();
135134
async_ids_stack_[2 * offset] = async_id_fields_[kExecutionAsyncId];
136135
async_ids_stack_[2 * offset + 1] = async_id_fields_[kTriggerAsyncId];
137-
fields_[kStackLength] = fields_[kStackLength] + 1;
136+
fields_[kStackLength] += 1;
138137
async_id_fields_[kExecutionAsyncId] = async_id;
139138
async_id_fields_[kTriggerAsyncId] = trigger_async_id;
140139
}
@@ -238,19 +237,19 @@ inline bool Environment::ImmediateInfo::has_outstanding() const {
238237
}
239238

240239
inline void Environment::ImmediateInfo::count_inc(uint32_t increment) {
241-
fields_[kCount] = fields_[kCount] + increment;
240+
fields_[kCount] += increment;
242241
}
243242

244243
inline void Environment::ImmediateInfo::count_dec(uint32_t decrement) {
245-
fields_[kCount] = fields_[kCount] - decrement;
244+
fields_[kCount] -= decrement;
246245
}
247246

248247
inline void Environment::ImmediateInfo::ref_count_inc(uint32_t increment) {
249-
fields_[kRefCount] = fields_[kRefCount] + increment;
248+
fields_[kRefCount] += increment;
250249
}
251250

252251
inline void Environment::ImmediateInfo::ref_count_dec(uint32_t decrement) {
253-
fields_[kRefCount] = fields_[kRefCount] - decrement;
252+
fields_[kRefCount] -= decrement;
254253
}
255254

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

463462
inline double Environment::new_async_id() {
464-
async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter] =
465-
async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter] + 1;
463+
async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter] += 1;
466464
return async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter];
467465
}
468466

test/cctest/test_aliased_buffer.cc

+30
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)