[HN Gopher] Compile and Run C in JavaScript
___________________________________________________________________
Compile and Run C in JavaScript
Author : ekosz
Score : 25 points
Date : 2024-09-18 16:20 UTC (6 hours ago)
(HTM) web link (bun.sh)
(TXT) w3m dump (bun.sh)
| Jarred wrote:
| This was an unplanned feature I worked on mostly a month ago on a
| Saturday for fun. Happy to answer any questions
|
| To get it out the door, ended up adding some patches to TinyCC to
| support .framework on macOS and fix a few things with dlopen and
| include paths. Also added support for parsing the deprecated
| attribute used in lots of Darwin headers. C parsers seem a lot
| simpler than JavaScript which is nice
| candleknight wrote:
| Awesome work on this, it looks like a game changer!
|
| From my (admittedly limited) knowledge on this space, it seems
| like this is a straight upgrade over WASM, with the only
| limiting factor being that your stack is limited to Bun + C.
| Are there any downsides of this feature when compared with the
| alternatives mentioned at the start of the article? There are
| some tradeoffs listed at the bottom but I'm not sure how
| napi/WASM perform in those aspects either.
| jll29 wrote:
| Cool - if this was an unplanned "accident" then I want to see
| how your planned projects look like.
| mati365 wrote:
| Lmao, I was first: https://github.com/Mati365/ts-c-compiler
| russfink wrote:
| I wonder about the security of this. Are programs executed in
| some kind of jail? Is there a limit to certain features, e.g.,
| opening a socket to somewhere?
| gyutff wrote:
| Is this local js only or can you compile the c code and serve it
| to a client to execute in the browser?
| seveibar wrote:
| I ran some benchmarks and got about a 10% improvement by porting
| a simple function used in autorouting to C. With the subfunction
| ported to C, Bun was still ~15% slower than node (v8). As the
| article hints at, you probably need to port fairly large
| subfunctions to see major performance gains. Results:
|
| Bun: 6.7410ms Bun FFI w/ C: 6.0413ms Node: 5.1307ms C only: 4.3ms
| (+- 1ms)
|
| I'm generally very bullish on Bun and was very happy with the DX
| for this C api. Great work to Jarred and the team!!!
|
| benchmark code: https://github.com/tscircuit/bun-ffi-benchmarking
___________________________________________________________________
(page generated 2024-09-18 23:01 UTC)