|
| 1 | +#include "node_blob.h" |
| 2 | +#include "async_wrap-inl.h" |
| 3 | +#include "base_object-inl.h" |
| 4 | +#include "env-inl.h" |
| 5 | +#include "memory_tracker-inl.h" |
| 6 | +#include "node_errors.h" |
| 7 | +#include "node_external_reference.h" |
| 8 | +#include "threadpoolwork-inl.h" |
| 9 | +#include "v8.h" |
| 10 | + |
| 11 | +#include <algorithm> |
| 12 | + |
| 13 | +namespace node { |
| 14 | + |
| 15 | +using v8::Array; |
| 16 | +using v8::ArrayBuffer; |
| 17 | +using v8::ArrayBufferView; |
| 18 | +using v8::BackingStore; |
| 19 | +using v8::Context; |
| 20 | +using v8::EscapableHandleScope; |
| 21 | +using v8::Function; |
| 22 | +using v8::FunctionCallbackInfo; |
| 23 | +using v8::FunctionTemplate; |
| 24 | +using v8::HandleScope; |
| 25 | +using v8::Local; |
| 26 | +using v8::MaybeLocal; |
| 27 | +using v8::Number; |
| 28 | +using v8::Object; |
| 29 | +using v8::Uint32; |
| 30 | +using v8::Undefined; |
| 31 | +using v8::Value; |
| 32 | + |
| 33 | +void Blob::Initialize(Environment* env, v8::Local<v8::Object> target) { |
| 34 | + env->SetMethod(target, "createBlob", New); |
| 35 | + FixedSizeBlobCopyJob::Initialize(env, target); |
| 36 | +} |
| 37 | + |
| 38 | +Local<FunctionTemplate> Blob::GetConstructorTemplate(Environment* env) { |
| 39 | + Local<FunctionTemplate> tmpl = env->blob_constructor_template(); |
| 40 | + if (tmpl.IsEmpty()) { |
| 41 | + tmpl = FunctionTemplate::New(env->isolate()); |
| 42 | + tmpl->InstanceTemplate()->SetInternalFieldCount(1); |
| 43 | + tmpl->Inherit(BaseObject::GetConstructorTemplate(env)); |
| 44 | + tmpl->SetClassName( |
| 45 | + FIXED_ONE_BYTE_STRING(env->isolate(), "Blob")); |
| 46 | + env->SetProtoMethod(tmpl, "toArrayBuffer", ToArrayBuffer); |
| 47 | + env->SetProtoMethod(tmpl, "slice", ToSlice); |
| 48 | + env->set_blob_constructor_template(tmpl); |
| 49 | + } |
| 50 | + return tmpl; |
| 51 | +} |
| 52 | + |
| 53 | +bool Blob::HasInstance(Environment* env, v8::Local<v8::Value> object) { |
| 54 | + return GetConstructorTemplate(env)->HasInstance(object); |
| 55 | +} |
| 56 | + |
| 57 | +BaseObjectPtr<Blob> Blob::Create( |
| 58 | + Environment* env, |
| 59 | + const std::vector<BlobEntry> store, |
| 60 | + size_t length) { |
| 61 | + |
| 62 | + HandleScope scope(env->isolate()); |
| 63 | + |
| 64 | + Local<Function> ctor; |
| 65 | + if (!GetConstructorTemplate(env)->GetFunction(env->context()).ToLocal(&ctor)) |
| 66 | + return BaseObjectPtr<Blob>(); |
| 67 | + |
| 68 | + Local<Object> obj; |
| 69 | + if (!ctor->NewInstance(env->context()).ToLocal(&obj)) |
| 70 | + return BaseObjectPtr<Blob>(); |
| 71 | + |
| 72 | + return MakeBaseObject<Blob>(env, obj, store, length); |
| 73 | +} |
| 74 | + |
| 75 | +void Blob::New(const FunctionCallbackInfo<Value>& args) { |
| 76 | + Environment* env = Environment::GetCurrent(args); |
| 77 | + CHECK(args[0]->IsArray()); // sources |
| 78 | + CHECK(args[1]->IsUint32()); // length |
| 79 | + |
| 80 | + std::vector<BlobEntry> entries; |
| 81 | + |
| 82 | + size_t length = args[1].As<Uint32>()->Value(); |
| 83 | + size_t len = 0; |
| 84 | + Local<Array> ary = args[0].As<Array>(); |
| 85 | + for (size_t n = 0; n < ary->Length(); n++) { |
| 86 | + Local<Value> entry; |
| 87 | + if (!ary->Get(env->context(), n).ToLocal(&entry)) |
| 88 | + return; |
| 89 | + CHECK(entry->IsArrayBufferView() || Blob::HasInstance(env, entry)); |
| 90 | + if (entry->IsArrayBufferView()) { |
| 91 | + Local<ArrayBufferView> view = entry.As<ArrayBufferView>(); |
| 92 | + CHECK_EQ(view->ByteOffset(), 0); |
| 93 | + std::shared_ptr<BackingStore> store = view->Buffer()->GetBackingStore(); |
| 94 | + size_t byte_length = view->ByteLength(); |
| 95 | + view->Buffer()->Detach(); // The Blob will own the backing store now. |
| 96 | + entries.emplace_back(BlobEntry{std::move(store), byte_length, 0}); |
| 97 | + len += byte_length; |
| 98 | + } else { |
| 99 | + Blob* blob; |
| 100 | + ASSIGN_OR_RETURN_UNWRAP(&blob, entry); |
| 101 | + auto source = blob->entries(); |
| 102 | + entries.insert(entries.end(), source.begin(), source.end()); |
| 103 | + len += blob->length(); |
| 104 | + } |
| 105 | + } |
| 106 | + CHECK_EQ(length, len); |
| 107 | + |
| 108 | + BaseObjectPtr<Blob> blob = Create(env, entries, length); |
| 109 | + if (blob) |
| 110 | + args.GetReturnValue().Set(blob->object()); |
| 111 | +} |
| 112 | + |
| 113 | +void Blob::ToArrayBuffer(const FunctionCallbackInfo<Value>& args) { |
| 114 | + Environment* env = Environment::GetCurrent(args); |
| 115 | + Blob* blob; |
| 116 | + ASSIGN_OR_RETURN_UNWRAP(&blob, args.Holder()); |
| 117 | + Local<Value> ret; |
| 118 | + if (blob->GetArrayBuffer(env).ToLocal(&ret)) |
| 119 | + args.GetReturnValue().Set(ret); |
| 120 | +} |
| 121 | + |
| 122 | +void Blob::ToSlice(const FunctionCallbackInfo<Value>& args) { |
| 123 | + Environment* env = Environment::GetCurrent(args); |
| 124 | + Blob* blob; |
| 125 | + ASSIGN_OR_RETURN_UNWRAP(&blob, args.Holder()); |
| 126 | + CHECK(args[0]->IsUint32()); |
| 127 | + CHECK(args[1]->IsUint32()); |
| 128 | + size_t start = args[0].As<Uint32>()->Value(); |
| 129 | + size_t end = args[1].As<Uint32>()->Value(); |
| 130 | + BaseObjectPtr<Blob> slice = blob->Slice(env, start, end); |
| 131 | + if (slice) |
| 132 | + args.GetReturnValue().Set(slice->object()); |
| 133 | +} |
| 134 | + |
| 135 | +void Blob::MemoryInfo(MemoryTracker* tracker) const { |
| 136 | + tracker->TrackFieldWithSize("store", length_); |
| 137 | +} |
| 138 | + |
| 139 | +MaybeLocal<Value> Blob::GetArrayBuffer(Environment* env) { |
| 140 | + EscapableHandleScope scope(env->isolate()); |
| 141 | + size_t len = length(); |
| 142 | + std::shared_ptr<BackingStore> store = |
| 143 | + ArrayBuffer::NewBackingStore(env->isolate(), len); |
| 144 | + if (len > 0) { |
| 145 | + unsigned char* dest = static_cast<unsigned char*>(store->Data()); |
| 146 | + size_t total = 0; |
| 147 | + for (const auto& entry : entries()) { |
| 148 | + unsigned char* src = static_cast<unsigned char*>(entry.store->Data()); |
| 149 | + src += entry.offset; |
| 150 | + memcpy(dest, src, entry.length); |
| 151 | + dest += entry.length; |
| 152 | + total += entry.length; |
| 153 | + CHECK_LE(total, len); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + return scope.Escape(ArrayBuffer::New(env->isolate(), store)); |
| 158 | +} |
| 159 | + |
| 160 | +BaseObjectPtr<Blob> Blob::Slice(Environment* env, size_t start, size_t end) { |
| 161 | + CHECK_LE(start, length()); |
| 162 | + CHECK_LE(end, length()); |
| 163 | + CHECK_LE(start, end); |
| 164 | + |
| 165 | + std::vector<BlobEntry> slices; |
| 166 | + size_t total = end - start; |
| 167 | + size_t remaining = total; |
| 168 | + |
| 169 | + if (total == 0) return Create(env, slices, 0); |
| 170 | + |
| 171 | + for (const auto& entry : entries()) { |
| 172 | + if (start + entry.offset > entry.store->ByteLength()) { |
| 173 | + start -= entry.length; |
| 174 | + continue; |
| 175 | + } |
| 176 | + |
| 177 | + size_t offset = entry.offset + start; |
| 178 | + size_t len = std::min(remaining, entry.store->ByteLength() - offset); |
| 179 | + slices.emplace_back(BlobEntry{entry.store, len, offset}); |
| 180 | + |
| 181 | + remaining -= len; |
| 182 | + start = 0; |
| 183 | + |
| 184 | + if (remaining == 0) |
| 185 | + break; |
| 186 | + } |
| 187 | + |
| 188 | + return Create(env, slices, total); |
| 189 | +} |
| 190 | + |
| 191 | +Blob::Blob( |
| 192 | + Environment* env, |
| 193 | + v8::Local<v8::Object> obj, |
| 194 | + const std::vector<BlobEntry>& store, |
| 195 | + size_t length) |
| 196 | + : BaseObject(env, obj), |
| 197 | + store_(store), |
| 198 | + length_(length) { |
| 199 | + MakeWeak(); |
| 200 | +} |
| 201 | + |
| 202 | +BaseObjectPtr<BaseObject> |
| 203 | +Blob::BlobTransferData::Deserialize( |
| 204 | + Environment* env, |
| 205 | + Local<Context> context, |
| 206 | + std::unique_ptr<worker::TransferData> self) { |
| 207 | + if (context != env->context()) { |
| 208 | + THROW_ERR_MESSAGE_TARGET_CONTEXT_UNAVAILABLE(env); |
| 209 | + return {}; |
| 210 | + } |
| 211 | + return Blob::Create(env, store_, length_); |
| 212 | +} |
| 213 | + |
| 214 | +BaseObject::TransferMode Blob::GetTransferMode() const { |
| 215 | + return BaseObject::TransferMode::kCloneable; |
| 216 | +} |
| 217 | + |
| 218 | +std::unique_ptr<worker::TransferData> Blob::CloneForMessaging() const { |
| 219 | + return std::make_unique<BlobTransferData>(store_, length_); |
| 220 | +} |
| 221 | + |
| 222 | +FixedSizeBlobCopyJob::FixedSizeBlobCopyJob( |
| 223 | + Environment* env, |
| 224 | + Local<Object> object, |
| 225 | + Blob* blob, |
| 226 | + FixedSizeBlobCopyJob::Mode mode) |
| 227 | + : AsyncWrap(env, object, AsyncWrap::PROVIDER_FIXEDSIZEBLOBCOPY), |
| 228 | + ThreadPoolWork(env), |
| 229 | + mode_(mode) { |
| 230 | + if (mode == FixedSizeBlobCopyJob::Mode::SYNC) MakeWeak(); |
| 231 | + source_ = blob->entries(); |
| 232 | + length_ = blob->length(); |
| 233 | +} |
| 234 | + |
| 235 | +void FixedSizeBlobCopyJob::AfterThreadPoolWork(int status) { |
| 236 | + Environment* env = AsyncWrap::env(); |
| 237 | + CHECK_EQ(mode_, Mode::ASYNC); |
| 238 | + CHECK(status == 0 || status == UV_ECANCELED); |
| 239 | + std::unique_ptr<FixedSizeBlobCopyJob> ptr(this); |
| 240 | + HandleScope handle_scope(env->isolate()); |
| 241 | + Context::Scope context_scope(env->context()); |
| 242 | + Local<Value> args[2]; |
| 243 | + |
| 244 | + if (status == UV_ECANCELED) { |
| 245 | + args[0] = Number::New(env->isolate(), status), |
| 246 | + args[1] = Undefined(env->isolate()); |
| 247 | + } else { |
| 248 | + args[0] = Undefined(env->isolate()); |
| 249 | + args[1] = ArrayBuffer::New(env->isolate(), destination_); |
| 250 | + } |
| 251 | + |
| 252 | + ptr->MakeCallback(env->ondone_string(), arraysize(args), args); |
| 253 | +} |
| 254 | + |
| 255 | +void FixedSizeBlobCopyJob::DoThreadPoolWork() { |
| 256 | + Environment* env = AsyncWrap::env(); |
| 257 | + destination_ = ArrayBuffer::NewBackingStore(env->isolate(), length_); |
| 258 | + unsigned char* dest = static_cast<unsigned char*>(destination_->Data()); |
| 259 | + if (length_ > 0) { |
| 260 | + size_t total = 0; |
| 261 | + for (const auto& entry : source_) { |
| 262 | + unsigned char* src = static_cast<unsigned char*>(entry.store->Data()); |
| 263 | + src += entry.offset; |
| 264 | + memcpy(dest, src, entry.length); |
| 265 | + dest += entry.length; |
| 266 | + total += entry.length; |
| 267 | + CHECK_LE(total, length_); |
| 268 | + } |
| 269 | + } |
| 270 | +} |
| 271 | + |
| 272 | +void FixedSizeBlobCopyJob::MemoryInfo(MemoryTracker* tracker) const { |
| 273 | + tracker->TrackFieldWithSize("source", length_); |
| 274 | + tracker->TrackFieldWithSize( |
| 275 | + "destination", |
| 276 | + destination_ ? destination_->ByteLength() : 0); |
| 277 | +} |
| 278 | + |
| 279 | +void FixedSizeBlobCopyJob::Initialize(Environment* env, Local<Object> target) { |
| 280 | + v8::Local<v8::FunctionTemplate> job = env->NewFunctionTemplate(New); |
| 281 | + job->Inherit(AsyncWrap::GetConstructorTemplate(env)); |
| 282 | + job->InstanceTemplate()->SetInternalFieldCount( |
| 283 | + AsyncWrap::kInternalFieldCount); |
| 284 | + env->SetProtoMethod(job, "run", Run); |
| 285 | + env->SetConstructorFunction(target, "FixedSizeBlobCopyJob", job); |
| 286 | +} |
| 287 | + |
| 288 | +void FixedSizeBlobCopyJob::New(const FunctionCallbackInfo<Value>& args) { |
| 289 | + static constexpr size_t kMaxSyncLength = 4096; |
| 290 | + static constexpr size_t kMaxEntryCount = 4; |
| 291 | + |
| 292 | + Environment* env = Environment::GetCurrent(args); |
| 293 | + CHECK(args.IsConstructCall()); |
| 294 | + CHECK(args[0]->IsObject()); |
| 295 | + CHECK(Blob::HasInstance(env, args[0])); |
| 296 | + |
| 297 | + Blob* blob; |
| 298 | + ASSIGN_OR_RETURN_UNWRAP(&blob, args[0]); |
| 299 | + |
| 300 | + // This is a fairly arbitrary heuristic. We want to avoid deferring to |
| 301 | + // the threadpool if the amount of data being copied is small and there |
| 302 | + // aren't that many entries to copy. |
| 303 | + FixedSizeBlobCopyJob::Mode mode = |
| 304 | + (blob->length() < kMaxSyncLength && |
| 305 | + blob->entries().size() < kMaxEntryCount) ? |
| 306 | + FixedSizeBlobCopyJob::Mode::SYNC : |
| 307 | + FixedSizeBlobCopyJob::Mode::ASYNC; |
| 308 | + |
| 309 | + new FixedSizeBlobCopyJob(env, args.This(), blob, mode); |
| 310 | +} |
| 311 | + |
| 312 | +void FixedSizeBlobCopyJob::Run(const FunctionCallbackInfo<Value>& args) { |
| 313 | + Environment* env = Environment::GetCurrent(args); |
| 314 | + FixedSizeBlobCopyJob* job; |
| 315 | + ASSIGN_OR_RETURN_UNWRAP(&job, args.Holder()); |
| 316 | + if (job->mode() == FixedSizeBlobCopyJob::Mode::ASYNC) |
| 317 | + return job->ScheduleWork(); |
| 318 | + |
| 319 | + job->DoThreadPoolWork(); |
| 320 | + args.GetReturnValue().Set( |
| 321 | + ArrayBuffer::New(env->isolate(), job->destination_)); |
| 322 | +} |
| 323 | + |
| 324 | +void FixedSizeBlobCopyJob::RegisterExternalReferences( |
| 325 | + ExternalReferenceRegistry* registry) { |
| 326 | + registry->Register(New); |
| 327 | + registry->Register(Run); |
| 328 | +} |
| 329 | + |
| 330 | +void Blob::RegisterExternalReferences(ExternalReferenceRegistry* registry) { |
| 331 | + registry->Register(Blob::New); |
| 332 | + registry->Register(Blob::ToArrayBuffer); |
| 333 | + registry->Register(Blob::ToSlice); |
| 334 | +} |
| 335 | + |
| 336 | +} // namespace node |
0 commit comments