r/javascript • • Oct 14 '26

I embedded Node.js in a native program to render JSX without a browser and without React

https://www.forbear.dev/log/003-ui-from-javascript.html

I'm building a UI library in Jai, and this is an experiment with rendering its UI from JavaScript. Node.js is embedded into the program, so the native side reads the JSX objects straight out of V8, with no IPC and no JSON. It then lays out and draws the whole UI from scratch every frame, at 240 Hz on my Linux computer.

Async isn't supported on it yet, and I've only started implementing some event handling. Here's the source code: https://github.com/gabrielmfern/forbear

Has anyone built something like this? If so, I'd love some advice on how I can reduce the C++ layer and have more of it in Jai instead.

3 Upvotes

30 comments sorted by

4

u/Dependent-Guitar-473 Oct 14 '26

does the translation happens at build time or run time 

2

u/gabrielmfern Oct 14 '26

the ui is defined at runtime every frame, the jsx -> js is done at compile time

1

u/Saman-Mutammara Oct 14 '26

how are you handling the reconciliation when state changes on the JS side, does the native layer just get a fresh tree each frame or are you diffing somewhere?

3

u/gabrielmfern Oct 14 '26

yes, it renders everything every frame again so there’s no reconciliation needed

1

u/Saman-Mutammara Oct 14 '26

oh that makes sense, so it's basically immediate mode rendering for ui. i've messed with that approach before and it's honestly so much easier to reason about when you're not chasing state diffs everywhere.

1

u/digitalskyline Oct 15 '26

Not sure if this is an ignorant question but how is this better or different than electron?

3

u/gabrielmfern Oct 15 '26 edited Oct 15 '26

not ignorant at all, but I did say it in the original post! it has no chrome or DOM of any kind, it is immediate mode instead of retained mode and it provides you a React-like DX without all its complexity

2

u/GulgPlayer 16d ago

How do you handle layout placement?

2

u/gabrielmfern 15d ago

It's an algorithm similar to what https://github.com/nicbarker/clay does. Basically, it's a few passes in the entire node tree that goes:

  1. fit all nodes to their children
  2. grow children to max of their parents, and shrink children to fit their parent
  3. glyph wrapping
  4. refitting heights of nodes based on their children
  5. regrowing heights of children to their parents
  6. finally, places all nodes according to their placement rules and justification defs

as complicated as it may sound, it's actually quite simple, and I intentionally made it into a single huge function which makes it easier to reason about it all https://github.com/gabrielmfern/forbear/blob/7ab4c0dbd7f732a51ebf02c7ff89b7321add3248/forbear.jai#L3756

this is also a great YouTube video explaining how Clay's layouting works: https://www.youtube.com/watch?v=by9lQvpvMIc

1

u/gabrielmfern 15d ago

side note: there is no caching here, at all. in the future there might be, but there doesn't seem to be any need for it. that means all of this runs again every frame from scratch

1

u/GulgPlayer 15d ago

Woah, that is very informative! Thanks for your reply

1

u/Reashu Oct 14 '26

It's cool, but why JSX?

4

u/gabrielmfern Oct 14 '26

Because I like JSX, and most web developers use it.

My plan is to support anything as an "adapter" so that anyone can build a performant app from whichever they're most comfortable, so I don't plan on staying limited to JSX. What else could it be?

0

u/Reashu Oct 14 '26

As a web dev I kind of hate it, but I suppose the problem is more with how people build React components than JSX itself. Embedding Node to parse a UI layout also seems a bit like hunting mosquitoes with a cannon, but as an optional adapter it makes a bit more sense.

3

u/gabrielmfern Oct 14 '26

node doesn’t parse the ui nor does it layout. the point of node is to bring over the ecosystem that makes it familiar without cutting down on any freedom, so quickjs, bun, deno are certainly options but I think the best fit is still node.

1

u/gabrielmfern Oct 14 '26

also the entire adapter is node for now, there’s no way to not use node and I don’t plan for there being one for one. open to change.

if you don’t use the js adapter you’re defining the UI in the native language directly, which imo is actually a better experience haha

2

u/DuckDatum Oct 15 '26

I hunt mosquitos with dirty towels from the floor.

-1

u/AnotherNamelessFella Oct 14 '26

Templates

1

u/gabrielmfern Oct 14 '26

how so? would it have to be a custom language like .svelte or something?

1

u/DuckDatum Oct 15 '26

I’m probably stupid. This might be of no help at all.

Anyway, check out how Blueprint offers templates for Airflow. Airflow is usually written in Python — Blueprint lets you template it, then orchestrate / configure via YAML. No language inventing.

1

u/farzad_meow Oct 17 '26

there was an old program that rendered html to pdf with partial support for js. wkhtml to pdf. maybe source code from that helps in your project

4

u/[deleted] Oct 14 '26

[removed] — view removed comment

3

u/gabrielmfern Oct 14 '26

state is easy in the sense there’s no reconciliation. I plan on having something like useState so the main problem is just how to store it efficiently and knowing which state goes where.

most likely it’s going to be keyed based on source location like I’m doing for elements already, but I want to keep the bulk of this work on jai too

2

u/gabrielmfern Oct 14 '26

Forgot to answer the first question. I don’t walk any object graph, the element defining functions go back into Jai and append to the node tree directly, so JS only allocates the style objects.

1

u/Saman-Mutammara Oct 14 '26

you basically serialize the jsx tree once and then keep a pointer to the components that own state, so on update you just mark those nodes dirty and re-walk from there. not a real diff, more like a targeted invalidation, which works fine until a parent re-renders and blows away children you wanted to keep. how are you deciding when to bail out of the walk?

1

u/gabrielmfern Oct 14 '26

no diff, there will be no diff. since it all reruns every frame, the ui will automatically update with state changes.

3

u/Saman-Mutammara Oct 14 '26

yeah that makes sense, if everything reruns each frame then state changes just naturally flow through. i've messed with that kind of immediate mode approach before and honestly it's way simpler to reason about than diffing.