| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 568256d commit cfafe43
13 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -59,9 +59,6 @@ const { | |||
| 59 | 59 | const pathModule = require('path'); | |
| 60 | 60 | const { isArrayBufferView } = require('internal/util/types'); | |
| 61 | 61 | ||
| 62 | - // We need to get the statValues from the binding at the callsite since | ||
| 63 | - // it's re-initialized after deserialization. | ||
| 64 | - | ||
| 65 | 62 | const binding = internalBinding('fs'); | |
| 66 | 63 | ||
| 67 | 64 | const { createBlobFromFilePath } = require('internal/blob'); | |
@@ -78,7 +75,10 @@ const { | |||
| 78 | 75 | uvException, | |
| 79 | 76 | } = require('internal/errors'); | |
| 80 | 77 | ||
| 81 | - const { FSReqCallback } = binding; | ||
| 78 | + const { | ||
| 79 | + FSReqCallback, | ||
| 80 | + statValues, | ||
| 81 | + } = binding; | ||
| 82 | 82 | const { toPathIfFileURL } = require('internal/url'); | |
| 83 | 83 | const { | |
| 84 | 84 | customPromisifyArgs: kCustomPromisifyArgsSymbol, | |
@@ -2569,8 +2569,8 @@ function realpathSync(p, options) { | |||
| 2569 | 2569 | ||
| 2570 | 2570 | // Continue if not a symlink, break if a pipe/socket | |
| 2571 | 2571 | if (knownHard.has(base) || cache?.get(base) === base) { | |
| 2572 | - if (isFileType(binding.statValues, S_IFIFO) || | ||
| 2573 | - isFileType(binding.statValues, S_IFSOCK)) { | ||
| 2572 | + if (isFileType(statValues, S_IFIFO) || | ||
| 2573 | + isFileType(statValues, S_IFSOCK)) { | ||
| 2574 | 2574 | break; | |
| 2575 | 2575 | } | |
| 2576 | 2576 | continue; | |
@@ -2727,8 +2727,8 @@ function realpath(p, options, callback) { | |||
| 2727 | 2727 | ||
| 2728 | 2728 | // Continue if not a symlink, break if a pipe/socket | |
| 2729 | 2729 | if (knownHard.has(base)) { | |
| 2730 | - if (isFileType(binding.statValues, S_IFIFO) || | ||
| 2731 | - isFileType(binding.statValues, S_IFSOCK)) { | ||
| 2730 | + if (isFileType(statValues, S_IFIFO) || | ||
| 2731 | + isFileType(statValues, S_IFSOCK)) { | ||
| 2732 | 2732 | return callback(null, encodeRealpathResult(p, options)); | |
| 2733 | 2733 | } | |
| 2734 | 2734 | return process.nextTick(LOOP); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -51,6 +51,7 @@ const { | |||
| 51 | 51 | const binding = internalBinding('encoding_binding'); | |
| 52 | 52 | const { | |
| 53 | 53 | encodeInto, | |
| 54 | + encodeIntoResults, | ||
| 54 | 55 | encodeUtf8String, | |
| 55 | 56 | decodeUTF8, | |
| 56 | 57 | } = binding; | |
@@ -341,7 +342,7 @@ class TextEncoder { | |||
| 341 | 342 | encodeInto(src, dest); | |
| 342 | 343 | // We need to read from the binding here since the buffer gets refreshed | |
| 343 | 344 | // from the snapshot. | |
| 344 | - const { 0: read, 1: written } = binding.encodeIntoResults; | ||
| 345 | + const { 0: read, 1: written } = encodeIntoResults; | ||
| 345 | 346 | return { read, written }; | |
| 346 | 347 | } | |
| 347 | 348 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -139,6 +139,10 @@ const { | |||
| 139 | 139 | kBytecodeAndMetadataSizeIndex, | |
| 140 | 140 | kExternalScriptSourceSizeIndex, | |
| 141 | 141 | kCPUProfilerMetaDataSizeIndex, | |
| 142 | + | ||
| 143 | + heapStatisticsBuffer, | ||
| 144 | + heapCodeStatisticsBuffer, | ||
| 145 | + heapSpaceStatisticsBuffer, | ||
| 142 | 146 | } = binding; | |
| 143 | 147 | ||
| 144 | 148 | const kNumberOfHeapSpaces = kHeapSpaces.length; | |
@@ -170,7 +174,7 @@ function setFlagsFromString(flags) { | |||
| 170 | 174 | * }} | |
| 171 | 175 | */ | |
| 172 | 176 | function getHeapStatistics() { | |
| 173 | - const buffer = binding.heapStatisticsBuffer; | ||
| 177 | + const buffer = heapStatisticsBuffer; | ||
| 174 | 178 | ||
| 175 | 179 | updateHeapStatisticsBuffer(); | |
| 176 | 180 | ||
@@ -204,7 +208,7 @@ function getHeapStatistics() { | |||
| 204 | 208 | */ | |
| 205 | 209 | function getHeapSpaceStatistics() { | |
| 206 | 210 | const heapSpaceStatistics = new Array(kNumberOfHeapSpaces); | |
| 207 | - const buffer = binding.heapSpaceStatisticsBuffer; | ||
| 211 | + const buffer = heapSpaceStatisticsBuffer; | ||
| 208 | 212 | ||
| 209 | 213 | for (let i = 0; i < kNumberOfHeapSpaces; i++) { | |
| 210 | 214 | updateHeapSpaceStatisticsBuffer(i); | |
@@ -230,7 +234,7 @@ function getHeapSpaceStatistics() { | |||
| 230 | 234 | * }} | |
| 231 | 235 | */ | |
| 232 | 236 | function getHeapCodeStatistics() { | |
| 233 | - const buffer = binding.heapCodeStatisticsBuffer; | ||
| 237 | + const buffer = heapCodeStatisticsBuffer; | ||
| 234 | 238 | ||
| 235 | 239 | updateHeapCodeStatisticsBuffer(); | |
| 236 | 240 | return { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -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); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -117,6 +117,14 @@ class AliasedBufferBase : public MemoryRetainer { | |||
| 117 | 117 | ||
| 118 | 118 | void Release(); | |
| 119 | 119 | ||
| 120 | + /** | ||
| 121 | + * Make the global reference to the typed array weak. The caller must make | ||
| 122 | + * sure that no operation can be done on the AliasedBuffer when the typed | ||
| 123 | + * array becomes unreachable. Usually this means the caller must maintain | ||
| 124 | + * a JS reference to the typed array from JS object. | ||
| 125 | + */ | ||
| 126 | + inline void MakeWeak(); | ||
| 127 | + | ||
| 120 | 128 | /** | |
| 121 | 129 | * Get the underlying v8::ArrayBuffer underlying the TypedArray and | |
| 122 | 130 | * overlaying the native buffer | |
@@ -164,11 +172,15 @@ class AliasedBufferBase : public MemoryRetainer { | |||
| 164 | 172 | inline void MemoryInfo(node::MemoryTracker* tracker) const override; | |
| 165 | 173 | ||
| 166 | 174 | private: | |
| 175 | + inline bool is_valid() const; | ||
| 176 | + static inline void WeakCallback( | ||
| 177 | + const v8::WeakCallbackInfo<AliasedBufferBase<NativeT, V8T>>& data); | ||
| 167 | 178 | v8::Isolate* isolate_ = nullptr; | |
| 168 | 179 | size_t count_ = 0; | |
| 169 | 180 | size_t byte_offset_ = 0; | |
| 170 | 181 | NativeT* buffer_ = nullptr; | |
| 171 | 182 | v8::Global<V8T> js_array_; | |
| 183 | + bool cleared_ = false; | ||
| 172 | 184 | ||
| 173 | 185 | // Deserialize data | |
| 174 | 186 | const AliasedBufferIndex* index_ = nullptr; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -28,30 +28,41 @@ void BindingData::MemoryInfo(MemoryTracker* tracker) const { | |||
| 28 | 28 | encode_into_results_buffer_); | |
| 29 | 29 | } | |
| 30 | 30 | ||
| 31 | - BindingData::BindingData(Realm* realm, v8::Local<v8::Object> object) | ||
| 31 | + BindingData::BindingData(Realm* realm, | ||
| 32 | + v8::Local<v8::Object> object, | ||
| 33 | + InternalFieldInfo* info) | ||
| 32 | 34 | : SnapshotableObject(realm, object, type_int), | |
| 33 | - encode_into_results_buffer_(realm->isolate(), kEncodeIntoResultsLength) { | ||
| 34 | - object | ||
| 35 | - ->Set(realm->context(), | ||
| 36 | - FIXED_ONE_BYTE_STRING(realm->isolate(), "encodeIntoResults"), | ||
| 37 | - encode_into_results_buffer_.GetJSArray()) | ||
| 38 | - .Check(); | ||
| 35 | + encode_into_results_buffer_( | ||
| 36 | + realm->isolate(), | ||
| 37 | + kEncodeIntoResultsLength, | ||
| 38 | + MAYBE_FIELD_PTR(info, encode_into_results_buffer)) { | ||
| 39 | + if (info == nullptr) { | ||
| 40 | + object | ||
| 41 | + ->Set(realm->context(), | ||
| 42 | + FIXED_ONE_BYTE_STRING(realm->isolate(), "encodeIntoResults"), | ||
| 43 | + encode_into_results_buffer_.GetJSArray()) | ||
| 44 | + .Check(); | ||
| 45 | + } else { | ||
| 46 | + encode_into_results_buffer_.Deserialize(realm->context()); | ||
| 47 | + } | ||
| 48 | + encode_into_results_buffer_.MakeWeak(); | ||
| 39 | 49 | } | |
| 40 | 50 | ||
| 41 | 51 | bool BindingData::PrepareForSerialization(Local<Context> context, | |
| 42 | 52 | v8::SnapshotCreator* creator) { | |
| 43 | - // We'll just re-initialize the buffers in the constructor since their | ||
| 44 | - // contents can be thrown away once consumed in the previous call. | ||
| 45 | - encode_into_results_buffer_.Release(); | ||
| 53 | + DCHECK_NULL(internal_field_info_); | ||
| 54 | + internal_field_info_ = InternalFieldInfoBase::New<InternalFieldInfo>(type()); | ||
| 55 | + internal_field_info_->encode_into_results_buffer = | ||
| 56 | + encode_into_results_buffer_.Serialize(context, creator); | ||
| 46 | 57 | // Return true because we need to maintain the reference to the binding from | |
| 47 | 58 | // JS land. | |
| 48 | 59 | return true; | |
| 49 | 60 | } | |
| 50 | 61 | ||
| 51 | 62 | InternalFieldInfoBase* BindingData::Serialize(int index) { | |
| 52 | 63 | DCHECK_EQ(index, BaseObject::kEmbedderType); | |
| 53 | - InternalFieldInfo* info = | ||
| 54 | - InternalFieldInfoBase::New<InternalFieldInfo>(type()); | ||
| 64 | + InternalFieldInfo* info = internal_field_info_; | ||
| 65 | + internal_field_info_ = nullptr; | ||
| 55 | 66 | return info; | |
| 56 | 67 | } | |
| 57 | 68 | ||
@@ -63,7 +74,9 @@ void BindingData::Deserialize(Local<Context> context, | |||
| 63 | 74 | v8::HandleScope scope(context->GetIsolate()); | |
| 64 | 75 | Realm* realm = Realm::GetCurrent(context); | |
| 65 | 76 | // Recreate the buffer in the constructor. | |
| 66 | - BindingData* binding = realm->AddBindingData<BindingData>(context, holder); | ||
| 77 | + InternalFieldInfo* casted_info = static_cast<InternalFieldInfo*>(info); | ||
| 78 | + BindingData* binding = | ||
| 79 | + realm->AddBindingData<BindingData>(context, holder, casted_info); | ||
| 67 | 80 | CHECK_NOT_NULL(binding); | |
| 68 | 81 | } | |
| 69 | 82 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,10 +14,13 @@ class ExternalReferenceRegistry; | |||
| 14 | 14 | namespace encoding_binding { | |
| 15 | 15 | class BindingData : public SnapshotableObject { | |
| 16 | 16 | public: | |
| 17 | - BindingData(Realm* realm, v8::Local<v8::Object> obj); | ||
| 18 | - | ||
| 19 | - using InternalFieldInfo = InternalFieldInfoBase; | ||
| 17 | + struct InternalFieldInfo : public node::InternalFieldInfoBase { | ||
| 18 | + AliasedBufferIndex encode_into_results_buffer; | ||
| 19 | + }; | ||
| 20 | 20 | ||
| 21 | + BindingData(Realm* realm, | ||
| 22 | + v8::Local<v8::Object> obj, | ||
| 23 | + InternalFieldInfo* info = nullptr); | ||
| 21 | 24 | SERIALIZABLE_OBJECT_METHODS() | |
| 22 | 25 | SET_BINDING_ID(encoding_binding_data) | |
| 23 | 26 | ||
@@ -39,6 +42,7 @@ class BindingData : public SnapshotableObject { | |||
| 39 | 42 | private: | |
| 40 | 43 | static constexpr size_t kEncodeIntoResultsLength = 2; | |
| 41 | 44 | AliasedUint32Array encode_into_results_buffer_; | |
| 45 | + InternalFieldInfo* internal_field_info_ = nullptr; | ||
| 42 | 46 | }; | |
| 43 | 47 | ||
| 44 | 48 | } // namespace encoding_binding | |
| Back | FazBrowse Home | New Git URL |
0 commit comments