Skip to content

Commit e155633

Browse files
committed
src: use string_view for utf-8 string creation
1 parent eece8d7 commit e155633

File tree

2 files changed

+34
-34
lines changed

2 files changed

+34
-34
lines changed

src/util.cc

+22-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,14 @@ 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()).ToLocalChecked();
376376
that->Set(context, name_string, function).Check();
377377
function->SetName(name_string); // NODE_SET_METHOD() compatibility.
378378
}
379379

380380
void SetMethod(v8::Isolate* isolate,
381381
v8::Local<v8::Template> that,
382-
const char* name,
382+
const std::string_view name,
383383
v8::FunctionCallback callback) {
384384
Local<v8::FunctionTemplate> t =
385385
NewFunctionTemplate(isolate,
@@ -390,13 +390,13 @@ void SetMethod(v8::Isolate* isolate,
390390
// kInternalized strings are created in the old space.
391391
const v8::NewStringType type = v8::NewStringType::kInternalized;
392392
Local<v8::String> name_string =
393-
v8::String::NewFromUtf8(isolate, name, type).ToLocalChecked();
393+
v8::String::NewFromUtf8(isolate, name.data(), type, name.size()).ToLocalChecked();
394394
that->Set(name_string, t);
395395
}
396396

397397
void SetFastMethod(Isolate* isolate,
398398
Local<Template> that,
399-
const char* name,
399+
const std::string_view name,
400400
v8::FunctionCallback slow_callback,
401401
const v8::CFunction* c_function) {
402402
Local<v8::FunctionTemplate> t =
@@ -409,13 +409,13 @@ void SetFastMethod(Isolate* isolate,
409409
// kInternalized strings are created in the old space.
410410
const v8::NewStringType type = v8::NewStringType::kInternalized;
411411
Local<v8::String> name_string =
412-
v8::String::NewFromUtf8(isolate, name, type).ToLocalChecked();
412+
v8::String::NewFromUtf8(isolate, name.data(), type, name.size()).ToLocalChecked();
413413
that->Set(name_string, t);
414414
}
415415

416416
void SetFastMethod(Local<v8::Context> context,
417417
Local<v8::Object> that,
418-
const char* name,
418+
const std::string_view name,
419419
v8::FunctionCallback slow_callback,
420420
const v8::CFunction* c_function) {
421421
Isolate* isolate = context->GetIsolate();
@@ -430,13 +430,13 @@ void SetFastMethod(Local<v8::Context> context,
430430
.ToLocalChecked();
431431
const v8::NewStringType type = v8::NewStringType::kInternalized;
432432
Local<v8::String> name_string =
433-
v8::String::NewFromUtf8(isolate, name, type).ToLocalChecked();
433+
v8::String::NewFromUtf8(isolate, name.data(), type, name.size()).ToLocalChecked();
434434
that->Set(context, name_string, function).Check();
435435
}
436436

437437
void SetFastMethodNoSideEffect(Local<v8::Context> context,
438438
Local<v8::Object> that,
439-
const char* name,
439+
const std::string_view name,
440440
v8::FunctionCallback slow_callback,
441441
const v8::CFunction* c_function) {
442442
Isolate* isolate = context->GetIsolate();
@@ -451,13 +451,13 @@ void SetFastMethodNoSideEffect(Local<v8::Context> context,
451451
.ToLocalChecked();
452452
const v8::NewStringType type = v8::NewStringType::kInternalized;
453453
Local<v8::String> name_string =
454-
v8::String::NewFromUtf8(isolate, name, type).ToLocalChecked();
454+
v8::String::NewFromUtf8(isolate, name.data(), type, name.size()).ToLocalChecked();
455455
that->Set(context, name_string, function).Check();
456456
}
457457

458458
void SetFastMethodNoSideEffect(Isolate* isolate,
459459
Local<Template> that,
460-
const char* name,
460+
const std::string_view name,
461461
v8::FunctionCallback slow_callback,
462462
const v8::CFunction* c_function) {
463463
Local<v8::FunctionTemplate> t =
@@ -470,13 +470,13 @@ void SetFastMethodNoSideEffect(Isolate* isolate,
470470
// kInternalized strings are created in the old space.
471471
const v8::NewStringType type = v8::NewStringType::kInternalized;
472472
Local<v8::String> name_string =
473-
v8::String::NewFromUtf8(isolate, name, type).ToLocalChecked();
473+
v8::String::NewFromUtf8(isolate, name.data(), type, name.size()).ToLocalChecked();
474474
that->Set(name_string, t);
475475
}
476476

477477
void SetMethodNoSideEffect(Local<v8::Context> context,
478478
Local<v8::Object> that,
479-
const char* name,
479+
const std::string_view name,
480480
v8::FunctionCallback callback) {
481481
Isolate* isolate = context->GetIsolate();
482482
Local<v8::Function> function =
@@ -490,14 +490,14 @@ void SetMethodNoSideEffect(Local<v8::Context> context,
490490
// kInternalized strings are created in the old space.
491491
const v8::NewStringType type = v8::NewStringType::kInternalized;
492492
Local<v8::String> name_string =
493-
v8::String::NewFromUtf8(isolate, name, type).ToLocalChecked();
493+
v8::String::NewFromUtf8(isolate, name.data(), type, name.size()).ToLocalChecked();
494494
that->Set(context, name_string, function).Check();
495495
function->SetName(name_string); // NODE_SET_METHOD() compatibility.
496496
}
497497

498498
void SetMethodNoSideEffect(Isolate* isolate,
499499
Local<v8::Template> that,
500-
const char* name,
500+
const std::string_view name,
501501
v8::FunctionCallback callback) {
502502
Local<v8::FunctionTemplate> t =
503503
NewFunctionTemplate(isolate,
@@ -508,13 +508,13 @@ void SetMethodNoSideEffect(Isolate* isolate,
508508
// kInternalized strings are created in the old space.
509509
const v8::NewStringType type = v8::NewStringType::kInternalized;
510510
Local<v8::String> name_string =
511-
v8::String::NewFromUtf8(isolate, name, type).ToLocalChecked();
511+
v8::String::NewFromUtf8(isolate, name.data(), type, name.size()).ToLocalChecked();
512512
that->Set(name_string, t);
513513
}
514514

515515
void SetProtoMethod(v8::Isolate* isolate,
516516
Local<v8::FunctionTemplate> that,
517-
const char* name,
517+
const std::string_view name,
518518
v8::FunctionCallback callback) {
519519
Local<v8::Signature> signature = v8::Signature::New(isolate, that);
520520
Local<v8::FunctionTemplate> t =
@@ -526,14 +526,14 @@ void SetProtoMethod(v8::Isolate* isolate,
526526
// kInternalized strings are created in the old space.
527527
const v8::NewStringType type = v8::NewStringType::kInternalized;
528528
Local<v8::String> name_string =
529-
v8::String::NewFromUtf8(isolate, name, type).ToLocalChecked();
529+
v8::String::NewFromUtf8(isolate, name.data(), type, name.size()).ToLocalChecked();
530530
that->PrototypeTemplate()->Set(name_string, t);
531531
t->SetClassName(name_string); // NODE_SET_PROTOTYPE_METHOD() compatibility.
532532
}
533533

534534
void SetProtoMethodNoSideEffect(v8::Isolate* isolate,
535535
Local<v8::FunctionTemplate> that,
536-
const char* name,
536+
const std::string_view name,
537537
v8::FunctionCallback callback) {
538538
Local<v8::Signature> signature = v8::Signature::New(isolate, that);
539539
Local<v8::FunctionTemplate> t =
@@ -545,14 +545,14 @@ void SetProtoMethodNoSideEffect(v8::Isolate* isolate,
545545
// kInternalized strings are created in the old space.
546546
const v8::NewStringType type = v8::NewStringType::kInternalized;
547547
Local<v8::String> name_string =
548-
v8::String::NewFromUtf8(isolate, name, type).ToLocalChecked();
548+
v8::String::NewFromUtf8(isolate, name.data(), type, name.size()).ToLocalChecked();
549549
that->PrototypeTemplate()->Set(name_string, t);
550550
t->SetClassName(name_string); // NODE_SET_PROTOTYPE_METHOD() compatibility.
551551
}
552552

553553
void SetInstanceMethod(v8::Isolate* isolate,
554554
Local<v8::FunctionTemplate> that,
555-
const char* name,
555+
const std::string_view name,
556556
v8::FunctionCallback callback) {
557557
Local<v8::Signature> signature = v8::Signature::New(isolate, that);
558558
Local<v8::FunctionTemplate> t =
@@ -564,7 +564,7 @@ void SetInstanceMethod(v8::Isolate* isolate,
564564
// kInternalized strings are created in the old space.
565565
const v8::NewStringType type = v8::NewStringType::kInternalized;
566566
Local<v8::String> name_string =
567-
v8::String::NewFromUtf8(isolate, name, type).ToLocalChecked();
567+
v8::String::NewFromUtf8(isolate, name.data(), type, name.size()).ToLocalChecked();
568568
that->InstanceTemplate()->Set(name_string, t);
569569
t->SetClassName(name_string);
570570
}

src/util.h

+12-12
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ class Utf8Value : public MaybeStackBuffer<char> {
527527
explicit Utf8Value(v8::Isolate* isolate, v8::Local<v8::Value> value);
528528

529529
inline std::string ToString() const { return std::string(out(), length()); }
530-
inline std::string_view ToStringView() const {
530+
constexpr inline std::string_view ToStringView() const {
531531
return std::string_view(out(), length());
532532
}
533533

@@ -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)