Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support customizing SymbolLookup for tree-sitter native library #61

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ Java bindings to the [tree-sitter] parsing library.
git clone https://github.com/tree-sitter/java-tree-sitter
cd java-tree-sitter
git submodule init
git submodule update
ObserverOfTime marked this conversation as resolved.
Show resolved Hide resolved
# build tree-sitter and tree-sitter-java native libraries,
# for example by replicating the steps in .github/workflows/ci.yml
ObserverOfTime marked this conversation as resolved.
Show resolved Hide resolved
mvn test
```

Expand Down
3 changes: 2 additions & 1 deletion pom.xml
ObserverOfTime marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@
<skip>${jextract.skip}</skip>
<target name="jextract">
<echo level="info">Generating sources using jextract</echo>
<exec osfamily="windows" executable="${project.basedir}/scripts/jextract.ps1" failonerror="true">
<exec osfamily="windows" executable="powershell" failonerror="true">
<arg value="${project.basedir}/scripts/jextract.ps1"/>
<arg value="${project.basedir}"/>
<arg value="${project.build.directory}"/>
</exec>
Expand Down
21 changes: 6 additions & 15 deletions scripts/TreeSitter_java.patch
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
--- a/generated-sources/jextract/io/github/treesitter/jtreesitter/internal/TreeSitter.java
+++ b/generated-sources/jextract/io/github/treesitter/jtreesitter/internal/TreeSitter.java
@@ -55,9 +55,16 @@ public class TreeSitter {
--- a/target/generated-sources/jextract/io/github/treesitter/jtreesitter/internal/TreeSitter.java
+++ b/target/generated-sources/jextract/io/github/treesitter/jtreesitter/internal/TreeSitter.java
@@ -55,9 +55,7 @@
ObserverOfTime marked this conversation as resolved.
Show resolved Hide resolved
};
}

- static final SymbolLookup SYMBOL_LOOKUP = SymbolLookup.libraryLookup(System.mapLibraryName("tree-sitter"), LIBRARY_ARENA)
- .or(SymbolLookup.loaderLookup())
- .or(Linker.nativeLinker().defaultLookup());
+ static final SymbolLookup SYMBOL_LOOKUP = findLibrary().or(Linker.nativeLinker().defaultLookup());
+
+ private static final SymbolLookup findLibrary() {
+ try {
+ String library = System.mapLibraryName("tree-sitter");
+ return SymbolLookup.libraryLookup(library, LIBRARY_ARENA);
+ } catch (IllegalArgumentException e) {
+ return SymbolLookup.loaderLookup();
+ }
+ }

+ static final SymbolLookup SYMBOL_LOOKUP = new ChainedLibraryLookup().get(LIBRARY_ARENA);

public static final ValueLayout.OfBoolean C_BOOL = ValueLayout.JAVA_BOOLEAN;
public static final ValueLayout.OfByte C_CHAR = ValueLayout.JAVA_BYTE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.github.treesitter.jtreesitter;

import java.lang.foreign.Arena;
import java.lang.foreign.SymbolLookup;

/**
* An interface implemented by clients that wish to customize the {@link SymbolLookup} used for the tree-sitter
* native library. Implementation classes must be registered by listing their fully qualified class name
* in a resource file named {@code META-INF/services/io.github.treesitter.jtreesitter.NativeLibraryLookup}.
*
* @see java.util.ServiceLoader
*/
public interface NativeLibraryLookup {
ObserverOfTime marked this conversation as resolved.
Show resolved Hide resolved
/**
* Returns the {@link SymbolLookup} to be used for the tree-sitter native library.
*
* @param arena an arena for the symbol lookup
* @return the {@link SymbolLookup} to be used for the tree-sitter native library
*/
SymbolLookup get(Arena arena);
}
ObserverOfTime marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.github.treesitter.jtreesitter.internal;

import io.github.treesitter.jtreesitter.NativeLibraryLookup;
import java.lang.foreign.Arena;
import java.lang.foreign.Linker;
import java.lang.foreign.SymbolLookup;
import java.util.Optional;
import java.util.ServiceLoader;

final class ChainedLibraryLookup implements NativeLibraryLookup {
@Override
public SymbolLookup get(Arena arena) {
var serviceLoader = ServiceLoader.load(NativeLibraryLookup.class);
SymbolLookup lookup = (name) -> Optional.empty();
for (var libraryLookup : serviceLoader) {
lookup = lookup.or(libraryLookup.get(arena));
}
return lookup.or(findLibrary(arena)).or(Linker.nativeLinker().defaultLookup());
ObserverOfTime marked this conversation as resolved.
Show resolved Hide resolved
}

private SymbolLookup findLibrary(Arena arena) {
try {
String library = System.mapLibraryName("tree-sitter");
ObserverOfTime marked this conversation as resolved.
Show resolved Hide resolved
return SymbolLookup.libraryLookup(library, arena);
} catch (IllegalArgumentException e) {
return SymbolLookup.loaderLookup();
}
}
}
Loading