Skip to content

Commit

Permalink
docs: add purego info
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Nov 24, 2024
1 parent 4d96fa6 commit ee9a3ad
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,30 @@ In the example above, to fetch the JavaScript grammar, you can run the following
go get github.com/tree-sitter/tree-sitter-javascript@latest
```

Alternatively you can also load grammars at runtime from a shared library via [purego](https://github.com/ebitengine/purego).

This demonstrates how to load the JavaScript grammar from a shared library (`libtree-sitter-name.so`) at runtime on Linux & macOS:

For more information on other platforms, see the [purego documentation](https://github.com/ebitengine/purego#supported-platforms)

```go
package main

import (
tree_sitter "github.com/tree-sitter/go-tree-sitter"
"github.com/ebitengine/purego"
)

func main() {
lib, err := purego.Dlopen(path, purego.RTLD_NOW|purego.RTLD_GLOBAL)

var newLanguage func() uintptr
purego.RegisterLibFunc(&newLanguage, "libtree-siiter-javascript.so", "tree_sitter_javascript")

language := tree_sitter.NewLanguage(unsafe.Pointer(newLanguage()))
}
```

Due to [bugs with `runtime.SetFinalizer` and CGO](https://groups.google.com/g/golang-nuts/c/LIWj6Gl--es), you must always call `Close`
on an object that allocates memory from C. This must be done for the `Parser`, `Tree`, `TreeCursor`, `Query`, `QueryCursor`, and `LookaheadIterator` objects.

Expand Down

0 comments on commit ee9a3ad

Please sign in to comment.