@@ -2562,7 +2562,7 @@ napi_status napi_create_arraybuffer(napi_env env,
25622562 // Optionally return a pointer to the buffer's data, to avoid another call to
25632563 // retrieve it.
25642564 if (data != nullptr ) {
2565- *data = buffer->GetContents (). Data ();
2565+ *data = buffer->GetBackingStore ()-> Data ();
25662566 }
25672567
25682568 *result = v8impl::JsValueFromV8LocalValue (buffer);
@@ -2608,15 +2608,15 @@ napi_status napi_get_arraybuffer_info(napi_env env,
26082608 v8::Local<v8::Value> value = v8impl::V8LocalValueFromJsValue (arraybuffer);
26092609 RETURN_STATUS_IF_FALSE (env, value->IsArrayBuffer (), napi_invalid_arg);
26102610
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 ();
26132613
26142614 if (data != nullptr ) {
2615- *data = contents. Data ();
2615+ *data = backing_store-> Data ();
26162616 }
26172617
26182618 if (byte_length != nullptr ) {
2619- *byte_length = contents. ByteLength ();
2619+ *byte_length = backing_store-> ByteLength ();
26202620 }
26212621
26222622 return napi_clear_last_error (env);
@@ -2747,9 +2747,15 @@ napi_status napi_get_typedarray_info(napi_env env,
27472747 *length = array->Length ();
27482748 }
27492749
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+
27512757 if (data != nullptr ) {
2752- *data = static_cast <uint8_t *>(buffer->GetContents (). Data ()) +
2758+ *data = static_cast <uint8_t *>(buffer->GetBackingStore ()-> Data ()) +
27532759 array->ByteOffset ();
27542760 }
27552761
@@ -2821,9 +2827,15 @@ napi_status napi_get_dataview_info(napi_env env,
28212827 *byte_length = array->ByteLength ();
28222828 }
28232829
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+
28252837 if (data != nullptr ) {
2826- *data = static_cast <uint8_t *>(buffer->GetContents (). Data ()) +
2838+ *data = static_cast <uint8_t *>(buffer->GetBackingStore ()-> Data ()) +
28272839 array->ByteOffset ();
28282840 }
28292841
@@ -3015,6 +3027,7 @@ napi_status napi_detach_arraybuffer(napi_env env, napi_value arraybuffer) {
30153027 env, value->IsArrayBuffer (), napi_arraybuffer_expected);
30163028
30173029 v8::Local<v8::ArrayBuffer> it = value.As <v8::ArrayBuffer>();
3030+ // TODO(addaleax): Remove the first condition once we have V8 8.0.
30183031 RETURN_STATUS_IF_FALSE (
30193032 env, it->IsExternal (), napi_detachable_arraybuffer_expected);
30203033 RETURN_STATUS_IF_FALSE (
0 commit comments