Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/platform/android/variant/callable_jni.cpp
10278 views
1
/**************************************************************************/
2
/* callable_jni.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "callable_jni.h"
32
33
#include "jni_utils.h"
34
35
#include "core/error/error_macros.h"
36
#include "core/object/object.h"
37
38
static Callable _generate_callable(JNIEnv *p_env, jlong p_object_id, jstring p_method_name, jobjectArray p_parameters) {
39
Object *obj = ObjectDB::get_instance(ObjectID(p_object_id));
40
ERR_FAIL_NULL_V(obj, Callable());
41
42
String str_method = jstring_to_string(p_method_name, p_env);
43
44
int count = p_env->GetArrayLength(p_parameters);
45
46
Variant *args = (Variant *)alloca(sizeof(Variant) * count);
47
const Variant **argptrs = (const Variant **)alloca(sizeof(Variant *) * count);
48
49
for (int i = 0; i < count; i++) {
50
jobject jobj = p_env->GetObjectArrayElement(p_parameters, i);
51
ERR_FAIL_NULL_V(jobj, Callable());
52
memnew_placement(&args[i], Variant(_jobject_to_variant(p_env, jobj)));
53
argptrs[i] = &args[i];
54
p_env->DeleteLocalRef(jobj);
55
}
56
57
Callable ret = Callable(obj, str_method).bindp(argptrs, count);
58
59
// Manually invoke the destructor to decrease the reference counts for the variant arguments.
60
for (int i = 0; i < count; i++) {
61
args[i].~Variant();
62
}
63
64
return ret;
65
}
66
67
extern "C" {
68
JNIEXPORT jobject JNICALL Java_org_godotengine_godot_variant_Callable_nativeCall(JNIEnv *p_env, jclass p_clazz, jlong p_native_callable, jobjectArray p_parameters) {
69
const Variant *callable_variant = reinterpret_cast<const Variant *>(p_native_callable);
70
ERR_FAIL_NULL_V(callable_variant, nullptr);
71
if (callable_variant->get_type() != Variant::CALLABLE) {
72
return nullptr;
73
}
74
75
int count = p_env->GetArrayLength(p_parameters);
76
77
Variant *args = (Variant *)alloca(sizeof(Variant) * count);
78
const Variant **argptrs = (const Variant **)alloca(sizeof(Variant *) * count);
79
80
for (int i = 0; i < count; i++) {
81
jobject jobj = p_env->GetObjectArrayElement(p_parameters, i);
82
ERR_FAIL_NULL_V(jobj, nullptr);
83
memnew_placement(&args[i], Variant(_jobject_to_variant(p_env, jobj)));
84
argptrs[i] = &args[i];
85
p_env->DeleteLocalRef(jobj);
86
}
87
88
Callable callable = *callable_variant;
89
jobject ret = nullptr;
90
if (callable.is_valid()) {
91
Callable::CallError err;
92
Variant result;
93
callable.callp(argptrs, count, result, err);
94
jvalret jresult = _variant_to_jvalue(p_env, result.get_type(), &result, true);
95
ret = jresult.obj;
96
}
97
98
// Manually invoke the destructor to decrease the reference counts for the variant arguments.
99
for (int i = 0; i < count; i++) {
100
args[i].~Variant();
101
}
102
103
return ret;
104
}
105
106
JNIEXPORT jobject JNICALL Java_org_godotengine_godot_variant_Callable_nativeCallObject(JNIEnv *p_env, jclass p_clazz, jlong p_object_id, jstring p_method_name, jobjectArray p_parameters) {
107
Callable callable = _generate_callable(p_env, p_object_id, p_method_name, p_parameters);
108
if (callable.is_valid()) {
109
Variant result = callable.call();
110
jvalret jresult = _variant_to_jvalue(p_env, result.get_type(), &result, true);
111
return jresult.obj;
112
} else {
113
return nullptr;
114
}
115
}
116
117
JNIEXPORT void JNICALL Java_org_godotengine_godot_variant_Callable_nativeCallObjectDeferred(JNIEnv *p_env, jclass p_clazz, jlong p_object_id, jstring p_method_name, jobjectArray p_parameters) {
118
Callable callable = _generate_callable(p_env, p_object_id, p_method_name, p_parameters);
119
if (callable.is_valid()) {
120
callable.call_deferred();
121
}
122
}
123
124
JNIEXPORT void JNICALL
125
Java_org_godotengine_godot_variant_Callable_releaseNativePointer(JNIEnv *p_env, jclass clazz, jlong p_native_pointer) {
126
Variant *variant = reinterpret_cast<Variant *>(p_native_pointer);
127
ERR_FAIL_NULL(variant);
128
memdelete(variant);
129
}
130
}
131
132