Skip to content

Commit 58dda5e

Browse files
fix: use allocation functions through FFM
1 parent 3afd4fd commit 58dda5e

File tree

5 files changed

+20
-9
lines changed

5 files changed

+20
-9
lines changed

scripts/TreeSitter_java.patch

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,12 @@
2020

2121
public static final ValueLayout.OfBoolean C_BOOL = ValueLayout.JAVA_BOOLEAN;
2222
public static final ValueLayout.OfByte C_CHAR = ValueLayout.JAVA_BYTE;
23+
@@ -8599,4 +8606,8 @@ public class TreeSitter {
24+
throw new AssertionError("should not reach here", ex$);
25+
}
26+
}
27+
+
28+
+ static {
29+
+ ts_set_allocator(malloc$address(), calloc$address(), realloc$address(), free$address());
30+
+ }
31+
}

scripts/jextract.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ $lib = "$($args[0])/core/lib"
1515
--include-struct TSRange `
1616
--include-struct TSTreeCursor `
1717
--include-function free `
18+
--include-function malloc `
19+
--include-function calloc `
20+
--include-function realloc `
21+
--include-function ts_set_allocator `
1822
--include-function ts_language_copy `
1923
--include-function ts_language_delete `
2024
--include-function ts_language_field_count `

scripts/jextract.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ exec jextract \
1717
--include-struct TSRange \
1818
--include-struct TSTreeCursor \
1919
--include-function free \
20+
--include-function malloc \
21+
--include-function calloc \
22+
--include-function realloc \
23+
--include-function ts_set_allocator \
2024
--include-function ts_language_copy \
2125
--include-function ts_language_delete \
2226
--include-function ts_language_field_count \

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ public final class Node {
2323
private final Arena arena = Arena.ofAuto();
2424
private boolean wasEdited = false;
2525

26-
// FIXME: figure out why free() crashes on Windows
27-
private static final boolean IS_UNIX = !System.getProperty("os.name").startsWith("Windows");
28-
2926
Node(MemorySegment self, Tree tree) {
3027
this.self = self;
3128
this.tree = tree;
@@ -446,7 +443,7 @@ public TreeCursor walk() {
446443
public String toSexp() {
447444
var string = ts_node_string(self);
448445
var result = string.getString(0);
449-
if (IS_UNIX) free(string);
446+
free(string);
450447
return result;
451448
}
452449

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ public final class Tree implements AutoCloseable, Cloneable {
2222
private final Language language;
2323
private @Nullable List<Range> includedRanges;
2424

25-
// FIXME: figure out why free() crashes on Windows
26-
private static final boolean IS_UNIX = !System.getProperty("os.name").startsWith("Windows");
27-
2825
Tree(MemorySegment self, Language language, @Nullable String source, @Nullable Charset charset) {
2926
arena = Arena.ofShared();
3027
this.self = self.reinterpret(arena, TreeSitter::ts_tree_delete);
@@ -95,7 +92,7 @@ public List<Range> getIncludedRanges() {
9592
var range = TSRange.asSlice(ranges, i);
9693
includedRanges.add(Range.from(range));
9794
}
98-
if (IS_UNIX) free(ranges);
95+
free(ranges);
9996
}
10097
}
10198
return Collections.unmodifiableList(includedRanges);
@@ -122,7 +119,7 @@ public List<Range> getChangedRanges(Tree newTree) {
122119
var range = TSRange.asSlice(ranges, i);
123120
changedRanges.add(Range.from(range));
124121
}
125-
if (IS_UNIX) free(ranges);
122+
free(ranges);
126123
return changedRanges;
127124
}
128125
}

0 commit comments

Comments
 (0)