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

Adds UAST encoding options and improves tests #126

Merged
merged 8 commits into from
Sep 10, 2019
Merged
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
3 changes: 2 additions & 1 deletion src/main/native/jni_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ JNIEnv *getJNIEnv() {
const char CLS_NODE[] = "org/bblfsh/client/v2/NodeExt";
const char CLS_CTX_EXT[] = "org/bblfsh/client/v2/ContextExt";
const char CLS_CTX[] = "org/bblfsh/client/v2/Context";
const char CLS_TO[] = "org/bblfsh/client/v2/TreeOrder";
const char CLS_TO[] = "org/bblfsh/client/v2/libuast/Libuast$TreeOrder";
const char CLS_ENCS[] = "org/bblfsh/client/v2/libuast/Libuast$UastFormat";
const char CLS_OBJ[] = "java/lang/Object";
const char CLS_RE[] = "java/lang/RuntimeException";
const char CLS_JNODE[] = "org/bblfsh/client/v2/JNode";
Expand Down
1 change: 1 addition & 0 deletions src/main/native/jni_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ extern const char CLS_CTX[];
extern const char CLS_OBJ[];
extern const char CLS_RE[];
extern const char CLS_TO[];
extern const char CLS_ENCS[];

// Fully qualified class names for Bablefish UAST types
extern const char CLS_JNODE[];
Expand Down
8 changes: 4 additions & 4 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.

8 changes: 4 additions & 4 deletions src/main/native/org_bblfsh_client_v2_ContextExt.h

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

32 changes: 22 additions & 10 deletions src/main/native/org_bblfsh_client_v2_libuast_Libuast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -631,8 +631,8 @@ class Context {
// ==========================================

JNIEXPORT jobject JNICALL Java_org_bblfsh_client_v2_libuast_Libuast_decode(
JNIEnv *env, jobject self, jobject directBuf) {
UastFormat format = UAST_BINARY; // TODO: make it arg
JNIEnv *env, jobject self, jobject directBuf, jint fmt) {
UastFormat format = (UastFormat) fmt;

// works only with ByteBuffer.allocateDirect()
void *buf = env->GetDirectBufferAddress(directBuf);
Expand Down Expand Up @@ -824,12 +824,12 @@ JNIEXPORT jobject JNICALL Java_org_bblfsh_client_v2_Context_filter(
return iter;
}

JNIEXPORT jobject JNICALL Java_org_bblfsh_client_v2_Context_encode(
JNIEnv *env, jobject self, jobject jnode) {
UastFormat fmt = UAST_BINARY; // TODO(#107): make it argument
JNIEXPORT jobject JNICALL Java_org_bblfsh_client_v2_Context_nativeEncode(
JNIEnv *env, jobject self, jobject jnode, jint fmt) {
UastFormat format = (UastFormat) fmt; // TODO(#107): make it argument

Context *p = getHandle<Context>(env, self, nativeContext);
return p->Encode(jnode, fmt);
return p->Encode(jnode, format);
}

JNIEXPORT jlong JNICALL
Expand Down Expand Up @@ -864,12 +864,12 @@ JNIEXPORT jobject JNICALL Java_org_bblfsh_client_v2_ContextExt_filter(
return filterUastIterExt(ctx, self, jquery, env);
}

JNIEXPORT jobject JNICALL Java_org_bblfsh_client_v2_ContextExt_encode(
JNIEnv *env, jobject self, jobject node) {
UastFormat fmt = UAST_BINARY; // TODO(#107): make it argument
JNIEXPORT jobject JNICALL Java_org_bblfsh_client_v2_ContextExt_nativeEncode(
JNIEnv *env, jobject self, jobject node, jint fmt) {
UastFormat format = (UastFormat) fmt;

ContextExt *p = getHandle<ContextExt>(env, self, nativeContext);
return p->Encode(node, fmt);
return p->Encode(node, format);
}

JNIEXPORT void JNICALL
Expand Down Expand Up @@ -924,6 +924,18 @@ JNIEXPORT jobject JNICALL Java_org_bblfsh_client_v2_libuast_Libuast_getTreeOrder
return jObj;
}

// ==========================================
// UAST encoding formats
// ==========================================
JNIEXPORT jobject JNICALL Java_org_bblfsh_client_v2_libuast_Libuast_getUastFormats(JNIEnv *env,
jobject self) {
jobject jObj = NewJavaObject(env, CLS_ENCS, "(II)V",
UAST_BINARY,
UAST_YAML);
return jObj;
}



JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) {
JNIEnv *env;
Expand Down
14 changes: 11 additions & 3 deletions src/main/native/org_bblfsh_client_v2_libuast_Libuast.h

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

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

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

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

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

72 changes: 52 additions & 20 deletions src/main/scala/org/bblfsh/client/v2/BblfshClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class BblfshClient(host: String, port: Int, maxMsgSize: Int) {
private val stubInfo = DriverHostGrpc.blockingStub(channel)

/**
* Parses file with a given name and content using
* Parses file with a given name and content using
* the provided timeout.
*
* @param name file name
Expand Down Expand Up @@ -110,32 +110,52 @@ object BblfshClient {
val DEFAULT_MAX_MSG_SIZE = 100 * 1024 * 1024 // bytes

private val libuast = new Libuast
private val orders = libuast.getTreeOrders
private val formats = libuast.getUastFormats

private val treeOrder = libuast.getTreeOrders
abstract class UastFormat(val toInt: Int)
abstract class TreeOrder(val toInt: Int)

val AnyOrder = treeOrder.AnyOrder
val PreOrder = treeOrder.PreOrder
val PostOrder = treeOrder.PostOrder
val LevelOrder = treeOrder.LevelOrder
val ChildrenOrder = treeOrder.ChildrenOrder
val PositionOrder = treeOrder.PositionOrder
// Lift tree order as constants
case object UastBinary extends UastFormat(formats.uastBinary)
case object UastYaml extends UastFormat(formats.uastYaml)

// Lift orders from libuast as types
case object AnyOrder extends TreeOrder(orders.anyOrder)
case object PreOrder extends TreeOrder(orders.preOrder)
case object PostOrder extends TreeOrder(orders.postOrder)
case object LevelOrder extends TreeOrder(orders.levelOrder)
case object ChildrenOrder extends TreeOrder(orders.childrenOrder)
case object PositionOrder extends TreeOrder(orders.positionOrder)

/** Creates a BblfshClient with default parameters */
def apply(
host: String, port: Int,
maxMsgSize: Int = DEFAULT_MAX_MSG_SIZE
): BblfshClient = new BblfshClient(host, port, maxMsgSize)

/**
* Decodes bytes from wired format of bblfsh protocol.v2.
* Requires a buffer in Direct mode.
* Requires a buffer in Direct mode, and the format
* to decode from
*
* Since v2.
*/
def decode(buf: ByteBuffer): ContextExt = Libuast.synchronized {
def decode(buf: ByteBuffer, fmt: UastFormat): ContextExt = Libuast.synchronized {
if (!buf.isDirect()) {
throw new RuntimeException("Only directly-allocated buffer decoding is supported.")
}
libuast.decode(buf)
libuast.decode(buf, fmt)
}

/**
* Decodes bytes from wired binary format of bblfsh protocol.v2.
* Requires a buffer in Direct mode
*
* Since v2.
*/
def decode(buf: ByteBuffer): ContextExt = Libuast.synchronized {
decode(buf, UastBinary)
}

/** Enables API: resp.uast.decode() */
Expand All @@ -146,10 +166,10 @@ object BblfshClient {
* Always copies memory to a new buffer in Direct mode,
* to be able to pass it to JNI.
*/
def decode(): ContextExt = {
def decode(fmt: UastFormat): ContextExt = {
val bufDirectCopy = ByteBuffer.allocateDirect(buf.size)
buf.copyTo(bufDirectCopy)
val result = BblfshClient.decode(bufDirectCopy)
val result = BblfshClient.decode(bufDirectCopy, fmt)
// Sometimes the direct buffer can take a lot to deallocate,
// causing Out of Memory, because it is not allocated in
// in the JVM heap and will only be deallocated them when
Expand All @@ -160,33 +180,46 @@ object BblfshClient {
System.gc()
result
}

/**
* Decodes in binary format
*/
def decode(): ContextExt = {
decode(UastBinary)
}
}

/** Enables API: resp.get() */
implicit class ResponseMethods(val resp: ParseResponse) {
def get(): JNode = {
val ctx = resp.uast.decode()
/** Gets the root decoding the tree in binary format */
def get(fmt: UastFormat): JNode = {
val ctx = resp.uast.decode(fmt)
val node = ctx.root().load()
ctx.dispose()
node
}

/** Gets the root node decoding the tree in binary format */
def get(): JNode = {
get(UastBinary)
}
}

/** Enables API: client.filter and client.iterator for client an instance of BblfshClient */
implicit class BblfshClientMethods(val client: BblfshClient) {
def filter(node: NodeExt, query: String) = BblfshClient.filter(node, query)
def filter(node: JNode, query: String) = BblfshClient.filter(node, query)
def iterator(node: NodeExt, treeOrder: Int) = BblfshClient.iterator(node, treeOrder)
def iterator(node: JNode, treeOrder: Int) = BblfshClient.iterator(node, treeOrder)
def iterator(node: NodeExt, treeOrder: TreeOrder) = BblfshClient.iterator(node, treeOrder)
def iterator(node: JNode, treeOrder: TreeOrder) = BblfshClient.iterator(node, treeOrder)
}

/** Factory method for iterator over an external/native node */
def iterator(node: NodeExt, treeOrder: Int): Libuast.UastIterExt = {
def iterator(node: NodeExt, treeOrder: TreeOrder): Libuast.UastIterExt = {
Libuast.UastIterExt(node, treeOrder)
}

/** Factory method for iterator over an managed node */
def iterator(node: JNode, treeOrder: Int): Libuast.UastIter = {
def iterator(node: JNode, treeOrder: TreeOrder): Libuast.UastIter = {
Libuast.UastIter(node, treeOrder)
}

Expand All @@ -203,4 +236,3 @@ object BblfshClient {
}

}

Loading