Live data from Hacker News

Compile and Run C in JavaScript

bun.sh

21–30 of 31 posts

Re: Compile and Run C in JavaScript

#23

...So is this a transpiler of some sort? If it is, consider looking in Nim. It compiles to C, and even compiles to Javascript. And is extremely performant while at it.

Of some sort. The C is transpiled to machine code so that the processor can execute it. Turns out CPUs don't understand C natively.

In addition, it automatically generates wrappers to transparently convert between Javascript's memory model and C's memory model to seamlessly allow Javascript to call upon the C functions imported using it.

Just your usual FFI, really, but with less call overhead than the FFI specification defined by Node (N-API). Albeit with some different tradeoffs made in order to accomplish that.

Re: Compile and Run C in JavaScript

#24
post #18
post #17

Earlier quoted context omitted.

I would say that the problem really is there. Dealing with native dependencies and addons was almost always a pain as the article describes (and not just from developer perspective), so anything that helps there is really appreciated. Not sure what you mean by the right tools in this context.

First of all, this is for C, and most extensions are written in C++ or Rust nowadays. Secondly, the right tools are having Python, the C and C++ compiler, node-gyp and cmake.js installed, and actually understand how they work. But what do I know, nowadays folks use C and C++ as scripting languages putting a full library into a single header file to avoid learning how to use the compiler and linker.

I know how to compile and link C. I've never done CJS FFI but could probably figure it out. But if I have relatively small C code in single files, why bother? I'll take the easy route unless there's a clear reason not to.

The thing is, I'm not already using the Bun runtime and wouldn't switch just for this.

Re: Compile and Run C in JavaScript

#25

...So is this a transpiler of some sort? If it is, consider looking in Nim. It compiles to C, and even compiles to Javascript. And is extremely performant while at it.

Of some sort. The C is transpiled to machine code so that the processor can execute it. Turns out CPUs don't understand C natively. In addition, it automatically generates wrappers to transparently convert between Javascript's memory model and C's memory model to seamlessly allow Javascript to call upon the C functions imported using it. Just your usual FFI, really, but with less call overhead than the FFI specificat…

C is compiled to machine code.

Re: Compile and Run C in JavaScript

#26
post #2

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

There is something I don't understand about something. Why are individual solutions sought instead of nodes with an existing and large community? Why don't you pass bun's properties to Node?

Re: Compile and Run C in JavaScript

#27
post #25

Earlier quoted context omitted.

Of some sort. The C is transpiled to machine code so that the processor can execute it. Turns out CPUs don't understand C natively. In addition, it automatically generates wrappers to transparently convert between Javascript's memory model and C's memory model to seamlessly allow Javascript to call upon the C functions imported using it. Just your usual FFI, really, but with less call overhead than the FFI specificat…

C is compiled to machine code.

Yes, that's right. Compile and transpile are the same thing.

Re: Compile and Run C in JavaScript

#29
post #28

Earlier quoted context omitted.

Yes, that's right. Compile and transpile are the same thing.

Only for Javascript folks without CS background in compilers.

Yes, it is true that Javascript folks do seem to want to think they are different. But they can never figure out exactly what is different.

Re: Compile and Run C in JavaScript

#30
Great feature from the Bun team.

It's worth noting that similar functionality is what allowed PHP to be useful well beyond its original capabilities when companies hit scale and needed more performance out of it. Companies would just write a C module for their critical path code, and load it in as a PHP extension.

An article from Facebook in 2010 about them writing and using a custom C extension for PHP, that was probably the precursor to JSX:

https://www.facebook.com/notes/10158791323777200/

An article from Zend in 2011 about how to write a PHP extension in C:

http://web.archive.org/web/20110222035803/http://devzone.zen...

Post reply on HN