Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.

Commit 44375ee

Browse files
committed
Rewords comments, unifies field strings in jni_utils
Complies with the team Reviewable review Signed-off-by: ncordon <[email protected]>
1 parent d9d930f commit 44375ee

File tree

4 files changed

+24
-27
lines changed

4 files changed

+24
-27
lines changed

src/main/native/jni_utils.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ const char METHOD_RE_INIT_CAUSE[] =
6161
const char METHOD_ITER_INIT[] = "(Lorg/bblfsh/client/v2/NodeExt;IJLorg/bblfsh/client/v2/ContextExt;)V";
6262
const char METHOD_JITER_INIT[] = "(Lorg/bblfsh/client/v2/JNode;IJLorg/bblfsh/client/v2/Context;)V";
6363

64+
const char METHOD_NODE_INIT[] = "(Lorg/bblfsh/client/v2/ContextExt;J)V";
65+
6466
// Field signatures
6567
const char FIELD_ITER_NODE[] = "Ljava/lang/Object;";
66-
const char FIELD_ITER_CTX[] = "Lorg/bblfsh/client/v2/Context;";
67-
const char FIELD_ITER_EXT_NODE[] = "Ljava/lang/Object;";
68-
const char FIELD_ITER_EXT_CTX[] = "Lorg/bblfsh/client/v2/ContextExt;";
69-
const char FIELD_NODE_EXT_CTX[] = "Lorg/bblfsh/client/v2/ContextExt;";
68+
const char FIELD_CTX[] = "Lorg/bblfsh/client/v2/Context;";
69+
const char FIELD_CTX_EXT[] = "Lorg/bblfsh/client/v2/ContextExt;";
7070

7171
// TODO(#114): cache classes&methods in JNI_OnLoad should speed this up
7272
void checkJvmException(std::string msg) {

src/main/native/jni_utils.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,12 @@ extern const char METHOD_RE_INIT[];
3636
extern const char METHOD_RE_INIT_CAUSE[];
3737
extern const char METHOD_ITER_INIT[];
3838
extern const char METHOD_JITER_INIT[];
39+
extern const char METHOD_NODE_INIT[];
3940

4041
// Field signatures
4142
extern const char FIELD_ITER_NODE[];
42-
extern const char FIELD_ITER_EXT_NODE[];
43-
extern const char FIELD_NODE_EXT_CTX[];
44-
extern const char FIELD_ITER_CTX[];
45-
extern const char FIELD_ITER_EXT_CTX[];
46-
43+
extern const char FIELD_CTX[];
44+
extern const char FIELD_CTX_EXT[];
4745
// Checks through JNI, if there is a pending excption on the JVM side.
4846
//
4947
// Throws new RuntimeException to the JVM in case there is,

src/main/native/org_bblfsh_client_v2_libuast_Libuast.cc

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ void setHandle(JNIEnv *env, jobject obj, T *t, const char *name,
5151

5252
jlong handle = reinterpret_cast<jlong>(t);
5353
env->SetLongField(obj, fId, handle);
54+
checkJvmException("failed to set handle for" + std::string(name));
5455
}
5556

5657
template <typename T>
@@ -61,6 +62,7 @@ void setHandle(JNIEnv *env, jobject obj, T *t, const char *name) {
6162
void setObjectField(JNIEnv *env, jobject obj, jobject field, const char *name, const char *sig) {
6263
jfieldID fId = FieldID(env, obj, name, sig);
6364
env->SetObjectField(obj, fId, field);
65+
checkJvmException("failed to set object field for" + std::string(name));
6466
}
6567

6668
jobject asJvmBuffer(uast::Buffer buf) {
@@ -103,7 +105,7 @@ class ContextExt {
103105
if (node == 0) return nullptr;
104106

105107
JNIEnv *env = getJNIEnv();
106-
jobject jObj = NewJavaObject(env, CLS_NODE, "(Lorg/bblfsh/client/v2/ContextExt;J)V", jCtxExt, node);
108+
jobject jObj = NewJavaObject(env, CLS_NODE, METHOD_NODE_INIT, jCtxExt, node);
107109
return jObj;
108110
}
109111

@@ -158,7 +160,7 @@ class ContextExt {
158160
// Attaches a Scala ContextExt object to the C ContextExt
159161
// We need this because a NodeExt from Scala side includes
160162
// a Scala ContextExt and a handle to the native C node
161-
void setJavaContext(jobject ctx) {
163+
void setManagedContext(jobject ctx) {
162164
jCtxExt = getJNIEnv()->NewWeakGlobalRef(ctx);
163165
}
164166

@@ -606,9 +608,8 @@ class Context {
606608

607609
jobject LoadFrom(jobject src) { // NodeExt
608610
JNIEnv *env = getJNIEnv();
609-
// NodeExt contains a ctx: ContextExt and ContextExt the
610-
// handle for the native context, called nativeContext
611-
jobject jCtxExt = ObjectField(env, src, "ctx", FIELD_NODE_EXT_CTX);
611+
// NodeExt contains a ctx: ContextExt (JVM ref) and a nativeContext: ContextExt (handle)
612+
jobject jCtxExt = ObjectField(env, src, "ctx", FIELD_CTX_EXT);
612613
ContextExt *nodeExtCtx = getHandle<ContextExt>(env, jCtxExt, nativeContext);
613614

614615
checkJvmException("failed to get NodeExt.ctx");
@@ -650,8 +651,8 @@ JNIEXPORT jobject JNICALL Java_org_bblfsh_client_v2_libuast_Libuast_decode(
650651

651652
jobject jCtxExt = NewJavaObject(env, CLS_CTX_EXT, "(J)V", p);
652653

653-
// Associates the JVM context ext to the native ContextExt
654-
p->setJavaContext(jCtxExt);
654+
// Saves weak reference to JVM ContextExt in the native ContextExt
655+
p->setManagedContext(jCtxExt);
655656

656657
if (env->ExceptionCheck() || !jCtxExt) {
657658
jCtxExt = nullptr;
@@ -686,7 +687,7 @@ Java_org_bblfsh_client_v2_libuast_Libuast_00024UastIter_nativeInit(
686687
// this.iter = it;
687688
setHandle<uast::Iterator<Node *>>(env, self, it, "iter");
688689
// this.ctx = Context(ctx);
689-
setObjectField(env, self, jCtx, "ctx", FIELD_ITER_CTX);
690+
setObjectField(env, self, jCtx, "ctx", FIELD_CTX);
690691

691692
return;
692693
}
@@ -695,7 +696,7 @@ JNIEXPORT void JNICALL
695696
Java_org_bblfsh_client_v2_libuast_Libuast_00024UastIter_nativeDispose(
696697
JNIEnv *env, jobject self) {
697698
// this.ctx will be disposed by Context finalizer
698-
setObjectField(env, self, nullptr, "ctx", FIELD_ITER_CTX);
699+
setObjectField(env, self, nullptr, "ctx", FIELD_CTX);
699700

700701
// this.iter
701702
auto iter = getHandle<uast::Iterator<Node *>>(env, self, "iter");
@@ -730,13 +731,12 @@ JNIEXPORT void JNICALL
730731
Java_org_bblfsh_client_v2_libuast_Libuast_00024UastIterExt_nativeInit(
731732
JNIEnv *env, jobject self) { // sets iter and ctx, given node: NodeExt
732733

733-
jobject nodeExt = ObjectField(env, self, "node", FIELD_ITER_EXT_NODE);
734+
jobject nodeExt = ObjectField(env, self, "node", FIELD_ITER_NODE);
734735
if (!nodeExt) {
735736
return;
736737
}
737738

738-
jobject jCtxExt = ObjectField(env, nodeExt, "ctx", FIELD_NODE_EXT_CTX);
739-
739+
jobject jCtxExt = ObjectField(env, nodeExt, "ctx", FIELD_CTX_EXT);
740740
if (!jCtxExt)
741741
return;
742742

@@ -753,7 +753,7 @@ Java_org_bblfsh_client_v2_libuast_Libuast_00024UastIterExt_nativeInit(
753753
// this.iter = it;
754754
setHandle<uast::Iterator<NodeHandle>>(env, self, it, "iter");
755755
// this.ctx = jCtxExt;
756-
setObjectField(env, self, jCtxExt, "ctx", FIELD_ITER_EXT_CTX);
756+
setObjectField(env, self, jCtxExt, "ctx", FIELD_CTX_EXT);
757757

758758
return;
759759
}
@@ -762,7 +762,7 @@ JNIEXPORT void JNICALL
762762
Java_org_bblfsh_client_v2_libuast_Libuast_00024UastIterExt_nativeDispose(
763763
JNIEnv *env, jobject self) {
764764
// this.ctx will be disposed by ContextExt finalizer
765-
setObjectField(env, self, nullptr, "ctx", FIELD_ITER_EXT_CTX);
765+
setObjectField(env, self, nullptr, "ctx", FIELD_CTX_EXT);
766766

767767
// this.iter
768768
auto iter = getHandle<uast::Iterator<NodeHandle>>(env, self, "iter");
@@ -789,7 +789,7 @@ Java_org_bblfsh_client_v2_libuast_Libuast_00024UastIterExt_nativeNext(
789789
NodeHandle node = iter->node();
790790
if (node == 0) return nullptr;
791791

792-
jobject jCtxExt = ObjectField(env, self, "ctx", FIELD_ITER_EXT_CTX);
792+
jobject jCtxExt = ObjectField(env, self, "ctx", FIELD_CTX_EXT);
793793
ContextExt *ctx = getHandle<ContextExt>(env, jCtxExt, nativeContext);
794794
return ctx->lookup(node);
795795
}
@@ -892,7 +892,7 @@ JNIEXPORT jobject JNICALL Java_org_bblfsh_client_v2_NodeExt_load(JNIEnv *env,
892892
// We need to make a local reference to node since ctx is going to be destroyed
893893
// before returning, and the global references that each Node carries with it.
894894
// If we do not copy node in a local ref, we return a null, whereas copying it in
895-
// a local ref ensures the native part returns the value to Java and after that
895+
// a local ref ensures the native part returns the value to Scala and after that
896896
// disposes of the local refs
897897
jobject result = getJNIEnv()->NewLocalRef(node);
898898
delete (ctx);
@@ -901,7 +901,7 @@ JNIEXPORT jobject JNICALL Java_org_bblfsh_client_v2_NodeExt_load(JNIEnv *env,
901901

902902
JNIEXPORT jobject JNICALL Java_org_bblfsh_client_v2_NodeExt_filter(
903903
JNIEnv *env, jobject self, jstring jquery) {
904-
jobject jCtxExt = ObjectField(env, self, "ctx", FIELD_NODE_EXT_CTX);
904+
jobject jCtxExt = ObjectField(env, self, "ctx", FIELD_CTX_EXT);
905905
ContextExt *ctx = getHandle<ContextExt>(env, jCtxExt, nativeContext);
906906
return filterUastIterExt(ctx, jCtxExt, jquery, env);
907907
}

src/main/native/project/build.properties

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)