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

encode() and a stub for the load() #89

Merged
merged 5 commits into from
Jun 14, 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
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ jobs:
script:
- sudo apt-get install -y binutils
- ./sbt assembly
- ./sbt "testOnly *Close* *ClientVersion* *SupportedLanguages*"
- ./sbt "testOnly org.bblfsh.client.v2.BblfshClientParseTest -- -z \"Decoded UAST after parsing\""
- ./sbt "testOnly org.bblfsh.client.v2.BblfshClientParseTest -- -z \"Decoded UAST RootNode\""
- ./sbt "testOnly *Close* *ClientVersion* *SupportedLanguages* *BblfshClientParseTest"

after_failure: *failure_logs_anchor

- name: 'Cross-compile, release & publish to Sonatype'
Expand Down
4 changes: 2 additions & 2 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.

2 changes: 1 addition & 1 deletion src/main/native/org_bblfsh_client_v2_Node.h

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

36 changes: 26 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 @@ -15,6 +15,8 @@ jobject asJvmBuffer(uast::Buffer buf) {
return env->NewDirectByteBuffer(buf.ptr, buf.size);
}

const char *nativeContext = "nativeContext";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nit: Either const char * const nativeContext = "…" or const char nativeContext[] = "…" is preferable here—to ensure the pointer itself is also const (either works). constexpr char nativeContext[] = "…" will also work if we assume C++ ≥ 11.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thank you, we do! Was thinking about constexpr as well - will replace as part of the next PR.


jfieldID getHandleField(JNIEnv *env, jobject obj, const char *name) {
jclass cls = env->GetObjectClass(obj);
if (env->ExceptionOccurred() || !cls) {
Expand Down Expand Up @@ -93,7 +95,7 @@ class ContextExt {
return toJ(root);
}

// Encode serializes existing-on-guest-side UAST.
// Encode serializes external UAST.
// Borrows the reference.
jobject Encode(jobject node, UastFormat format) {
NodeHandle h = toHandle(node);
Expand Down Expand Up @@ -137,25 +139,39 @@ JNIEXPORT jobject JNICALL Java_org_bblfsh_client_v2_libuast_Libuast_decode(
return jCtxExt;
}

JNIEXPORT void JNICALL Java_org_bblfsh_client_v2_Context_dispose(JNIEnv *env,
jobject self) {
ContextExt *p = getHandle<ContextExt>(env, self, "nativeContext");
setHandle<ContextExt>(env, self, 0, "nativeContext");
delete p;
}

JNIEXPORT jobject JNICALL Java_org_bblfsh_client_v2_libuast_Libuast_filter(
JNIEnv *, jobject, jobject, jstring) {
return NULL; // TODO(bzz): implement
return nullptr; // TODO(bzz): implement
}

// v2.Context()
JNIEXPORT jobject JNICALL Java_org_bblfsh_client_v2_Context_root(JNIEnv *env,
jobject self) {
ContextExt *p = getHandle<ContextExt>(env, self, "nativeContext");
ContextExt *p = getHandle<ContextExt>(env, self, nativeContext);
return p->RootNode();
}

JNIEXPORT jobject JNICALL Java_org_bblfsh_client_v2_Context_encode(
JNIEnv *env, jobject self, jobject node) {
UastFormat fmt = UAST_BINARY; // TODO(bzz): make it argument & enum

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

JNIEXPORT void JNICALL Java_org_bblfsh_client_v2_Context_dispose(JNIEnv *env,
jobject self) {
ContextExt *p = getHandle<ContextExt>(env, self, nativeContext);
setHandle<ContextExt>(env, self, 0, nativeContext);
delete p;
}

// v2.Node()
JNIEXPORT jobject JNICALL Java_org_bblfsh_client_v2_Node_load(JNIEnv *,
jobject) {
return nullptr; // TODO(bzz): implement
}

JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) {
JNIEnv *env;
if (vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_8) != JNI_OK) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/org/bblfsh/client/v2/Context.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import java.nio.ByteBuffer

case class Context(nativeContext: Long) {
@native def root(): Node
@native def encode(n: Node, format: Int): ByteBuffer
@native def encode(n: Node): ByteBuffer
@native def dispose()

@native def filter()
Expand Down
15 changes: 15 additions & 0 deletions src/main/scala/org/bblfsh/client/v2/JNode.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.bblfsh.client.v2

/** UAST nodes representation on the JVM side.
*
* Mirrors https://godoc.org/github.com/bblfsh/sdk/uast/nodes
*/
sealed abstract class JNode
case class JString(s: String) extends JNode
case class JDouble(num: Double) extends JNode
case class JLong(num: Long) extends JNode
case class JInt(num: BigInt) extends JNode
case class JBool(value: Boolean) extends JNode
case class JObject(obj: List[JField]) extends JNode
case class JArray(arr: List[JNode]) extends JNode

3 changes: 1 addition & 2 deletions src/main/scala/org/bblfsh/client/v2/Node.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.bblfsh.client.v2

case class Node(ctx: Long, handle: Long) {
// TODO(bzz) make sure single string value or an array can also be loaded
@native def load(): Map[String, _]
@native def load(): JNode
}
11 changes: 11 additions & 0 deletions src/main/scala/org/bblfsh/client/v2/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.bblfsh.client

/** Bblfsh client for protocol v2.
* See https://github.com/bblfsh/sdk/blob/v3.1.0/protocol/driver.proto
*
* The main class to use is [[org.bblfsh.client.v2.BblfshClient]]
*/
package object v2 {
/** Key, Value representation of [[org.bblfsh.client.v2.JObject]] */
type JField = (String, JNode)
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package org.bblfsh.client.v2

import gopkg.in.bblfsh.sdk.v2.protocol.driver.VersionResponse
import org.scalatest.{BeforeAndAfter, FunSuite}

import scala.io.Source

class BblfshClientClose extends FunSuite with BeforeAndAfter {
val client = BblfshClient("0.0.0.0", 9432)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ class BblfshClientParseTest extends FlatSpec
}

"Encoding back the RootNode of decoded UAST" should "produce same bytes" in {
val uast = resp.uast.decode()
val rootNode: Node = uast.root()
val uastCtx: Context = resp.uast.decode()
val rootNode: Node = uastCtx.root()
println(s"Root node: $rootNode")

val encodedBytes: ByteBuffer = uast.encode(rootNode, 0)
val encodedBytes: ByteBuffer = uastCtx.encode(rootNode)

encodedBytes.capacity should be (resp.uast.asReadOnlyByteBuffer.capacity)
encodedBytes shouldEqual resp.uast.asReadOnlyByteBuffer
Expand Down