@@ -70,14 +70,14 @@ AliasedBufferBase<NativeT, V8T>::AliasedBufferBase(
7070 count_ (that.count_),
7171 byte_offset_(that.byte_offset_),
7272 buffer_(that.buffer_) {
73- DCHECK_NULL (index_ );
73+ DCHECK ( is_valid () );
7474 js_array_ = v8::Global<V8T>(that.isolate_ , that.GetJSArray ());
7575}
7676
7777template <typename NativeT, typename V8T>
7878AliasedBufferIndex AliasedBufferBase<NativeT, V8T>::Serialize(
7979 v8::Local<v8::Context> context, v8::SnapshotCreator* creator) {
80- DCHECK_NULL (index_ );
80+ DCHECK ( is_valid () );
8181 return creator->AddData (context, GetJSArray ());
8282}
8383
@@ -100,7 +100,7 @@ inline void AliasedBufferBase<NativeT, V8T>::Deserialize(
100100template <typename NativeT, typename V8T>
101101AliasedBufferBase<NativeT, V8T>& AliasedBufferBase<NativeT, V8T>::operator =(
102102 AliasedBufferBase<NativeT, V8T>&& that) noexcept {
103- DCHECK_NULL (index_ );
103+ DCHECK ( is_valid () );
104104 this ->~AliasedBufferBase ();
105105 isolate_ = that.isolate_ ;
106106 count_ = that.count_ ;
@@ -116,7 +116,7 @@ AliasedBufferBase<NativeT, V8T>& AliasedBufferBase<NativeT, V8T>::operator=(
116116
117117template <typename NativeT, typename V8T>
118118v8::Local<V8T> AliasedBufferBase<NativeT, V8T>::GetJSArray() const {
119- DCHECK_NULL (index_ );
119+ DCHECK ( is_valid () );
120120 return js_array_.Get (isolate_);
121121}
122122
@@ -126,6 +126,21 @@ void AliasedBufferBase<NativeT, V8T>::Release() {
126126 js_array_.Reset ();
127127}
128128
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+
129144template <typename NativeT, typename V8T>
130145v8::Local<v8::ArrayBuffer> AliasedBufferBase<NativeT, V8T>::GetArrayBuffer()
131146 const {
@@ -134,7 +149,7 @@ v8::Local<v8::ArrayBuffer> AliasedBufferBase<NativeT, V8T>::GetArrayBuffer()
134149
135150template <typename NativeT, typename V8T>
136151inline const NativeT* AliasedBufferBase<NativeT, V8T>::GetNativeBuffer() const {
137- DCHECK_NULL (index_ );
152+ DCHECK ( is_valid () );
138153 return buffer_;
139154}
140155
@@ -147,22 +162,22 @@ template <typename NativeT, typename V8T>
147162inline void AliasedBufferBase<NativeT, V8T>::SetValue(const size_t index,
148163 NativeT value) {
149164 DCHECK_LT (index, count_);
150- DCHECK_NULL (index_ );
165+ DCHECK ( is_valid () );
151166 buffer_[index] = value;
152167}
153168
154169template <typename NativeT, typename V8T>
155170inline const NativeT AliasedBufferBase<NativeT, V8T>::GetValue(
156171 const size_t index) const {
157- DCHECK_NULL (index_ );
172+ DCHECK ( is_valid () );
158173 DCHECK_LT (index, count_);
159174 return buffer_[index];
160175}
161176
162177template <typename NativeT, typename V8T>
163178typename AliasedBufferBase<NativeT, V8T>::Reference
164179AliasedBufferBase<NativeT, V8T>::operator [](size_t index) {
165- DCHECK_NULL (index_ );
180+ DCHECK ( is_valid () );
166181 return Reference (this , index);
167182}
168183
@@ -178,7 +193,7 @@ size_t AliasedBufferBase<NativeT, V8T>::Length() const {
178193
179194template <typename NativeT, typename V8T>
180195void AliasedBufferBase<NativeT, V8T>::reserve(size_t new_capacity) {
181- DCHECK_NULL (index_ );
196+ DCHECK ( is_valid () );
182197 DCHECK_GE (new_capacity, count_);
183198 DCHECK_EQ (byte_offset_, 0 );
184199 const v8::HandleScope handle_scope (isolate_);
@@ -206,6 +221,11 @@ void AliasedBufferBase<NativeT, V8T>::reserve(size_t new_capacity) {
206221 count_ = new_capacity;
207222}
208223
224+ template <typename NativeT, typename V8T>
225+ inline bool AliasedBufferBase<NativeT, V8T>::is_valid() const {
226+ return index_ == nullptr && !cleared_;
227+ }
228+
209229template <typename NativeT, typename V8T>
210230inline size_t AliasedBufferBase<NativeT, V8T>::SelfSize() const {
211231 return sizeof (*this );
0 commit comments