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

Commit

Permalink
v2: implement Node.load()
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Bezzubov <bzz@apache.org>
  • Loading branch information
bzz committed Jul 1, 2019
1 parent 2bc2167 commit 5ec22ca
Show file tree
Hide file tree
Showing 15 changed files with 730 additions and 67 deletions.
11 changes: 0 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ stages:
- name: release
if: tag IS present


jobs:
include:
- name: 'All tests'
stage: test
if: tag IS present # TODO(bzz): enable on PRs as soon as migrated to V2
install: &test_setup_anchor
- docker run --privileged -d -p 9432:9432 --name bblfsh bblfsh/bblfshd
- docker exec -it bblfsh bblfshctl driver install --recommended
Expand All @@ -34,15 +32,6 @@ jobs:
after_failure: &failure_logs_anchor
- docker logs bblfsh

- name: 'V2: passing tests' # TODO(#83): remove, after both tests sets converge
install: *test_setup_anchor
script:
- sudo apt-get install -y binutils
- ./sbt assembly
- ./sbt "testOnly *Close* *ClientVersion* *SupportedLanguages* *BblfshClientParseTest"

after_failure: *failure_logs_anchor

- name: 'Cross-compile, release & publish to Sonatype'
stage: release
before_install:
Expand Down
78 changes: 71 additions & 7 deletions src/main/native/jni_utils.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#include "jni_utils.h"

// Class fully qualified names
const char *CLS_NODE = "org/bblfsh/client/v2/Node";
const char *CLS_CTX = "org/bblfsh/client/v2/Context";

extern JavaVM *jvm; // FIXME(bzz): double-check and document

JNIEnv *getJNIEnv() {
Expand All @@ -21,24 +17,46 @@ JNIEnv *getJNIEnv() {
return pEnv;
}

// Class fully qualified names
const char *CLS_NODE = "org/bblfsh/client/v2/Node";
const char *CLS_CTX = "org/bblfsh/client/v2/Context";

const char *CLS_JNODE = "org/bblfsh/client/v2/JNode";
const char *CLS_JNULL = "org/bblfsh/client/v2/JNull";
const char *CLS_JSTR = "org/bblfsh/client/v2/JString";
const char *CLS_JINT = "org/bblfsh/client/v2/JInt";
const char *CLS_JFLT = "org/bblfsh/client/v2/JFloat";
const char *CLS_JBOL = "org/bblfsh/client/v2/JBool";
const char *CLS_JUINT = "org/bblfsh/client/v2/JUint";
const char *CLS_JARR = "org/bblfsh/client/v2/JArray";
const char *CLS_JOBJ = "org/bblfsh/client/v2/JObject";

const char *METHOD_JNODE_KEY_AT = "(I)Ljava/lang/String;";
const char *METHOD_JNODE_VALUE_AT = "(I)Lorg/bblfsh/client/v2/JNode;";
const char *METHOD_JOBJ_ADD =
"(Ljava/lang/String;Lorg/bblfsh/client/v2/JNode;)Lscala/collection/"
"mutable/Buffer;";
const char *METHOD_JARR_ADD =
"(Lorg/bblfsh/client/v2/JNode;)Lscala/collection/mutable/Buffer;";

jobject NewJavaObject(JNIEnv *env, const char *className, const char *initSign,
...) {
jclass cls = env->FindClass(className);
if (env->ExceptionOccurred() || !cls) {
return NULL;
return nullptr;
}

jmethodID initId = env->GetMethodID(cls, "<init>", initSign);
if (env->ExceptionOccurred() || !initId) {
return NULL;
return nullptr;
}

va_list varargs;
va_start(varargs, initSign);
jobject instance = env->NewObjectV(cls, initId, varargs);
va_end(varargs);
if (env->ExceptionOccurred() || !instance) {
return NULL;
return nullptr;
}

return instance;
Expand All @@ -56,3 +74,49 @@ jfieldID getField(JNIEnv *env, jobject obj, const char *name) {
}
return jfid;
}

static jmethodID MethodID(JNIEnv *env, const char *method,
const char *signature, const char *className) {
jclass cls = env->FindClass(className);
if (env->ExceptionOccurred() || !cls) {
return nullptr;
}

jmethodID mId = env->GetMethodID(cls, method, signature);
if (env->ExceptionOccurred()) {
return nullptr;
}

return mId;
}

jint IntMethod(JNIEnv *env, const char *method, const char *signature,
const char *className, const jobject *object) {
jmethodID mId = MethodID(env, method, signature, className);
if (env->ExceptionOccurred() || !mId) return 0;
// TODO(bzz): add better error handling
// ExceptionOccurred()
// ExceptionDescribe()
// ExceptionClear()
// ExceptionCheck()
// ThrowNew("failed to call $className.$method") to JVM

jint res = env->CallIntMethod(*object, mId);
if (env->ExceptionOccurred()) return 0;

return res;
}

jobject ObjectMethod(JNIEnv *env, const char *method, const char *signature,
const char *className, const jobject *object, ...) {
jmethodID mId = MethodID(env, method, signature, className);
if (env->ExceptionOccurred() || !mId) return nullptr;

va_list varargs;
va_start(varargs, object);
jobject res = env->CallObjectMethodV(*object, mId, varargs);
va_end(varargs);
if (env->ExceptionOccurred() || !res) return nullptr;

return res;
}
21 changes: 21 additions & 0 deletions src/main/native/jni_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,29 @@
extern const char *CLS_NODE;
extern const char *CLS_CTX;

extern const char *CLS_JNODE;
extern const char *CLS_JNULL;
extern const char *CLS_JSTR;
extern const char *CLS_JINT;
extern const char *CLS_JFLT;
extern const char *CLS_JBOL;
extern const char *CLS_JUINT;
extern const char *CLS_JARR;
extern const char *CLS_JOBJ;

extern const char *METHOD_JNODE_KEY_AT;
extern const char *METHOD_JNODE_VALUE_AT;
extern const char *METHOD_JOBJ_ADD;
extern const char *METHOD_JARR_ADD;

JNIEnv *getJNIEnv();
jobject NewJavaObject(JNIEnv *, const char *, const char *, ...);
jfieldID getField(JNIEnv *env, jobject obj, const char *name);

jint IntMethod(JNIEnv *, const char *, const char *, const char *,
const jobject *);

jobject ObjectMethod(JNIEnv *, const char *, const char *, const char *,
const jobject *, ...);

#endif
21 changes: 21 additions & 0 deletions src/main/native/org_bblfsh_client_v2_Context__.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5ec22ca

Please sign in to comment.