Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
BasedOnStyle: Google
IndentWidth: 2
ColumnLimit: 80
AllowShortFunctionsOnASingleLine: Empty
DerivePointerAlignment: false
PointerAlignment: Left
BreakBeforeBraces: Attach
AllowShortIfStatementsOnASingleLine: false
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
Cpp11BracedListStyle: true
Standard: Latest
8 changes: 6 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
bazel*
.vscode
.vscode/
.clwb/
.ijwb/
.idea/
.vscode/
MODULE.bazel.lock
cmake_build
cmake_install
cmake_install
11 changes: 11 additions & 0 deletions implementation/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ cc_library(
name = "field_ref",
hdrs = ["field_ref.h"],
deps = [
":proxy_temporary",
":default_class_loader",
":field_selection",
":id",
Expand Down Expand Up @@ -788,6 +789,7 @@ cc_library(
name = "method_ref",
hdrs = ["overload_ref.h"],
deps = [
":proxy_temporary",
":configuration",
":id_type",
":promotion_mechanics_tags",
Expand Down Expand Up @@ -1052,6 +1054,7 @@ cc_library(
name = "proxy_definitions_string",
hdrs = ["proxy_definitions_string.h"],
deps = [
":proxy_temporary",
":default_class_loader",
":forward_declarations",
":jvm",
Expand All @@ -1076,6 +1079,14 @@ cc_test(
],
)

################################################################################
# ProxyTemporary.
################################################################################
cc_library(
name = "proxy_temporary",
hdrs = ["proxy_temporary.h"],
)

################################################################################
# RefBase.
################################################################################
Expand Down
9 changes: 7 additions & 2 deletions implementation/field_ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
#include <cstddef>
#include <type_traits>
#include <utility>
#include <utility>
#include <vector>

#include "implementation/proxy_temporary.h"
#include "implementation/default_class_loader.h"
#include "implementation/field_selection.h"
#include "implementation/id.h"
Expand Down Expand Up @@ -113,10 +115,13 @@ class FieldRef {

template <typename T>
void Set(T&& value) {
fprintf(stderr, "pre field set\n");
FieldHelper<CDecl_t<typename IdT::RawValT>, IdT::kRank,
IdT::kIsStatic>::SetValue(SelfVal(), GetFieldID(class_ref_),
Proxy_t<T>::ProxyAsArg(
std::forward<T>(value)));
ForwardWithRefStrip(
Proxy_t<T>::ProxyAsArg(
std::forward<T>(value))));
fprintf(stderr, "post field set\n");
}

private:
Expand Down
20 changes: 10 additions & 10 deletions implementation/jni_helper/field_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ struct FieldHelper<jboolean, 0, false, void> {
}

static inline void SetValue(const jobject object_ref,
const jfieldID field_ref_, jboolean&& value) {
const jfieldID field_ref_, jboolean value) {
Trace(metaprogramming::LambdaToStr(STR("GetBooleanValue")), object_ref,
field_ref_);

Expand All @@ -79,7 +79,7 @@ struct FieldHelper<jbyte, 0, false, void> {
}

static inline void SetValue(const jobject object_ref,
const jfieldID field_ref_, jbyte&& value) {
const jfieldID field_ref_, jbyte value) {
Trace(metaprogramming::LambdaToStr(STR("SetByteValue")), object_ref,
field_ref_);

Expand All @@ -105,7 +105,7 @@ struct FieldHelper<jchar, 0, false, void> {
}

static inline void SetValue(const jobject object_ref,
const jfieldID field_ref_, jchar&& value) {
const jfieldID field_ref_, jchar value) {
Trace(metaprogramming::LambdaToStr(STR("SetCharValue")), object_ref,
field_ref_);

Expand All @@ -131,7 +131,7 @@ struct FieldHelper<jshort, 0, false, void> {
}

static inline void SetValue(const jobject object_ref,
const jfieldID field_ref_, jshort&& value) {
const jfieldID field_ref_, jshort value) {
Trace(metaprogramming::LambdaToStr(STR("SetShortValue")), object_ref,
field_ref_);

Expand All @@ -157,7 +157,7 @@ struct FieldHelper<jint, 0, false, void> {
}

static inline void SetValue(const jobject object_ref,
const jfieldID field_ref_, jint&& value) {
const jfieldID field_ref_, jint value) {
Trace(metaprogramming::LambdaToStr(STR("SetIntValue")), object_ref,
field_ref_);

Expand All @@ -183,7 +183,7 @@ struct FieldHelper<jlong, 0, false, void> {
}

static inline void SetValue(const jobject object_ref,
const jfieldID field_ref_, jlong&& value) {
const jfieldID field_ref_, jlong value) {
Trace(metaprogramming::LambdaToStr(STR("SetLongField")), object_ref,
field_ref_);

Expand All @@ -209,7 +209,7 @@ struct FieldHelper<jfloat, 0, false, void> {
}

static inline void SetValue(const jobject object_ref,
const jfieldID field_ref_, jfloat&& value) {
const jfieldID field_ref_, jfloat value) {
Trace(metaprogramming::LambdaToStr(STR("SetFloatField")), object_ref,
field_ref_);

Expand All @@ -235,7 +235,7 @@ struct FieldHelper<jdouble, 0, false, void> {
}

static inline void SetValue(const jobject object_ref,
const jfieldID field_ref_, jdouble&& value) {
const jfieldID field_ref_, jdouble value) {
Trace(metaprogramming::LambdaToStr(STR("SetDoubleField")), object_ref,
field_ref_);

Expand All @@ -261,7 +261,7 @@ struct FieldHelper<jobject, 0, false, void> {
}

static inline void SetValue(const jobject object_ref,
const jfieldID field_ref_, jobject&& new_value) {
const jfieldID field_ref_, jobject new_value) {
Trace(metaprogramming::LambdaToStr(STR("SetObjectField")), object_ref,
field_ref_);

Expand All @@ -288,7 +288,7 @@ struct FieldHelper<jstring, 0, false, void> {
}

static inline void SetValue(const jobject object_ref,
const jfieldID field_ref_, jstring&& new_value) {
const jfieldID field_ref_, jstring new_value) {
Trace(metaprogramming::LambdaToStr(STR("SetObjectField")), object_ref,
field_ref_);

Expand Down
68 changes: 39 additions & 29 deletions implementation/overload_ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
#include "jni_dep.h"
#include "metaprogramming/double_locked_value.h"
#include "metaprogramming/string_concatenate.h"
#include "implementation/id.h"

namespace jni {

// Transforms a OverloadRef IdT into a fully qualified ID. Storage is keyed
// against these IDs to reduce excess MethodID lookups.
template <typename IdT>
Expand All @@ -56,7 +56,7 @@ struct OverloadRefUniqueId {
// Dashes are solely for readability in debugging.
static constexpr std::string_view TypeName() {
return metaprogramming::StringConcatenate_v<
kClassQualifier, kDash, kOverloadName, kDash, Signature_v<IdT>>;
kClassQualifier, kDash, kOverloadName, kDash, Signature_v<IdT>>;
}
};

Expand All @@ -68,25 +68,25 @@ struct OverloadRef {
using SelfIdT = typename IdT::template ChangeIdType<IdType::CLASS>;

using ReturnProxied = std::conditional_t<
ReturnIdT::kIsSelf,
Return_t<typename SelfIdT::MaterializeCDeclT, SelfIdT>,
Return_t<typename ReturnIdT::MaterializeCDeclT, ReturnIdT>>;
ReturnIdT::kIsSelf,
Return_t<typename SelfIdT::MaterializeCDeclT, SelfIdT>,
Return_t<typename ReturnIdT::MaterializeCDeclT, ReturnIdT>>;

static jmethodID GetMethodID(jclass clazz) {
static auto get_lambda =
[clazz](metaprogramming::DoubleLockedValue<jmethodID>* storage) {
if (kConfiguration.release_method_ids_on_teardown_) {
DefaultRefs<jmethodID>().push_back(storage);
}
if (kConfiguration.release_method_ids_on_teardown_) {
DefaultRefs<jmethodID>().push_back(storage);
}

if constexpr (IdT::kIsStatic) {
return jni::JniHelper::GetStaticMethodID(clazz, IdT::Name(),
Signature_v<IdT>.data());
} else {
return jni::JniHelper::GetMethodID(clazz, IdT::Name(),
Signature_v<IdT>.data());
}
};
if constexpr (IdT::kIsStatic) {
return jni::JniHelper::GetStaticMethodID(clazz, IdT::Name(),
Signature_v<IdT>.data());
} else {
return jni::JniHelper::GetMethodID(clazz, IdT::Name(),
Signature_v<IdT>.data());
}
};

return RefStorage<decltype(get_lambda), OverloadRefUniqueId<IdT>>::Get(
get_lambda);
Expand All @@ -100,32 +100,42 @@ struct OverloadRef {
const jmethodID mthd = OverloadRef::GetMethodID(clazz);

if constexpr (std::is_same_v<ReturnProxied, void>) {
return InvokeHelper<void, kRank, kStatic>::Invoke(
fprintf(stderr, "pre invoke void \n");
InvokeHelper<void, kRank, kStatic>::Invoke(
object, clazz, mthd,
Proxy_t<Params>::ProxyAsArg(std::forward<Params>(params))...);
ForwardWithRefStrip(Proxy_t<Params>::ProxyAsArg(
std::forward<Params>(params)))...);
} else if constexpr (IdT::kIsConstructor) {
fprintf(stderr, "pre constructor invoke\n");
return ReturnProxied{
AdoptLocal{},
LifecycleHelper<jobject, LifecycleType::LOCAL>::Construct(
clazz, mthd,
Proxy_t<Params>::ProxyAsArg(std::forward<Params>(params))...)};
ForwardWithRefStrip(
Proxy_t<Params>::ProxyAsArg(
std::forward<Params>(params)))...)};
} else {
fprintf(stderr, "pre non-constructor invoke\n");
if constexpr (std::is_base_of_v<RefBaseBase, ReturnProxied>) {
return ReturnProxied{
AdoptLocal{},
InvokeHelper<typename ReturnIdT::CDecl, kRank, kStatic>::Invoke(
object, clazz, mthd,
Proxy_t<Params>::ProxyAsArg(std::forward<Params>(params))...)};
return
InvokeHelper<typename ReturnIdT::CDecl, kRank, kStatic>::Invoke(
object, clazz, mthd,
ForwardWithRefStrip(
Proxy_t<Params>::ProxyAsArg(std::forward<Params>(params)))
...);
//fprintf(stderr, "post invoke");
} else {
fprintf(stderr, "pre non-refbase invoke\n");
return static_cast<ReturnProxied>(
InvokeHelper<typename ReturnIdT::CDecl, kRank, kStatic>::Invoke(
object, clazz, mthd,
Proxy_t<Params>::ProxyAsArg(std::forward<Params>(params))...));
InvokeHelper<typename ReturnIdT::CDecl, kRank, kStatic>::Invoke(
object, clazz, mthd,
ForwardWithRefStrip(
Proxy_t<Params>::ProxyAsArg(
std::forward<Params>(params)))...));
}
}
}
};

} // namespace jni
} // namespace jni

#endif // JNI_BIND_OVERLOAD_REF_H
32 changes: 24 additions & 8 deletions implementation/proxy_definitions_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@
#include "implementation/jni_helper/lifecycle_string.h"
#include "implementation/jvm.h"
#include "implementation/proxy.h"
#include "implementation/proxy_temporary.h"
#include "implementation/proxy_convenience_aliases.h"
#include "implementation/ref_base.h"
#include "jni_dep.h"

namespace jni {

template <typename JString>
struct Proxy<JString,
typename std::enable_if_t<std::is_same_v<JString, jstring>>>
Expand Down Expand Up @@ -68,22 +68,39 @@ struct Proxy<JString,
IsConvertibleKey<T>::template value<std::string_view> ||
std::is_same_v<T, LocalString> || std::is_same_v<T, GlobalString>;

static constexpr auto DeleteLambda = [](const jstring& s) {
JniEnv::GetEnv()->DeleteLocalRef(static_cast<jobject>(s));
};

struct DeleteLocalRef {
static void Call(const jstring& s) {
JniEnv::GetEnv()->DeleteLocalRef(static_cast<jobject>(s));
}
};

// These leak local instances of strings. Usually, RAII mechanisms would
// correctly release local instances, but here we are stripping that so it can
// be used in a method. This could be obviated by wrapping the calling scope
// in a local stack frame.
static jstring ProxyAsArg(jstring s) { return s; }
//static ProxyTemporary<jstring, DeleteLocalRef> ProxyAsArg(jstring s) {
static jstring ProxyAsArg(jstring s) {
//LocalObject<kJavaLangString, kDefaultClassLoader, kDefaultJvm> obj{"afsd"};

//return {s};
return s;
}

template <typename T,
typename = std::enable_if_t<std::is_same_v<T, const char*> ||
std::is_same_v<T, std::string> ||
std::is_same_v<T, std::string_view>>>
static jstring ProxyAsArg(T s) {
// static jstring ProxyAsArg(T s) {
static ProxyTemporary<jstring, DeleteLocalRef> ProxyAsArg(T s) {
if constexpr (std::is_same_v<T, const char*>) {
return LifecycleHelper<jstring, LifecycleType::LOCAL>::Construct(s);
return {LifecycleHelper<jstring, LifecycleType::LOCAL>::Construct(s)};
} else {
return LifecycleHelper<jstring, LifecycleType::LOCAL>::Construct(
s.data());
return {LifecycleHelper<jstring, LifecycleType::LOCAL>::Construct(
s.data())};
}
}

Expand All @@ -101,7 +118,6 @@ struct Proxy<JString,
return t.Release();
}
};

} // namespace jni
} // namespace jni

#endif // JNI_BIND_IMPLEMENTATION_PROXY_DEFINITIONS_STRING_H_
Loading
Loading