Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/platform/android/variant/callable_jni.cpp
20784 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
ret = _variant_to_jobject(p_env, result.get_type(), &result);
95
}
96
97
// Manually invoke the destructor to decrease the reference counts for the variant arguments.
98
for (int i = 0; i < count; i++) {
99
args[i].~Variant();
100
}
101
102
return ret;
103
}
104
105
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) {
106
Callable callable = _generate_callable(p_env, p_object_id, p_method_name, p_parameters);
107
if (callable.is_valid()) {
108
Variant result = callable.call();
109
return _variant_to_jobject(p_env, result.get_type(), &result);
110
} else {
111
return nullptr;
112
}
113
}
114
115
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) {
116
Callable callable = _generate_callable(p_env, p_object_id, p_method_name, p_parameters);
117
if (callable.is_valid()) {
118
callable.call_deferred();
119
}
120
}
121
122
JNIEXPORT void JNICALL
123
Java_org_godotengine_godot_variant_Callable_releaseNativePointer(JNIEnv *p_env, jclass clazz, jlong p_native_pointer) {
124
Variant *variant = reinterpret_cast<Variant *>(p_native_pointer);
125
ERR_FAIL_NULL(variant);
126
memdelete(variant);
127
}
128
}
129
130