@@ -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,20 @@ 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
+ }
136
+
137
+ template <typename NativeT, typename V8T>
138
+ inline void AliasedBufferBase<NativeT, V8T>::MakeWeak() {
139
+ DCHECK (is_valid ());
140
+ js_array_.SetWeak (this , WeakCallback, v8::WeakCallbackType::kParameter );
141
+ }
142
+
129
143
template <typename NativeT, typename V8T>
130
144
v8::Local<v8::ArrayBuffer> AliasedBufferBase<NativeT, V8T>::GetArrayBuffer()
131
145
const {
@@ -134,7 +148,7 @@ v8::Local<v8::ArrayBuffer> AliasedBufferBase<NativeT, V8T>::GetArrayBuffer()
134
148
135
149
template <typename NativeT, typename V8T>
136
150
inline const NativeT* AliasedBufferBase<NativeT, V8T>::GetNativeBuffer() const {
137
- DCHECK_NULL (index_ );
151
+ DCHECK ( is_valid () );
138
152
return buffer_;
139
153
}
140
154
@@ -147,22 +161,22 @@ template <typename NativeT, typename V8T>
147
161
inline void AliasedBufferBase<NativeT, V8T>::SetValue(const size_t index,
148
162
NativeT value) {
149
163
DCHECK_LT (index, count_);
150
- DCHECK_NULL (index_ );
164
+ DCHECK ( is_valid () );
151
165
buffer_[index] = value;
152
166
}
153
167
154
168
template <typename NativeT, typename V8T>
155
169
inline const NativeT AliasedBufferBase<NativeT, V8T>::GetValue(
156
170
const size_t index) const {
157
- DCHECK_NULL (index_ );
171
+ DCHECK ( is_valid () );
158
172
DCHECK_LT (index, count_);
159
173
return buffer_[index];
160
174
}
161
175
162
176
template <typename NativeT, typename V8T>
163
177
typename AliasedBufferBase<NativeT, V8T>::Reference
164
178
AliasedBufferBase<NativeT, V8T>::operator [](size_t index) {
165
- DCHECK_NULL (index_ );
179
+ DCHECK ( is_valid () );
166
180
return Reference (this , index);
167
181
}
168
182
@@ -178,7 +192,7 @@ size_t AliasedBufferBase<NativeT, V8T>::Length() const {
178
192
179
193
template <typename NativeT, typename V8T>
180
194
void AliasedBufferBase<NativeT, V8T>::reserve(size_t new_capacity) {
181
- DCHECK_NULL (index_ );
195
+ DCHECK ( is_valid () );
182
196
DCHECK_GE (new_capacity, count_);
183
197
DCHECK_EQ (byte_offset_, 0 );
184
198
const v8::HandleScope handle_scope (isolate_);
@@ -206,6 +220,11 @@ void AliasedBufferBase<NativeT, V8T>::reserve(size_t new_capacity) {
206
220
count_ = new_capacity;
207
221
}
208
222
223
+ template <typename NativeT, typename V8T>
224
+ inline bool AliasedBufferBase<NativeT, V8T>::is_valid() const {
225
+ return index_ == nullptr && !cleared_;
226
+ }
227
+
209
228
template <typename NativeT, typename V8T>
210
229
inline size_t AliasedBufferBase<NativeT, V8T>::SelfSize() const {
211
230
return sizeof (*this );
0 commit comments