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

Commit

Permalink
address review feedback
Browse files Browse the repository at this point in the history
 - add new runtime sanity-checks
 - better doc
 - remoced '== null' style comparison

Signed-off-by: Alexander Bezzubov <bzz@apache.org>
  • Loading branch information
bzz committed Jul 4, 2019
1 parent 9fd1529 commit 54ea17b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
34 changes: 29 additions & 5 deletions src/main/native/org_bblfsh_client_v2_libuast_Libuast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,28 @@ jobject asJvmBuffer(uast::Buffer buf) {
return env->NewDirectByteBuffer(buf.ptr, buf.size);
}

// Checks if a given object is of Node/JNode class
bool isContext(jobject obj, JNIEnv *env) {
if (!obj) return false;

jclass ctxCls = env->FindClass(CLS_CTX);
checkJvmException("failed to find class " + std::string(CLS_CTX));

return env->IsInstanceOf(obj, ctxCls);
}

bool assertNotContext(jobject obj) {
JNIEnv *env = getJNIEnv();
if (isContext(obj, env)) {
auto reCls = env->FindClass(CLS_RE);
checkJvmException("failed to find class " + std::string(CLS_RE));

env->ThrowNew(reCls, "cannot use UAST Context as a Node");
return false;
}
return true;
}

// ==========================================
// External UAST Context (managed by libuast)
// ==========================================
Expand All @@ -76,7 +98,9 @@ class ContextExt {
checkJvmException("failed to find class " + std::string(CLS_NODE));

if (!env->IsInstanceOf(obj, cls)) {
const char *err = "ContextExt.toHandle() called not on Node type";
auto err = std::string("ContextExt.toHandle() called not on")
.append(CLS_NODE)
.append(" type");
ctx->SetError(err);
return 0;
}
Expand All @@ -103,7 +127,7 @@ class ContextExt {
// Encode serializes the external UAST.
// Borrows the reference.
jobject Encode(jobject node, UastFormat format) {
// if (!assertNotContext(node)) return nullptr;
if (!assertNotContext(node)) return nullptr;

uast::Buffer data = ctx->Encode(toHandle(node), format);
return asJvmBuffer(data);
Expand Down Expand Up @@ -358,7 +382,7 @@ class Interface : public uast::NodeCreator<Node *> {
// toJ returns a JVM object associated with a node.
// Returns a new reference.
jobject toJ(Node *node) {
if (node == nullptr) return nullptr;
if (!node) return nullptr;
jobject obj = getJNIEnv()->NewGlobalRef(node->obj);
return obj;
}
Expand Down Expand Up @@ -420,7 +444,7 @@ class Context {
// toJ returns a JVM object associated with a node.
// Returns a new reference.
jobject toJ(Node *node) {
if (node == nullptr) return nullptr;
if (!node) return nullptr;
return iface->toJ(node);
}
// toNode returns a node associated with a JVM object.
Expand Down Expand Up @@ -452,7 +476,7 @@ class Context {
// Encode serializes UAST.
// Creates a new reference.
jobject Encode(jobject node, UastFormat format) {
// if (!assertNotContext(node)) return nullptr;
if (!assertNotContext(node)) return nullptr;

Node *n = toNode(node);
uast::Buffer data = ctx->Encode(n, format);
Expand Down
8 changes: 4 additions & 4 deletions src/main/scala/org/bblfsh/client/v2/ContextExt.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package org.bblfsh.client.v2
import java.nio.ByteBuffer

/**
* pyuast.ContextExt
*
* Represents Go-side results of Libuast.decode()
*
* This is equivalent of pyuast.ContextExt API
*/
case class ContextExt(nativeContext: Long) {
@native def root(): Node
Expand All @@ -21,9 +21,9 @@ case class ContextExt(nativeContext: Long) {


/**
* pyuast.Context
* Represents JVM-side constructed tree
*
* Represents JVM-side constructed tree.
* This is equivalent of pyuast.Context API
*/
case class Context(nativeContext: Long) {
@native def root(): JNode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,24 @@ class BblfshClientLoadTest extends BblfshClientBaseTest {
}

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

println(s"Loading $rootNode")
val root = rootNode.load()

val ctx = Context()
val data = ctx.encode(root)

data should equal (resp.uast.asReadOnlyByteBuffer())

// decode -> load -> encoded -> decode -> load, and compare trees
val uast2 = BblfshClient.decode(data)
val rootNode2: Node = uast2.root()
println(s"Loading $rootNode2")
val root2 = rootNode2.load()
root2 should equal (root)

data should equal (resp.uast.asReadOnlyByteBuffer())
// data.compareTo(resp.uast.asReadOnlyByteBuffer()) shouldBe 0
root2 should equal (root)
}

}

0 comments on commit 54ea17b

Please sign in to comment.