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

Commit 2bc2167

Browse files
authored
Merge pull request #89 from bzz/add-encode-load
encode() and a stub for the load()
2 parents bc7f973 + 0d76c98 commit 2bc2167

10 files changed

+62
-24
lines changed

.travis.yml

+2-3
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ jobs:
3939
script:
4040
- sudo apt-get install -y binutils
4141
- ./sbt assembly
42-
- ./sbt "testOnly *Close* *ClientVersion* *SupportedLanguages*"
43-
- ./sbt "testOnly org.bblfsh.client.v2.BblfshClientParseTest -- -z \"Decoded UAST after parsing\""
44-
- ./sbt "testOnly org.bblfsh.client.v2.BblfshClientParseTest -- -z \"Decoded UAST RootNode\""
42+
- ./sbt "testOnly *Close* *ClientVersion* *SupportedLanguages* *BblfshClientParseTest"
43+
4544
after_failure: *failure_logs_anchor
4645

4746
- name: 'Cross-compile, release & publish to Sonatype'

src/main/native/org_bblfsh_client_v2_Context.h

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/native/org_bblfsh_client_v2_Node.h

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/native/org_bblfsh_client_v2_libuast_Libuast.cc

+26-10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ jobject asJvmBuffer(uast::Buffer buf) {
1515
return env->NewDirectByteBuffer(buf.ptr, buf.size);
1616
}
1717

18+
const char *nativeContext = "nativeContext";
19+
1820
jfieldID getHandleField(JNIEnv *env, jobject obj, const char *name) {
1921
jclass cls = env->GetObjectClass(obj);
2022
if (env->ExceptionOccurred() || !cls) {
@@ -93,7 +95,7 @@ class ContextExt {
9395
return toJ(root);
9496
}
9597

96-
// Encode serializes existing-on-guest-side UAST.
98+
// Encode serializes external UAST.
9799
// Borrows the reference.
98100
jobject Encode(jobject node, UastFormat format) {
99101
NodeHandle h = toHandle(node);
@@ -137,25 +139,39 @@ JNIEXPORT jobject JNICALL Java_org_bblfsh_client_v2_libuast_Libuast_decode(
137139
return jCtxExt;
138140
}
139141

140-
JNIEXPORT void JNICALL Java_org_bblfsh_client_v2_Context_dispose(JNIEnv *env,
141-
jobject self) {
142-
ContextExt *p = getHandle<ContextExt>(env, self, "nativeContext");
143-
setHandle<ContextExt>(env, self, 0, "nativeContext");
144-
delete p;
145-
}
146-
147142
JNIEXPORT jobject JNICALL Java_org_bblfsh_client_v2_libuast_Libuast_filter(
148143
JNIEnv *, jobject, jobject, jstring) {
149-
return NULL; // TODO(bzz): implement
144+
return nullptr; // TODO(bzz): implement
150145
}
151146

152147
// v2.Context()
153148
JNIEXPORT jobject JNICALL Java_org_bblfsh_client_v2_Context_root(JNIEnv *env,
154149
jobject self) {
155-
ContextExt *p = getHandle<ContextExt>(env, self, "nativeContext");
150+
ContextExt *p = getHandle<ContextExt>(env, self, nativeContext);
156151
return p->RootNode();
157152
}
158153

154+
JNIEXPORT jobject JNICALL Java_org_bblfsh_client_v2_Context_encode(
155+
JNIEnv *env, jobject self, jobject node) {
156+
UastFormat fmt = UAST_BINARY; // TODO(bzz): make it argument & enum
157+
158+
ContextExt *p = getHandle<ContextExt>(env, self, nativeContext);
159+
return p->Encode(node, fmt);
160+
}
161+
162+
JNIEXPORT void JNICALL Java_org_bblfsh_client_v2_Context_dispose(JNIEnv *env,
163+
jobject self) {
164+
ContextExt *p = getHandle<ContextExt>(env, self, nativeContext);
165+
setHandle<ContextExt>(env, self, 0, nativeContext);
166+
delete p;
167+
}
168+
169+
// v2.Node()
170+
JNIEXPORT jobject JNICALL Java_org_bblfsh_client_v2_Node_load(JNIEnv *,
171+
jobject) {
172+
return nullptr; // TODO(bzz): implement
173+
}
174+
159175
JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) {
160176
JNIEnv *env;
161177
if (vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_8) != JNI_OK) {

src/main/scala/org/bblfsh/client/v2/Context.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import java.nio.ByteBuffer
44

55
case class Context(nativeContext: Long) {
66
@native def root(): Node
7-
@native def encode(n: Node, format: Int): ByteBuffer
7+
@native def encode(n: Node): ByteBuffer
88
@native def dispose()
99

1010
@native def filter()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.bblfsh.client.v2
2+
3+
/** UAST nodes representation on the JVM side.
4+
*
5+
* Mirrors https://godoc.org/github.com/bblfsh/sdk/uast/nodes
6+
*/
7+
sealed abstract class JNode
8+
case class JString(s: String) extends JNode
9+
case class JDouble(num: Double) extends JNode
10+
case class JLong(num: Long) extends JNode
11+
case class JInt(num: BigInt) extends JNode
12+
case class JBool(value: Boolean) extends JNode
13+
case class JObject(obj: List[JField]) extends JNode
14+
case class JArray(arr: List[JNode]) extends JNode
15+
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.bblfsh.client.v2
22

33
case class Node(ctx: Long, handle: Long) {
4-
// TODO(bzz) make sure single string value or an array can also be loaded
5-
@native def load(): Map[String, _]
4+
@native def load(): JNode
65
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.bblfsh.client
2+
3+
/** Bblfsh client for protocol v2.
4+
* See https://github.com/bblfsh/sdk/blob/v3.1.0/protocol/driver.proto
5+
*
6+
* The main class to use is [[org.bblfsh.client.v2.BblfshClient]]
7+
*/
8+
package object v2 {
9+
/** Key, Value representation of [[org.bblfsh.client.v2.JObject]] */
10+
type JField = (String, JNode)
11+
}

src/test/scala/org/bblfsh/client/v2/BblfshClientCloseTest.scala

-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package org.bblfsh.client.v2
22

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

6-
import scala.io.Source
75

86
class BblfshClientClose extends FunSuite with BeforeAndAfter {
97
val client = BblfshClient("0.0.0.0", 9432)

src/test/scala/org/bblfsh/client/v2/BblfshClientParseTest.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ class BblfshClientParseTest extends FlatSpec
5555
}
5656

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

62-
val encodedBytes: ByteBuffer = uast.encode(rootNode, 0)
62+
val encodedBytes: ByteBuffer = uastCtx.encode(rootNode)
6363

6464
encodedBytes.capacity should be (resp.uast.asReadOnlyByteBuffer.capacity)
6565
encodedBytes shouldEqual resp.uast.asReadOnlyByteBuffer

0 commit comments

Comments
 (0)