Skip to content

Commit 8f19cd7

Browse files
fix(bindings): handle library lookup error
1 parent 0cc6aaa commit 8f19cd7

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/main/java/io/github/treesitter/jtreesitter/Language.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package io.github.treesitter.jtreesitter;
22

3-
import java.lang.foreign.*;
43
import static io.github.treesitter.jtreesitter.internal.TreeSitter.*;
54

65
import io.github.treesitter.jtreesitter.internal.TreeSitter;
7-
6+
import java.lang.foreign.*;
87
import org.jspecify.annotations.NullMarked;
98
import org.jspecify.annotations.Nullable;
109

@@ -23,7 +22,7 @@ public final class Language {
2322
public static final @Unsigned int MIN_COMPATIBLE_LANGUAGE_VERSION = TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION();
2423

2524
private static final ValueLayout VOID_PTR =
26-
ValueLayout.ADDRESS.withTargetLayout(MemoryLayout.sequenceLayout(Long.MAX_VALUE, ValueLayout.JAVA_BYTE));
25+
ValueLayout.ADDRESS.withTargetLayout(MemoryLayout.sequenceLayout(Long.MAX_VALUE, ValueLayout.JAVA_BYTE));
2726

2827
private static final FunctionDescriptor FUNC_DESC = FunctionDescriptor.of(VOID_PTR);
2928

src/test/java/io/github/treesitter/jtreesitter/languages/TreeSitterJava.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ public final class TreeSitterJava {
1111
private static final TreeSitterJava INSTANCE = new TreeSitterJava();
1212

1313
private final Arena arena = Arena.ofAuto();
14-
private final String library = System.mapLibraryName("tree-sitter-java");
15-
private final SymbolLookup symbols =
16-
SymbolLookup.libraryLookup(library, arena).or(SymbolLookup.loaderLookup());
14+
private final SymbolLookup symbols = findLibrary();
1715

1816
/**
1917
* {@snippet lang=c :
@@ -24,6 +22,15 @@ public static MemorySegment language() {
2422
return INSTANCE.call("tree_sitter_java");
2523
}
2624

25+
private SymbolLookup findLibrary() {
26+
try {
27+
var library = System.mapLibraryName("tree-sitter-java");
28+
return SymbolLookup.libraryLookup(library, arena);
29+
} catch (IllegalArgumentException e) {
30+
return SymbolLookup.loaderLookup();
31+
}
32+
}
33+
2734
private static UnsatisfiedLinkError unresolved(String name) {
2835
return new UnsatisfiedLinkError("Unresolved symbol: %s".formatted(name));
2936
}

0 commit comments

Comments
 (0)