Skip to content

Commit 3c1f0ae

Browse files
committed
url: add V8 Fast API for Blob RevokeObjectURL
1 parent 0b6b2a4 commit 3c1f0ae

File tree

2 files changed

+58
-28
lines changed

2 files changed

+58
-28
lines changed

src/node_blob.cc

+54-28
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "path.h"
1212
#include "permission/permission.h"
1313
#include "util.h"
14+
#include "v8-fast-api-calls.h"
1415
#include "v8.h"
1516

1617
#include <algorithm>
@@ -123,14 +124,49 @@ void BlobFromFilePath(const FunctionCallbackInfo<Value>& args) {
123124
}
124125
} // namespace
125126

127+
// Fast API version
128+
void FastRevokeObjectURL(v8::Local<Value> receiver,
129+
const v8::FastOneByteString& url_string) {
130+
auto out = ada::parse<ada::url_aggregator>(url_string.data);
131+
132+
if (!out) {
133+
return;
134+
}
135+
136+
const auto& pathname = out->get_pathname();
137+
auto start_index = pathname.find(':');
138+
139+
if (start_index != std::string_view::npos &&
140+
start_index + 1 < pathname.size()) {
141+
auto end_index = pathname.find(':', start_index + 1);
142+
if (end_index == std::string_view::npos) {
143+
std::string id(pathname.substr(start_index + 1));
144+
145+
v8::Isolate* isolate = v8::Isolate::GetCurrent();
146+
Realm* realm = Realm::GetCurrent(isolate);
147+
BlobBindingData* binding_data = realm->GetBindingData<BlobBindingData>();
148+
149+
binding_data->revoke_data_object(std::move(id));
150+
}
151+
}
152+
}
153+
154+
static v8::CFunction fast_revoke_object_url(
155+
v8::CFunction::Make(FastRevokeObjectURL));
156+
126157
void Blob::CreatePerIsolateProperties(IsolateData* isolate_data,
127158
Local<ObjectTemplate> target) {
128159
Isolate* isolate = isolate_data->isolate();
129160

130161
SetMethod(isolate, target, "createBlob", New);
131162
SetMethod(isolate, target, "storeDataObject", StoreDataObject);
132163
SetMethod(isolate, target, "getDataObject", GetDataObject);
133-
SetMethod(isolate, target, "revokeObjectURL", RevokeObjectURL);
164+
SetFastMethodNoSideEffect(isolate,
165+
target,
166+
"revokeObjectURL",
167+
RevokeObjectURL,
168+
&fast_revoke_object_url);
169+
134170
SetMethod(isolate, target, "concat", Concat);
135171
SetMethod(isolate, target, "createBlobFromFilePath", BlobFromFilePath);
136172
}
@@ -150,8 +186,7 @@ Local<FunctionTemplate> Blob::GetConstructorTemplate(Environment* env) {
150186
tmpl = NewFunctionTemplate(isolate, nullptr);
151187
tmpl->InstanceTemplate()->SetInternalFieldCount(
152188
BaseObject::kInternalFieldCount);
153-
tmpl->SetClassName(
154-
FIXED_ONE_BYTE_STRING(env->isolate(), "Blob"));
189+
tmpl->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Blob"));
155190
SetProtoMethod(isolate, tmpl, "getReader", GetReader);
156191
SetProtoMethod(isolate, tmpl, "slice", ToSlice);
157192
env->set_blob_constructor_template(tmpl);
@@ -242,8 +277,7 @@ void Blob::New(const FunctionCallbackInfo<Value>& args) {
242277
}
243278

244279
auto blob = Create(env, DataQueue::CreateIdempotent(std::move(entries)));
245-
if (blob)
246-
args.GetReturnValue().Set(blob->object());
280+
if (blob) args.GetReturnValue().Set(blob->object());
247281
}
248282

249283
void Blob::GetReader(const FunctionCallbackInfo<Value>& args) {
@@ -265,8 +299,7 @@ void Blob::ToSlice(const FunctionCallbackInfo<Value>& args) {
265299
size_t start = args[0].As<Uint32>()->Value();
266300
size_t end = args[1].As<Uint32>()->Value();
267301
BaseObjectPtr<Blob> slice = blob->Slice(env, start, end);
268-
if (slice)
269-
args.GetReturnValue().Set(slice->object());
302+
if (slice) args.GetReturnValue().Set(slice->object());
270303
}
271304

272305
void Blob::MemoryInfo(MemoryTracker* tracker) const {
@@ -395,8 +428,7 @@ void Blob::Reader::Pull(const FunctionCallbackInfo<Value>& args) {
395428
std::move(next), node::bob::OPTIONS_END, nullptr, 0));
396429
}
397430

398-
BaseObjectPtr<BaseObject>
399-
Blob::BlobTransferData::Deserialize(
431+
BaseObjectPtr<BaseObject> Blob::BlobTransferData::Deserialize(
400432
Environment* env,
401433
Local<Context> context,
402434
std::unique_ptr<worker::TransferData> self) {
@@ -418,10 +450,10 @@ std::unique_ptr<worker::TransferData> Blob::CloneForMessaging() const {
418450
void Blob::StoreDataObject(const v8::FunctionCallbackInfo<v8::Value>& args) {
419451
Realm* realm = Realm::GetCurrent(args);
420452

421-
CHECK(args[0]->IsString()); // ID key
453+
CHECK(args[0]->IsString()); // ID key
422454
CHECK(Blob::HasInstance(realm->env(), args[1])); // Blob
423-
CHECK(args[2]->IsUint32()); // Length
424-
CHECK(args[3]->IsString()); // Type
455+
CHECK(args[2]->IsUint32()); // Length
456+
CHECK(args[3]->IsString()); // Type
425457

426458
BlobBindingData* binding_data = realm->GetBindingData<BlobBindingData>();
427459
Isolate* isolate = realm->isolate();
@@ -435,13 +467,11 @@ void Blob::StoreDataObject(const v8::FunctionCallbackInfo<v8::Value>& args) {
435467

436468
binding_data->store_data_object(
437469
std::string(*key, key.length()),
438-
BlobBindingData::StoredDataObject(
439-
BaseObjectPtr<Blob>(blob),
440-
length,
441-
std::string(*type, type.length())));
470+
BlobBindingData::StoredDataObject(BaseObjectPtr<Blob>(blob),
471+
length,
472+
std::string(*type, type.length())));
442473
}
443474

444-
// TODO(@anonrig): Add V8 Fast API to the following function
445475
void Blob::RevokeObjectURL(const FunctionCallbackInfo<Value>& args) {
446476
CHECK_GE(args.Length(), 1);
447477
CHECK(args[0]->IsString());
@@ -502,12 +532,8 @@ void BlobBindingData::StoredDataObject::MemoryInfo(
502532
}
503533

504534
BlobBindingData::StoredDataObject::StoredDataObject(
505-
const BaseObjectPtr<Blob>& blob_,
506-
size_t length_,
507-
const std::string& type_)
508-
: blob(blob_),
509-
length(length_),
510-
type(type_) {}
535+
const BaseObjectPtr<Blob>& blob_, size_t length_, const std::string& type_)
536+
: blob(blob_), length(length_), type(type_) {}
511537

512538
BlobBindingData::BlobBindingData(Realm* realm, Local<Object> wrap)
513539
: SnapshotableObject(realm, wrap, type_int) {
@@ -521,8 +547,7 @@ void BlobBindingData::MemoryInfo(MemoryTracker* tracker) const {
521547
}
522548

523549
void BlobBindingData::store_data_object(
524-
const std::string& uuid,
525-
const BlobBindingData::StoredDataObject& object) {
550+
const std::string& uuid, const BlobBindingData::StoredDataObject& object) {
526551
data_objects_[uuid] = object;
527552
}
528553

@@ -537,8 +562,7 @@ void BlobBindingData::revoke_data_object(const std::string& uuid) {
537562
BlobBindingData::StoredDataObject BlobBindingData::get_data_object(
538563
const std::string& uuid) {
539564
auto entry = data_objects_.find(uuid);
540-
if (entry == data_objects_.end())
541-
return BlobBindingData::StoredDataObject {};
565+
if (entry == data_objects_.end()) return BlobBindingData::StoredDataObject{};
542566
return entry->second;
543567
}
544568

@@ -578,8 +602,10 @@ void Blob::RegisterExternalReferences(ExternalReferenceRegistry* registry) {
578602
registry->Register(Blob::Reader::Pull);
579603
registry->Register(Concat);
580604
registry->Register(BlobFromFilePath);
581-
}
582605

606+
registry->Register(FastRevokeObjectURL);
607+
registry->Register(fast_revoke_object_url.GetTypeInfo());
608+
}
583609
} // namespace node
584610

585611
NODE_BINDING_CONTEXT_AWARE_INTERNAL(blob,

src/node_external_reference.h

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
namespace node {
1212

13+
using CFunctionCallbackWithOneByteStringVoid =
14+
void (*)(v8::Local<v8::Value>, const v8::FastOneByteString&);
15+
1316
using CFunctionCallbackWithOneByteString =
1417
uint32_t (*)(v8::Local<v8::Value>, const v8::FastOneByteString&);
1518
using CFunctionCallback = void (*)(v8::Local<v8::Value> receiver);
@@ -96,6 +99,7 @@ class ExternalReferenceRegistry {
9699
V(CFunctionWithBool) \
97100
V(CFunctionBufferCopy) \
98101
V(CFunctionWriteString) \
102+
V(CFunctionCallbackWithOneByteStringVoid) \
99103
V(const v8::CFunctionInfo*) \
100104
V(v8::FunctionCallback) \
101105
V(v8::AccessorNameGetterCallback) \

0 commit comments

Comments
 (0)