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
Signed-off-by: Alexander Bezzubov <bzz@apache.org>
  • Loading branch information
bzz committed Jul 4, 2019
1 parent 9fd1529 commit b149524
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
36 changes: 31 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,30 @@ jobject asJvmBuffer(uast::Buffer buf) {
return env->NewDirectByteBuffer(buf.ptr, buf.size);
}

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;
}

bool isContext(jobject obj, JNIEnv *env) {
if (!obj) return false;

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

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

return env->IsInstanceOf(obj, nodeCls) || env->IsInstanceOf(obj, jnodeCls);
}

// ==========================================
// External UAST Context (managed by libuast)
// ==========================================
Expand All @@ -76,7 +100,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 +129,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 +384,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 +446,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 +478,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 b149524

Please sign in to comment.