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

Commit 54ea17b

Browse files
committed
address review feedback
- add new runtime sanity-checks - better doc - remoced '== null' style comparison Signed-off-by: Alexander Bezzubov <[email protected]>
1 parent 9fd1529 commit 54ea17b

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

src/main/native/org_bblfsh_client_v2_libuast_Libuast.cc

+29-5
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,28 @@ jobject asJvmBuffer(uast::Buffer buf) {
5050
return env->NewDirectByteBuffer(buf.ptr, buf.size);
5151
}
5252

53+
// Checks if a given object is of Node/JNode class
54+
bool isContext(jobject obj, JNIEnv *env) {
55+
if (!obj) return false;
56+
57+
jclass ctxCls = env->FindClass(CLS_CTX);
58+
checkJvmException("failed to find class " + std::string(CLS_CTX));
59+
60+
return env->IsInstanceOf(obj, ctxCls);
61+
}
62+
63+
bool assertNotContext(jobject obj) {
64+
JNIEnv *env = getJNIEnv();
65+
if (isContext(obj, env)) {
66+
auto reCls = env->FindClass(CLS_RE);
67+
checkJvmException("failed to find class " + std::string(CLS_RE));
68+
69+
env->ThrowNew(reCls, "cannot use UAST Context as a Node");
70+
return false;
71+
}
72+
return true;
73+
}
74+
5375
// ==========================================
5476
// External UAST Context (managed by libuast)
5577
// ==========================================
@@ -76,7 +98,9 @@ class ContextExt {
7698
checkJvmException("failed to find class " + std::string(CLS_NODE));
7799

78100
if (!env->IsInstanceOf(obj, cls)) {
79-
const char *err = "ContextExt.toHandle() called not on Node type";
101+
auto err = std::string("ContextExt.toHandle() called not on")
102+
.append(CLS_NODE)
103+
.append(" type");
80104
ctx->SetError(err);
81105
return 0;
82106
}
@@ -103,7 +127,7 @@ class ContextExt {
103127
// Encode serializes the external UAST.
104128
// Borrows the reference.
105129
jobject Encode(jobject node, UastFormat format) {
106-
// if (!assertNotContext(node)) return nullptr;
130+
if (!assertNotContext(node)) return nullptr;
107131

108132
uast::Buffer data = ctx->Encode(toHandle(node), format);
109133
return asJvmBuffer(data);
@@ -358,7 +382,7 @@ class Interface : public uast::NodeCreator<Node *> {
358382
// toJ returns a JVM object associated with a node.
359383
// Returns a new reference.
360384
jobject toJ(Node *node) {
361-
if (node == nullptr) return nullptr;
385+
if (!node) return nullptr;
362386
jobject obj = getJNIEnv()->NewGlobalRef(node->obj);
363387
return obj;
364388
}
@@ -420,7 +444,7 @@ class Context {
420444
// toJ returns a JVM object associated with a node.
421445
// Returns a new reference.
422446
jobject toJ(Node *node) {
423-
if (node == nullptr) return nullptr;
447+
if (!node) return nullptr;
424448
return iface->toJ(node);
425449
}
426450
// toNode returns a node associated with a JVM object.
@@ -452,7 +476,7 @@ class Context {
452476
// Encode serializes UAST.
453477
// Creates a new reference.
454478
jobject Encode(jobject node, UastFormat format) {
455-
// if (!assertNotContext(node)) return nullptr;
479+
if (!assertNotContext(node)) return nullptr;
456480

457481
Node *n = toNode(node);
458482
uast::Buffer data = ctx->Encode(n, format);

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package org.bblfsh.client.v2
33
import java.nio.ByteBuffer
44

55
/**
6-
* pyuast.ContextExt
7-
*
86
* Represents Go-side results of Libuast.decode()
7+
*
8+
* This is equivalent of pyuast.ContextExt API
99
*/
1010
case class ContextExt(nativeContext: Long) {
1111
@native def root(): Node
@@ -21,9 +21,9 @@ case class ContextExt(nativeContext: Long) {
2121

2222

2323
/**
24-
* pyuast.Context
24+
* Represents JVM-side constructed tree
2525
*
26-
* Represents JVM-side constructed tree.
26+
* This is equivalent of pyuast.Context API
2727
*/
2828
case class Context(nativeContext: Long) {
2929
@native def root(): JNode

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,24 @@ class BblfshClientLoadTest extends BblfshClientBaseTest {
5353
}
5454

5555
"Decoding, loading & encoding to different context" should "produce the same results" in {
56+
// decode -> load -> encode, and compare bytes
5657
val uast = resp.uast.decode()
5758
val rootNode: Node = uast.root()
5859

5960
println(s"Loading $rootNode")
6061
val root = rootNode.load()
61-
6262
val ctx = Context()
6363
val data = ctx.encode(root)
6464

65+
data should equal (resp.uast.asReadOnlyByteBuffer())
66+
67+
// decode -> load -> encoded -> decode -> load, and compare trees
6568
val uast2 = BblfshClient.decode(data)
6669
val rootNode2: Node = uast2.root()
6770
println(s"Loading $rootNode2")
6871
val root2 = rootNode2.load()
69-
root2 should equal (root)
7072

71-
data should equal (resp.uast.asReadOnlyByteBuffer())
72-
// data.compareTo(resp.uast.asReadOnlyByteBuffer()) shouldBe 0
73+
root2 should equal (root)
7374
}
7475

7576
}

0 commit comments

Comments
 (0)