@@ -59,6 +59,7 @@ using v8::ArrayBuffer; |
59 | 59 | using v8::ArrayBufferView; |
60 | 60 | using v8::BackingStore; |
61 | 61 | using v8::BackingStoreInitializationMode; |
| 62 | +using v8::BackingStoreOnFailureMode; |
62 | 63 | using v8::CFunction; |
63 | 64 | using v8::Context; |
64 | 65 | using v8::EscapableHandleScope; |
@@ -304,17 +305,20 @@ MaybeLocal<Object> New(Isolate* isolate, |
304 | 305 | EscapableHandleScope scope(isolate); |
305 | 306 |
|
306 | 307 | size_t length; |
307 | | - if (!StringBytes::Size(isolate, string, enc).To(&length)) |
308 | | - return Local<Object>(); |
| 308 | + if (!StringBytes::Size(isolate, string, enc).To(&length)) return {}; |
309 | 309 | size_t actual = 0; |
310 | 310 | std::unique_ptr<BackingStore> store; |
311 | 311 |
|
312 | 312 | if (length > 0) { |
313 | | - store = ArrayBuffer::NewBackingStore(isolate, length); |
| 313 | + store = ArrayBuffer::NewBackingStore( |
| 314 | + isolate, |
| 315 | + length, |
| 316 | + BackingStoreInitializationMode::kZeroInitialized, |
| 317 | + BackingStoreOnFailureMode::kReturnNull); |
314 | 318 |
|
315 | 319 | if (!store) [[unlikely]] { |
316 | 320 | THROW_ERR_MEMORY_ALLOCATION_FAILED(isolate); |
317 | | - return Local<Object>(); |
| 321 | + return {}; |
318 | 322 | } |
319 | 323 |
|
320 | 324 | actual = StringBytes::Write( |
@@ -329,7 +333,14 @@ MaybeLocal<Object> New(Isolate* isolate, |
329 | 333 | if (actual < length) { |
330 | 334 | std::unique_ptr<BackingStore> old_store = std::move(store); |
331 | 335 | store = ArrayBuffer::NewBackingStore( |
332 | | - isolate, actual, BackingStoreInitializationMode::kUninitialized); |
| 336 | + isolate, |
| 337 | + actual, |
| 338 | + BackingStoreInitializationMode::kUninitialized, |
| 339 | + BackingStoreOnFailureMode::kReturnNull); |
| 340 | + if (!store) [[unlikely]] { |
| 341 | + THROW_ERR_MEMORY_ALLOCATION_FAILED(isolate); |
| 342 | + return {}; |
| 343 | + } |
333 | 344 | memcpy(store->Data(), old_store->Data(), actual); |
334 | 345 | } |
335 | 346 | Local<ArrayBuffer> buf = ArrayBuffer::New(isolate, std::move(store)); |
@@ -372,7 +383,14 @@ MaybeLocal<Object> New(Environment* env, size_t length) { |
372 | 383 | Local<ArrayBuffer> ab; |
373 | 384 | { |
374 | 385 | std::unique_ptr<BackingStore> bs = ArrayBuffer::NewBackingStore( |
375 | | - isolate, length, BackingStoreInitializationMode::kUninitialized); |
| 386 | + isolate, |
| 387 | + length, |
| 388 | + BackingStoreInitializationMode::kUninitialized, |
| 389 | + BackingStoreOnFailureMode::kReturnNull); |
| 390 | + if (!bs) [[unlikely]] { |
| 391 | + THROW_ERR_MEMORY_ALLOCATION_FAILED(isolate); |
| 392 | + return {}; |
| 393 | + } |
376 | 394 |
|
377 | 395 | CHECK(bs); |
378 | 396 |
|
@@ -412,9 +430,14 @@ MaybeLocal<Object> Copy(Environment* env, const char* data, size_t length) { |
412 | 430 | } |
413 | 431 |
|
414 | 432 | std::unique_ptr<BackingStore> bs = ArrayBuffer::NewBackingStore( |
415 | | - isolate, length, BackingStoreInitializationMode::kUninitialized); |
416 | | - |
417 | | - CHECK(bs); |
| 433 | + isolate, |
| 434 | + length, |
| 435 | + BackingStoreInitializationMode::kUninitialized, |
| 436 | + BackingStoreOnFailureMode::kReturnNull); |
| 437 | + if (!bs) [[unlikely]] { |
| 438 | + THROW_ERR_MEMORY_ALLOCATION_FAILED(isolate); |
| 439 | + return {}; |
| 440 | + } |
418 | 441 |
|
419 | 442 | if (length > 0) memcpy(bs->Data(), data, length); |
420 | 443 |
|
@@ -1449,8 +1472,8 @@ void CreateUnsafeArrayBuffer(const FunctionCallbackInfo<Value>& args) { |
1449 | 1472 | BackingStoreInitializationMode::kUninitialized, |
1450 | 1473 | v8::BackingStoreOnFailureMode::kReturnNull); |
1451 | 1474 |
|
1452 | | - if (!store) { |
1453 | | - env->ThrowRangeError("Array buffer allocation failed"); |
| 1475 | + if (!store) [[unlikely]] { |
| 1476 | + THROW_ERR_MEMORY_ALLOCATION_FAILED(env); |
1454 | 1477 | return; |
1455 | 1478 | } |
1456 | 1479 |
|
|