| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 27d7588 commit 3649ec5
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -303,28 +303,36 @@ MaybeLocal<Object> New(Isolate* isolate, | |||
| 303 | 303 | if (!StringBytes::Size(isolate, string, enc).To(&length)) | |
| 304 | 304 | return Local<Object>(); | |
| 305 | 305 | size_t actual = 0; | |
| 306 | - char* data = nullptr; | ||
| 306 | + std::unique_ptr<BackingStore> store; | ||
| 307 | 307 | ||
| 308 | 308 | if (length > 0) { | |
| 309 | - data = UncheckedMalloc(length); | ||
| 309 | + store = ArrayBuffer::NewBackingStore(isolate, length); | ||
| 310 | 310 | ||
| 311 | - if (data == nullptr) { | ||
| 311 | + if (UNLIKELY(!store)) { | ||
| 312 | 312 | THROW_ERR_MEMORY_ALLOCATION_FAILED(isolate); | |
| 313 | 313 | return Local<Object>(); | |
| 314 | 314 | } | |
| 315 | 315 | ||
| 316 | - actual = StringBytes::Write(isolate, data, length, string, enc); | ||
| 316 | + actual = StringBytes::Write( | ||
| 317 | + isolate, | ||
| 318 | + static_cast<char*>(store->Data()), | ||
| 319 | + length, | ||
| 320 | + string, | ||
| 321 | + enc); | ||
| 317 | 322 | CHECK(actual <= length); | |
| 318 | 323 | ||
| 319 | - if (actual == 0) { | ||
| 320 | - free(data); | ||
| 321 | - data = nullptr; | ||
| 322 | - } else if (actual < length) { | ||
| 323 | - data = node::Realloc(data, actual); | ||
| 324 | + if (LIKELY(actual > 0)) { | ||
| 325 | + if (actual < length) | ||
| 326 | + store = BackingStore::Reallocate(isolate, std::move(store), actual); | ||
| 327 | + Local<ArrayBuffer> buf = ArrayBuffer::New(isolate, std::move(store)); | ||
| 328 | + Local<Object> obj; | ||
| 329 | + if (UNLIKELY(!New(isolate, buf, 0, actual).ToLocal(&obj))) | ||
| 330 | + return MaybeLocal<Object>(); | ||
| 331 | + return scope.Escape(obj); | ||
| 324 | 332 | } | |
| 325 | 333 | } | |
| 326 | 334 | ||
| 327 | - return scope.EscapeMaybe(New(isolate, data, actual)); | ||
| 335 | + return scope.EscapeMaybe(New(isolate, 0)); | ||
| 328 | 336 | } | |
| 329 | 337 | ||
| 330 | 338 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments