| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent f17c794 commit 1a92c88
13 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -42,7 +42,7 @@ class AliasedBufferBase { | |||
| 42 | 42 | // allocate v8 ArrayBuffer | |
| 43 | 43 | v8::Local<v8::ArrayBuffer> ab = v8::ArrayBuffer::New( | |
| 44 | 44 | isolate_, size_in_bytes); | |
| 45 | - buffer_ = static_cast<NativeT*>(ab->GetContents().Data()); | ||
| 45 | + buffer_ = static_cast<NativeT*>(ab->GetBackingStore()->Data()); | ||
| 46 | 46 | ||
| 47 | 47 | // allocate v8 TypedArray | |
| 48 | 48 | v8::Local<V8T> js_array = V8T::New(ab, byte_offset_, count); | |
@@ -228,7 +228,7 @@ class AliasedBufferBase { | |||
| 228 | 228 | isolate_, new_size_in_bytes); | |
| 229 | 229 | ||
| 230 | 230 | // allocate new native buffer | |
| 231 | - NativeT* new_buffer = static_cast<NativeT*>(ab->GetContents().Data()); | ||
| 231 | + NativeT* new_buffer = static_cast<NativeT*>(ab->GetBackingStore()->Data()); | ||
| 232 | 232 | // copy old content | |
| 233 | 233 | memcpy(new_buffer, buffer_, old_size_in_bytes); | |
| 234 | 234 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -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( | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -192,16 +192,13 @@ bool HasInstance(Local<Object> obj) { | |||
| 192 | 192 | char* Data(Local<Value> val) { | |
| 193 | 193 | CHECK(val->IsArrayBufferView()); | |
| 194 | 194 | Local<ArrayBufferView> ui = val.As<ArrayBufferView>(); | |
| 195 | - ArrayBuffer::Contents ab_c = ui->Buffer()->GetContents(); | ||
| 196 | - return static_cast<char*>(ab_c.Data()) + ui->ByteOffset(); | ||
| 195 | + return static_cast<char*>(ui->Buffer()->GetBackingStore()->Data()) + | ||
| 196 | + ui->ByteOffset(); | ||
| 197 | 197 | } | |
| 198 | 198 | ||
| 199 | 199 | ||
| 200 | 200 | char* Data(Local<Object> obj) { | |
| 201 | - CHECK(obj->IsArrayBufferView()); | ||
| 202 | - Local<ArrayBufferView> ui = obj.As<ArrayBufferView>(); | ||
| 203 | - ArrayBuffer::Contents ab_c = ui->Buffer()->GetContents(); | ||
| 204 | - return static_cast<char*>(ab_c.Data()) + ui->ByteOffset(); | ||
| 201 | + return Data(obj.As<Value>()); | ||
| 205 | 202 | } | |
| 206 | 203 | ||
| 207 | 204 | ||
@@ -1060,13 +1057,13 @@ static void EncodeInto(const FunctionCallbackInfo<Value>& args) { | |||
| 1060 | 1057 | Local<Uint8Array> dest = args[1].As<Uint8Array>(); | |
| 1061 | 1058 | Local<ArrayBuffer> buf = dest->Buffer(); | |
| 1062 | 1059 | char* write_result = | |
| 1063 | - static_cast<char*>(buf->GetContents().Data()) + dest->ByteOffset(); | ||
| 1060 | + static_cast<char*>(buf->GetBackingStore()->Data()) + dest->ByteOffset(); | ||
| 1064 | 1061 | size_t dest_length = dest->ByteLength(); | |
| 1065 | 1062 | ||
| 1066 | 1063 | // results = [ read, written ] | |
| 1067 | 1064 | Local<Uint32Array> result_arr = args[2].As<Uint32Array>(); | |
| 1068 | 1065 | uint32_t* results = reinterpret_cast<uint32_t*>( | |
| 1069 | - static_cast<char*>(result_arr->Buffer()->GetContents().Data()) + | ||
| 1066 | + static_cast<char*>(result_arr->Buffer()->GetBackingStore()->Data()) + | ||
| 1070 | 1067 | result_arr->ByteOffset()); | |
| 1071 | 1068 | ||
| 1072 | 1069 | int nchars; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -697,8 +697,8 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) { | |||
| 697 | 697 | ||
| 698 | 698 | ScriptCompiler::CachedData* cached_data = nullptr; | |
| 699 | 699 | if (!cached_data_buf.IsEmpty()) { | |
| 700 | - ArrayBuffer::Contents contents = cached_data_buf->Buffer()->GetContents(); | ||
| 701 | - uint8_t* data = static_cast<uint8_t*>(contents.Data()); | ||
| 700 | + uint8_t* data = static_cast<uint8_t*>( | ||
| 701 | + cached_data_buf->Buffer()->GetBackingStore()->Data()); | ||
| 702 | 702 | cached_data = new ScriptCompiler::CachedData( | |
| 703 | 703 | data + cached_data_buf->ByteOffset(), cached_data_buf->ByteLength()); | |
| 704 | 704 | } | |
@@ -1044,8 +1044,8 @@ void ContextifyContext::CompileFunction( | |||
| 1044 | 1044 | // Read cache from cached data buffer | |
| 1045 | 1045 | ScriptCompiler::CachedData* cached_data = nullptr; | |
| 1046 | 1046 | if (!cached_data_buf.IsEmpty()) { | |
| 1047 | - ArrayBuffer::Contents contents = cached_data_buf->Buffer()->GetContents(); | ||
| 1048 | - uint8_t* data = static_cast<uint8_t*>(contents.Data()); | ||
| 1047 | + uint8_t* data = static_cast<uint8_t*>( | ||
| 1048 | + cached_data_buf->Buffer()->GetBackingStore()->Data()); | ||
| 1049 | 1049 | cached_data = new ScriptCompiler::CachedData( | |
| 1050 | 1050 | data + cached_data_buf->ByteOffset(), cached_data_buf->ByteLength()); | |
| 1051 | 1051 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -297,12 +297,19 @@ Maybe<bool> Message::Serialize(Environment* env, | |||
| 297 | 297 | // Currently, we support ArrayBuffers and MessagePorts. | |
| 298 | 298 | if (entry->IsArrayBuffer()) { | |
| 299 | 299 | Local<ArrayBuffer> ab = entry.As<ArrayBuffer>(); | |
| 300 | - // If we cannot render the ArrayBuffer unusable in this Isolate and | ||
| 301 | - // take ownership of its memory, copying the buffer will have to do. | ||
| 302 | - if (!ab->IsDetachable() || ab->IsExternal() || | ||
| 303 | - !env->isolate_data()->uses_node_allocator()) { | ||
| 300 | + // If we cannot render the ArrayBuffer unusable in this Isolate, | ||
| 301 | + // copying the buffer will have to do. | ||
| 302 | + // Note that we can currently transfer ArrayBuffers even if they were | ||
| 303 | + // not allocated by Node’s ArrayBufferAllocator in the first place, | ||
| 304 | + // because we pass the underlying v8::BackingStore around rather than | ||
| 305 | + // raw data *and* an Isolate with a non-default ArrayBuffer allocator | ||
| 306 | + // is always going to outlive any Workers it creates, and so will its | ||
| 307 | + // allocator along with it. | ||
| 308 | + // TODO(addaleax): Eventually remove the IsExternal() condition, | ||
| 309 | + // see https://github.com/nodejs/node/pull/30339#issuecomment-552225353 | ||
| 310 | + // for details. | ||
| 311 | + if (!ab->IsDetachable() || ab->IsExternal()) | ||
| 304 | 312 | continue; | |
| 305 | - } | ||
| 306 | 313 | if (std::find(array_buffers.begin(), array_buffers.end(), ab) != | |
| 307 | 314 | array_buffers.end()) { | |
| 308 | 315 | ThrowDataCloneException( | |
@@ -362,7 +369,9 @@ Maybe<bool> Message::Serialize(Environment* env, | |||
| 362 | 369 | for (Local<ArrayBuffer> ab : array_buffers) { | |
| 363 | 370 | // If serialization succeeded, we render it inaccessible in this Isolate. | |
| 364 | 371 | std::shared_ptr<BackingStore> backing_store = ab->GetBackingStore(); | |
| 365 | - ab->Externalize(backing_store); | ||
| 372 | + // TODO(addaleax): This can/should be dropped once we have V8 8.0. | ||
| 373 | + if (!ab->IsExternal()) | ||
| 374 | + ab->Externalize(backing_store); | ||
| 366 | 375 | ab->Detach(); | |
| 367 | 376 | ||
| 368 | 377 | array_buffers_.emplace_back(std::move(backing_store)); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -171,7 +171,7 @@ static void GetLoadAvg(const FunctionCallbackInfo<Value>& args) { | |||
| 171 | 171 | Local<Float64Array> array = args[0].As<Float64Array>(); | |
| 172 | 172 | CHECK_EQ(array->Length(), 3); | |
| 173 | 173 | Local<ArrayBuffer> ab = array->Buffer(); | |
| 174 | - double* loadavg = static_cast<double*>(ab->GetContents().Data()); | ||
| 174 | + double* loadavg = static_cast<double*>(ab->GetBackingStore()->Data()); | ||
| 175 | 175 | uv_loadavg(loadavg); | |
| 176 | 176 | } | |
| 177 | 177 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -109,7 +109,7 @@ static void CPUUsage(const FunctionCallbackInfo<Value>& args) { | |||
| 109 | 109 | Local<Float64Array> array = args[0].As<Float64Array>(); | |
| 110 | 110 | CHECK_EQ(array->Length(), 2); | |
| 111 | 111 | Local<ArrayBuffer> ab = array->Buffer(); | |
| 112 | - double* fields = static_cast<double*>(ab->GetContents().Data()); | ||
| 112 | + double* fields = static_cast<double*>(ab->GetBackingStore()->Data()); | ||
| 113 | 113 | ||
| 114 | 114 | // Set the Float64Array elements to be user / system values in microseconds. | |
| 115 | 115 | fields[0] = MICROS_PER_SEC * rusage.ru_utime.tv_sec + rusage.ru_utime.tv_usec; | |
@@ -148,7 +148,7 @@ static void Hrtime(const FunctionCallbackInfo<Value>& args) { | |||
| 148 | 148 | uint64_t t = uv_hrtime(); | |
| 149 | 149 | ||
| 150 | 150 | Local<ArrayBuffer> ab = args[0].As<Uint32Array>()->Buffer(); | |
| 151 | - uint32_t* fields = static_cast<uint32_t*>(ab->GetContents().Data()); | ||
| 151 | + uint32_t* fields = static_cast<uint32_t*>(ab->GetBackingStore()->Data()); | ||
| 152 | 152 | ||
| 153 | 153 | fields[0] = (t / NANOS_PER_SEC) >> 32; | |
| 154 | 154 | fields[1] = (t / NANOS_PER_SEC) & 0xffffffff; | |
@@ -157,7 +157,7 @@ static void Hrtime(const FunctionCallbackInfo<Value>& args) { | |||
| 157 | 157 | ||
| 158 | 158 | static void HrtimeBigInt(const FunctionCallbackInfo<Value>& args) { | |
| 159 | 159 | Local<ArrayBuffer> ab = args[0].As<BigUint64Array>()->Buffer(); | |
| 160 | - uint64_t* fields = static_cast<uint64_t*>(ab->GetContents().Data()); | ||
| 160 | + uint64_t* fields = static_cast<uint64_t*>(ab->GetBackingStore()->Data()); | ||
| 161 | 161 | fields[0] = uv_hrtime(); | |
| 162 | 162 | } | |
| 163 | 163 | ||
@@ -204,7 +204,7 @@ static void MemoryUsage(const FunctionCallbackInfo<Value>& args) { | |||
| 204 | 204 | Local<Float64Array> array = args[0].As<Float64Array>(); | |
| 205 | 205 | CHECK_EQ(array->Length(), 4); | |
| 206 | 206 | Local<ArrayBuffer> ab = array->Buffer(); | |
| 207 | - double* fields = static_cast<double*>(ab->GetContents().Data()); | ||
| 207 | + double* fields = static_cast<double*>(ab->GetBackingStore()->Data()); | ||
| 208 | 208 | ||
| 209 | 209 | fields[0] = rss; | |
| 210 | 210 | fields[1] = v8_heap_stats.total_heap_size(); | |
@@ -301,7 +301,7 @@ static void ResourceUsage(const FunctionCallbackInfo<Value>& args) { | |||
| 301 | 301 | Local<Float64Array> array = args[0].As<Float64Array>(); | |
| 302 | 302 | CHECK_EQ(array->Length(), 16); | |
| 303 | 303 | Local<ArrayBuffer> ab = array->Buffer(); | |
| 304 | - double* fields = static_cast<double*>(ab->GetContents().Data()); | ||
| 304 | + double* fields = static_cast<double*>(ab->GetBackingStore()->Data()); | ||
| 305 | 305 | ||
| 306 | 306 | fields[0] = MICROS_PER_SEC * rusage.ru_utime.tv_sec + rusage.ru_utime.tv_usec; | |
| 307 | 307 | fields[1] = MICROS_PER_SEC * rusage.ru_stime.tv_sec + rusage.ru_stime.tv_usec; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -623,7 +623,9 @@ void Worker::GetResourceLimits(const FunctionCallbackInfo<Value>& args) { | |||
| 623 | 623 | ||
| 624 | 624 | Local<Float64Array> Worker::GetResourceLimits(Isolate* isolate) const { | |
| 625 | 625 | Local<ArrayBuffer> ab = ArrayBuffer::New(isolate, sizeof(resource_limits_)); | |
| 626 | - memcpy(ab->GetContents().Data(), resource_limits_, sizeof(resource_limits_)); | ||
| 626 | + memcpy(ab->GetBackingStore()->Data(), | ||
| 627 | + resource_limits_, | ||
| 628 | + sizeof(resource_limits_)); | ||
| 627 | 629 | return Float64Array::New(ab, 0, kTotalResourceLimitCount); | |
| 628 | 630 | } | |
| 629 | 631 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -590,7 +590,8 @@ class ZlibStream : public CompressionStream<ZlibContext> { | |||
| 590 | 590 | CHECK(args[4]->IsUint32Array()); | |
| 591 | 591 | Local<Uint32Array> array = args[4].As<Uint32Array>(); | |
| 592 | 592 | Local<ArrayBuffer> ab = array->Buffer(); | |
| 593 | - uint32_t* write_result = static_cast<uint32_t*>(ab->GetContents().Data()); | ||
| 593 | + uint32_t* write_result = static_cast<uint32_t*>( | ||
| 594 | + ab->GetBackingStore()->Data()); | ||
| 594 | 595 | ||
| 595 | 596 | CHECK(args[5]->IsFunction()); | |
| 596 | 597 | Local<Function> write_js_callback = args[5].As<Function>(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -514,7 +514,7 @@ void ArrayBufferViewContents<T, S>::Read(v8::Local<v8::ArrayBufferView> abv) { | |||
| 514 | 514 | static_assert(sizeof(T) == 1, "Only supports one-byte data at the moment"); | |
| 515 | 515 | length_ = abv->ByteLength(); | |
| 516 | 516 | if (length_ > sizeof(stack_storage_) || abv->HasBuffer()) { | |
| 517 | - data_ = static_cast<T*>(abv->Buffer()->GetContents().Data()) + | ||
| 517 | + data_ = static_cast<T*>(abv->Buffer()->GetBackingStore()->Data()) + | ||
| 518 | 518 | abv->ByteOffset(); | |
| 519 | 519 | } else { | |
| 520 | 520 | abv->CopyContents(stack_storage_, sizeof(stack_storage_)); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments