mirror of
https://github.com/ton-blockchain/ton
synced 2025-03-09 15:40:10 +00:00
Update jni native-lib ganeration (#497)
* Add python-like triple quotes for multiline strings * Add test for multiline asm * Allow asm definition duplicate * Asm duplicate: add test & fixes * Fix multiline asm * Fix asm duplicate * generate_java: Add support for # and Int256 * generate_java: Add # and Int256 to native-lib * Fix 'jni.h: no such file' * Fix TonApi.java * Fix android build.sh * Fix android OPENSSL_ROOT_DIR
This commit is contained in:
parent
906336f881
commit
0ddf2a7f9f
7 changed files with 206 additions and 45 deletions
|
|
@ -30,12 +30,6 @@ namespace jni {
|
|||
|
||||
thread_local bool parse_error;
|
||||
|
||||
static jclass BooleanClass;
|
||||
static jclass IntegerClass;
|
||||
static jclass LongClass;
|
||||
static jclass DoubleClass;
|
||||
static jclass StringClass;
|
||||
static jclass ObjectClass;
|
||||
jmethodID GetConstructorID;
|
||||
jmethodID BooleanGetValueMethodID;
|
||||
jmethodID IntegerGetValueMethodID;
|
||||
|
|
|
|||
|
|
@ -27,12 +27,19 @@
|
|||
|
||||
#include "td/utils/Slice.h"
|
||||
#include "td/utils/SharedSlice.h"
|
||||
#include "common/bitstring.h"
|
||||
|
||||
namespace td {
|
||||
namespace jni {
|
||||
|
||||
extern thread_local bool parse_error;
|
||||
|
||||
static jclass BooleanClass;
|
||||
static jclass IntegerClass;
|
||||
static jclass LongClass;
|
||||
static jclass DoubleClass;
|
||||
static jclass StringClass;
|
||||
static jclass ObjectClass;
|
||||
extern jmethodID GetConstructorID;
|
||||
extern jmethodID BooleanGetValueMethodID;
|
||||
extern jmethodID IntegerGetValueMethodID;
|
||||
|
|
@ -106,6 +113,29 @@ SecureString from_bytes_secure(JNIEnv *env, jbyteArray arr);
|
|||
jbyteArray to_bytes(JNIEnv *env, Slice b);
|
||||
jbyteArray to_bytes_secure(JNIEnv *env, Slice b);
|
||||
|
||||
template<unsigned int n>
|
||||
td::BitArray<n> from_bits(JNIEnv *env, jbyteArray arr) {
|
||||
td::BitArray<n> b;
|
||||
if (arr != nullptr) {
|
||||
jsize length = env->GetArrayLength(arr);
|
||||
assert(length * 8 == n);
|
||||
env->GetByteArrayRegion(arr, 0, length, reinterpret_cast<jbyte *>(b.as_slice().begin()));
|
||||
env->DeleteLocalRef(arr);
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
template<unsigned int n>
|
||||
jbyteArray to_bits(JNIEnv *env, td::BitArray<n> b) {
|
||||
assert(n % 8 == 0);
|
||||
jsize length = n / 8;
|
||||
jbyteArray arr = env->NewByteArray(length);
|
||||
if (arr != nullptr) {
|
||||
env->SetByteArrayRegion(arr, 0, length, reinterpret_cast<const jbyte *>(b.data()));
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
void init_vars(JNIEnv *env, const char *td_api_java_package);
|
||||
|
||||
jintArray store_vector(JNIEnv *env, const std::vector<std::int32_t> &v);
|
||||
|
|
@ -118,6 +148,22 @@ jobjectArray store_vector(JNIEnv *env, const std::vector<std::string> &v);
|
|||
|
||||
jobjectArray store_vector(JNIEnv *env, const std::vector<SecureString> &v);
|
||||
|
||||
template<unsigned int n>
|
||||
jobjectArray store_vector(JNIEnv *env, const std::vector<td::BitArray<n>> &v) {
|
||||
jint length = static_cast<jint>(v.size());
|
||||
jobjectArray arr = env->NewObjectArray(length, ObjectClass, jobject());
|
||||
if (arr != nullptr) {
|
||||
for (jsize i = 0; i < length; i++) {
|
||||
jbyteArray bits = to_bits<n>(env, v[i]);
|
||||
if (bits) {
|
||||
env->SetObjectArrayElement(arr, i, bits);
|
||||
env->DeleteLocalRef(bits);
|
||||
}
|
||||
}
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
jobjectArray store_vector(JNIEnv *env, const std::vector<T> &v) {
|
||||
jint length = static_cast<jint>(v.size());
|
||||
|
|
@ -230,6 +276,26 @@ struct FetchVector<SecureString> {
|
|||
}
|
||||
};
|
||||
|
||||
template<unsigned int n>
|
||||
struct FetchVector<td::BitArray<n>> {
|
||||
static std::vector<td::BitArray<n>> fetch(JNIEnv *env, jobjectArray arr) {
|
||||
std::vector<td::BitArray<n>> result;
|
||||
if (arr != nullptr) {
|
||||
jsize length = env->GetArrayLength(arr);
|
||||
result.reserve(length);
|
||||
for (jsize i = 0; i < length; i++) {
|
||||
jbyteArray bits = (jbyteArray)env->GetObjectArrayElement(arr, i);
|
||||
result.push_back(jni::from_bits<n>(env, bits));
|
||||
if (bits) {
|
||||
env->DeleteLocalRef(bits);
|
||||
}
|
||||
}
|
||||
env->DeleteLocalRef(arr);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct FetchVector<std::vector<T>> {
|
||||
static auto fetch(JNIEnv *env, jobjectArray arr) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue