Class: TreeHaver::Backends::FFI::Tree
- Inherits:
-
Object
- Object
- TreeHaver::Backends::FFI::Tree
- Defined in:
- lib/tree_haver/backends/ffi.rb
Overview
FFI-based tree-sitter tree
Wraps a TSTree pointer and manages its lifecycle with a finalizer.
Note: Tree objects DO use finalizers (unlike Parser objects) because:
- Trees are typically short-lived and numerous (one per parse)
- ts_tree_delete is safer than ts_parser_delete during GC
- Memory leaks from accumulated trees are more problematic
- The finalizer silently ignores errors for safety
Class Method Summary collapse
-
.finalizer(ptr) ⇒ Proc
Returns a finalizer proc that deletes the tree.
Instance Method Summary collapse
-
#initialize(ptr) ⇒ Tree
constructor
private
A new instance of Tree.
-
#root_node ⇒ Node
Get the root node of the syntax tree.
Constructor Details
#initialize(ptr) ⇒ Tree
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Tree.
643 644 645 646 |
# File 'lib/tree_haver/backends/ffi.rb', line 643 def initialize(ptr) @ptr = ptr ObjectSpace.define_finalizer(self, self.class.finalizer(@ptr)) end |
Class Method Details
.finalizer(ptr) ⇒ Proc
Returns a finalizer proc that deletes the tree
This is public API for testing purposes, but not intended for
direct use. The finalizer is automatically registered when
creating a Tree object.
658 659 660 661 662 663 664 665 666 667 668 669 |
# File 'lib/tree_haver/backends/ffi.rb', line 658 def finalizer(ptr) proc { begin Native.ts_tree_delete(ptr) rescue StandardError # Silently ignore errors during finalization to prevent crashes # during GC. If the library is unloaded or ptr is invalid, we # don't want to crash the entire process. nil end } end |