Skip to content

Truly independent web browser

LibJS: Let code ask whether its thread has a VM

Andreas Kling47bfeff19082
diff --git a/Libraries/LibJS/Runtime/VM.cpp b/Libraries/LibJS/Runtime/VM.cpp
index 96e6658f3e8..bc2d246dec5 100644
--- a/Libraries/LibJS/Runtime/VM.cpp
+++ b/Libraries/LibJS/Runtime/VM.cpp
@@ -56,6 +56,11 @@ VM& VM::the()
{
return *s_the;
}
+
+bool VM::exists_on_this_thread()
+{
+ return s_the != nullptr;
+}
#else
constinit thread_local VM* VM::s_the = nullptr;
#endif
diff --git a/Libraries/LibJS/Runtime/VM.h b/Libraries/LibJS/Runtime/VM.h
index a6b252a49c5..397049cd58e 100644
--- a/Libraries/LibJS/Runtime/VM.h
+++ b/Libraries/LibJS/Runtime/VM.h
@@ -132,8 +132,10 @@ public:
// The VM of the calling thread. A thread that runs a VM creates and destroys it, and has at most one at a time.
#if defined(AK_OS_WINDOWS)
static VM& the();
+ static bool exists_on_this_thread();
#else
ALWAYS_INLINE static VM& the() { return *s_the; }
+ ALWAYS_INLINE static bool exists_on_this_thread() { return s_the != nullptr; }
#endif
// A thread that runs a VM should have a stack of at least this size, which is what main threads usually get. The