@@ -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
5657template <typename T>
@@ -61,6 +62,7 @@ void setHandle(JNIEnv *env, jobject obj, T *t, const char *name) {
6162void 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
6668jobject 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
695696Java_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
730731Java_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
762762Java_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
902902JNIEXPORT 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}
0 commit comments