Function construct_ts_lang_from_shared_lib

Source
pub fn construct_ts_lang_from_shared_lib(
    language_name: &str,
    parser_path: &Path,
) -> Result<Language, LoadingError>
Expand description

Create a tree sitter Language from a shared library object.

This creates a memory leak by leaking the shared library that’s loaded from the file path (assuming that loading is succesful). This memory leak is necessary otherwise the program will segfault when trying to use the generated Language object with the tree-sitter library. The tree-sitter rust bindings wrap the tree-sitter C FFI interface, so the shared library object has to be loaded into memory while we want to use the Language object with any method in tree_sitter.

§Arguments

  • language_name: The tree-sitter language name.
  • parser_path: The path to the shared library object file.

§Errors

This will return an error if the file path doesn’t exist or if there’s an error trying to load symbols from the shared library object.

§Safety

This uses the libloading library to load symbols from the shared library object. This is inherently unsafe because it loads symbols from an arbitrary shared library object. Both the file path and the actual loaded symbol name can be generated from user input. This method does leak the shared library loaded with libloading to prevent segfaults because the parser loaded from the shared library may be used at any point for the duration of the program.