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

Commit fabffdd

Browse files
authored
Merge pull request #105 from bzz/fix-encoding-bool
Fix encoding for JUint
2 parents 5668ebd + 6207790 commit fabffdd

File tree

3 files changed

+44
-32
lines changed

3 files changed

+44
-32
lines changed

src/main/native/org_bblfsh_client_v2_libuast_Libuast.cc

+7-7
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ class Node : public uast::Node<Node *> {
164164
} else if (env->IsInstanceOf(obj, env->FindClass(CLS_JBOOL))) {
165165
return NODE_BOOL;
166166
} else if (env->IsInstanceOf(obj, env->FindClass(CLS_JUINT))) {
167-
return NODE_BOOL;
167+
return NODE_UINT;
168168
} else if (env->IsInstanceOf(obj, env->FindClass(CLS_JARR))) {
169169
return NODE_ARRAY;
170170
}
@@ -449,7 +449,7 @@ class Context {
449449
}
450450
// toNode returns a node associated with a JVM object.
451451
// Returns a new reference.
452-
Node *toNode(jobject obj) { return iface->lookupOrCreate(obj); }
452+
Node *toNode(jobject jnode) { return iface->lookupOrCreate(jnode); }
453453

454454
public:
455455
Context() {
@@ -475,10 +475,10 @@ class Context {
475475

476476
// Encode serializes UAST.
477477
// Creates a new reference.
478-
jobject Encode(jobject node, UastFormat format) {
479-
if (!assertNotContext(node)) return nullptr;
478+
jobject Encode(jobject jnode, UastFormat format) {
479+
if (!assertNotContext(jnode)) return nullptr;
480480

481-
Node *n = toNode(node);
481+
Node *n = toNode(jnode);
482482
uast::Buffer data = ctx->Encode(n, format);
483483
return asJvmBuffer(data);
484484
}
@@ -547,11 +547,11 @@ JNIEXPORT jobject JNICALL Java_org_bblfsh_client_v2_libuast_Libuast_filter(
547547
// ==========================================
548548

549549
JNIEXPORT jobject JNICALL Java_org_bblfsh_client_v2_Context_encode(
550-
JNIEnv *env, jobject self, jobject node) {
550+
JNIEnv *env, jobject self, jobject jnode) {
551551
UastFormat fmt = UAST_BINARY; // TODO(bzz): make it argument
552552

553553
Context *p = getHandle<Context>(env, self, nativeContext);
554-
return p->Encode(node, fmt);
554+
return p->Encode(jnode, fmt);
555555
}
556556

557557
JNIEXPORT jlong JNICALL

src/test/resources/python_file.py

+4-23
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,6 @@
1-
import os
2-
import sys
1+
#!/usr/bin/env python
32

3+
from __future__ import print_function
44

5-
class BblfshClient(object):
6-
"""
7-
Babelfish gRPC client. Currently it is only capable of fetching UASTs.
8-
"""
9-
10-
def __init__(self, endpoint):
11-
"""
12-
Initializes a new instance of BblfshClient.
13-
14-
:param endpoint: The address of the Babelfish server, \
15-
for example "0.0.0.0:9432"
16-
:type endpoint: str
17-
"""
18-
19-
def parse(self, filename, language=None, contents=None, timeout=None,
20-
unicode_errors="ignore"):
21-
request = ParseRequest(filename=os.path.basename(filename),
22-
content=contents,
23-
language=language)
24-
response = self._stub.Parse(request, timeout=timeout)
25-
return response
5+
if __name__ == '__main__':
6+
print('hello world')

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

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

33
import java.nio.ByteBuffer
4-
4+
import scala.io.Source
55

66
class BblfshClientParseTest extends BblfshClientBaseTest {
77

@@ -34,7 +34,7 @@ class BblfshClientParseTest extends BblfshClientBaseTest {
3434
uast.dispose()
3535
}
3636

37-
"Encoding back the RootNode of decoded UAST" should "produce same bytes" in {
37+
"Encoding UAST to the same ContextExt" should "produce the same bytes" in {
3838
val uastCtx: ContextExt = resp.uast.decode()
3939
val rootNode: NodeExt = uastCtx.root()
4040
println(s"Root node: $rootNode")
@@ -48,4 +48,35 @@ class BblfshClientParseTest extends BblfshClientBaseTest {
4848
println(encodedBytes)
4949
}
5050

51+
"Encoding java UAST to a new Context" should "produce the same bytes" in {
52+
val node = resp.get
53+
54+
val encodedBytes = node.toByteBuffer
55+
56+
val nodeEncodedDecoded = JNode.parseFrom(encodedBytes)
57+
nodeEncodedDecoded shouldEqual node
58+
59+
encodedBytes.capacity should be(resp.uast.asReadOnlyByteBuffer.capacity)
60+
encodedBytes shouldEqual resp.uast.asReadOnlyByteBuffer
61+
}
62+
63+
64+
"Encoding python UAST to a new Context" should "produce the same bytes" in {
65+
val client = BblfshClient("localhost", 9432)
66+
val fileName = "src/test/resources/python_file.py"
67+
val fileContent = Source.fromFile(fileName).getLines.mkString("\n")
68+
val resp = client.parse(fileName, fileContent)
69+
val node = resp.get
70+
71+
// when
72+
val encodedBytes = node.toByteBuffer
73+
74+
val nodeEncodedDecoded = JNode.parseFrom(encodedBytes)
75+
nodeEncodedDecoded shouldEqual node
76+
77+
encodedBytes.capacity should be(resp.uast.asReadOnlyByteBuffer.capacity)
78+
encodedBytes shouldEqual resp.uast.asReadOnlyByteBuffer
79+
}
80+
81+
5182
}

0 commit comments

Comments
 (0)