Skip to content

Commit 7a7e8a9

Browse files
committed
Adapt jvm/wrapper
1 parent 8f08559 commit 7a7e8a9

26 files changed

+161
-176
lines changed

src/jvm/wrapper/bootstrap.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ void Bootstrap::init_native_image(jni::Env& p_env) {
5555
wrapped.call_void_method(p_env, INIT_NATIVE_IMAGE);
5656
}
5757

58-
String Bootstrap::get_version(jni::Env& p_env) {
58+
godot::String Bootstrap::get_version(jni::Env& p_env) {
5959
jni::JString str {wrapped.call_object_method(p_env, GET_VERSION)};
60-
String ret {p_env.from_jstring(str)};
60+
godot::String ret {p_env.from_jstring(str)};
6161
str.delete_local_ref(p_env);
6262
return ret;
6363
}

src/jvm/wrapper/bootstrap.h

+8-12
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,16 @@ JVM_INSTANCE_WRAPPER(Bootstrap, "godot.runtime.Bootstrap") {
2323
// clang-format on
2424

2525
public:
26+
static void load_classes(JNIEnv * p_env, jobject p_this, jobjectArray p_classes);
27+
static void register_engine_type(JNIEnv * p_env, jobject p_this, jobjectArray p_classes_names, jobjectArray p_singleton_names);
2628

27-
static void load_classes(JNIEnv* p_env, jobject p_this, jobjectArray p_classes);
28-
static void register_engine_type(
29-
JNIEnv* p_env, jobject p_this, jobjectArray p_classes_names,
30-
jobjectArray p_singleton_names
31-
);
32-
33-
Bootstrap(jni::Env& p_env, jni::JObject p_wrapped);
29+
Bootstrap(jni::Env & p_env, jni::JObject p_wrapped);
3430
~Bootstrap() = default;
3531

36-
void init_jar(jni::Env& p_env, const jni::JObject& p_class_loader);
37-
void init_native_image(jni::Env& p_env);
38-
String get_version(jni::Env& p_env);
39-
void finish(jni::Env& p_env);
32+
void init_jar(jni::Env & p_env, const jni::JObject& p_class_loader);
33+
void init_native_image(jni::Env & p_env);
34+
godot::String get_version(jni::Env & p_env);
35+
void finish(jni::Env & p_env);
4036
};
4137

42-
#endif// GODOT_JVM_BOOTSTRAP_H
38+
#endif // GODOT_JVM_BOOTSTRAP_H

src/jvm/wrapper/jvm_instance_wrapper.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
\
3333
public: \
3434
static void initialize_jni_binding(jni::Env& p_env, ClassLoader* class_loader) { \
35-
godot::Vector<jni::JNativeMethod> methods; \
35+
godot::Vector<jni::JNativeMethod> methods; \
3636
jni::JClass clazz; \
3737
if (class_loader) { \
3838
clazz = class_loader->load_class(p_env, fq_name); \
@@ -156,4 +156,4 @@ const jni::JObject& JvmInstanceWrapper<Derived, FqName>::get_wrapped() const {
156156
return wrapped;
157157
}
158158

159-
#endif// GODOT_JVM_JVM_INSTANCE_WRAPPER_H
159+
#endif // GODOT_JVM_JVM_INSTANCE_WRAPPER_H

src/jvm/wrapper/jvm_singleton_wrapper.h

+6-7
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class JvmSingletonWrapper : public JvmInstanceWrapper<Derived, FqName> {
5454

5555
template<class Derived, const char* FqName>
5656
bool JvmSingletonWrapper<Derived, FqName>::initialize(jni::Env& p_env, ClassLoader* class_loader) {
57-
JVM_DEV_ASSERT(!_instance, String(FqName) + " singleton is already initialized.");
57+
JVM_DEV_ASSERT(!_instance, godot::String(FqName) + " singleton is already initialized.");
5858

5959
jni::JClass singleton_cls;
6060
if (class_loader) {
@@ -66,24 +66,23 @@ bool JvmSingletonWrapper<Derived, FqName>::initialize(jni::Env& p_env, ClassLoad
6666
singleton_cls.get_static_field_id(p_env, "INSTANCE", godot::vformat("L%s;", FqName).replace(".", "/").utf8().ptr());
6767
jni::JObject singleton_instance = singleton_cls.get_static_object_field(p_env, singleton_instance_field);
6868

69-
JVM_ERR_FAIL_COND_V_MSG(singleton_instance.is_null(), false, "Failed to retrieve " + String(FqName) + " singleton");
69+
JVM_ERR_FAIL_COND_V_MSG(singleton_instance.is_null(), false, "Failed to retrieve " + godot::String(FqName) + " singleton");
7070

7171
_instance = new Derived(p_env, singleton_instance);
7272

7373
Derived::initialize_jni_binding(p_env, class_loader);
7474
return true;
7575
}
7676

77-
7877
template<class Derived, const char* FqName>
79-
Derived* JvmSingletonWrapper<Derived, FqName>::create_instance(jni::Env& p_env, ClassLoader* class_loader) {
78+
Derived* JvmSingletonWrapper<Derived, FqName>::create_instance(jni::Env&, ClassLoader*) {
8079
JVM_DEV_ASSERT(true, "Can't create a new instance of a this class. Returning the singleton instead");
8180
return _instance;
8281
}
8382

8483
template<class Derived, const char* FqName>
8584
void JvmSingletonWrapper<Derived, FqName>::finalize(jni::Env& p_env, ClassLoader* class_loader) {
86-
JVM_DEV_ASSERT(_instance, String(FqName) + " singleton is not initialized.");
85+
JVM_DEV_ASSERT(_instance, godot::String(FqName) + " singleton is not initialized.");
8786
delete _instance;
8887
_instance = nullptr;
8988
Derived::finalize_jni_binding(p_env, class_loader);
@@ -94,12 +93,12 @@ Derived* JvmSingletonWrapper<Derived, FqName>::_instance {nullptr};
9493

9594
template<class Derived, const char* FqName>
9695
Derived& JvmSingletonWrapper<Derived, FqName>::get_instance() {
97-
JVM_DEV_ASSERT(_instance, String(FqName) + " singleton is not initialized.");
96+
JVM_DEV_ASSERT(_instance, godot::String(FqName) + " singleton is not initialized.");
9897
return *_instance;
9998
}
10099

101100
template<class Derived, const char* FqName>
102101
JvmSingletonWrapper<Derived, FqName>::JvmSingletonWrapper(jni::Env& p_env, jni::JObject p_wrapped) :
103102
JvmInstanceWrapper<Derived, FqName>(p_env, p_wrapped) {}
104103

105-
#endif// GODOT_JVM_JVM_SINGLETON_WRAPPER_H
104+
#endif // GODOT_JVM_JVM_SINGLETON_WRAPPER_H

src/jvm/wrapper/kotlin_callable_custom.cpp

+15-26
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
#include "jvm/wrapper/memory/transfer_context.h"
55

66
void LambdaCallable::invoke(jni::Env& p_env, const godot::Variant** p_args, int args_count, godot::Variant& r_ret) const {
7-
TransferContext& transfer_context{TransferContext::get_instance()};
7+
TransferContext& transfer_context {TransferContext::get_instance()};
88
transfer_context.write_args(p_env, p_args, args_count);
99

1010
if (has_return_value) {
11-
jni::JObject ret{wrapped.call_object_method<false>(p_env, INVOKE_WITH_RETURN)};
11+
jni::JObject ret {wrapped.call_object_method<false>(p_env, INVOKE_WITH_RETURN)};
1212
transfer_context.read_return_value(p_env, r_ret);
1313
ret.delete_local_ref(p_env);
1414

@@ -21,9 +21,7 @@ void LambdaCallable::invoke(jni::Env& p_env, const godot::Variant** p_args, int
2121
}
2222

2323
void LambdaCallable::on_destroy(jni::Env& p_env) const {
24-
if (!has_on_cancel || has_been_called) {
25-
return;
26-
}
24+
if (!has_on_cancel || has_been_called) { return; }
2725

2826
wrapped.call_void_method<false>(p_env, ON_CANCEL);
2927
}
@@ -33,23 +31,21 @@ int LambdaCallable::get_hash_code() const {
3331
}
3432

3533
bool LambdaCallable::equals(const LambdaCallable& other) const {
36-
jni::Env env{jni::Jvm::current_env()};
34+
jni::Env env {jni::Jvm::current_env()};
3735
return wrapped.is_same_object(env, other.wrapped);
3836
}
3937

40-
LambdaCallable::LambdaCallable(jni::Env& p_env, jni::JObject p_wrapped, godot::Variant::Type return_type, int p_hash_code,
41-
bool p_has_on_cancel) : JvmInstanceWrapper(p_env, p_wrapped) {
38+
LambdaCallable::LambdaCallable(jni::Env& p_env, jni::JObject p_wrapped, godot::Variant::Type return_type, int p_hash_code, bool p_has_on_cancel) :
39+
JvmInstanceWrapper(p_env, p_wrapped) {
4240
has_return_value = return_type != godot::Variant::NIL;
4341

4442
hash_code = p_hash_code;
4543
has_on_cancel = p_has_on_cancel;
4644
has_been_called = false;
4745
}
4846

49-
50-
void KotlinCallableCustom::call(const godot::Variant** p_arguments, int p_argcount, godot::Variant& r_return_value,
51-
Callable::CallError& r_call_error) const {
52-
jni::Env env{jni::Jvm::current_env()};
47+
void KotlinCallableCustom::call(const godot::Variant** p_arguments, int p_argcount, godot::Variant& r_return_value, Callable::CallError& r_call_error) const {
48+
jni::Env env {jni::Jvm::current_env()};
5349
kt_callable.invoke(env, p_arguments, p_argcount, r_return_value);
5450
}
5551

@@ -74,12 +70,10 @@ CallableCustom::CompareLessFunc KotlinCallableCustom::get_compare_less_func() co
7470
}
7571

7672
bool KotlinCallableCustom::compare_equal(const CallableCustom* p_a, const CallableCustom* p_b) {
77-
auto a{dynamic_cast<const KotlinCallableCustom*>(p_a)};
78-
auto b{dynamic_cast<const KotlinCallableCustom*>(p_b)};
73+
auto a {dynamic_cast<const KotlinCallableCustom*>(p_a)};
74+
auto b {dynamic_cast<const KotlinCallableCustom*>(p_b)};
7975

80-
if (!a || !b) {
81-
return false;
82-
}
76+
if (!a || !b) { return false; }
8377

8478
return a->kt_callable.equals(b->kt_callable);
8579
}
@@ -88,16 +82,11 @@ bool KotlinCallableCustom::compare_less(const CallableCustom* p_a, const Callabl
8882
return !compare_equal(p_a, p_b) && p_a < p_b;
8983
}
9084

91-
KotlinCallableCustom::KotlinCallableCustom(jni::Env& p_env, jni::JObject p_wrapped, godot::Variant::Type return_type,
92-
int p_hash_code, bool p_has_on_destroy) : CallableCustom(),
93-
kt_callable(p_env, p_wrapped,
94-
return_type,
95-
p_hash_code,
96-
p_has_on_destroy) {
97-
98-
}
85+
KotlinCallableCustom::KotlinCallableCustom(jni::Env& p_env, jni::JObject p_wrapped, godot::Variant::Type return_type, int p_hash_code, bool p_has_on_destroy) :
86+
CallableCustom(),
87+
kt_callable(p_env, p_wrapped, return_type, p_hash_code, p_has_on_destroy) {}
9988

10089
KotlinCallableCustom::~KotlinCallableCustom() {
101-
jni::Env env{jni::Jvm::current_env()};
90+
jni::Env env {jni::Jvm::current_env()};
10291
kt_callable.on_destroy(env);
10392
}

src/jvm/wrapper/kotlin_callable_custom.h

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#ifndef GODOT_JVM_KOTLIN_CALLABLE_CUSTOM_H
22
#define GODOT_JVM_KOTLIN_CALLABLE_CUSTOM_H
33

4-
#include "jvm/wrapper/jvm_instance_wrapper.h"
54
#include "core/variant/callable.h"
5+
#include "jvm/wrapper/jvm_instance_wrapper.h"
66

77
JVM_INSTANCE_WRAPPER(LambdaCallable, "godot.core.LambdaCallable") {
88
JVM_CLASS(LambdaCallable)
@@ -25,12 +25,12 @@ JVM_INSTANCE_WRAPPER(LambdaCallable, "godot.core.LambdaCallable") {
2525
// clang-format on
2626

2727
public:
28-
void invoke(jni::Env& p_env, const godot::Variant** p_args, int args_count, godot::Variant& r_ret) const;
29-
void on_destroy(jni::Env& p_env) const;
28+
void invoke(jni::Env & p_env, const godot::Variant** p_args, int args_count, godot::Variant& r_ret) const;
29+
void on_destroy(jni::Env & p_env) const;
3030
int get_hash_code() const;
3131
bool equals(const LambdaCallable& other) const;
3232

33-
LambdaCallable(jni::Env& p_env, jni::JObject p_wrapped, godot::Variant::Type return_type, int p_hash_code, bool p_has_on_cancel);
33+
LambdaCallable(jni::Env & p_env, jni::JObject p_wrapped, godot::Variant::Type return_type, int p_hash_code, bool p_has_on_cancel);
3434

3535
private:
3636
int hash_code;
@@ -59,5 +59,4 @@ class KotlinCallableCustom : public CallableCustom {
5959
static bool compare_less(const CallableCustom* p_a, const CallableCustom* p_b);
6060
};
6161

62-
63-
#endif //GODOT_JVM_KOTLIN_CALLABLE_CUSTOM_H
62+
#endif // GODOT_JVM_KOTLIN_CALLABLE_CUSTOM_H

src/jvm/wrapper/memory/long_string_queue.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
// If changed, remember to change also LongStringQueue::stringMaxSize on JVM side and the StringTest.kt
44
uint16_t LongStringQueue::max_string_size = 512;
55

6-
thread_local static godot::List<godot::String> string_queue;// NOLINT(cert-err58-cpp)
7-
6+
thread_local static godot::List<godot::String> string_queue; // NOLINT(cert-err58-cpp)
87

98
void LongStringQueue::set_string_max_size(jni::Env& p_env, int max_size) {
109
LongStringQueue::max_string_size = max_size;
@@ -28,7 +27,7 @@ void LongStringQueue::send_string_to_jvm(jni::Env& p_env, const godot::String& s
2827
wrapped.call_void_method(p_env, QUEUE_STRING, args);
2928
}
3029

31-
void LongStringQueue::send_string_to_cpp(JNIEnv* p_raw_env, jobject p_instance, jstring p_string) {
30+
void LongStringQueue::send_string_to_cpp(JNIEnv* p_raw_env, jobject, jstring p_string) {
3231
jni::Env env {p_raw_env};
3332
const godot::String nativeString = env.from_jstring(jni::JString {p_string});
3433
queue_string(nativeString);

src/jvm/wrapper/memory/long_string_queue.h

+4-5
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@ JVM_SINGLETON_WRAPPER(LongStringQueue, "godot.internal.memory.LongStringQueue")
1919
public:
2020
static uint16_t max_string_size;
2121

22-
void set_string_max_size(jni::Env& p_env, int max_size);
23-
void send_string_to_jvm(jni::Env& p_env, const godot::String& str);
22+
void set_string_max_size(jni::Env & p_env, int max_size);
23+
void send_string_to_jvm(jni::Env & p_env, const godot::String& str);
2424

2525
static godot::String poll_string();
2626
static void queue_string(const godot::String& str);
2727

28-
static void send_string_to_cpp(JNIEnv* p_raw_env, jobject p_instance, jstring p_string);
29-
28+
static void send_string_to_cpp(JNIEnv * p_raw_env, jobject p_instance, jstring p_string);
3029
};
3130

32-
#endif// GODOT_JVM_LONG_STRING_QUEUE_H
31+
#endif // GODOT_JVM_LONG_STRING_QUEUE_H

src/jvm/wrapper/memory/memory_manager.cpp

+13-15
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,23 @@ static godot::LocalVector<uint64_t> ids;
99
static godot::LocalVector<uintptr_t> pointers;
1010
static godot::LocalVector<uint32_t> variant_types;
1111

12-
bool MemoryManager::check_instance(JNIEnv* p_raw_env, jobject p_instance, jlong p_raw_ptr, jlong instance_id) {
12+
bool MemoryManager::check_instance(JNIEnv*, jobject, jlong p_raw_ptr, jlong instance_id) {
1313
auto* instance {reinterpret_cast<godot::Object*>(static_cast<uintptr_t>(p_raw_ptr))};
1414
return instance == godot::ObjectDB::get_instance(static_cast<godot::ObjectID>(static_cast<uint64_t>(instance_id)));
1515
}
1616

17-
void MemoryManager::release_binding(JNIEnv* p_raw_env, jobject p_instance, jlong instance_id) {
17+
void MemoryManager::release_binding(JNIEnv*, jobject, jlong instance_id) {
1818
godot::Object* obj = godot::ObjectDB::get_instance(static_cast<godot::ObjectID>(static_cast<uint64_t>(instance_id)));
1919
if (obj == nullptr) { return; }
2020

21-
JvmBindingManager::free_binding(obj);
21+
godot::JvmBindingManager::free_binding(obj);
2222
if (is_ref_counted(obj)) {
2323
godot::RefCounted* ref = reinterpret_cast<godot::RefCounted*>(obj);
2424
if (ref->unreference()) { memdelete(ref); }
2525
}
2626
}
2727

28-
void MemoryManager::unref_native_core_types(JNIEnv* p_raw_env, jobject p_instance, jobject p_ptr_array, jobject p_var_type_array) {
28+
void MemoryManager::unref_native_core_types(JNIEnv* p_raw_env, jobject, jobject p_ptr_array, jobject p_var_type_array) {
2929
jni::Env env {p_raw_env};
3030
jni::JLongArray ptr_array {p_ptr_array};
3131
jni::JIntArray var_type_array {p_var_type_array};
@@ -37,7 +37,7 @@ void MemoryManager::unref_native_core_types(JNIEnv* p_raw_env, jobject p_instanc
3737
ptr_array.get_array_elements(env, reinterpret_cast<jlong*>(pointers.ptr()), size);
3838
var_type_array.get_array_elements(env, reinterpret_cast<jint*>(variant_types.ptr()), size);
3939

40-
for(int i = 0; i < size; ++i){
40+
for (int i = 0; i < size; ++i) {
4141
uintptr_t p_raw_ptr = pointers[i];
4242
uint32_t var_type = variant_types[i];
4343

@@ -97,17 +97,15 @@ void MemoryManager::unref_native_core_types(JNIEnv* p_raw_env, jobject p_instanc
9797
variant_types.clear();
9898
}
9999

100-
void MemoryManager::query_sync(JNIEnv* p_raw_env, jobject p_instance) {
100+
void MemoryManager::query_sync(JNIEnv* p_raw_env, jobject) {
101101
jni::Env env {p_raw_env};
102102
MemoryManager::get_instance().sync_memory(env);
103103
}
104104

105-
106105
void MemoryManager::sync_memory(jni::Env& p_env) {
107-
108106
// Read the list of references to demote, we do it at the end of a frame instead of the constant ping-pong happening each call.
109107
to_demote_mutex.lock();
110-
for (JvmInstance* script_instance : to_demote_objects) {
108+
for (godot::JvmInstance* script_instance : to_demote_objects) {
111109
script_instance->demote_reference();
112110
}
113111
to_demote_objects.clear();
@@ -132,8 +130,9 @@ void MemoryManager::sync_memory(jni::Env& p_env) {
132130
refs_to_decrement.delete_local_ref(p_env);
133131

134132
for (uint64_t id : ids) {
135-
godot::RefCounted* ref = reinterpret_cast<godot::RefCounted*>(godot::ObjectDB::get_instance(static_cast<godot::ObjectID>(id)));
136-
JvmBindingManager::free_binding(ref);
133+
godot::RefCounted* ref =
134+
reinterpret_cast<godot::RefCounted*>(godot::ObjectDB::get_instance(static_cast<godot::ObjectID>(id)));
135+
godot::JvmBindingManager::free_binding(ref);
137136
if (ref->unreference()) { memdelete(ref); }
138137
}
139138

@@ -152,19 +151,19 @@ void MemoryManager::queue_dead_object(godot::Object* obj) {
152151
dead_objects_mutex.unlock();
153152
}
154153

155-
void MemoryManager::queue_demotion(JvmInstance* script_instance) {
154+
void MemoryManager::queue_demotion(godot::JvmInstance* script_instance) {
156155
to_demote_mutex.lock();
157156
to_demote_objects.insert(script_instance);
158157
to_demote_mutex.unlock();
159158
}
160159

161-
void MemoryManager::cancel_demotion(JvmInstance* script_instance) {
160+
void MemoryManager::cancel_demotion(godot::JvmInstance* script_instance) {
162161
to_demote_mutex.lock();
163162
to_demote_objects.erase(script_instance);
164163
to_demote_mutex.unlock();
165164
}
166165

167-
void MemoryManager::try_promotion(JvmInstance* script_instance) {
166+
void MemoryManager::try_promotion(godot::JvmInstance* script_instance) {
168167
to_demote_mutex.lock();
169168
script_instance->promote_reference();
170169
to_demote_mutex.unlock();
@@ -177,4 +176,3 @@ void MemoryManager::direct_object_deletion(jni::Env& p_env, godot::Object* p_obj
177176
}
178177

179178
MemoryManager::~MemoryManager() = default;
180-

src/jvm/wrapper/memory/memory_manager.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#include <jni.h>
1010

1111
#include <classes/mutex.hpp>
12-
#include <templates/local_vector.hpp>
1312
#include <templates/hash_set.hpp>
13+
#include <templates/local_vector.hpp>
1414

1515
// clang-format off
1616
JVM_SINGLETON_WRAPPER(MemoryManager, "godot.internal.memory.MemoryManager") {
@@ -34,7 +34,7 @@ JVM_SINGLETON_WRAPPER(MemoryManager, "godot.internal.memory.MemoryManager") {
3434
godot::LocalVector<godot::ObjectID> dead_objects;
3535

3636
godot::Mutex to_demote_mutex;
37-
godot::HashSet<JvmInstance*> to_demote_objects;
37+
godot::HashSet<godot::JvmInstance*> to_demote_objects;
3838

3939
static bool check_instance(JNIEnv* p_raw_env, jobject p_instance, jlong p_raw_ptr, jlong instance_id);
4040
static void unref_native_core_types(JNIEnv* p_raw_env, jobject p_instance, jobject p_ptr_array, jobject p_var_type_array);
@@ -44,9 +44,9 @@ JVM_SINGLETON_WRAPPER(MemoryManager, "godot.internal.memory.MemoryManager") {
4444
public:
4545
void direct_object_deletion(jni::Env& p_env, godot::Object* obj);
4646
void queue_dead_object(godot::Object* obj);
47-
void queue_demotion(JvmInstance* script_instance);
48-
void cancel_demotion(JvmInstance* script_instance);
49-
void try_promotion(JvmInstance* script_instance);
47+
void queue_demotion(godot::JvmInstance* script_instance);
48+
void cancel_demotion(godot::JvmInstance* script_instance);
49+
void try_promotion(godot::JvmInstance* script_instance);
5050
void sync_memory(jni::Env& p_env);
5151
void clean_up(jni::Env& p_env);
5252
};

0 commit comments

Comments
 (0)