new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Deno · all subjects

advanced features

7 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

FFI allows calling native C libraries

The FFI (foreign function interface) API allows users to call libraries written in native languages that support C ABIs (C/C++, Rust, Zig, V, etc.) using Deno.dlopen. Requires --allow-ffi and --unstable flags.

FFI example: calling Rust function from Deno

Example showing how to call a Rust function add(a: isize, b: isize) -> isize from Deno after compiling to C dynamic library: ```typescript const dylib = Deno.dlopen( "./libadd.so", { "add": { parameters: ["isize", "isize"], result: "isize" }, } as const, ); const result = dylib.symbols.add(35, 34); // 69 ```

Non-blocking FFI calls return Promise

Symbols can be marked nonblocking in Deno.dlopen. These function calls run on a dedicated blocking thread and return a Promise resolving to the desired result. Example: { sleep: { parameters: ["usize"], result: "void", nonblocking: true } }. Call returns a Promise: library.symbols.sleep(500).then(() => console.log("After")).

FFI callbacks with Deno.UnsafeCallback

Create C callbacks from JavaScript functions using new Deno.UnsafeCallback({ parameters: [...], result: "..." }, callbackFunction). Pass the pointer to native libraries: library.symbols.set_callback(callback.pointer). If callback throws, error propagates to function that triggered callback. Must call callback.close() to properly dispose and prevent use-after-free bugs.

FFI supported types table

FFI types and their Deno/C/Rust mappings: i8 (number/char/i8), u8 (number/unsigned char/u8), i16 (number/short int/i16), u16 (number/unsigned short int/u16), i32 (number/int/i32), u32 (number/unsigned int/u32), i64 (bigint/long long int/i64), u64 (bigint/unsigned long long int/u64), usize (bigint/size_t/usize), isize (bigint/size_t/isize), f32 (number/float/f32), f64 (number/double/f64), void (undefined/void/() - result only), pointer ({}/null/void*/*mut c_void), buffer (TypedArray/null/uint8_t*/*mut u8), function ({}/null/void (*)/Option<extern "C" fn()>), struct (TypedArray/struct MyStruct/MyStruct - copy semantics).

FFI pointer type changed in Deno 1.31

As of Deno 1.31, the JavaScript representation of pointer type has become an opaque pointer object or null for null pointers. Previously accepted TypedArrays. The buffer type (added in Deno 1.25) accepts TypedArrays as parameter but returns pointer object or null as result.

deno_bindgen tool for Rust FFI

deno_bindgen is the official tool to simplify glue code generation of Deno FFI libraries written in Rust, similar to wasm-bindgen in the Rust Wasm ecosystem. Allows automatic bindings generation from Rust code with #[deno_bindgen] macro. Issues should be reported at https://github.com/denoland/deno_bindgen/issues.

Give your agent this brain