Follow Slashdot blog updates by subscribing to our blog RSS feed

 



Forgot your password?
typodupeerror
×
Programming

'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!"

This discussion has been archived. No new comments can be posted.

'Compile and Run C in JavaScript', Promises Bun

Comments Filter:
  • by quonset ( 4839537 ) on Sunday September 22, 2024 @11:40AM (#64807769)

    All these shenanigans attempting to run one language inside or along with another reminds me of the English language [imgur.com].

    • 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)

    by bussdriver ( 620565 ) on Sunday September 22, 2024 @11:56AM (#64807819)

    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.

    • 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.

    • 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.

      • by tlhIngan ( 30335 )

        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.

        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

        • 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.

      • 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)

    by Dwedit ( 232252 ) on Sunday September 22, 2024 @12:11PM (#64807863) Homepage

    This is straight out of "The Birth and Death of Yavascript" [destroyallsoftware.com]

    • 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?

  • ....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

    • by Jeremi ( 14640 )

      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!

    • 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)

      by jonbryce ( 703250 ) on Monday September 23, 2024 @08:10AM (#64809317) Homepage

      20 years ago, everything was going to run in a Java virtual machine inside a browser.

  • by NoSleepDemon ( 1521253 ) on Sunday September 22, 2024 @12:48PM (#64807939)
    Would you want to do this? *Why* do you want to run C from JS? Surely at that point you already fucked up big time. JS is a shitty language designed to render UI in a browser. If you need to do system level stuff then you need to either write a server with services end points your browser hits, or an actual desktop application with its own UI.
    • 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.....

    • by jb_nizet ( 98713 )

      Bun is a Node.js/Deno alternative. I.e. it's made to run JavaScript code *on the server*.

    • 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.

      • 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

  • Except the embedding layer is integrated into the browser through endless translation layers. We are already at this point where we can run translated executables in the browser, and even whole operating systems in browser vms, the madness will continue.
  • Now I can run one shitty language inside another, even shittier, language for absolutely no upsides.

    • 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...

      • by Bahbus ( 1180627 )

        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.

  • Hey, remember Flash?
  • When are they gonna be able to run 6502 assembly inside javascript, on top of bun, inside a docker container, in a vm.

    • by caseih ( 160668 )

      Already done. You can run Apple II software in your browser on archive.org right now.

  • 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

    • 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

    • 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.

  • by jddj ( 1085169 ) on Sunday September 22, 2024 @06:39PM (#64808527) Journal

    "Abomination!"

  • How do they maintain JS isolation from the underlying system? I mean, it looks scary if any random website can run system() from your browser.
  • 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."

  • 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

  • If JavaScript programmers want to do C so badly, why don't they just learn C?
    • 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!

      • Fair enough, i read it backwards. It still stands that if you need a c library, you probably shouldn't be doing it with JavaScript.
        • 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.

          • Python is a general purpose language. JavaScript is a web language.
            • 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!

              • Why is it just a backend scripting language for eclipse? Why is eclipse not made out of it? How many applications are made out of nodejs from the ground up as opposed to just using it in the back end?
                • 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,

                  • 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.

                    • 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.

                    • So how would you do a user interface without tetgering to a browser? Qt? Visial studio? Flutter? Most useful applications have. graphical interface.
  • 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.

  • 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.

It is better to travel hopefully than to fly Continental.

Working...