Commit 8dbeee0d authored by jameslahm's avatar jameslahm Committed by V8 LUCI CQ

[test] Move cctest/test-global-object to unittests/

... objects/global-object-unittest.

Bug: v8:12781
Change-Id: I40a8d00301531e7d1a6dce90e1226c4568c8d72c
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3713521
Commit-Queue: 王澳 <wangao.james@bytedance.com>
Reviewed-by: 's avatarLeszek Swirski <leszeks@chromium.org>
Cr-Commit-Position: refs/heads/main@{#81390}
parent fda71f4c
...@@ -189,7 +189,6 @@ v8_source_set("cctest_sources") { ...@@ -189,7 +189,6 @@ v8_source_set("cctest_sources") {
"test-field-type-tracking.cc", "test-field-type-tracking.cc",
"test-func-name-inference.cc", "test-func-name-inference.cc",
"test-global-handles.cc", "test-global-handles.cc",
"test-global-object.cc",
"test-heap-profiler.cc", "test-heap-profiler.cc",
"test-icache.cc", "test-icache.cc",
"test-identity-map.cc", "test-identity-map.cc",
......
...@@ -426,6 +426,7 @@ v8_source_set("unittests_sources") { ...@@ -426,6 +426,7 @@ v8_source_set("unittests_sources") {
"objects/concurrent-string-unittest.cc", "objects/concurrent-string-unittest.cc",
"objects/concurrent-transition-array-unittest.cc", "objects/concurrent-transition-array-unittest.cc",
"objects/elements-kind-unittest.cc", "objects/elements-kind-unittest.cc",
"objects/global-object-unittest.cc",
"objects/hashcode-unittest.cc", "objects/hashcode-unittest.cc",
"objects/managed-unittest.cc", "objects/managed-unittest.cc",
"objects/modules-unittest.cc", "objects/modules-unittest.cc",
......
...@@ -27,93 +27,81 @@ ...@@ -27,93 +27,81 @@
#include "src/init/v8.h" #include "src/init/v8.h"
#include "src/objects/objects-inl.h" #include "src/objects/objects-inl.h"
#include "test/cctest/cctest.h" #include "test/unittests/test-utils.h"
#include "testing/gmock-support.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::v8::Array; namespace v8 {
using ::v8::Context;
using ::v8::Local; using GlobalObjectTest = TestWithContext;
using ::v8::Value;
// This test fails if properties on the prototype of the global object appear // This test fails if properties on the prototype of the global object appear
// as declared globals. // as declared globals.
TEST(StrictUndeclaredGlobalVariable) { TEST_F(GlobalObjectTest, StrictUndeclaredGlobalVariable) {
v8::HandleScope scope(CcTest::isolate()); Local<String> var_name = NewString("x");
v8::Local<v8::String> var_name = v8_str("x"); TryCatch try_catch(isolate());
LocalContext context; Local<Object> proto = Object::New(isolate());
v8::TryCatch try_catch(CcTest::isolate()); Local<Object> global = context()->Global()->GetPrototype().As<Object>();
v8::Local<v8::Script> script = v8_compile("\"use strict\"; x = 42;"); proto->Set(context(), var_name, Number::New(isolate(), 100)).FromJust();
v8::Local<v8::Object> proto = v8::Object::New(CcTest::isolate()); global->SetPrototype(context(), proto).FromJust();
v8::Local<v8::Object> global = CHECK(TryRunJS("\"use strict\"; x = 42;").IsEmpty());
context->Global()->GetPrototype().As<v8::Object>();
proto->Set(context.local(), var_name, v8_num(100)).FromJust();
global->SetPrototype(context.local(), proto).FromJust();
CHECK(script->Run(context.local()).IsEmpty());
CHECK(try_catch.HasCaught()); CHECK(try_catch.HasCaught());
v8::String::Utf8Value exception(CcTest::isolate(), try_catch.Exception()); String::Utf8Value exception(isolate(), try_catch.Exception());
CHECK_EQ(0, strcmp("ReferenceError: x is not defined", *exception)); CHECK_EQ(0, strcmp("ReferenceError: x is not defined", *exception));
} }
TEST_F(GlobalObjectTest, KeysGlobalObject_Regress2764) {
TEST(KeysGlobalObject_Regress2764) { Local<Context> env1 = context();
LocalContext env1;
v8::HandleScope scope(env1->GetIsolate());
// Create second environment. // Create second environment.
v8::Local<Context> env2 = Context::New(env1->GetIsolate()); Local<Context> env2 = Context::New(env1->GetIsolate());
Local<Value> token = v8_str("foo"); Local<Value> token = NewString("foo");
// Set same security token for env1 and env2. // Set same security token for env1 and env2.
env1->SetSecurityToken(token); env1->SetSecurityToken(token);
env2->SetSecurityToken(token); env2->SetSecurityToken(token);
// Create a reference to env2 global from env1 global. // Create a reference to env2 global from env1 global.
env1->Global() env1->Global()->Set(env1, NewString("global2"), env2->Global()).FromJust();
->Set(env1.local(), v8_str("global2"), env2->Global())
.FromJust();
// Set some global variables in global2 // Set some global variables in global2
env2->Global()->Set(env2, v8_str("a"), v8_str("a")).FromJust(); env2->Global()->Set(env2, NewString("a"), NewString("a")).FromJust();
env2->Global()->Set(env2, v8_str("42"), v8_str("42")).FromJust(); env2->Global()->Set(env2, NewString("42"), NewString("42")).FromJust();
// List all entries from global2. // List all entries from global2.
Local<Array> result; Local<Array> result;
result = Local<Array>::Cast(CompileRun("Object.keys(global2)")); result = Local<Array>::Cast(RunJS("Object.keys(global2)"));
CHECK_EQ(2u, result->Length()); CHECK_EQ(2u, result->Length());
CHECK( CHECK(NewString("42")
v8_str("42") ->Equals(env1, result->Get(env1, 0).ToLocalChecked())
->Equals(env1.local(), result->Get(env1.local(), 0).ToLocalChecked()) .FromJust());
.FromJust()); CHECK(NewString("a")
CHECK( ->Equals(env1, result->Get(env1, 1).ToLocalChecked())
v8_str("a") .FromJust());
->Equals(env1.local(), result->Get(env1.local(), 1).ToLocalChecked())
.FromJust()); result = Local<Array>::Cast(RunJS("Object.getOwnPropertyNames(global2)"));
result =
Local<Array>::Cast(CompileRun("Object.getOwnPropertyNames(global2)"));
CHECK_LT(2u, result->Length()); CHECK_LT(2u, result->Length());
// Check that all elements are in the property names // Check that all elements are in the property names
ExpectTrue("-1 < Object.getOwnPropertyNames(global2).indexOf('42')"); CHECK(RunJS("-1 < Object.getOwnPropertyNames(global2).indexOf('42')")
ExpectTrue("-1 < Object.getOwnPropertyNames(global2).indexOf('a')"); ->IsTrue());
CHECK(
RunJS("-1 < Object.getOwnPropertyNames(global2).indexOf('a')")->IsTrue());
// Hold on to global from env2 and detach global from env2. // Hold on to global from env2 and detach global from env2.
env2->DetachGlobal(); env2->DetachGlobal();
// List again all entries from the detached global2. // List again all entries from the detached global2.
result = Local<Array>::Cast(CompileRun("Object.keys(global2)")); result = Local<Array>::Cast(RunJS("Object.keys(global2)"));
CHECK_EQ(0u, result->Length()); CHECK_EQ(0u, result->Length());
result = result = Local<Array>::Cast(RunJS("Object.getOwnPropertyNames(global2)"));
Local<Array>::Cast(CompileRun("Object.getOwnPropertyNames(global2)"));
CHECK_EQ(0u, result->Length()); CHECK_EQ(0u, result->Length());
} }
TEST(KeysGlobalObject_SetPrototype) { TEST_F(GlobalObjectTest, KeysGlobalObject_SetPrototype) {
LocalContext env1; Local<Context> env1 = context();
v8::HandleScope scope(env1->GetIsolate());
// Create second environment. // Create second environment.
v8::Local<Context> env2 = Context::New(env1->GetIsolate()); Local<Context> env2 = Context::New(env1->GetIsolate());
Local<Value> token = v8_str("foo"); Local<Value> token = NewString("foo");
// Set same security token for env1 and env2. // Set same security token for env1 and env2.
env1->SetSecurityToken(token); env1->SetSecurityToken(token);
...@@ -122,13 +110,15 @@ TEST(KeysGlobalObject_SetPrototype) { ...@@ -122,13 +110,15 @@ TEST(KeysGlobalObject_SetPrototype) {
// Create a reference to env2 global from env1 global. // Create a reference to env2 global from env1 global.
env1->Global() env1->Global()
->GetPrototype() ->GetPrototype()
.As<v8::Object>() .As<Object>()
->SetPrototype(env1.local(), env2->Global()->GetPrototype()) ->SetPrototype(env1, env2->Global()->GetPrototype())
.FromJust(); .FromJust();
// Set some global variables in global2 // Set some global variables in global2
env2->Global()->Set(env2, v8_str("a"), v8_str("a")).FromJust(); env2->Global()->Set(env2, NewString("a"), NewString("a")).FromJust();
env2->Global()->Set(env2, v8_str("42"), v8_str("42")).FromJust(); env2->Global()->Set(env2, NewString("42"), NewString("42")).FromJust();
// List all entries from global2. // List all entries from global2.
ExpectTrue("a == 'a'"); CHECK(RunJS("a == 'a'")->IsTrue());
} }
} // 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