Skip to content

Commit

Permalink
remove javassist
Browse files Browse the repository at this point in the history
  • Loading branch information
kubukoz committed Oct 30, 2024
1 parent fc6ff5f commit df755d6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 34 deletions.
11 changes: 5 additions & 6 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ lazy val core = crossProject(JVMPlatform)
.jvmSettings(
commonJVMSettings,
libraryDependencies ++= Seq(
"net.java.dev.jna" % "jna" % "5.14.0",
"org.javassist" % "javassist" % "3.28.0-GA",
"net.java.dev.jna" % "jna" % "5.14.0"
),
)

Expand All @@ -85,10 +84,10 @@ lazy val tests = crossProject(JVMPlatform)
.settings(
commonSettings,
run / fork := true,
javaOptions ++= Seq(
"--add-opens",
"java.base/java.lang=ALL-UNNAMED",
),
// Test / javaOptions ++= Seq(
// "-Djna.debug_load=true",
// "-Djna.debug_load.jna=true",
// ),
)
.dependsOn(bindingsPython)
.jvmSettings(commonJVMSettings)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@

package org.polyvariant.treesitter4s.lowlevel

import org.polyvariant.treesitter4s.internal.TreeSitterLibrary
import com.sun.jna.*
import javassist.ClassPool
import javassist.CtMethod
import util.chaining.*
import javassist.CtClass
import org.polyvariant.treesitter4s.internal.TreeSitterLibrary

import java.util.concurrent.ConcurrentHashMap

object TreeSitterPlatform {
Expand All @@ -48,33 +45,20 @@ object TreeSitterPlatform {

type Language = org.polyvariant.treesitter4s.Language

private val languageMap = new ConcurrentHashMap[String, Language]

val Language: LanguageMethods =
new {
private val languageMap = new ConcurrentHashMap[String, Language]

def apply(languageName: String): Language = languageMap.computeIfAbsent(
def apply(
languageName: String
): Language = languageMap.computeIfAbsent(
languageName,
{ _ =>

val methodName = s"tree_sitter_$languageName"

val bindingsInterface = {
val pool = ClassPool.getDefault
val interfaceName = s"Bindings_$languageName"
val returnType = pool.get("org.polyvariant.treesitter4s.Language")

pool
.makeInterface(interfaceName)
.tap(_.addInterface(pool.get(classOf[Library].getName)))
.tap(owner =>
owner.addMethod(new CtMethod(returnType, methodName, Array(), owner))
)
.toClass
.asSubclass(classOf[Library])
}

val library = Native.load(s"tree-sitter-$languageName", bindingsInterface)
bindingsInterface.getMethod(methodName).invoke(library).asInstanceOf[Language]
_ => {
val library = NativeLibrary.getInstance("tree-sitter-python")

val function = library.getFunction("tree_sitter_python");
function.invoke(classOf[Language], Array()).asInstanceOf[Language]
},
)

Expand Down

0 comments on commit df755d6

Please sign in to comment.