Support for calling C code has been added to the JavaScript platform Bun.

In the Bun platform, developed as an alternative to Node.js for isolated execution of applications written in JavaScript, JSX, and TypeScript, an experimental feature for compiling and executing handlers written in C has been implemented. Previously, calling low-level code required either creating N-API add-ons or compiling the code into WebAssembly (WASM/WASI) format, which complicated development and imposed certain limitations. Now, C code can be directly embedded in JavaScript and compiled on the fly using the TinyCC compiler, as well as linking dynamically linked libraries. import { cc } from "bun:ffi"; export const { symbols: { hello }, } = cc({ source: ".\/hello.c", symbols: { hello: { returns: "void", args: [], }, }, }); hello();

To work with C, the bun:ffi layer is used, which compiles the source code into machine instructions, injects it into memory at runtime, and provides automatic data type conversion between JavaScript and C. It is noted that the overhead when accessing compiled C code is minimized, resulting in a delay of about 2 nanoseconds for each call to external C functions. As an example, it is shown how to directly access FFmpeg libraries for converting short videos — by eliminating operations like forking a separate process and allocating memory for each video, the overall execution time of the test task using bun:ffi was reduced by three times.

Moreover, in the Bun 1.1.29 release, support for N-API (napi) has been added for C code, allowing C-written handlers to return objects, strings, arrays, and other non-primitive JavaScript values, in addition to C types like int and float. The mechanism for calling C handlers also works when dynamically loading any libraries with C ABI, regardless of the original programming language (C++, Rust, Zig, etc.), allowing JavaScript code to call functions provided by these libraries without a separate rebuilding stage. For example, it is now possible to access system APIs from JavaScript.

It is worth noting that the Bun project is being developed with the aim of ensuring compatibility with server applications written for Node.js and supports most of the Node.js API. The platform includes a set of tools for creating and running applications in JavaScript and TypeScript, as well as a runtime for executing JavaScript applications outside the browser, a package manager (compatible with NPM), testing tools, and a system for building self-sufficient packages. In terms of performance, Bun significantly outperforms Deno and Node.js (in tests based on the React framework, the Bun platform is twice as fast as Deno and almost five times faster than Node.js). The Bun code is written in Zig and C++ and is distributed under the MIT license. The JavaScriptCore engine and components from the WebKit project with additional patches are used for executing JavaScript.

Source: opennet.ru

Buy reliable website hosting with DDoS protection, VPS VDS servers 🔥 Buy reliable website hosting with DDoS protection, VPS VDS servers | ProHoster