node-tree-sitter icon indicating copy to clipboard operation
node-tree-sitter copied to clipboard

Tree-sitter in the render process of electron; how I got it working, and some questions on my decision process

Open guidocalvano opened this issue 2 years ago • 0 comments

Electron links the chrome browser to a node instance so you can create desktop applications. For security it has two (or maybe more) processes, executing two separate node instances:

  1. the main process
  2. the render process

These processes do not share memory so communication between them might cause delays, also because data must be serialized and cannot be directly used to update views. This is why I'd prefer having tree-sitter inside the render process.

Because of optimization and security reasons modules in electron's render process must be instantiateable multiple times in multiple contexts. This means it must be "context aware". Making a module context aware is generally just a matter of using different macros to construct the module. Only if memory must be deallocated is there a potential problem.

The code changes to do this can be found here for both node-tree-sitter and tree-sitter-python:

  • https://github.com/guidocalvano/node-tree-sitter/tree/feature/context_aware
  • https://github.com/guidocalvano/tree-sitter-python/tree/feature/context_aware

QUESTION: I assumed that no memory needs to be deallocated. Is this correct? Are my modifications ok? Would it be an idea to put this into an npm package?

The module must also be compiled against the same version of the node api. Getting the exact same node instance to compile against is impossible, because electron creates its own node versions. This means that you must use a tool called electron-rebuild to convert your node module into an electron node module.

npx electron-rebuild -f -w tree-sitter npx electron-rebuild -f -w tree-sitter-python

Tree-sitter uses constructs that are no longer used by the most recent version of electron. So it is necessary to use electron at version 19.. Installing this electron will make electron rebuild use the right c bindings. Don't forget to start this version of electron with npx electron yourelectronmain.js. If you don't you will use the old version and your modules won't register.

Finally, the tree-sitter commandline application must be used to generate the c code + bindings necessary to create the language module. I brew installed tree-sitter. This worked. However, this sets the language version to 14. This will create problems with the node-tree-sitter module, which expects language version 13. So instead of just running tree-sitter generate I ran tree-sitter generate --abi 13.

guidocalvano avatar Nov 03 '22 15:11 guidocalvano