11
11
#include " path.h"
12
12
#include " permission/permission.h"
13
13
#include " util.h"
14
+ #include " v8-fast-api-calls.h"
14
15
#include " v8.h"
15
16
16
17
#include < algorithm>
@@ -123,14 +124,49 @@ void BlobFromFilePath(const FunctionCallbackInfo<Value>& args) {
123
124
}
124
125
} // namespace
125
126
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
+
126
157
void Blob::CreatePerIsolateProperties (IsolateData* isolate_data,
127
158
Local<ObjectTemplate> target) {
128
159
Isolate* isolate = isolate_data->isolate ();
129
160
130
161
SetMethod (isolate, target, " createBlob" , New);
131
162
SetMethod (isolate, target, " storeDataObject" , StoreDataObject);
132
163
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
+
134
170
SetMethod (isolate, target, " concat" , Concat);
135
171
SetMethod (isolate, target, " createBlobFromFilePath" , BlobFromFilePath);
136
172
}
@@ -150,8 +186,7 @@ Local<FunctionTemplate> Blob::GetConstructorTemplate(Environment* env) {
150
186
tmpl = NewFunctionTemplate (isolate, nullptr );
151
187
tmpl->InstanceTemplate ()->SetInternalFieldCount (
152
188
BaseObject::kInternalFieldCount );
153
- tmpl->SetClassName (
154
- FIXED_ONE_BYTE_STRING (env->isolate (), " Blob" ));
189
+ tmpl->SetClassName (FIXED_ONE_BYTE_STRING (env->isolate (), " Blob" ));
155
190
SetProtoMethod (isolate, tmpl, " getReader" , GetReader);
156
191
SetProtoMethod (isolate, tmpl, " slice" , ToSlice);
157
192
env->set_blob_constructor_template (tmpl);
@@ -242,8 +277,7 @@ void Blob::New(const FunctionCallbackInfo<Value>& args) {
242
277
}
243
278
244
279
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 ());
247
281
}
248
282
249
283
void Blob::GetReader (const FunctionCallbackInfo<Value>& args) {
@@ -265,8 +299,7 @@ void Blob::ToSlice(const FunctionCallbackInfo<Value>& args) {
265
299
size_t start = args[0 ].As <Uint32>()->Value ();
266
300
size_t end = args[1 ].As <Uint32>()->Value ();
267
301
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 ());
270
303
}
271
304
272
305
void Blob::MemoryInfo (MemoryTracker* tracker) const {
@@ -395,8 +428,7 @@ void Blob::Reader::Pull(const FunctionCallbackInfo<Value>& args) {
395
428
std::move (next), node::bob::OPTIONS_END, nullptr , 0 ));
396
429
}
397
430
398
- BaseObjectPtr<BaseObject>
399
- Blob::BlobTransferData::Deserialize (
431
+ BaseObjectPtr<BaseObject> Blob::BlobTransferData::Deserialize (
400
432
Environment* env,
401
433
Local<Context> context,
402
434
std::unique_ptr<worker::TransferData> self) {
@@ -418,10 +450,10 @@ std::unique_ptr<worker::TransferData> Blob::CloneForMessaging() const {
418
450
void Blob::StoreDataObject (const v8::FunctionCallbackInfo<v8::Value>& args) {
419
451
Realm* realm = Realm::GetCurrent (args);
420
452
421
- CHECK (args[0 ]->IsString ()); // ID key
453
+ CHECK (args[0 ]->IsString ()); // ID key
422
454
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
425
457
426
458
BlobBindingData* binding_data = realm->GetBindingData <BlobBindingData>();
427
459
Isolate* isolate = realm->isolate ();
@@ -435,13 +467,11 @@ void Blob::StoreDataObject(const v8::FunctionCallbackInfo<v8::Value>& args) {
435
467
436
468
binding_data->store_data_object (
437
469
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 ())));
442
473
}
443
474
444
- // TODO(@anonrig): Add V8 Fast API to the following function
445
475
void Blob::RevokeObjectURL (const FunctionCallbackInfo<Value>& args) {
446
476
CHECK_GE (args.Length (), 1 );
447
477
CHECK (args[0 ]->IsString ());
@@ -502,12 +532,8 @@ void BlobBindingData::StoredDataObject::MemoryInfo(
502
532
}
503
533
504
534
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_) {}
511
537
512
538
BlobBindingData::BlobBindingData (Realm* realm, Local<Object> wrap)
513
539
: SnapshotableObject(realm, wrap, type_int) {
@@ -521,8 +547,7 @@ void BlobBindingData::MemoryInfo(MemoryTracker* tracker) const {
521
547
}
522
548
523
549
void BlobBindingData::store_data_object (
524
- const std::string& uuid,
525
- const BlobBindingData::StoredDataObject& object) {
550
+ const std::string& uuid, const BlobBindingData::StoredDataObject& object) {
526
551
data_objects_[uuid] = object;
527
552
}
528
553
@@ -537,8 +562,7 @@ void BlobBindingData::revoke_data_object(const std::string& uuid) {
537
562
BlobBindingData::StoredDataObject BlobBindingData::get_data_object (
538
563
const std::string& uuid) {
539
564
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{};
542
566
return entry->second ;
543
567
}
544
568
@@ -578,8 +602,10 @@ void Blob::RegisterExternalReferences(ExternalReferenceRegistry* registry) {
578
602
registry->Register (Blob::Reader::Pull);
579
603
registry->Register (Concat);
580
604
registry->Register (BlobFromFilePath);
581
- }
582
605
606
+ registry->Register (FastRevokeObjectURL);
607
+ registry->Register (fast_revoke_object_url.GetTypeInfo ());
608
+ }
583
609
} // namespace node
584
610
585
611
NODE_BINDING_CONTEXT_AWARE_INTERNAL (blob,
0 commit comments