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

Conversation

odenix
Copy link

@odenix odenix commented Dec 6, 2024

To demonstrate that this PR solves a real problem, I've updated pkl-lsp here to no longer patch and build java-tree-sitter from source.

Motivation:

  • Enable clients to customize the SymbolLookup used for the tree-sitter native library. For example, clients can use this capability to embed the tree-sitter native library in their JAR, which is a common way to solve the native library distribution problem.

Changes:

  • Add interface NativeLibraryLookup.
  • Add class ChainedLibraryLookup, which loads NativeLibraryLookup implementations using java.util.ServiceLoader and chains the SymbolLookup's returned by them.
  • Change TreeSitter.java patch to delegate to ChainedLibraryLookup.
  • Update build instructions in README.
  • Fix execution of jextract.ps1 script in Windows build.

Result:

  • Clients that need to customize the SymbolLookup used for the tree-sitter native library no longer need to patch and build java-tree-sitter from source.

Motivation:
- Enable clients to customize the SymbolLookup used for the tree-sitter native library.
  For example, clients can use this capability to embed the tree-sitter native library
  in their JAR, which is a common way to solve the native library distribution problem.

Changes:
- Add interface NativeLibraryLookup.
- Add class ChainedLibraryLookup, which loads NativeLibraryLookup implementations
  using java.util.ServiceLoader and chains the SymbolLookup's returned by them.
- Change TreeSitter.java patch to delegate to ChainedLibraryLookup.
- Update build instructions in README.
- Fix execution of jextract.ps1 script in Windows build.

Result:
- Clients that need to customize the SymbolLookup used for the tree-sitter native library
  no longer need to patch and build java-tree-sitter from source.
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
pom.xml Outdated Show resolved Hide resolved
scripts/TreeSitter_java.patch Outdated Show resolved Hide resolved
@ObserverOfTime
Copy link
Member

I would appreciate your thoughts on this. @Marcono1234 @bioball

Copy link
Contributor

@Marcono1234 Marcono1234 left a comment

Choose a reason for hiding this comment

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

To me this ServiceLoader approach looks quite useful. One small suggestion: It might be nice if NativeLibraryLookup#get could permit users to use their own Arena. (With the current implementation it seems that is possible, but it would be good if it was officially documented as supported.)

For example something like this:

    /*
     * ...
     * @param arena an arena for the symbol lookup; library lookups can instead
     *     at their own risk use their own arena, but must make sure it is not
     *     closed while tree-sitter is still in use
     * ...
     */
    SymbolLookup get(Arena arena);

For Windows this could be useful when loading the library from a temp file, because you can only delete the temp file when the library is unloaded (by calling Arena#close, so you need to provide your own Arena).

Would that be ok @ObserverOfTime?

Comment on lines +24 to +27
var library = System.mapLibraryName("tree-sitter");
return SymbolLookup.libraryLookup(library, arena);
} catch (IllegalArgumentException e) {
return SymbolLookup.loaderLookup();
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?

@ObserverOfTime ObserverOfTime force-pushed the master branch 3 times, most recently from ab995e8 to 7369f7f Compare December 8, 2024 12:53
@bioball
Copy link

bioball commented Dec 20, 2024

This looks great! This is basically what I was looking for when I opened #36.

@Emmeral
Copy link

Emmeral commented Jan 3, 2025

Any updates on when this will be merged?

@ObserverOfTime
Copy link
Member

After tree-sitter 0.25 is released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants