All Projects → denoland → deno_bindgen

denoland / deno_bindgen

Licence: MIT license
Simplified glue code generation for Deno FFI libraries written in Rust.

Programming Languages

rust
11053 projects
typescript
32286 projects

Projects that are alternatives of or similar to deno bindgen

denoffi
Deno Foreign Function Interface.
Stars: ✭ 37 (-73.94%)
Mutual labels:  ffi, deno
sqlite3
The fastest and correct module for SQLite3 in Deno.
Stars: ✭ 143 (+0.7%)
Mutual labels:  ffi, deno
rid-examples
Examples showing how to use Rid in order to build Dart/Flutter apps integrated with Rust.
Stars: ✭ 191 (+34.51%)
Mutual labels:  ffi
deno-fetch-event-adapter
Dispatches global fetch events using Deno's native http server.
Stars: ✭ 18 (-87.32%)
Mutual labels:  deno
php-fuse
PHP FFI bindings for libfuse
Stars: ✭ 53 (-62.68%)
Mutual labels:  ffi
rippledb
Embeddable key-value database engine in pure TypeScript, based on LSM-Tree
Stars: ✭ 33 (-76.76%)
Mutual labels:  deno
deno-task-runner
Task runner for deno
Stars: ✭ 31 (-78.17%)
Mutual labels:  deno
dem
A module version manager for Deno.
Stars: ✭ 58 (-59.15%)
Mutual labels:  deno
asmdot
[Unstable] Fast, zero-copy and lightweight (Arm | Mips | x86) assembler in (C | C++ | C# | Go | Haskell | Javascript | Nim | OCaml | Python | Rust).
Stars: ✭ 23 (-83.8%)
Mutual labels:  ffi
database
towards a common interface for SQL databases in Deno TypeScript
Stars: ✭ 15 (-89.44%)
Mutual labels:  deno
deno-drash-realworld-example-app
Deno + Drash RealWorld example app
Stars: ✭ 56 (-60.56%)
Mutual labels:  deno
logrocket deno api
A functional CRUD-like API with Deno and Postgres
Stars: ✭ 23 (-83.8%)
Mutual labels:  deno
deno install
Deno 安装器(国内加速)
Stars: ✭ 58 (-59.15%)
Mutual labels:  deno
php-ffi-rust
PHP7.4 + Rust
Stars: ✭ 28 (-80.28%)
Mutual labels:  ffi
vscode-deno-extensionpack
Deno VS Code Extension Pack
Stars: ✭ 12 (-91.55%)
Mutual labels:  deno
iam-policies
Iam policies implementation for create roles and manage permissions
Stars: ✭ 20 (-85.92%)
Mutual labels:  deno
react-native-fast-openpgp
OpenPGP for react native made with golang for fast performance
Stars: ✭ 29 (-79.58%)
Mutual labels:  ffi
i18next-http-backend
i18next-http-backend is a backend layer for i18next using in Node.js, in the browser and for Deno.
Stars: ✭ 270 (+90.14%)
Mutual labels:  deno
erc20-balance
💎 Get 2000+ ERC-20 token balances with JavaScript. Supports Node.js and Deno
Stars: ✭ 18 (-87.32%)
Mutual labels:  deno
progress
ProgressBar in terminal for deno
Stars: ✭ 39 (-72.54%)
Mutual labels:  deno

deno_bindgen

This tool aims to simplify glue code generation for Deno FFI libraries written in Rust.

QuickStart

Annotate on top of Rust fn, struct and enum to make them avaiable to Deno.

// add.rs
use deno_bindgen::deno_bindgen;

#[deno_bindgen]
pub struct Input {
  a: i32,
  b: i32,
}

#[deno_bindgen]
fn add(input: Input) -> i32 {
  input.a + input.b
}

Invoke the CLI to compile and generate bindings:

$ deno_bindgen

And finally import the generated bindings in your JS

// add.ts
import { add } from "./bindings/bindings.ts";

add({ a: 1, b: 2 }); // 3

Installation

  • Install the deno_bindgen CLI with Deno.
deno install -Afq -n deno_bindgen https://deno.land/x/deno_bindgen/cli.ts

Add the following dependencies to your crate.

# Cargo.toml
[dependencies]
deno_bindgen = "0.7.0"
serde = { version = "1", features = ["derive"] }

Change your crate-type to cdylib and set your package name as well.

[lib]
name = "___"
crate-type = ["cdylib"]

Bindings

Put #[deno_bindgen] on top of a "serde-deriavable" struct, enum or fn.

struct (named fields)

These transform into Typescript types.

// lib.rs
#[deno_bindgen]
pub struct A {
  b: Vec<Vec<String>>,
}

becomes:

// bindings/bindings.ts
export type A = {
  b: Array<Array<string>>;
};

enum

Enums become type unions in Typescript.

#[deno_bindgen]
pub enum Event {
  Quit,
  MouseMove {
    x: i32,
    y: i32,
  }
}

becomes:

export type Enum =
  | "quit"
  | {
    mouse_move: {
      x: number;
      y: number;
    };
  };

fn

Functions are exposed through the FFI boundaries.

#[deno_bindgen]
fn greet(name: &str) {
  println!("Hello, {}!", name);
}

becomes:

export function greet(name: string) {
  // ... glue code for calling the
  // symbol.
}

Notes

  • Use #[deno_bindgen(non_blocking)] attribute to call symbol without blocking JS event loop. Exposed as an async funtion from bindings.

  • Rust doc comments transform to JS docs.

    #[deno_bindgen]
    pub struct Me {
      /// My name...
      /// ...it is
      name: String,
    }

    becomes:

    export type Me = {
      /**
       * My name...
       * ...it is
       */
      name: string;
    };
  • If the argument type of Rust is f32, the calculation result may be different.
    Number in Java Script is float64, when data is passed to Rust, it becomes float32, so the number may change.
    e.g: 1.3 + 1.5 will be 2.799999952316284

CLI

The deno_bindgen CLI tool provides the following flags:

  • Pass --release to create a release build.

  • --release=URL will load library artifacts from a remote location. This is useful for updating bindings for end users after a release:

    deno_bindgen --release=https://github.com/littledivy/deno_sdl2/releases/download/0.2-alpha.1

    Under the hood this uses x/plug to fetch and cache the artifact.

    Artifacts must be following the remote asset naming scheme, as follows:

    OS Arch Naming
    Windows x86_64 name.dll
    Linux x86_64 libname.so
    MacOS x86_64 libname.dylib
    MacOS arm64 libname_arm64.dylib
  • Flags after -- will be passed to cargo build. Example:

    deno_bindgen -- --features "cool_stuff"
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].