Commit 8e364522 authored by Camillo Bruni's avatar Camillo Bruni Committed by V8 LUCI CQ

[api] Add v8::Isolate::IsCurrent() method

Add a helper method for isolate == v8::Isolate::TryGetCurrent();

Bug: v8:12346
Change-Id: Ibbd1ce1865b0092c8dbcfad5a3bad99fb5858980
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3251173Reviewed-by: 's avatarMarja Hölttä <marja@chromium.org>
Reviewed-by: 's avatarVictor Gomes <victorgomes@chromium.org>
Commit-Queue: Victor Gomes <victorgomes@chromium.org>
Auto-Submit: Camillo Bruni <cbruni@chromium.org>
Cr-Commit-Position: refs/heads/main@{#77609}
parent 4d937998
......@@ -591,6 +591,11 @@ class V8_EXPORT Isolate {
*/
static Isolate* TryGetCurrent();
/**
* Return true if this isolate is currently active.
**/
bool IsCurrent() const;
/**
* Clears the set of objects held strongly by the heap. This set of
* objects are originally built when a WeakRef is created or
......
......@@ -8487,6 +8487,10 @@ Isolate* Isolate::TryGetCurrent() {
return reinterpret_cast<Isolate*>(isolate);
}
bool Isolate::IsCurrent() const {
return reinterpret_cast<const i::Isolate*>(this)->IsCurrent();
}
// static
Isolate* Isolate::Allocate() {
return reinterpret_cast<Isolate*>(i::Isolate::New());
......
......@@ -604,6 +604,8 @@ class V8_EXPORT_PRIVATE Isolate final : private HiddenFactory {
return isolate;
}
bool IsCurrent() const { return this == TryGetCurrent(); }
// Usually called by Init(), but can be called early e.g. to allow
// testing components that require logging but not the whole
// isolate.
......
......@@ -18570,7 +18570,17 @@ TEST(RunTwoIsolatesOnSingleThread) {
v8::Isolate::CreateParams create_params;
create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
v8::Isolate* isolate1 = v8::Isolate::New(create_params);
CHECK(CcTest::isolate()->IsCurrent());
CHECK(!isolate1->IsCurrent());
isolate1->Enter();
CHECK(!CcTest::isolate()->IsCurrent());
CHECK(isolate1->IsCurrent());
CHECK_EQ(isolate1, v8::Isolate::GetCurrent());
CHECK_EQ(isolate1, v8::Isolate::TryGetCurrent());
v8::Persistent<v8::Context> context1;
{
v8::HandleScope scope(isolate1);
......@@ -18591,8 +18601,16 @@ TEST(RunTwoIsolatesOnSingleThread) {
v8::Isolate* isolate2 = v8::Isolate::New(create_params);
v8::Persistent<v8::Context> context2;
CHECK(!CcTest::isolate()->IsCurrent());
CHECK(isolate1->IsCurrent());
CHECK(!isolate2->IsCurrent());
{
v8::Isolate::Scope iscope(isolate2);
CHECK(!isolate1->IsCurrent());
CHECK(isolate2->IsCurrent());
CHECK_EQ(isolate2, v8::Isolate::GetCurrent());
CHECK_EQ(isolate2, v8::Isolate::TryGetCurrent());
v8::HandleScope scope(isolate2);
context2.Reset(isolate2, Context::New(isolate2));
v8::Local<v8::Context> context =
......@@ -18604,6 +18622,10 @@ TEST(RunTwoIsolatesOnSingleThread) {
ExpectString("function f() { return foo; }; f()", "isolate 2");
}
CHECK(!CcTest::isolate()->IsCurrent());
CHECK(isolate1->IsCurrent());
CHECK(!isolate2->IsCurrent());
{
v8::HandleScope scope(isolate1);
v8::Local<v8::Context> context =
......@@ -18614,11 +18636,16 @@ TEST(RunTwoIsolatesOnSingleThread) {
}
isolate1->Exit();
CHECK(CcTest::isolate()->IsCurrent());
CHECK(!isolate1->IsCurrent());
CHECK(!isolate2->IsCurrent());
// Run some stuff in default isolate.
v8::Persistent<v8::Context> context_default;
{
v8::Isolate* isolate = CcTest::isolate();
CHECK_EQ(isolate, v8::Isolate::GetCurrent());
CHECK_EQ(isolate, v8::Isolate::TryGetCurrent());
v8::Isolate::Scope iscope(isolate);
v8::HandleScope scope(isolate);
context_default.Reset(isolate, Context::New(isolate));
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