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) { ...@@ -140,10 +140,6 @@ MaybeLocal<Value> CompileRun(Isolate* isolate, const char* source) {
return script->Run(context); return script->Run(context);
} }
v8::Local<v8::String> v8_str(const char* x) {
return v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), x).ToLocalChecked();
}
} // namespace } // namespace
TEST_F(AccessCheckTest, GetOwnPropertyDescriptor) { TEST_F(AccessCheckTest, GetOwnPropertyDescriptor) {
...@@ -159,7 +155,7 @@ TEST_F(AccessCheckTest, GetOwnPropertyDescriptor) { ...@@ -159,7 +155,7 @@ TEST_F(AccessCheckTest, GetOwnPropertyDescriptor) {
Local<FunctionTemplate> setter_template = FunctionTemplate::New( Local<FunctionTemplate> setter_template = FunctionTemplate::New(
isolate(), [](const FunctionCallbackInfo<v8::Value>& info) { FAIL(); }); isolate(), [](const FunctionCallbackInfo<v8::Value>& info) { FAIL(); });
setter_template->SetAcceptAnyReceiver(false); setter_template->SetAcceptAnyReceiver(false);
global_template->SetAccessorProperty(v8_str("property"), getter_template, global_template->SetAccessorProperty(NewString("property"), getter_template,
setter_template); setter_template);
Local<Context> target_context = Local<Context> target_context =
...@@ -168,7 +164,7 @@ TEST_F(AccessCheckTest, GetOwnPropertyDescriptor) { ...@@ -168,7 +164,7 @@ TEST_F(AccessCheckTest, GetOwnPropertyDescriptor) {
Context::New(isolate(), nullptr, global_template); Context::New(isolate(), nullptr, global_template);
accessing_context->Global() accessing_context->Global()
->Set(accessing_context, v8_str("other"), target_context->Global()) ->Set(accessing_context, NewString("other"), target_context->Global())
.FromJust(); .FromJust();
Context::Scope context_scope(accessing_context); Context::Scope context_scope(accessing_context);
...@@ -212,7 +208,7 @@ TEST_F(AccessRegressionTest, ...@@ -212,7 +208,7 @@ TEST_F(AccessRegressionTest,
isolate(), [](const FunctionCallbackInfo<v8::Value>&) { FAIL(); }); isolate(), [](const FunctionCallbackInfo<v8::Value>&) { FAIL(); });
Local<ObjectTemplate> object_template = ObjectTemplate::New(isolate()); Local<ObjectTemplate> object_template = ObjectTemplate::New(isolate());
object_template->SetAccessorProperty(v8_str("property"), getter_template, object_template->SetAccessorProperty(NewString("property"), getter_template,
setter_template); setter_template);
Local<Context> context1 = Context::New(isolate(), nullptr); Local<Context> context1 = Context::New(isolate(), nullptr);
...@@ -221,7 +217,7 @@ TEST_F(AccessRegressionTest, ...@@ -221,7 +217,7 @@ TEST_F(AccessRegressionTest,
Local<Object> object = Local<Object> object =
object_template->NewInstance(context1).ToLocalChecked(); object_template->NewInstance(context1).ToLocalChecked();
context2->Global() context2->Global()
->Set(context2, v8_str("object_from_context1"), object) ->Set(context2, NewString("object_from_context1"), object)
.Check(); .Check();
i::Handle<i::JSFunction> getter = RetrieveFunctionFrom( i::Handle<i::JSFunction> getter = RetrieveFunctionFrom(
...@@ -257,18 +253,18 @@ TEST_F(AccessRegressionTest, ...@@ -257,18 +253,18 @@ TEST_F(AccessRegressionTest,
Local<ObjectTemplate> object_template = ObjectTemplate::New(isolate()); Local<ObjectTemplate> object_template = ObjectTemplate::New(isolate());
object_template->Set(isolate(), "breakfn", break_template); object_template->Set(isolate(), "breakfn", break_template);
object_template->SetAccessorProperty(v8_str("property"), getter_template, object_template->SetAccessorProperty(NewString("property"), getter_template,
setter_template); setter_template);
Local<Object> object1 = Local<Object> object1 =
object_template->NewInstance(context1).ToLocalChecked(); object_template->NewInstance(context1).ToLocalChecked();
EXPECT_TRUE( EXPECT_TRUE(
context1->Global()->Set(context1, v8_str("object"), object1).IsJust()); context1->Global()->Set(context1, NewString("object"), object1).IsJust());
Local<Object> object2 = Local<Object> object2 =
object_template->NewInstance(context2).ToLocalChecked(); object_template->NewInstance(context2).ToLocalChecked();
EXPECT_TRUE( 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 // Force instantiation of the JSFunction for the getter and setter
// of {object.property} by setting a break point on {object.breakfn} // of {object.property} by setting a break point on {object.breakfn}
......
...@@ -14,8 +14,6 @@ ...@@ -14,8 +14,6 @@
namespace v8 { namespace v8 {
namespace { namespace {
using ScriptTest = TestWithContext;
namespace { namespace {
bool ValueEqualsString(v8::Isolate* isolate, Local<Value> lhs, bool ValueEqualsString(v8::Isolate* isolate, Local<Value> lhs,
const char* rhs) { const char* rhs) {
...@@ -24,30 +22,6 @@ bool ValueEqualsString(v8::Isolate* isolate, Local<Value> lhs, ...@@ -24,30 +22,6 @@ bool ValueEqualsString(v8::Isolate* isolate, Local<Value> lhs,
String::Utf8Value utf8_lhs(isolate, lhs); String::Utf8Value utf8_lhs(isolate, lhs);
return strcmp(rhs, *utf8_lhs) == 0; 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) { std::string from_v8_string(Isolate* isolate, Local<String> str) {
String::Utf8Value utf8(isolate, str); String::Utf8Value utf8(isolate, str);
...@@ -78,64 +52,87 @@ v8::MaybeLocal<Module> ResolveToTopLevelAwait(Local<Context> context, ...@@ -78,64 +52,87 @@ v8::MaybeLocal<Module> ResolveToTopLevelAwait(Local<Context> context,
UNREACHABLE(); 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); auto res = v8::ScriptCompiler::CompileModule(isolate, &source);
return res; return res;
} }
void TestGetStalledTopLevelAwaitMessage( class ScriptTest : public TestWithContext {
v8::Isolate* isolate, const char* source_str, protected:
std::vector<std::string> expected_stalled) { void TestGetStalledTopLevelAwaitMessage(
v8::Isolate::Scope iscope(isolate); const char* source_str, std::vector<std::string> expected_stalled) {
v8::HandleScope scope(isolate); v8::Isolate::Scope iscope(isolate());
v8::Local<v8::Context> context = v8::Context::New(isolate); v8::HandleScope scope(isolate());
v8::Context::Scope cscope(context); 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::ScriptOrigin origin(isolate(), NewString("root.mjs"), 0, 0, false, -1,
v8::ScriptCompiler::Source source(v8_str(source_str), origin); Local<Value>(), false, false, true);
Local<Module> root = v8::ScriptCompiler::Source source(NewString(source_str), origin);
v8::ScriptCompiler::CompileModule(isolate, &source).ToLocalChecked(); Local<Module> root =
v8::ScriptCompiler::CompileModule(isolate(), &source).ToLocalChecked();
CHECK(root->InstantiateModule(context, ResolveToTopLevelAwait)
.FromMaybe(false)); CHECK(root->InstantiateModule(context, ResolveToTopLevelAwait)
.FromMaybe(false));
Local<v8::Promise> promise =
root->Evaluate(context).ToLocalChecked().As<v8::Promise>(); Local<v8::Promise> promise =
isolate->PerformMicrotaskCheckpoint(); root->Evaluate(context).ToLocalChecked().As<v8::Promise>();
CHECK_EQ(expected_stalled.size() > 0 ? v8::Promise::PromiseState::kPending isolate()->PerformMicrotaskCheckpoint();
: v8::Promise::PromiseState::kFulfilled, CHECK_EQ(expected_stalled.size() > 0
promise->State()); ? v8::Promise::PromiseState::kPending
: v8::Promise::PromiseState::kFulfilled,
std::vector<std::tuple<Local<Module>, Local<Message>>> stalled = promise->State());
root->GetStalledTopLevelAwaitMessage(isolate);
CHECK_EQ(expected_stalled.size(), stalled.size()); std::vector<std::tuple<Local<Module>, Local<Message>>> stalled =
for (size_t i = 0; i < stalled.size(); ++i) { root->GetStalledTopLevelAwaitMessage(isolate());
Local<Message> message = std::get<1>(stalled[i]); CHECK_EQ(expected_stalled.size(), stalled.size());
CHECK_EQ("Top-level await promise never resolved", for (size_t i = 0; i < stalled.size(); ++i) {
from_v8_string(isolate, message->Get())); Local<Message> message = std::get<1>(stalled[i]);
CHECK_EQ( CHECK_EQ("Top-level await promise never resolved",
expected_stalled[i], from_v8_string(isolate(), message->Get()));
from_v8_string(isolate, message->GetScriptResourceName().As<String>())); CHECK_EQ(expected_stalled[i],
CHECK_EQ("await promise;", from_v8_string(isolate(),
from_v8_string(isolate, message->GetScriptResourceName().As<String>()));
message->GetSourceLine(context).ToLocalChecked())); CHECK_EQ(
"await promise;",
CHECK_EQ(7, message->GetLineNumber(context).ToChecked()); from_v8_string(isolate(),
CHECK_EQ(0, message->GetStartColumn(context).ToChecked()); message->GetSourceLine(context).ToLocalChecked()));
CHECK_EQ(1, message->GetEndColumn(context).ToChecked());
CHECK_EQ(7, message->GetLineNumber(context).ToChecked());
CHECK_EQ(0, message->GetStartColumn(context).ToChecked());
CHECK_EQ(1, message->GetEndColumn(context).ToChecked());
}
} }
} };
} // namespace } // 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) { TEST_F(ScriptTest, GetSingleStalledTopLevelAwaitMessage) {
TestGetStalledTopLevelAwaitMessage(isolate(), "import 'stall.mjs';", TestGetStalledTopLevelAwaitMessage("import 'stall.mjs';", {"stall.mjs"});
{"stall.mjs"});
} }
TEST_F(ScriptTest, GetMultipleStalledTopLevelAwaitMessage) { TEST_F(ScriptTest, GetMultipleStalledTopLevelAwaitMessage) {
TestGetStalledTopLevelAwaitMessage( TestGetStalledTopLevelAwaitMessage(
isolate(),
"import 'stall.mjs';\n" "import 'stall.mjs';\n"
"import 'stall_2.mjs';\n" "import 'stall_2.mjs';\n"
"import 'stall_3.mjs';\n" "import 'stall_3.mjs';\n"
...@@ -144,20 +141,20 @@ TEST_F(ScriptTest, GetMultipleStalledTopLevelAwaitMessage) { ...@@ -144,20 +141,20 @@ TEST_F(ScriptTest, GetMultipleStalledTopLevelAwaitMessage) {
} }
TEST_F(ScriptTest, GetMixedStalledTopLevelAwaitMessage) { TEST_F(ScriptTest, GetMixedStalledTopLevelAwaitMessage) {
TestGetStalledTopLevelAwaitMessage(isolate(), TestGetStalledTopLevelAwaitMessage(
"import 'stall.mjs';\n" "import 'stall.mjs';\n"
"import 'resolve.mjs';\n" "import 'resolve.mjs';\n"
"import 'stall_2.mjs';\n" "import 'stall_2.mjs';\n"
"import 'resolve.mjs';\n", "import 'resolve.mjs';\n",
{"stall.mjs", "stall_2.mjs"}); {"stall.mjs", "stall_2.mjs"});
} }
TEST_F(ScriptTest, GetEmptyStalledTopLevelAwaitMessage) { TEST_F(ScriptTest, GetEmptyStalledTopLevelAwaitMessage) {
TestGetStalledTopLevelAwaitMessage(isolate(), TestGetStalledTopLevelAwaitMessage(
"import 'resolve.mjs';\n" "import 'resolve.mjs';\n"
"import 'resolve_2.mjs';\n" "import 'resolve_2.mjs';\n"
"import 'resolve_3.mjs';\n", "import 'resolve_3.mjs';\n",
{}); {});
} }
} // namespace } // 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