Commit 85cef38d authored by Dan Elphick's avatar Dan Elphick Committed by Commit Bot

Add v8::String::StringEquals to API

This new method only compares Strings and so doesn't need a Context. It
also can't throw so it returns bool. Can be used in place of the
deprecated Equals method and many Equals call currently taking a
Context.

Cq-Include-Trybots: luci.chromium.try:linux_chromium_rel_ng
Change-Id: I4cfe7747aa140e5a55d9513681ee4704414e1545
Reviewed-on: https://chromium-review.googlesource.com/1151321
Commit-Queue: Dan Elphick <delphick@chromium.org>
Reviewed-by: 's avatarYang Guo <yangguo@chromium.org>
Reviewed-by: 's avatarBen Titzer <titzer@chromium.org>
Reviewed-by: 's avatarLeszek Swirski <leszeks@chromium.org>
Cr-Commit-Position: refs/heads/master@{#54812}
parent bf066d85
......@@ -2999,6 +2999,11 @@ class V8_EXPORT String : public Name {
*/
bool CanMakeExternal();
/**
* Returns true if the strings values are equal. Same as JS ==/===.
*/
bool StringEquals(Local<String> str);
/**
* Converts an object to a UTF-8-encoded character array. Useful if
* you want to print the object. If conversion to a string fails
......
......@@ -6998,6 +6998,11 @@ bool v8::String::CanMakeExternal() {
return !i::Heap::InNewSpace(obj);
}
bool v8::String::StringEquals(Local<String> that) {
auto self = Utils::OpenHandle(this);
auto other = Utils::OpenHandle(*that);
return self->Equals(*other);
}
Isolate* v8::Object::GetIsolate() {
i::Isolate* i_isolate = Utils::OpenHandle(this)->GetIsolate();
......
......@@ -787,9 +787,7 @@ void WebAssemblyTable(const v8::FunctionCallbackInfo<v8::Value>& args) {
if (!maybe.ToLocal(&value)) return;
v8::Local<v8::String> string;
if (!value->ToString(context).ToLocal(&string)) return;
bool equal;
if (!string->Equals(context, v8_str(isolate, "anyfunc")).To(&equal)) return;
if (!equal) {
if (!string->StringEquals(v8_str(isolate, "anyfunc"))) {
thrower.TypeError("Descriptor property 'element' must be 'anyfunc'");
return;
}
......@@ -937,14 +935,11 @@ void WebAssemblyGlobal(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::Local<v8::String> string;
if (!value->ToString(context).ToLocal(&string)) return;
bool equal;
if (string->Equals(context, v8_str(isolate, "i32")).To(&equal) && equal) {
if (string->StringEquals(v8_str(isolate, "i32"))) {
type = i::wasm::kWasmI32;
} else if (string->Equals(context, v8_str(isolate, "f32")).To(&equal) &&
equal) {
} else if (string->StringEquals(v8_str(isolate, "f32"))) {
type = i::wasm::kWasmF32;
} else if (string->Equals(context, v8_str(isolate, "f64")).To(&equal) &&
equal) {
} else if (string->StringEquals(v8_str(isolate, "f64"))) {
type = i::wasm::kWasmF64;
} else {
thrower.TypeError(
......
......@@ -1664,6 +1664,35 @@ TEST(HashArrayIndexStrings) {
isolate->factory()->one_string()->Hash());
}
TEST(StringEquals) {
v8::V8::Initialize();
v8::Isolate* isolate = CcTest::isolate();
v8::HandleScope scope(isolate);
auto foo_str =
v8::String::NewFromUtf8(isolate, "foo", v8::NewStringType::kNormal)
.ToLocalChecked();
auto bar_str =
v8::String::NewFromUtf8(isolate, "bar", v8::NewStringType::kNormal)
.ToLocalChecked();
auto foo_str2 =
v8::String::NewFromUtf8(isolate, "foo", v8::NewStringType::kNormal)
.ToLocalChecked();
uint16_t* two_byte_source = AsciiToTwoByteString("foo");
auto foo_two_byte_str =
v8::String::NewFromTwoByte(isolate, two_byte_source,
v8::NewStringType::kNormal)
.ToLocalChecked();
i::DeleteArray(two_byte_source);
CHECK(foo_str->StringEquals(foo_str));
CHECK(!foo_str->StringEquals(bar_str));
CHECK(foo_str->StringEquals(foo_str2));
CHECK(foo_str->StringEquals(foo_two_byte_str));
CHECK(!bar_str->StringEquals(foo_str2));
}
} // namespace test_strings
} // namespace internal
} // namespace v8
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment