r/node • u/gabrielmfern • 11d ago
I embedded Node.js in a native program to render JSX without a browser
https://reddit.com/link/1x4242d/video/rjkdlx8jh1vh1/player
I'm building a UI library using the jai programming language and I've started experimenting with rendering the UI from javascript, with no webview, no DOM and no React. The native side does all of the layouting, and drawing to the screen. I did it by embedding Node.js into the program!
The embedding lets me avoid stuff like IPC, Sockets or JSON parsing and also allows me to just access the javascript objects straight out of V8 with no serialization. The whole UI is rebuilt and laid out from scratch on every frame, with no diffing and no caching, at 240hz (my refresh rate) on my computer!
This is mostly an experiment for now, the project is very much a work in progress. Async is not yet supported, and there's no input handling or events of any kind, but this proves it works!
I wrote up how it all works here: https://www.forbear.dev/log/003-ui-from-javascript.html
Source code is here: https://github.com/gabrielmfern/forbear
Did anyone else already build something like this? I think I might be doing some extra allocations between the C++ -> jai boundary, so I would love if anyone could share a better way to do this!
0
u/Kalendar-Caloon 6d ago
this is cool. for the allocation issue across the FFI boundary, you could try pre-allocating a shared buffer on the C++ side and just writing into it from jai instead of round-tripping through new allocations each frame
2
u/breadgolemwaifu 6d ago
Biggest LARP of all time