@@ -2562,7 +2562,7 @@ napi_status napi_create_arraybuffer(napi_env env,
2562
2562
// Optionally return a pointer to the buffer's data, to avoid another call to
2563
2563
// retrieve it.
2564
2564
if (data != nullptr ) {
2565
- *data = buffer->GetContents (). Data ();
2565
+ *data = buffer->GetBackingStore ()-> Data ();
2566
2566
}
2567
2567
2568
2568
*result = v8impl::JsValueFromV8LocalValue (buffer);
@@ -2608,15 +2608,15 @@ napi_status napi_get_arraybuffer_info(napi_env env,
2608
2608
v8::Local<v8::Value> value = v8impl::V8LocalValueFromJsValue (arraybuffer);
2609
2609
RETURN_STATUS_IF_FALSE (env, value->IsArrayBuffer (), napi_invalid_arg);
2610
2610
2611
- v8::ArrayBuffer::Contents contents =
2612
- value.As <v8::ArrayBuffer>()->GetContents ();
2611
+ std::shared_ptr<v8::BackingStore> backing_store =
2612
+ value.As <v8::ArrayBuffer>()->GetBackingStore ();
2613
2613
2614
2614
if (data != nullptr ) {
2615
- *data = contents. Data ();
2615
+ *data = backing_store-> Data ();
2616
2616
}
2617
2617
2618
2618
if (byte_length != nullptr ) {
2619
- *byte_length = contents. ByteLength ();
2619
+ *byte_length = backing_store-> ByteLength ();
2620
2620
}
2621
2621
2622
2622
return napi_clear_last_error (env);
@@ -2747,9 +2747,15 @@ napi_status napi_get_typedarray_info(napi_env env,
2747
2747
*length = array->Length ();
2748
2748
}
2749
2749
2750
- v8::Local<v8::ArrayBuffer> buffer = array->Buffer ();
2750
+ v8::Local<v8::ArrayBuffer> buffer;
2751
+ if (data != nullptr || arraybuffer != nullptr ) {
2752
+ // Calling Buffer() may have the side effect of allocating the buffer,
2753
+ // so only do this when it’s needed.
2754
+ buffer = array->Buffer ();
2755
+ }
2756
+
2751
2757
if (data != nullptr ) {
2752
- *data = static_cast <uint8_t *>(buffer->GetContents (). Data ()) +
2758
+ *data = static_cast <uint8_t *>(buffer->GetBackingStore ()-> Data ()) +
2753
2759
array->ByteOffset ();
2754
2760
}
2755
2761
@@ -2821,9 +2827,15 @@ napi_status napi_get_dataview_info(napi_env env,
2821
2827
*byte_length = array->ByteLength ();
2822
2828
}
2823
2829
2824
- v8::Local<v8::ArrayBuffer> buffer = array->Buffer ();
2830
+ v8::Local<v8::ArrayBuffer> buffer;
2831
+ if (data != nullptr || arraybuffer != nullptr ) {
2832
+ // Calling Buffer() may have the side effect of allocating the buffer,
2833
+ // so only do this when it’s needed.
2834
+ buffer = array->Buffer ();
2835
+ }
2836
+
2825
2837
if (data != nullptr ) {
2826
- *data = static_cast <uint8_t *>(buffer->GetContents (). Data ()) +
2838
+ *data = static_cast <uint8_t *>(buffer->GetBackingStore ()-> Data ()) +
2827
2839
array->ByteOffset ();
2828
2840
}
2829
2841
@@ -3015,6 +3027,7 @@ napi_status napi_detach_arraybuffer(napi_env env, napi_value arraybuffer) {
3015
3027
env, value->IsArrayBuffer (), napi_arraybuffer_expected);
3016
3028
3017
3029
v8::Local<v8::ArrayBuffer> it = value.As <v8::ArrayBuffer>();
3030
+ // TODO(addaleax): Remove the first condition once we have V8 8.0.
3018
3031
RETURN_STATUS_IF_FALSE (
3019
3032
env, it->IsExternal (), napi_detachable_arraybuffer_expected);
3020
3033
RETURN_STATUS_IF_FALSE (
0 commit comments