Skip to content

Commit 881be28

Browse files
anonrigCeres6
authored andcommitted
src: use string_view for utf-8 string creation
PR-URL: nodejs#48722 Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 094b106 commit 881be28

File tree

2 files changed

+44
-33
lines changed

2 files changed

+44
-33
lines changed

src/util.cc

+33-22
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ Local<v8::FunctionTemplate> NewFunctionTemplate(
358358

359359
void SetMethod(Local<v8::Context> context,
360360
Local<v8::Object> that,
361-
const char* name,
361+
const std::string_view name,
362362
v8::FunctionCallback callback) {
363363
Isolate* isolate = context->GetIsolate();
364364
Local<v8::Function> function =
@@ -372,14 +372,15 @@ void SetMethod(Local<v8::Context> context,
372372
// kInternalized strings are created in the old space.
373373
const v8::NewStringType type = v8::NewStringType::kInternalized;
374374
Local<v8::String> name_string =
375-
v8::String::NewFromUtf8(isolate, name, type).ToLocalChecked();
375+
v8::String::NewFromUtf8(isolate, name.data(), type, name.size())
376+
.ToLocalChecked();
376377
that->Set(context, name_string, function).Check();
377378
function->SetName(name_string); // NODE_SET_METHOD() compatibility.
378379
}
379380

380381
void SetMethod(v8::Isolate* isolate,
381382
v8::Local<v8::Template> that,
382-
const char* name,
383+
const std::string_view name,
383384
v8::FunctionCallback callback) {
384385
Local<v8::FunctionTemplate> t =
385386
NewFunctionTemplate(isolate,
@@ -390,13 +391,14 @@ void SetMethod(v8::Isolate* isolate,
390391
// kInternalized strings are created in the old space.
391392
const v8::NewStringType type = v8::NewStringType::kInternalized;
392393
Local<v8::String> name_string =
393-
v8::String::NewFromUtf8(isolate, name, type).ToLocalChecked();
394+
v8::String::NewFromUtf8(isolate, name.data(), type, name.size())
395+
.ToLocalChecked();
394396
that->Set(name_string, t);
395397
}
396398

397399
void SetFastMethod(Isolate* isolate,
398400
Local<Template> that,
399-
const char* name,
401+
const std::string_view name,
400402
v8::FunctionCallback slow_callback,
401403
const v8::CFunction* c_function) {
402404
Local<v8::FunctionTemplate> t =
@@ -409,13 +411,14 @@ void SetFastMethod(Isolate* isolate,
409411
// kInternalized strings are created in the old space.
410412
const v8::NewStringType type = v8::NewStringType::kInternalized;
411413
Local<v8::String> name_string =
412-
v8::String::NewFromUtf8(isolate, name, type).ToLocalChecked();
414+
v8::String::NewFromUtf8(isolate, name.data(), type, name.size())
415+
.ToLocalChecked();
413416
that->Set(name_string, t);
414417
}
415418

416419
void SetFastMethod(Local<v8::Context> context,
417420
Local<v8::Object> that,
418-
const char* name,
421+
const std::string_view name,
419422
v8::FunctionCallback slow_callback,
420423
const v8::CFunction* c_function) {
421424
Isolate* isolate = context->GetIsolate();
@@ -430,13 +433,14 @@ void SetFastMethod(Local<v8::Context> context,
430433
.ToLocalChecked();
431434
const v8::NewStringType type = v8::NewStringType::kInternalized;
432435
Local<v8::String> name_string =
433-
v8::String::NewFromUtf8(isolate, name, type).ToLocalChecked();
436+
v8::String::NewFromUtf8(isolate, name.data(), type, name.size())
437+
.ToLocalChecked();
434438
that->Set(context, name_string, function).Check();
435439
}
436440

437441
void SetFastMethodNoSideEffect(Local<v8::Context> context,
438442
Local<v8::Object> that,
439-
const char* name,
443+
const std::string_view name,
440444
v8::FunctionCallback slow_callback,
441445
const v8::CFunction* c_function) {
442446
Isolate* isolate = context->GetIsolate();
@@ -451,13 +455,14 @@ void SetFastMethodNoSideEffect(Local<v8::Context> context,
451455
.ToLocalChecked();
452456
const v8::NewStringType type = v8::NewStringType::kInternalized;
453457
Local<v8::String> name_string =
454-
v8::String::NewFromUtf8(isolate, name, type).ToLocalChecked();
458+
v8::String::NewFromUtf8(isolate, name.data(), type, name.size())
459+
.ToLocalChecked();
455460
that->Set(context, name_string, function).Check();
456461
}
457462

458463
void SetFastMethodNoSideEffect(Isolate* isolate,
459464
Local<Template> that,
460-
const char* name,
465+
const std::string_view name,
461466
v8::FunctionCallback slow_callback,
462467
const v8::CFunction* c_function) {
463468
Local<v8::FunctionTemplate> t =
@@ -470,13 +475,14 @@ void SetFastMethodNoSideEffect(Isolate* isolate,
470475
// kInternalized strings are created in the old space.
471476
const v8::NewStringType type = v8::NewStringType::kInternalized;
472477
Local<v8::String> name_string =
473-
v8::String::NewFromUtf8(isolate, name, type).ToLocalChecked();
478+
v8::String::NewFromUtf8(isolate, name.data(), type, name.size())
479+
.ToLocalChecked();
474480
that->Set(name_string, t);
475481
}
476482

477483
void SetMethodNoSideEffect(Local<v8::Context> context,
478484
Local<v8::Object> that,
479-
const char* name,
485+
const std::string_view name,
480486
v8::FunctionCallback callback) {
481487
Isolate* isolate = context->GetIsolate();
482488
Local<v8::Function> function =
@@ -490,14 +496,15 @@ void SetMethodNoSideEffect(Local<v8::Context> context,
490496
// kInternalized strings are created in the old space.
491497
const v8::NewStringType type = v8::NewStringType::kInternalized;
492498
Local<v8::String> name_string =
493-
v8::String::NewFromUtf8(isolate, name, type).ToLocalChecked();
499+
v8::String::NewFromUtf8(isolate, name.data(), type, name.size())
500+
.ToLocalChecked();
494501
that->Set(context, name_string, function).Check();
495502
function->SetName(name_string); // NODE_SET_METHOD() compatibility.
496503
}
497504

498505
void SetMethodNoSideEffect(Isolate* isolate,
499506
Local<v8::Template> that,
500-
const char* name,
507+
const std::string_view name,
501508
v8::FunctionCallback callback) {
502509
Local<v8::FunctionTemplate> t =
503510
NewFunctionTemplate(isolate,
@@ -508,13 +515,14 @@ void SetMethodNoSideEffect(Isolate* isolate,
508515
// kInternalized strings are created in the old space.
509516
const v8::NewStringType type = v8::NewStringType::kInternalized;
510517
Local<v8::String> name_string =
511-
v8::String::NewFromUtf8(isolate, name, type).ToLocalChecked();
518+
v8::String::NewFromUtf8(isolate, name.data(), type, name.size())
519+
.ToLocalChecked();
512520
that->Set(name_string, t);
513521
}
514522

515523
void SetProtoMethod(v8::Isolate* isolate,
516524
Local<v8::FunctionTemplate> that,
517-
const char* name,
525+
const std::string_view name,
518526
v8::FunctionCallback callback) {
519527
Local<v8::Signature> signature = v8::Signature::New(isolate, that);
520528
Local<v8::FunctionTemplate> t =
@@ -526,14 +534,15 @@ void SetProtoMethod(v8::Isolate* isolate,
526534
// kInternalized strings are created in the old space.
527535
const v8::NewStringType type = v8::NewStringType::kInternalized;
528536
Local<v8::String> name_string =
529-
v8::String::NewFromUtf8(isolate, name, type).ToLocalChecked();
537+
v8::String::NewFromUtf8(isolate, name.data(), type, name.size())
538+
.ToLocalChecked();
530539
that->PrototypeTemplate()->Set(name_string, t);
531540
t->SetClassName(name_string); // NODE_SET_PROTOTYPE_METHOD() compatibility.
532541
}
533542

534543
void SetProtoMethodNoSideEffect(v8::Isolate* isolate,
535544
Local<v8::FunctionTemplate> that,
536-
const char* name,
545+
const std::string_view name,
537546
v8::FunctionCallback callback) {
538547
Local<v8::Signature> signature = v8::Signature::New(isolate, that);
539548
Local<v8::FunctionTemplate> t =
@@ -545,14 +554,15 @@ void SetProtoMethodNoSideEffect(v8::Isolate* isolate,
545554
// kInternalized strings are created in the old space.
546555
const v8::NewStringType type = v8::NewStringType::kInternalized;
547556
Local<v8::String> name_string =
548-
v8::String::NewFromUtf8(isolate, name, type).ToLocalChecked();
557+
v8::String::NewFromUtf8(isolate, name.data(), type, name.size())
558+
.ToLocalChecked();
549559
that->PrototypeTemplate()->Set(name_string, t);
550560
t->SetClassName(name_string); // NODE_SET_PROTOTYPE_METHOD() compatibility.
551561
}
552562

553563
void SetInstanceMethod(v8::Isolate* isolate,
554564
Local<v8::FunctionTemplate> that,
555-
const char* name,
565+
const std::string_view name,
556566
v8::FunctionCallback callback) {
557567
Local<v8::Signature> signature = v8::Signature::New(isolate, that);
558568
Local<v8::FunctionTemplate> t =
@@ -564,7 +574,8 @@ void SetInstanceMethod(v8::Isolate* isolate,
564574
// kInternalized strings are created in the old space.
565575
const v8::NewStringType type = v8::NewStringType::kInternalized;
566576
Local<v8::String> name_string =
567-
v8::String::NewFromUtf8(isolate, name, type).ToLocalChecked();
577+
v8::String::NewFromUtf8(isolate, name.data(), type, name.size())
578+
.ToLocalChecked();
568579
that->InstanceTemplate()->Set(name_string, t);
569580
t->SetClassName(name_string);
570581
}

src/util.h

+11-11
Original file line numberDiff line numberDiff line change
@@ -878,57 +878,57 @@ v8::Local<v8::FunctionTemplate> NewFunctionTemplate(
878878
// Convenience methods for NewFunctionTemplate().
879879
void SetMethod(v8::Local<v8::Context> context,
880880
v8::Local<v8::Object> that,
881-
const char* name,
881+
const std::string_view name,
882882
v8::FunctionCallback callback);
883883
// Similar to SetProtoMethod but without receiver signature checks.
884884
void SetMethod(v8::Isolate* isolate,
885885
v8::Local<v8::Template> that,
886-
const char* name,
886+
const std::string_view name,
887887
v8::FunctionCallback callback);
888888

889889
void SetFastMethod(v8::Isolate* isolate,
890890
v8::Local<v8::Template> that,
891-
const char* name,
891+
const std::string_view name,
892892
v8::FunctionCallback slow_callback,
893893
const v8::CFunction* c_function);
894894
void SetFastMethod(v8::Local<v8::Context> context,
895895
v8::Local<v8::Object> that,
896-
const char* name,
896+
const std::string_view name,
897897
v8::FunctionCallback slow_callback,
898898
const v8::CFunction* c_function);
899899
void SetFastMethodNoSideEffect(v8::Isolate* isolate,
900900
v8::Local<v8::Template> that,
901-
const char* name,
901+
const std::string_view name,
902902
v8::FunctionCallback slow_callback,
903903
const v8::CFunction* c_function);
904904
void SetFastMethodNoSideEffect(v8::Local<v8::Context> context,
905905
v8::Local<v8::Object> that,
906-
const char* name,
906+
const std::string_view name,
907907
v8::FunctionCallback slow_callback,
908908
const v8::CFunction* c_function);
909909

910910
void SetProtoMethod(v8::Isolate* isolate,
911911
v8::Local<v8::FunctionTemplate> that,
912-
const char* name,
912+
const std::string_view name,
913913
v8::FunctionCallback callback);
914914

915915
void SetInstanceMethod(v8::Isolate* isolate,
916916
v8::Local<v8::FunctionTemplate> that,
917-
const char* name,
917+
const std::string_view name,
918918
v8::FunctionCallback callback);
919919

920920
// Safe variants denote the function has no side effects.
921921
void SetMethodNoSideEffect(v8::Local<v8::Context> context,
922922
v8::Local<v8::Object> that,
923-
const char* name,
923+
const std::string_view name,
924924
v8::FunctionCallback callback);
925925
void SetProtoMethodNoSideEffect(v8::Isolate* isolate,
926926
v8::Local<v8::FunctionTemplate> that,
927-
const char* name,
927+
const std::string_view name,
928928
v8::FunctionCallback callback);
929929
void SetMethodNoSideEffect(v8::Isolate* isolate,
930930
v8::Local<v8::Template> that,
931-
const char* name,
931+
const std::string_view name,
932932
v8::FunctionCallback callback);
933933

934934
enum class SetConstructorFunctionFlag {

0 commit comments

Comments
 (0)