| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent a04e4ae commit 27754c5
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -141,6 +141,22 @@ class AliasedBuffer { | |||
| 141 | 141 | return aliased_buffer_->GetValue(index_); | |
| 142 | 142 | } | |
| 143 | 143 | ||
| 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 | + | ||
| 144 | 160 | private: | |
| 145 | 161 | AliasedBuffer<NativeT, V8T>* aliased_buffer_; | |
| 146 | 162 | size_t index_; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -111,8 +111,7 @@ inline v8::Local<v8::String> Environment::AsyncHooks::provider_string(int idx) { | |||
| 111 | 111 | } | |
| 112 | 112 | ||
| 113 | 113 | 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; | ||
| 116 | 115 | } | |
| 117 | 116 | ||
| 118 | 117 | inline Environment* Environment::AsyncHooks::env() { | |
@@ -134,7 +133,7 @@ inline void Environment::AsyncHooks::push_async_ids(double async_id, | |||
| 134 | 133 | grow_async_ids_stack(); | |
| 135 | 134 | async_ids_stack_[2 * offset] = async_id_fields_[kExecutionAsyncId]; | |
| 136 | 135 | async_ids_stack_[2 * offset + 1] = async_id_fields_[kTriggerAsyncId]; | |
| 137 | - fields_[kStackLength] = fields_[kStackLength] + 1; | ||
| 136 | + fields_[kStackLength] += 1; | ||
| 138 | 137 | async_id_fields_[kExecutionAsyncId] = async_id; | |
| 139 | 138 | async_id_fields_[kTriggerAsyncId] = trigger_async_id; | |
| 140 | 139 | } | |
@@ -238,19 +237,19 @@ inline bool Environment::ImmediateInfo::has_outstanding() const { | |||
| 238 | 237 | } | |
| 239 | 238 | ||
| 240 | 239 | inline void Environment::ImmediateInfo::count_inc(uint32_t increment) { | |
| 241 | - fields_[kCount] = fields_[kCount] + increment; | ||
| 240 | + fields_[kCount] += increment; | ||
| 242 | 241 | } | |
| 243 | 242 | ||
| 244 | 243 | inline void Environment::ImmediateInfo::count_dec(uint32_t decrement) { | |
| 245 | - fields_[kCount] = fields_[kCount] - decrement; | ||
| 244 | + fields_[kCount] -= decrement; | ||
| 246 | 245 | } | |
| 247 | 246 | ||
| 248 | 247 | inline void Environment::ImmediateInfo::ref_count_inc(uint32_t increment) { | |
| 249 | - fields_[kRefCount] = fields_[kRefCount] + increment; | ||
| 248 | + fields_[kRefCount] += increment; | ||
| 250 | 249 | } | |
| 251 | 250 | ||
| 252 | 251 | inline void Environment::ImmediateInfo::ref_count_dec(uint32_t decrement) { | |
| 253 | - fields_[kRefCount] = fields_[kRefCount] - decrement; | ||
| 252 | + fields_[kRefCount] -= decrement; | ||
| 254 | 253 | } | |
| 255 | 254 | ||
| 256 | 255 | inline Environment::TickInfo::TickInfo(v8::Isolate* isolate) | |
@@ -461,8 +460,7 @@ inline std::vector<double>* Environment::destroy_async_id_list() { | |||
| 461 | 460 | } | |
| 462 | 461 | ||
| 463 | 462 | 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; | ||
| 466 | 464 | return async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter]; | |
| 467 | 465 | } | |
| 468 | 466 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -207,3 +207,33 @@ TEST_F(AliasBufferTest, SharedArrayBuffer4) { | |||
| 207 | 207 | int8_t, v8::Int8Array, | |
| 208 | 208 | int32_t, v8::Int32Array>(isolate_, 1, 3, 1); | |
| 209 | 209 | } | |
| 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 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments