Commit a217f66b authored by Feng Yu's avatar Feng Yu Committed by V8 LUCI CQ

[test] Replace v8_str with NewString in unittests/

This pr refactor all usage of v8_str in unittests/ into NewString
method in test-utils.h

Change-Id: I8ffc456851488e8c050d7a38f5459aebfa3f314f
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3813064Reviewed-by: 's avatarLeszek Swirski <leszeks@chromium.org>
Commit-Queue: 王澳 <wangao.james@bytedance.com>
Cr-Commit-Position: refs/heads/main@{#82314}
parent 850b2250
......@@ -140,10 +140,6 @@ MaybeLocal<Value> CompileRun(Isolate* isolate, const char* source) {
return script->Run(context);
}
v8::Local<v8::String> v8_str(const char* x) {
return v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), x).ToLocalChecked();
}
} // namespace
TEST_F(AccessCheckTest, GetOwnPropertyDescriptor) {
......@@ -159,7 +155,7 @@ TEST_F(AccessCheckTest, GetOwnPropertyDescriptor) {
Local<FunctionTemplate> setter_template = FunctionTemplate::New(
isolate(), [](const FunctionCallbackInfo<v8::Value>& info) { FAIL(); });
setter_template->SetAcceptAnyReceiver(false);
global_template->SetAccessorProperty(v8_str("property"), getter_template,
global_template->SetAccessorProperty(NewString("property"), getter_template,
setter_template);
Local<Context> target_context =
......@@ -168,7 +164,7 @@ TEST_F(AccessCheckTest, GetOwnPropertyDescriptor) {
Context::New(isolate(), nullptr, global_template);
accessing_context->Global()
->Set(accessing_context, v8_str("other"), target_context->Global())
->Set(accessing_context, NewString("other"), target_context->Global())
.FromJust();
Context::Scope context_scope(accessing_context);
......@@ -212,7 +208,7 @@ TEST_F(AccessRegressionTest,
isolate(), [](const FunctionCallbackInfo<v8::Value>&) { FAIL(); });
Local<ObjectTemplate> object_template = ObjectTemplate::New(isolate());
object_template->SetAccessorProperty(v8_str("property"), getter_template,
object_template->SetAccessorProperty(NewString("property"), getter_template,
setter_template);
Local<Context> context1 = Context::New(isolate(), nullptr);
......@@ -221,7 +217,7 @@ TEST_F(AccessRegressionTest,
Local<Object> object =
object_template->NewInstance(context1).ToLocalChecked();
context2->Global()
->Set(context2, v8_str("object_from_context1"), object)
->Set(context2, NewString("object_from_context1"), object)
.Check();
i::Handle<i::JSFunction> getter = RetrieveFunctionFrom(
......@@ -257,18 +253,18 @@ TEST_F(AccessRegressionTest,
Local<ObjectTemplate> object_template = ObjectTemplate::New(isolate());
object_template->Set(isolate(), "breakfn", break_template);
object_template->SetAccessorProperty(v8_str("property"), getter_template,
object_template->SetAccessorProperty(NewString("property"), getter_template,
setter_template);
Local<Object> object1 =
object_template->NewInstance(context1).ToLocalChecked();
EXPECT_TRUE(
context1->Global()->Set(context1, v8_str("object"), object1).IsJust());
context1->Global()->Set(context1, NewString("object"), object1).IsJust());
Local<Object> object2 =
object_template->NewInstance(context2).ToLocalChecked();
EXPECT_TRUE(
context2->Global()->Set(context2, v8_str("object"), object2).IsJust());
context2->Global()->Set(context2, NewString("object"), object2).IsJust());
// Force instantiation of the JSFunction for the getter and setter
// of {object.property} by setting a break point on {object.breakfn}
......
......@@ -14,8 +14,6 @@
namespace v8 {
namespace {
using ScriptTest = TestWithContext;
namespace {
bool ValueEqualsString(v8::Isolate* isolate, Local<Value> lhs,
const char* rhs) {
......@@ -24,30 +22,6 @@ bool ValueEqualsString(v8::Isolate* isolate, Local<Value> lhs,
String::Utf8Value utf8_lhs(isolate, lhs);
return strcmp(rhs, *utf8_lhs) == 0;
}
} // namespace
TEST_F(ScriptTest, UnboundScriptPosition) {
const char* url = "http://www.foo.com/foo.js";
v8::ScriptOrigin origin(isolate(), NewString(url), 13, 0);
v8::ScriptCompiler::Source script_source(NewString("var foo;"), origin);
Local<Script> script =
v8::ScriptCompiler::Compile(v8_context(), &script_source).ToLocalChecked();
EXPECT_TRUE(
ValueEqualsString(isolate(), script->GetUnboundScript()->GetScriptName(),
url));
Local<UnboundScript> unbound_script = script->GetUnboundScript();
int line_number = unbound_script->GetLineNumber();
EXPECT_EQ(13, line_number);
int column_number = unbound_script->GetColumnNumber();
EXPECT_EQ(0, column_number);
}
namespace {
v8::Local<v8::String> v8_str(const char* x) {
return v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), x).ToLocalChecked();
}
std::string from_v8_string(Isolate* isolate, Local<String> str) {
String::Utf8Value utf8(isolate, str);
......@@ -78,64 +52,87 @@ v8::MaybeLocal<Module> ResolveToTopLevelAwait(Local<Context> context,
UNREACHABLE();
}
v8::ScriptCompiler::Source source(v8_str(source_string.c_str()), origin);
v8::ScriptCompiler::Source source(
v8::String::NewFromUtf8(isolate, source_string.c_str()).ToLocalChecked(),
origin);
auto res = v8::ScriptCompiler::CompileModule(isolate, &source);
return res;
}
void TestGetStalledTopLevelAwaitMessage(
v8::Isolate* isolate, const char* source_str,
std::vector<std::string> expected_stalled) {
v8::Isolate::Scope iscope(isolate);
v8::HandleScope scope(isolate);
v8::Local<v8::Context> context = v8::Context::New(isolate);
v8::Context::Scope cscope(context);
v8::ScriptOrigin origin(isolate, v8_str("root.mjs"), 0, 0, false, -1,
Local<Value>(), false, false, true);
v8::ScriptCompiler::Source source(v8_str(source_str), origin);
Local<Module> root =
v8::ScriptCompiler::CompileModule(isolate, &source).ToLocalChecked();
CHECK(root->InstantiateModule(context, ResolveToTopLevelAwait)
.FromMaybe(false));
Local<v8::Promise> promise =
root->Evaluate(context).ToLocalChecked().As<v8::Promise>();
isolate->PerformMicrotaskCheckpoint();
CHECK_EQ(expected_stalled.size() > 0 ? v8::Promise::PromiseState::kPending
: v8::Promise::PromiseState::kFulfilled,
promise->State());
std::vector<std::tuple<Local<Module>, Local<Message>>> stalled =
root->GetStalledTopLevelAwaitMessage(isolate);
CHECK_EQ(expected_stalled.size(), stalled.size());
for (size_t i = 0; i < stalled.size(); ++i) {
Local<Message> message = std::get<1>(stalled[i]);
CHECK_EQ("Top-level await promise never resolved",
from_v8_string(isolate, message->Get()));
CHECK_EQ(
expected_stalled[i],
from_v8_string(isolate, message->GetScriptResourceName().As<String>()));
CHECK_EQ("await promise;",
from_v8_string(isolate,
message->GetSourceLine(context).ToLocalChecked()));
CHECK_EQ(7, message->GetLineNumber(context).ToChecked());
CHECK_EQ(0, message->GetStartColumn(context).ToChecked());
CHECK_EQ(1, message->GetEndColumn(context).ToChecked());
class ScriptTest : public TestWithContext {
protected:
void TestGetStalledTopLevelAwaitMessage(
const char* source_str, std::vector<std::string> expected_stalled) {
v8::Isolate::Scope iscope(isolate());
v8::HandleScope scope(isolate());
v8::Local<v8::Context> context = v8::Context::New(isolate());
v8::Context::Scope cscope(context);
v8::ScriptOrigin origin(isolate(), NewString("root.mjs"), 0, 0, false, -1,
Local<Value>(), false, false, true);
v8::ScriptCompiler::Source source(NewString(source_str), origin);
Local<Module> root =
v8::ScriptCompiler::CompileModule(isolate(), &source).ToLocalChecked();
CHECK(root->InstantiateModule(context, ResolveToTopLevelAwait)
.FromMaybe(false));
Local<v8::Promise> promise =
root->Evaluate(context).ToLocalChecked().As<v8::Promise>();
isolate()->PerformMicrotaskCheckpoint();
CHECK_EQ(expected_stalled.size() > 0
? v8::Promise::PromiseState::kPending
: v8::Promise::PromiseState::kFulfilled,
promise->State());
std::vector<std::tuple<Local<Module>, Local<Message>>> stalled =
root->GetStalledTopLevelAwaitMessage(isolate());
CHECK_EQ(expected_stalled.size(), stalled.size());
for (size_t i = 0; i < stalled.size(); ++i) {
Local<Message> message = std::get<1>(stalled[i]);
CHECK_EQ("Top-level await promise never resolved",
from_v8_string(isolate(), message->Get()));
CHECK_EQ(expected_stalled[i],
from_v8_string(isolate(),
message->GetScriptResourceName().As<String>()));
CHECK_EQ(
"await promise;",
from_v8_string(isolate(),
message->GetSourceLine(context).ToLocalChecked()));
CHECK_EQ(7, message->GetLineNumber(context).ToChecked());
CHECK_EQ(0, message->GetStartColumn(context).ToChecked());
CHECK_EQ(1, message->GetEndColumn(context).ToChecked());
}
}
}
};
} // namespace
TEST_F(ScriptTest, UnboundScriptPosition) {
const char* url = "http://www.foo.com/foo.js";
v8::ScriptOrigin origin(isolate(), NewString(url), 13, 0);
v8::ScriptCompiler::Source script_source(NewString("var foo;"), origin);
Local<Script> script =
v8::ScriptCompiler::Compile(v8_context(), &script_source)
.ToLocalChecked();
EXPECT_TRUE(ValueEqualsString(
isolate(), script->GetUnboundScript()->GetScriptName(), url));
Local<UnboundScript> unbound_script = script->GetUnboundScript();
int line_number = unbound_script->GetLineNumber();
EXPECT_EQ(13, line_number);
int column_number = unbound_script->GetColumnNumber();
EXPECT_EQ(0, column_number);
}
TEST_F(ScriptTest, GetSingleStalledTopLevelAwaitMessage) {
TestGetStalledTopLevelAwaitMessage(isolate(), "import 'stall.mjs';",
{"stall.mjs"});
TestGetStalledTopLevelAwaitMessage("import 'stall.mjs';", {"stall.mjs"});
}
TEST_F(ScriptTest, GetMultipleStalledTopLevelAwaitMessage) {
TestGetStalledTopLevelAwaitMessage(
isolate(),
"import 'stall.mjs';\n"
"import 'stall_2.mjs';\n"
"import 'stall_3.mjs';\n"
......@@ -144,20 +141,20 @@ TEST_F(ScriptTest, GetMultipleStalledTopLevelAwaitMessage) {
}
TEST_F(ScriptTest, GetMixedStalledTopLevelAwaitMessage) {
TestGetStalledTopLevelAwaitMessage(isolate(),
"import 'stall.mjs';\n"
"import 'resolve.mjs';\n"
"import 'stall_2.mjs';\n"
"import 'resolve.mjs';\n",
{"stall.mjs", "stall_2.mjs"});
TestGetStalledTopLevelAwaitMessage(
"import 'stall.mjs';\n"
"import 'resolve.mjs';\n"
"import 'stall_2.mjs';\n"
"import 'resolve.mjs';\n",
{"stall.mjs", "stall_2.mjs"});
}
TEST_F(ScriptTest, GetEmptyStalledTopLevelAwaitMessage) {
TestGetStalledTopLevelAwaitMessage(isolate(),
"import 'resolve.mjs';\n"
"import 'resolve_2.mjs';\n"
"import 'resolve_3.mjs';\n",
{});
TestGetStalledTopLevelAwaitMessage(
"import 'resolve.mjs';\n"
"import 'resolve_2.mjs';\n"
"import 'resolve_3.mjs';\n",
{});
}
} // namespace
......
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