@@ -70,14 +70,14 @@ AliasedBufferBase<NativeT, V8T>::AliasedBufferBase(
70
70
count_ (that.count_),
71
71
byte_offset_(that.byte_offset_),
72
72
buffer_(that.buffer_) {
73
- DCHECK_NULL (index_ );
73
+ DCHECK ( is_valid () );
74
74
js_array_ = v8::Global<V8T>(that.isolate_ , that.GetJSArray ());
75
75
}
76
76
77
77
template <typename NativeT, typename V8T>
78
78
AliasedBufferIndex AliasedBufferBase<NativeT, V8T>::Serialize(
79
79
v8::Local<v8::Context> context, v8::SnapshotCreator* creator) {
80
- DCHECK_NULL (index_ );
80
+ DCHECK ( is_valid () );
81
81
return creator->AddData (context, GetJSArray ());
82
82
}
83
83
@@ -100,7 +100,7 @@ inline void AliasedBufferBase<NativeT, V8T>::Deserialize(
100
100
template <typename NativeT, typename V8T>
101
101
AliasedBufferBase<NativeT, V8T>& AliasedBufferBase<NativeT, V8T>::operator =(
102
102
AliasedBufferBase<NativeT, V8T>&& that) noexcept {
103
- DCHECK_NULL (index_ );
103
+ DCHECK ( is_valid () );
104
104
this ->~AliasedBufferBase ();
105
105
isolate_ = that.isolate_ ;
106
106
count_ = that.count_ ;
@@ -116,7 +116,7 @@ AliasedBufferBase<NativeT, V8T>& AliasedBufferBase<NativeT, V8T>::operator=(
116
116
117
117
template <typename NativeT, typename V8T>
118
118
v8::Local<V8T> AliasedBufferBase<NativeT, V8T>::GetJSArray() const {
119
- DCHECK_NULL (index_ );
119
+ DCHECK ( is_valid () );
120
120
return js_array_.Get (isolate_);
121
121
}
122
122
@@ -126,6 +126,21 @@ void AliasedBufferBase<NativeT, V8T>::Release() {
126
126
js_array_.Reset ();
127
127
}
128
128
129
+ template <typename NativeT, typename V8T>
130
+ inline void AliasedBufferBase<NativeT, V8T>::WeakCallback(
131
+ const v8::WeakCallbackInfo<AliasedBufferBase<NativeT, V8T>>& data) {
132
+ AliasedBufferBase<NativeT, V8T>* buffer = data.GetParameter ();
133
+ DCHECK (buffer->is_valid ());
134
+ buffer->cleared_ = true ;
135
+ buffer->js_array_ .Reset ();
136
+ }
137
+
138
+ template <typename NativeT, typename V8T>
139
+ inline void AliasedBufferBase<NativeT, V8T>::MakeWeak() {
140
+ DCHECK (is_valid ());
141
+ js_array_.SetWeak (this , WeakCallback, v8::WeakCallbackType::kParameter );
142
+ }
143
+
129
144
template <typename NativeT, typename V8T>
130
145
v8::Local<v8::ArrayBuffer> AliasedBufferBase<NativeT, V8T>::GetArrayBuffer()
131
146
const {
@@ -134,7 +149,7 @@ v8::Local<v8::ArrayBuffer> AliasedBufferBase<NativeT, V8T>::GetArrayBuffer()
134
149
135
150
template <typename NativeT, typename V8T>
136
151
inline const NativeT* AliasedBufferBase<NativeT, V8T>::GetNativeBuffer() const {
137
- DCHECK_NULL (index_ );
152
+ DCHECK ( is_valid () );
138
153
return buffer_;
139
154
}
140
155
@@ -147,22 +162,22 @@ template <typename NativeT, typename V8T>
147
162
inline void AliasedBufferBase<NativeT, V8T>::SetValue(const size_t index,
148
163
NativeT value) {
149
164
DCHECK_LT (index, count_);
150
- DCHECK_NULL (index_ );
165
+ DCHECK ( is_valid () );
151
166
buffer_[index] = value;
152
167
}
153
168
154
169
template <typename NativeT, typename V8T>
155
170
inline const NativeT AliasedBufferBase<NativeT, V8T>::GetValue(
156
171
const size_t index) const {
157
- DCHECK_NULL (index_ );
172
+ DCHECK ( is_valid () );
158
173
DCHECK_LT (index, count_);
159
174
return buffer_[index];
160
175
}
161
176
162
177
template <typename NativeT, typename V8T>
163
178
typename AliasedBufferBase<NativeT, V8T>::Reference
164
179
AliasedBufferBase<NativeT, V8T>::operator [](size_t index) {
165
- DCHECK_NULL (index_ );
180
+ DCHECK ( is_valid () );
166
181
return Reference (this , index);
167
182
}
168
183
@@ -178,7 +193,7 @@ size_t AliasedBufferBase<NativeT, V8T>::Length() const {
178
193
179
194
template <typename NativeT, typename V8T>
180
195
void AliasedBufferBase<NativeT, V8T>::reserve(size_t new_capacity) {
181
- DCHECK_NULL (index_ );
196
+ DCHECK ( is_valid () );
182
197
DCHECK_GE (new_capacity, count_);
183
198
DCHECK_EQ (byte_offset_, 0 );
184
199
const v8::HandleScope handle_scope (isolate_);
@@ -206,6 +221,11 @@ void AliasedBufferBase<NativeT, V8T>::reserve(size_t new_capacity) {
206
221
count_ = new_capacity;
207
222
}
208
223
224
+ template <typename NativeT, typename V8T>
225
+ inline bool AliasedBufferBase<NativeT, V8T>::is_valid() const {
226
+ return index_ == nullptr && !cleared_;
227
+ }
228
+
209
229
template <typename NativeT, typename V8T>
210
230
inline size_t AliasedBufferBase<NativeT, V8T>::SelfSize() const {
211
231
return sizeof (*this );
0 commit comments