Skip to content

Commit 1dfa11d

Browse files
authored
fix(cpp): check if JVM is initialized in KotlinLanguage::thread_enter and KotlinLanguage::thread_exit as a quick fix for project setup problems (#559)
1 parent a4dc6e8 commit 1dfa11d

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

src/jni/jvm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ namespace jni {
3131

3232
static Type get_type();
3333

34+
static bool is_initialized();
35+
3436
private:
3537
Jvm() = default;
3638
static JavaVM* vm;

src/jni/platforms/jvm_android.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ namespace jni {
4545
Jvm::Type Jvm::get_type() {
4646
return vm_type;
4747
}
48+
49+
bool Jvm::is_initialized() {
50+
return true;
51+
}
4852
}// namespace jni
4953

5054
#endif

src/jni/platforms/jvm_default.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ namespace jni {
100100
Jvm::Type Jvm::get_type() {
101101
return vm_type;
102102
}
103+
104+
bool Jvm::is_initialized() {
105+
return vm;
106+
}
103107
}// namespace jni
104108

105109
#endif

src/kotlin_language.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,26 @@ void KotlinLanguage::remove_named_global_constant(const StringName& p_name) {
277277
}
278278

279279
void KotlinLanguage::thread_enter() {
280+
//TODO: Remove this ifdef and its content while reworking GDKotlin and moving out logic of finding JVM.
281+
#ifdef DEBUG_ENABLED
282+
if (!jni::Jvm::is_initialized()) {
283+
LOG_ERROR("JavaVM is not initialized, please make sure your project contains an embedded JVM or JAVA_HOME environment variable is setup");
284+
return;
285+
}
286+
#endif
287+
280288
jni::Jvm::attach();
281289
}
282290

283291
void KotlinLanguage::thread_exit() {
292+
//TODO: Remove this ifdef and its content while reworking GDKotlin and moving out logic of finding JVM.
293+
#ifdef DEBUG_ENABLED
294+
if (!jni::Jvm::is_initialized()) {
295+
LOG_ERROR("JavaVM is not initialized, please make sure your project contains an embedded JVM or JAVA_HOME environment variable is setup");
296+
return;
297+
}
298+
#endif
299+
284300
jni::Jvm::detach();
285301
}
286302

0 commit comments

Comments
 (0)