'Compile and Run C in JavaScript', Promises Bun (thenewstack.io) 54
The JavaScript runtime Bun is a Node.js/Deno alternative (that's also a bundler/test runner/package manager).
And Bun 1.1.28 now includes experimental support for ">compiling and running native C from JavaScript, according to this report from The New Stack: "From compression to cryptography to networking to the web browser you're reading this on, the world runs on C," wrote Jarred Sumner, creator of Bun. "If it's not written in C, it speaks the C ABI (C++, Rust, Zig, etc.) and is available as a C library. C and the C ABI are the past, present, and future of systems programming." This is a low-boilerplate way to use C libraries and system libraries from JavaScript, he said, adding that this feature allows the same project that runs JavaScript to also run C without a separate build step... "It's good for glue code that binds C or C-like libraries to JavaScript. Sometimes, you want to use a C library or system API from JavaScript, and that library was never meant to be used from JavaScript," Sumner added.
It's currently possible to achieve this by compiling to WebAssembly or writing a N-API (napi) addon or V8 C++ API library addon, the team explained. But both are suboptimal... WebAssembly can do this but its isolated memory model comes with serious tradeoffs, the team wrote, including an inability to make system calls and a requirement to clone everything. "Modern processors support about 280 TB of addressable memory (48 bits). WebAssembly is 32-bit and can only access its own memory," Sumner wrote. "That means by default, passing strings and binary data JavaScript WebAssembly must clone every time. For many projects, this negates any performance gain from leveraging WebAssembly."
The latest version of Bun, released Friday, builds on this by adding N-API (nap) support to cc [Bun's C compiler, which uses TinyCC to compile the C code]. "This makes it easier to return JavaScript strings, objects, arrays and other non-primitive values from C code," wrote Sumner. "You can continue to use types like int, float, double to send & receive primitive values from C code, but now you can also use N-API types! Also, this works when using dlopen to load shared libraries with bun:ffi (such as Rust or C++ libraries with C ABI exports)....
"TinyCC compiles to decently performant C, but it won't do advanced optimizations that Clang or GCC does like autovectorization or very specialized CPU instructions," Sumner wrote. "You probably won't get much of a performance gain from micro-optimizing small parts of your codebase through C, but happy to be proven wrong!"
And Bun 1.1.28 now includes experimental support for ">compiling and running native C from JavaScript, according to this report from The New Stack: "From compression to cryptography to networking to the web browser you're reading this on, the world runs on C," wrote Jarred Sumner, creator of Bun. "If it's not written in C, it speaks the C ABI (C++, Rust, Zig, etc.) and is available as a C library. C and the C ABI are the past, present, and future of systems programming." This is a low-boilerplate way to use C libraries and system libraries from JavaScript, he said, adding that this feature allows the same project that runs JavaScript to also run C without a separate build step... "It's good for glue code that binds C or C-like libraries to JavaScript. Sometimes, you want to use a C library or system API from JavaScript, and that library was never meant to be used from JavaScript," Sumner added.
It's currently possible to achieve this by compiling to WebAssembly or writing a N-API (napi) addon or V8 C++ API library addon, the team explained. But both are suboptimal... WebAssembly can do this but its isolated memory model comes with serious tradeoffs, the team wrote, including an inability to make system calls and a requirement to clone everything. "Modern processors support about 280 TB of addressable memory (48 bits). WebAssembly is 32-bit and can only access its own memory," Sumner wrote. "That means by default, passing strings and binary data JavaScript WebAssembly must clone every time. For many projects, this negates any performance gain from leveraging WebAssembly."
The latest version of Bun, released Friday, builds on this by adding N-API (nap) support to cc [Bun's C compiler, which uses TinyCC to compile the C code]. "This makes it easier to return JavaScript strings, objects, arrays and other non-primitive values from C code," wrote Sumner. "You can continue to use types like int, float, double to send & receive primitive values from C code, but now you can also use N-API types! Also, this works when using dlopen to load shared libraries with bun:ffi (such as Rust or C++ libraries with C ABI exports)....
"TinyCC compiles to decently performant C, but it won't do advanced optimizations that Clang or GCC does like autovectorization or very specialized CPU instructions," Sumner wrote. "You probably won't get much of a performance gain from micro-optimizing small parts of your codebase through C, but happy to be proven wrong!"
For some reason (Score:5, Funny)
All these shenanigans attempting to run one language inside or along with another reminds me of the English language [imgur.com].
Clang (Score:3)
Didn't they do Clang 'c' compile to three address code and then cross compile this to web assembly 5 or more years ago.
Emulators of popular old school consoles in the browser.
insanity (Score:5, Insightful)
Webassembly with js as the glue is the way to go. All this cross compiling is insanity. JS shouldn't be making anything huge and certainly should not be treated like it is a virtual machine.
Re: (Score:2)
This is really how javascript devs think. Once your "stack" is 8 deep and you're still targetting only one language and only work in one browser, one begins to wonder.
Re: insanity (Score:3)
Webassembly is java like bytecode in a fancy dress. I really dont get the fuss. Craplets failed first time around, they'll fail 2nd time too.
Re: (Score:3)
Webassembly is just pre-parsed javascript.
And there are plenty of C compilers to Webassembly. Because there are plenty of C programs running under Webassembly already.
Many of them you already know. Like DOSBox, MAME and a few others are already running great on Webassembly, as demonstrated by the Internet Archive's playable DOS, console and arcade games that just run o
Re: (Score:3)
Webassembly is just pre-parsed javascript.
Yes, but also that oddly isn't the point. Really, the point is asm.js which is a subset of JS which can be somewhat trivially converted into fast machine code (no fancy type deduction required, which is what kills fast performance in JS). 99% of the point of wasm is to package up asm.js since there's really no need to parse something that's been compiled then un-parsed back into a javascript representation of machine instructions.
Re: (Score:2)
It's not the applet approach, it's more the intermediary or IL language approach.
With CSharp for example at development time you compile down to a binary that's in a language called IL.
Then at runtime you compile that IL into machine code.
The IL is fairly close to native machine code so there isn't much work do to at runtime, but at the same time it's platform independent so can be run on multiple architectures such as ARM or x86 without the need for multiple binaries.
In the case of Javascript it's already
Yavascript (Score:4, Informative)
This is straight out of "The Birth and Death of Yavascript" [destroyallsoftware.com]
Re: Yavascript (Score:2)
That was insightful. I wasn't clear why anyone would go through the agony of doing all this until he said the result is about 1/2 the speed of C.
My question is: does this let me do multi threading, or is JavaScript still stubbornly single threaded?
Or... (Score:2)
....just build C apps without JavaScript, and skip the gigs of memory and hundreds of megs of space required, not to mention the CPU overhead, and limitations of the "app platform" of the browser.
Y'know, this is how the world got so fucked. People compromising, doing the easy thing that they knew was wasteful, because they were too comfortable and lazy to do the hard thing that was right. This is why technology is so shitty now, and products have so many bugs.
If you told me 20 years ago that every new appli
Re: (Score:3)
The upside of everyone layering things 13 levels deep is that it's easy to look like a genius, simply by solving the underlying problem directly rather than going through 13 levels of "convenience layering". Look guys, a 1,000,000% speedup in 200 lines of C code! Amazing!
Re: Or... (Score:2)
This is what you get when every 2nd rate code monkey who learnt some HTML in college thinks they're a software engineer.
Re:Or... (Score:4, Insightful)
20 years ago, everything was going to run in a Java virtual machine inside a browser.
But Why The Fuck (Score:3)
Re: But Why The Fuck (Score:1)
You don't need to run a new client and/or server every time an end user wants to *do_something(*wtf) which is a little funny to since I feel like that is exactly what javascript is for.....
Re: (Score:2)
Bun is a Node.js/Deno alternative. I.e. it's made to run JavaScript code *on the server*.
Re: (Score:2)
You probably have legacy c code that you don't feel like rewriting/debugging and you want to run it server-side but with sandboxing and some support code not in c.
Re: (Score:2)
You probably have legacy c code that you don't feel like rewriting/debugging and you want to run it server-side but with sandboxing and some support code not in c.
So what you are basically saying is "This invention allows truly pathetically lazy programmers to become EVEN MORE SLACK."
Methinks this invention was truly created by a devout devotee of J.R. "Bob" Dobbs
The new ActiveX/Applet/Plugin (Score:2)
Oh good (Score:2)
Now I can run one shitty language inside another, even shittier, language for absolutely no upsides.
Re: (Score:2)
Well, other than being able to leverage the vast (and I mean vast) amount of existing C code. And the fact that most other important languages already have C interop, and...
Re: (Score:2)
Blah blah blah. Don't care. Doesn't change the fact that both languages are ass. It doesn't matter how popular they are. It doesn't matter important they have been over the years. The entire design of both is ass.
Re: (Score:1)
Re: (Score:2)
I'm sure you'd know.
Re: (Score:1)
Re: (Score:2)
Cool story. Means nothing as you still don't know anything relevant to me, this story, or why those two languages are ass. So, yeah.
Hey, remember Flash? (Score:1)
Raw, inscrutable, hexadecimal. Indirectly. (Score:2)
When are they gonna be able to run 6502 assembly inside javascript, on top of bun, inside a docker container, in a vm.
Re: (Score:2)
Already done. You can run Apple II software in your browser on archive.org right now.
He is right that C is the real thing (Score:2)
Sure, too many wannabees cannot handle it and mess up when they touch it. But the very fact of the Linux kernel shows that C is not a problem, but an asset. Sure, there are some areas, like drivers, are where code by less capable people comes in and here Rust could help (as far as you can write drivers without going "unsafe"), but the core? It could realistically not be written in anything but C.
The search for a better systems programming language is as old as the need for systems programming languages. Not
Re: (Score:2)
Yep. At the end of the day, Microsoft is still shipping compiled C/C++ code. Apple is still shipping compiled C/Objective-C code (they're *trying* to switch everything to Swift, but they're still a long, long way from that, 10 years later). As you note, the Linux kernel is still C code. Just about every embedded device on the planet is running compiled C code.
To a first approximation, everything that isn't running C is running JavaScript (which is, of course, generally implemented in compiled C code).
I'm
Re: (Score:1)
Most embedded projects I was involved in, are C++.
No one uses C if he can avoid it.
Re: (Score:1)
C is famous/obiqous because it used to be easy to port.
There are plenty of better languages out there, which also used to have similar traction than C. For example (UCSD) Pascal, Modula II, Ada, Erlang - or Oberon.
The Bene Gesserit have this one covered. (Score:3)
"Abomination!"
Security (Score:2)
Of course this can be done (Score:2)
Alan Turing proved that you can always do this, but...
"Your scientists were so preoccupied with whether or not they could, they didn't stop to think if they should."
The singularity (Score:2)
Here people think it's AI that needs to be controlled to save humanity, but at this rate it's Javascript that will become self-aware first
Education (Score:2)
Re: (Score:1)
Why not just reading the summary?
And comprehending it?
What has compiling C code with a C compiler written in JavaScript to be able to call the binary just created from JavaScript to do with learning C?
Oh, obviously everything. Or do you think people who write/adapt C compilers, can not program in C? Facepalm!
Re: (Score:2)
Re: (Score:1)
The Python guys like to glue together C libraries with Python.
As I see it JavaScript guys can do now similar things, even with on the fly compilation of C code.
I personally do not see a use case for myself for that at the moment.
But as a software experiment, I find it at least entertaining to think about it.
Re: (Score:2)
Re: (Score:1)
Tell that the guys that use JavaScript on the backend (Node, cough cough) or use it as scripting language for Applications, e.g. Eclipse.
Obviously a language like JavaScript is a general purpose language.
The language itself has nothing to do with Web at all.
Except for missing type declaration: it looks and works exactly like C!
Re: (Score:2)
Re: (Score:1)
Eclipse is written in Java.
Not sure what your nitpicking is regarding that it is scriptable with JavaScript. There are probably more language options, never checked it.
There are hundreds of desktop applications that run on Node, in a simple web browser frame, and are written in JavaScript.
Basically every cross platform chat application runs on Node. Telegram, WhatsApp, Signal, Discord, Line, WeChat, Skype, MS Teams ... and so on.
If you just script Eclipse with it, for example to remote control the debugger,
Re: (Score:2)
in a simple web browser frame
My point exactly. It relies on a web front end therefore its abilities are limited to only the things a browser can do. It's a web language.
Re: (Score:1)
It can do anything you want it to do.
You do not need a web browser to run a JavaScript program that calculates you ten trillion digits of PI and write it to a file or sent it via internet radio into the rather. FACEPALM.
Re: (Score:2)
As Bender from Futurama would say... (Score:2)
As Bender from Futurama would say "It's going to be fun on the bun..." https://www.youtube.com/watch?... [youtube.com]
Fun being like the "fun" function in LISP, getting there is half de-fun.
I had a professor that said an in-joke in the CS department was the course in COBOL which because they were using a Honeywell Bull mainframe with a fixed format, so the course was "Fun COBOL" as "Fundamentals of COBOL."
I guess we shall see...
JoshK.
Wonderful NOT! (Score:2)
So, great, you go to a website, and javascript runs ON YOUR MACHINE and compiles a hack - even harder to detrect than it is now.
Re: Wonderful NOT! (Score:2)
Well, it has first to escape the sandbox before doing anything to your machine.