Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1a96b61

Browse files
author
Igor Tarasov
authoredDec 10, 2019
Add GetAttachedEnv function. (#50)
1 parent 4757af5 commit 1a96b61

File tree

3 files changed

+57
-17
lines changed

3 files changed

+57
-17
lines changed
 

‎Makefile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@ VARIANT ?= android
22
BUILD := build/$(VARIANT)
33

44
COMPILER := $(shell CXX="${CXX}" misc/compiler.sh)
5+
COMPILER_MAJOR_VERSION := $(shell CXX="${CXX}" misc/compiler-major-version.sh)
6+
57
ifeq ($(COMPILER), clang)
6-
CXXFLAGS_WARNINGS := -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-exit-time-destructors
8+
CXXFLAGS_WARNINGS := -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-exit-time-destructors
9+
ifeq ($(shell test $(COMPILER_MAJOR_VERSION) -gt 4; echo $$?),0)
10+
CXXFLAGS_WARNINGS += -Wno-unused-template
11+
endif
712
else ifeq ($(COMPILER), gcc)
8-
CXXFLAGS_WARNINGS := -Wall -Wextra -pedantic -Wno-unused-but-set-variable
13+
CXXFLAGS_WARNINGS := -Wall -Wextra -pedantic -Wno-unused-but-set-variable
914
endif
1015

1116
CXXFLAGS := $(CXXFLAGS) --std=c++14 -fPIC -Iinclude $(CXXFLAGS_WARNINGS) -Werror

‎include/jni/functions.hpp

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -628,23 +628,28 @@ namespace jni
628628
}
629629

630630

631+
namespace {
632+
// Some implementations type the parameter as JNIEnv**, others as void**.
633+
// See https://bugs.openjdk.java.net/browse/JDK-6569899
634+
struct JNIEnvCast
635+
{
636+
using FunVoid = jint (JavaVM::*)(void**, void*);
637+
using FunEnv = jint (JavaVM::*)(JNIEnv**, void*);
638+
639+
template <typename Fun, typename = std::enable_if_t<std::is_same<Fun, FunVoid>::value>>
640+
void** operator()(JNIEnv** env, Fun) noexcept {
641+
return reinterpret_cast<void**>(env);
642+
}
643+
644+
template <typename Fun, typename = std::enable_if_t<std::is_same<Fun, FunEnv>::value>>
645+
JNIEnv** operator()(JNIEnv** env, Fun) noexcept {
646+
return env;
647+
}
648+
};
649+
}
650+
631651
inline UniqueEnv AttachCurrentThread(JavaVM& vm)
632652
{
633-
// Some implementations type the parameter as JNIEnv**, others as void**.
634-
// See https://bugs.openjdk.java.net/browse/JDK-6569899
635-
struct JNIEnvCast
636-
{
637-
void** operator()(JNIEnv** env, jint (JavaVM::*)(void**, void*))
638-
{
639-
return reinterpret_cast<void**>(env);
640-
}
641-
642-
JNIEnv** operator()(JNIEnv** env, jint (JavaVM::*)(JNIEnv**, void*))
643-
{
644-
return env;
645-
}
646-
};
647-
648653
JNIEnv* result;
649654
CheckErrorCode(vm.AttachCurrentThread(JNIEnvCast()(&result, &JavaVM::AttachCurrentThread), nullptr));
650655
return UniqueEnv(result, JNIEnvDeleter(vm));
@@ -662,4 +667,18 @@ namespace jni
662667
CheckErrorCode(vm.GetEnv(reinterpret_cast<void**>(&env), Unwrap(version)));
663668
return *env;
664669
}
670+
671+
inline JNIEnv& GetAttachedEnv(JavaVM& vm, version version = jni_version_1_1)
672+
{
673+
JNIEnv* env = nullptr;
674+
auto code = vm.GetEnv(reinterpret_cast<void**>(&env), Unwrap(version));
675+
676+
if (code == JNI_EDETACHED) {
677+
CheckErrorCode(vm.AttachCurrentThread(JNIEnvCast()(&env, &JavaVM::AttachCurrentThread), nullptr));
678+
} else {
679+
CheckErrorCode(code);
680+
}
681+
682+
return *env;
683+
}
665684
}

‎misc/compiler-major-version.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
compiler=$(${CXX} -v 2>&1)
6+
7+
case $compiler in
8+
*clang*|*gcc*)
9+
version=`${CXX} --version | grep -o '\([0-9]\)\+.\([0-9]\)\+.\([0-9]\)\+'`
10+
echo ${version::1}
11+
;;
12+
*)
13+
echo
14+
unknown
15+
;;
16+
esac

0 commit comments

Comments
 (0)
Please sign in to comment.