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

Commit

Permalink
Merge pull request #35 from juanjux/fix/libload
Browse files Browse the repository at this point in the history
Synchronized on the Libuast Object didn't work.
  • Loading branch information
juanjux authored Oct 20, 2017
2 parents fb6bc8e + 15834a5 commit 4154e90
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name := "bblfsh-client"
organization := "org.bblfsh"
version := "1.3.1"
version := "1.3.2"

scalaVersion := "2.11.11"
val libuastVersion = "v1.3.0"
Expand Down
27 changes: 15 additions & 12 deletions src/main/scala/org/bblfsh/client/libuast/Libuast.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,38 @@ import java.nio.file.Paths
import org.apache.commons.io.{IOUtils, FileUtils}

object Libuast {
var loaded = false
final var loaded = false

// Extract the native module from the jar
private def loadBinaryLib(name: String) = synchronized {
val tempDir = System.getProperty("java.io.tmpdir")
private final def loadBinaryLib(name: String) = {
val ext = if (System.getProperty("os.name").toLowerCase == "mac os x") ".dylib" else ".so"
val fullLibName = name + ext
val outPath = Paths.get(tempDir, fullLibName).toString

val in = getClass.getResourceAsStream(Paths.get("/lib", fullLibName).toString)
val fout = new File(outPath)

val prefix = "libscalauast_"
val fout = File.createTempFile(prefix, ext)
val out = FileUtils.openOutputStream(fout)

try {
IOUtils.copy(in, out)
System.load(outPath)
loaded = true
} finally {
in.close()
out.close()
}
}

System.load(fout.getAbsolutePath)
loaded = true
}
}

class Libuast {
private val libName = "libscalauast"
if (!Libuast.loaded) {
Libuast.loadBinaryLib(libName)
initialize()

// Note: moving this to the Object doesn't synchronize correctly
private def initialize() = Libuast.synchronized {
if (!Libuast.loaded) {
Libuast.loadBinaryLib("libscalauast")
}
}

@native def filter(node: Node, query: String): List[Node]
Expand Down

0 comments on commit 4154e90

Please sign in to comment.