chore: pass base::StringPiece args by value (#19432)

https://cs.chromium.org/chromium/src/base/strings/string_piece.h?l=14
discusses this, saying "Prefer passing StringPieces by value" because
"[p]assing by value generates slightly smaller code."
This commit is contained in:
Charles Kerr
2019-07-25 10:19:04 -05:00
committed by GitHub
parent 539078f281
commit f6fb877de9
12 changed files with 46 additions and 54 deletions

View File

@@ -138,9 +138,8 @@ v8::Local<v8::Value> Converter<const char*>::ToV8(v8::Isolate* isolate,
.ToLocalChecked();
}
v8::Local<v8::Value> Converter<base::StringPiece>::ToV8(
v8::Isolate* isolate,
const base::StringPiece& val) {
v8::Local<v8::Value> Converter<base::StringPiece>::ToV8(v8::Isolate* isolate,
base::StringPiece val) {
return v8::String::NewFromUtf8(isolate, val.data(),
v8::NewStringType::kNormal,
static_cast<uint32_t>(val.length()))
@@ -260,7 +259,7 @@ bool Converter<v8::Local<v8::Value>>::FromV8(v8::Isolate* isolate,
}
v8::Local<v8::String> StringToSymbol(v8::Isolate* isolate,
const base::StringPiece& val) {
base::StringPiece val) {
return v8::String::NewFromUtf8(isolate, val.data(),
v8::NewStringType::kInternalized,
static_cast<uint32_t>(val.length()))

View File

@@ -123,8 +123,7 @@ struct Converter<const char*> {
template <>
struct Converter<base::StringPiece> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const base::StringPiece& val);
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, base::StringPiece val);
// No conversion out is possible because StringPiece does not contain storage.
};
@@ -138,7 +137,7 @@ struct Converter<std::string> {
};
v8::Local<v8::String> StringToSymbol(v8::Isolate* isolate,
const base::StringPiece& input);
base::StringPiece input);
template <>
struct Converter<v8::Local<v8::Function>> {
@@ -343,7 +342,7 @@ bool ConvertFromV8(v8::Isolate* isolate,
}
inline v8::Local<v8::String> StringToV8(v8::Isolate* isolate,
const base::StringPiece& input) {
base::StringPiece input) {
return ConvertToV8(isolate, input).As<v8::String>();
}

View File

@@ -41,7 +41,7 @@ class Dictionary {
static Dictionary CreateEmpty(v8::Isolate* isolate);
template <typename T>
bool Get(const base::StringPiece& key, T* out) const {
bool Get(base::StringPiece key, T* out) const {
// Check for existence before getting, otherwise this method will always
// returns true when T == v8::Local<v8::Value>.
v8::Local<v8::Context> context = isolate_->GetCurrentContext();
@@ -56,7 +56,7 @@ class Dictionary {
}
template <typename T>
bool GetHidden(const base::StringPiece& key, T* out) const {
bool GetHidden(base::StringPiece key, T* out) const {
v8::Local<v8::Context> context = isolate_->GetCurrentContext();
v8::Local<v8::Private> privateKey =
v8::Private::ForApi(isolate_, StringToV8(isolate_, key));
@@ -69,7 +69,7 @@ class Dictionary {
}
template <typename T>
bool Set(const base::StringPiece& key, const T& val) {
bool Set(base::StringPiece key, const T& val) {
v8::Local<v8::Value> v8_value;
if (!TryConvertToV8(isolate_, val, &v8_value))
return false;
@@ -79,7 +79,7 @@ class Dictionary {
}
template <typename T>
bool SetHidden(const base::StringPiece& key, T val) {
bool SetHidden(base::StringPiece key, T val) {
v8::Local<v8::Value> v8_value;
if (!TryConvertToV8(isolate_, val, &v8_value))
return false;
@@ -92,7 +92,7 @@ class Dictionary {
}
template <typename T>
bool SetReadOnly(const base::StringPiece& key, T val) {
bool SetReadOnly(base::StringPiece key, T val) {
v8::Local<v8::Value> v8_value;
if (!TryConvertToV8(isolate_, val, &v8_value))
return false;
@@ -103,7 +103,7 @@ class Dictionary {
}
template <typename T>
bool SetMethod(const base::StringPiece& key, const T& callback) {
bool SetMethod(base::StringPiece key, const T& callback) {
return GetHandle()
->Set(isolate_->GetCurrentContext(), StringToV8(isolate_, key),
CallbackTraits<T>::CreateTemplate(isolate_, callback)
@@ -112,7 +112,7 @@ class Dictionary {
.ToChecked();
}
bool Delete(const base::StringPiece& key) {
bool Delete(base::StringPiece key) {
v8::Maybe<bool> result = GetHandle()->Delete(isolate_->GetCurrentContext(),
StringToV8(isolate_, key));
return !result.IsNothing() && result.FromJust();

View File

@@ -13,15 +13,14 @@ ObjectTemplateBuilder::ObjectTemplateBuilder(
ObjectTemplateBuilder::~ObjectTemplateBuilder() {}
ObjectTemplateBuilder& ObjectTemplateBuilder::SetImpl(
const base::StringPiece& name,
v8::Local<v8::Data> val) {
ObjectTemplateBuilder& ObjectTemplateBuilder::SetImpl(base::StringPiece name,
v8::Local<v8::Data> val) {
template_->Set(StringToSymbol(isolate_, name), val);
return *this;
}
ObjectTemplateBuilder& ObjectTemplateBuilder::SetPropertyImpl(
const base::StringPiece& name,
base::StringPiece name,
v8::Local<v8::FunctionTemplate> getter,
v8::Local<v8::FunctionTemplate> setter) {
template_->SetAccessorProperty(StringToSymbol(isolate_, name), getter,

View File

@@ -72,7 +72,7 @@ class ObjectTemplateBuilder {
// poetic license here in order that all calls to Set() can be via the '.'
// operator and line up nicely.
template <typename T>
ObjectTemplateBuilder& SetValue(const base::StringPiece& name, T val) {
ObjectTemplateBuilder& SetValue(base::StringPiece name, T val) {
return SetImpl(name, ConvertToV8(isolate_, val));
}
@@ -81,17 +81,17 @@ class ObjectTemplateBuilder {
// use one of the first two options. Also see mate::CreateFunctionTemplate()
// for creating raw function templates.
template <typename T>
ObjectTemplateBuilder& SetMethod(const base::StringPiece& name, T callback) {
ObjectTemplateBuilder& SetMethod(base::StringPiece name, T callback) {
return SetImpl(name, CallbackTraits<T>::CreateTemplate(isolate_, callback));
}
template <typename T>
ObjectTemplateBuilder& SetProperty(const base::StringPiece& name, T getter) {
ObjectTemplateBuilder& SetProperty(base::StringPiece name, T getter) {
return SetPropertyImpl(name,
CallbackTraits<T>::CreateTemplate(isolate_, getter),
v8::Local<v8::FunctionTemplate>());
}
template <typename T, typename U>
ObjectTemplateBuilder& SetProperty(const base::StringPiece& name,
ObjectTemplateBuilder& SetProperty(base::StringPiece name,
T getter,
U setter) {
return SetPropertyImpl(name,
@@ -105,10 +105,10 @@ class ObjectTemplateBuilder {
v8::Local<v8::ObjectTemplate> Build();
private:
ObjectTemplateBuilder& SetImpl(const base::StringPiece& name,
ObjectTemplateBuilder& SetImpl(base::StringPiece name,
v8::Local<v8::Data> val);
ObjectTemplateBuilder& SetPropertyImpl(
const base::StringPiece& name,
base::StringPiece name,
v8::Local<v8::FunctionTemplate> getter,
v8::Local<v8::FunctionTemplate> setter);