A Bun JavaScript-platformon hozzáadták a C nyelvű kódhívás támogatását.

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

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

Furthermore, in the Bun 1.1.29 release, support for N-API (napi) has been added for C code, allowing C handlers to return objects, strings, arrays, and other non-primitive JavaScript values, in addition to the C types such as int and float. The mechanism for calling handlers in C also works with the dynamic loading of any libraries with C ABI, regardless of the language they are originally written in (C++, Rust, Zig, etc.), which allows calling functions provided by these libraries from JavaScript code without a separate rebuilding stage. For example, you can now access system APIs from JavaScript.

Emlékeztetünk arra, hogy a Bun projekt a Node.js-re írt szerveralkalmazások kompatibilitásának figyelembevételével fejlődik, és a Node.js API-jának nagy részét támogatja. A platform tartalmaz egy eszközkészletet a JavaScript és TypeScript nyelveken írt alkalmazások létrehozásához és végrehajtásához, valamint egy runtime-ot a JavaScript alkalmazások böngésző nélküli futtatásához, egy csomagkezelőt (NPM kompatibilis), tesztelési eszközkészletet és egy önálló csomagok készítésére szolgáló rendszert. Teljesítmény szempontjából a Bun jelentősen felülmúlja a Denót és a Node.js-t (a React keretrendszeren alapuló tesztek alapján a Bun platform kétszer gyorsabb, mint a Deno, és majdnem ötször gyorsabb, mint a Node.js). A Bun kódja Zig és C++ nyelveken íródott, és MIT licenc alatt terjesztik. A JavaScript végrehajtásához a JavaScriptCore JavaScript motor és a WebKit projekt komponensei, valamint további javítások kerültek felhasználásra.

Forrás: opennet.ru

Megbízható hosting vásárlása weboldalak számára, DDoS védelemmel, VPS VDS szerverek. 🔥 Megbízható webtárhely vásárlása DDoS védelemmel, VPS VDS szerverek | ProHoster