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 all commits
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: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ Java bindings to the [tree-sitter] parsing library.

- Install JDK 22 and set `JAVA_HOME` to it
- Download [jextract] and add it to your `PATH`
- Install the `tree-sitter` & `tree-sitter-java` libraries

```bash
git clone https://github.com/tree-sitter/java-tree-sitter
cd java-tree-sitter
git submodule init
git submodule update --init
mvn test
```

Expand Down
17 changes: 4 additions & 13 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 {
@@ -55,9 +55,7 @@
};
}

- 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,22 @@
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
*/
@FunctionalInterface
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,30 @@
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);
// NOTE: can't use _ because of palantir/palantir-java-format#934
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 static SymbolLookup findLibrary(Arena arena) {
try {
var library = System.mapLibraryName("tree-sitter");
return SymbolLookup.libraryLookup(library, arena);
} catch (IllegalArgumentException e) {
return SymbolLookup.loaderLookup();
Comment on lines +24 to +27
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As side note: This fallback with SymbolLookup.loaderLookup() probably still requires that the users call either System#loadLibrary or System#load, and even then only System#loadLibrary is affected by java.library.path.

So maybe it would be good to update the package Javadoc (which lists requirements and shows usage) to

  • mention System#loadLibrary or System#load
    (and maybe not even mention java.library.path)
  • also mention the new NativeLibraryLookup interface, otherwise it might be easy to miss

What do you think @ObserverOfTime?

}
}
}