Skip to content

Commit

Permalink
fix(cpp): simplify INIT_JNI_BINDINGS macros
Browse files Browse the repository at this point in the history
  • Loading branch information
piiertho committed Aug 21, 2024
1 parent 7df1928 commit 809c286
Show file tree
Hide file tree
Showing 26 changed files with 27 additions and 35 deletions.
1 change: 1 addition & 0 deletions src/jvm_wrapper/bootstrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "jvm_wrapper/jvm_instance_wrapper.h"

JVM_INSTANCE_WRAPPER(Bootstrap, "godot.runtime.Bootstrap") {
JVM_CLASS(Bootstrap)

// clang-format off
JNI_METHOD(INIT)
Expand Down
4 changes: 2 additions & 2 deletions src/jvm_wrapper/bridge/packed_array_bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#define PACKED_ARRAY_BRIDGE_CLASS(NAME, ELEMENT_TYPE) \
friend class PackedArrayBridge<NAME, ELEMENT_TYPE, NAME##QualifiedName>; \
friend class JvmSingletonWrapper<NAME, NAME##QualifiedName>; \
static inline constexpr const char* fq_name = NAME##QualifiedName; \
JVM_CLASS(NAME) \
\
public: \
NAME(const NAME&) = delete; \
Expand Down Expand Up @@ -43,7 +43,7 @@ namespace bridges {
~PackedArrayBridge() = default;

// clang-format off
INIT_JNI_BINDINGS_TEMPLATE(
INIT_JNI_BINDINGS(
INIT_NATIVE_METHOD("engine_call_constructor", "()J", (PackedArrayBridge<Derived, T, fq_name>::engine_call_constructor))
INIT_NATIVE_METHOD("engine_call_constructor_packed_array", "()J", (PackedArrayBridge<Derived, T, fq_name>::engine_call_constructor_packed_array))
INIT_NATIVE_METHOD("engine_call_constructor_array", "()J", (PackedArrayBridge<Derived, T, fq_name>::engine_call_constructor_array))
Expand Down
2 changes: 1 addition & 1 deletion src/jvm_wrapper/bridge/packed_byte_array_bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace bridges {
PACKED_ARRAY_BRIDGE_CLASS(PackedByteArrayBridge, uint8_t)

// clang-format off
INIT_JNI_BINDINGS_TEMPLATE(
INIT_JNI_BINDINGS(
PackedArrayBridge<PackedByteArrayBridge, uint8_t, PackedByteArrayBridgeQualifiedName>::initialize_jni_binding(p_env, class_loader);

INIT_NATIVE_METHOD("engine_call_compress", "(J)V", PackedByteArrayBridge::engine_call_compress)
Expand Down
Empty file.
2 changes: 1 addition & 1 deletion src/jvm_wrapper/bridge/packed_float_32_array_bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace bridges {
PACKED_ARRAY_BRIDGE_CLASS(PackedFloat32ArrayBridge, float)

// clang-format off
INIT_JNI_BINDINGS_TEMPLATE(
INIT_JNI_BINDINGS(
PackedArrayBridge<PackedFloat32ArrayBridge, float, PackedFloat32ArrayBridgeQualifiedName>::initialize_jni_binding(p_env, class_loader);
INIT_NATIVE_METHOD("engine_convert_to_godot", "([F)J", PackedFloat32ArrayBridge::engine_convert_to_godot)
INIT_NATIVE_METHOD("engine_convert_to_jvm", "(J)[F", PackedFloat32ArrayBridge::engine_convert_to_jvm)
Expand Down
Empty file.
2 changes: 1 addition & 1 deletion src/jvm_wrapper/bridge/packed_float_64_array_bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace bridges {
PACKED_ARRAY_BRIDGE_CLASS(PackedFloat64ArrayBridge, double)

// clang-format off
INIT_JNI_BINDINGS_TEMPLATE(
INIT_JNI_BINDINGS(
PackedArrayBridge<PackedFloat64ArrayBridge, double, PackedFloat64ArrayBridgeQualifiedName>::initialize_jni_binding(p_env, class_loader);
INIT_NATIVE_METHOD("engine_convert_to_godot", "([D)J", PackedFloat64ArrayBridge::engine_convert_to_godot)
INIT_NATIVE_METHOD("engine_convert_to_jvm", "(J)[D", PackedFloat64ArrayBridge::engine_convert_to_jvm)
Expand Down
Empty file.
2 changes: 1 addition & 1 deletion src/jvm_wrapper/bridge/packed_int_32_array_bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace bridges {
PACKED_ARRAY_BRIDGE_CLASS(PackedInt32ArrayBridge, int)

// clang-format off
INIT_JNI_BINDINGS_TEMPLATE(
INIT_JNI_BINDINGS(
PackedArrayBridge<PackedInt32ArrayBridge, int, PackedInt32ArrayBridgeQualifiedName>::initialize_jni_binding(p_env, class_loader);

INIT_NATIVE_METHOD("engine_convert_to_godot", "([I)J", PackedInt32ArrayBridge::engine_convert_to_godot)
Expand Down
Empty file.
2 changes: 1 addition & 1 deletion src/jvm_wrapper/bridge/packed_int_64_array_bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace bridges {
PACKED_ARRAY_BRIDGE_CLASS(PackedInt64ArrayBridge, int64_t)

// clang-format off
INIT_JNI_BINDINGS_TEMPLATE(
INIT_JNI_BINDINGS(
PackedArrayBridge<PackedInt64ArrayBridge, int64_t, PackedInt64ArrayBridgeQualifiedName>::initialize_jni_binding(p_env, class_loader);

INIT_NATIVE_METHOD("engine_convert_to_godot", "([J)J", PackedInt64ArrayBridge::engine_convert_to_godot)
Expand Down
Empty file.
Empty file.
34 changes: 6 additions & 28 deletions src/jvm_wrapper/jvm_instance_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
inline constexpr char NAME##QualifiedName[] = FQNAME; \
class NAME : public JvmInstanceWrapper<NAME, NAME##QualifiedName>

#define JVM_CLASS(NAME) \
friend class JvmInstanceWrapper<NAME, NAME##QualifiedName>; \
static inline constexpr const char* fq_name = NAME##QualifiedName;

#define JNI_METHOD(var_name) inline static jni::MethodId var_name {nullptr};

#define INIT_JNI_METHOD(var_name, name, signature) var_name = clazz.get_method_id(p_env, name, signature);
Expand All @@ -22,9 +26,9 @@ public:
Vector<jni::JNativeMethod> methods; \
jni::JClass clazz; \
if (class_loader) { \
clazz = class_loader->load_class(p_env, get_fully_qualified_name()); \
clazz = class_loader->load_class(p_env, fq_name); \
} else { \
clazz = p_env.find_class(get_fully_qualified_name()); \
clazz = p_env.find_class(fq_name); \
} \
\
__VA_ARGS__ \
Expand All @@ -34,25 +38,6 @@ public:
\
private:

#define INIT_JNI_BINDINGS_TEMPLATE(...) \
\
public: \
static void initialize_jni_binding(jni::Env& p_env, ClassLoader* class_loader) { \
Vector<jni::JNativeMethod> methods; \
jni::JClass clazz; \
if (class_loader) { \
clazz = class_loader->load_class(p_env, fq_name); \
} else { \
clazz = p_env.find_class(fq_name); \
} \
\
__VA_ARGS__ \
if (methods.size() > 0) { clazz.register_natives(p_env, methods); } \
clazz.delete_local_ref(p_env); \
} \
\
private:

/**
* This class wraps a JObject representing a JVM instance.
* This class is a base that allows to setup JavaToNative and NativeToJava call easily.
Expand All @@ -79,8 +64,6 @@ class JvmInstanceWrapper {

void swap_to_weak_unsafe(jni::Env& p_env);

static constexpr const char* get_fully_qualified_name();

static Derived* create_instance(jni::Env& p_env, ClassLoader* class_loader);
};

Expand Down Expand Up @@ -137,11 +120,6 @@ void JvmInstanceWrapper<Derived, FqName>::swap_to_weak_unsafe(jni::Env& p_env) {
is_weak = true;
}

template<class Derived, const char* FqName>
constexpr const char* JvmInstanceWrapper<Derived, FqName>::get_fully_qualified_name() {
return FqName;
}

template<class Derived, const char* FqName>
const jni::JObject& JvmInstanceWrapper<Derived, FqName>::get_wrapped() const {
return wrapped;
Expand Down
1 change: 1 addition & 0 deletions src/jvm_wrapper/jvm_singleton_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#define SINGLETON_CLASS(NAME) \
friend class JvmSingletonWrapper<NAME, NAME##QualifiedName>; \
JVM_CLASS(NAME) \
\
public: \
NAME(const NAME&) = delete; \
Expand Down
1 change: 1 addition & 0 deletions src/jvm_wrapper/memory/kt_binding.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "jvm_wrapper/jvm_instance_wrapper.h"

JVM_INSTANCE_WRAPPER(KtBinding, "godot.core.memory.GodotBinding") {
JVM_CLASS(KtBinding)
public:
explicit KtBinding(jni::Env& p_env, jni::JObject p_wrapped);
~KtBinding();
Expand Down
Empty file.
Empty file.
1 change: 1 addition & 0 deletions src/jvm_wrapper/registration/kt_class.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
const int MAX_CONSTRUCTOR_SIZE = MAX_CONSTRUCTOR_ARG_COUNT + 1;

JVM_INSTANCE_WRAPPER(KtClass, "godot.core.KtClass") {
JVM_CLASS(KtClass)

// clang-format off
JNI_METHOD(GET_REGISTERED_NAME)
Expand Down
2 changes: 2 additions & 0 deletions src/jvm_wrapper/registration/kt_constructor.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "kt_object.h"

JVM_INSTANCE_WRAPPER(KtConstructor, "godot.core.KtConstructor") {
JVM_CLASS(KtConstructor)

// clang-format off
JNI_METHOD(GET_PARAMETER_COUNT)
JNI_METHOD(CONSTRUCT)
Expand Down
2 changes: 2 additions & 0 deletions src/jvm_wrapper/registration/kt_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "kt_rpc_config.h"

JVM_INSTANCE_WRAPPER(KtFunctionInfo, "godot.core.KtFunctionInfo") {
JVM_CLASS(KtFunctionInfo)

// clang-format off
JNI_METHOD(GET_NAME)
Expand Down Expand Up @@ -35,6 +36,7 @@ JVM_INSTANCE_WRAPPER(KtFunctionInfo, "godot.core.KtFunctionInfo") {
};

JVM_INSTANCE_WRAPPER(KtFunction, "godot.core.KtFunction") {
JVM_CLASS(KtFunction)

// clang-format off
JNI_METHOD(GET_FUNCTION_INFO)
Expand Down
1 change: 1 addition & 0 deletions src/jvm_wrapper/registration/kt_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "jvm_wrapper/jvm_instance_wrapper.h"

JVM_INSTANCE_WRAPPER(KtObject, "godot.core.KtObject") {
JVM_CLASS(KtObject)

// clang-format off
JNI_METHOD(ON_DESTROY)
Expand Down
2 changes: 2 additions & 0 deletions src/jvm_wrapper/registration/kt_property.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "kt_object.h"

JVM_INSTANCE_WRAPPER(KtPropertyInfo, "godot.core.KtPropertyInfo") {
JVM_CLASS(KtPropertyInfo)

// clang-format off
JNI_METHOD(GET_TYPE)
Expand Down Expand Up @@ -43,6 +44,7 @@ JVM_INSTANCE_WRAPPER(KtPropertyInfo, "godot.core.KtPropertyInfo") {
};

JVM_INSTANCE_WRAPPER(KtProperty, "godot.core.KtProperty") {
JVM_CLASS(KtProperty)

// clang-format off
JNI_METHOD(GET_KT_PROPERTY_INFO)
Expand Down
1 change: 1 addition & 0 deletions src/jvm_wrapper/registration/kt_rpc_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "scene/main/multiplayer_api.h"

JVM_INSTANCE_WRAPPER(KtRpcConfig, "godot.core.KtRpcConfig") {
JVM_CLASS(KtRpcConfig)

// clang-format off
JNI_METHOD(GET_RPC_MODE_ID)
Expand Down
1 change: 1 addition & 0 deletions src/jvm_wrapper/registration/kt_signal_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "kt_property.h"

JVM_INSTANCE_WRAPPER(KtSignalInfo, "godot.core.KtSignalInfo") {
JVM_CLASS(KtSignalInfo)

// clang-format off
JNI_METHOD(GET_NAME)
Expand Down
1 change: 1 addition & 0 deletions src/kotlin_callable_custom.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "core/variant/callable.h"

JVM_INSTANCE_WRAPPER(KtCallable, "godot.core.callable.KtCallable") {
JVM_CLASS(KtCallable)
// clang-format off

JNI_METHOD(INVOKE_NO_RETURN)
Expand Down

0 comments on commit 809c286

Please sign in to comment.