Giter VIP home page Giter VIP logo

ocaml-bindgen's Introduction

bindgen

bindgen automatically generates raw OCaml FFI bindings to C (and some C++) libraries both as OCaml external definitions, types, constants, and the appropriate C shim code.

Non-goals: It does not aim to provide very safe, very idiomatic, pure bindings, but rather give you direct access to the underlying C library with a few conveniences. But you're more than welcome to build idiomatic, type-safe wrappers for bindgen generated libraries.

For example, given the C header doggo.h:

typedef struct Doggo {
    int many;
    char wow;
} Doggo;

void eleven_out_of_ten_majestic_af(Doggo* pupper);

bindgen produces OCaml FFI code allowing you to call into the doggo library's function and use its types:

(* automatically generated by ocaml-bindgen 0.0.1 *)

type doggo = {
  many: int
  wow: char
}

external eleven_out_of_ten_majestic_af : doggo -> unit
    = "caml_eleven_out_of_ten_majestic_af"

And the follow C shim:

/* automatically generated by ocaml-bindgen 0.0.1 */

value caml_Doggo_to_value(struct Doggo *pupper) {
  CAMLparam0();
  CAMLlocal1(caml_pupper);
  event = caml_alloc_tuple(2);
  Store_field(caml_pupper, 0, Val_int(pupper->many));
  Store_field(caml_pupper, 1, Val_char(pupper->wow));
  CAMLreturn(caml_pupper);
}

Doggo* caml_Doggo_of_value(value caml_pupper) {
    Doggo* pupper = malloc(sizeof(struct Doggo));
    pupper.many = Int_val(Field(caml_pupper, 0));
    pupper.wow = Char_val(Field(caml_pupper, 1));
    return pupper
}

CAMLprim value caml_eleven_out_of_ten_majestic_af(value caml_pupper) {
    CAMLparam1(caml_pupper);
    Doggo* pupper = caml_Doggo_of_value(caml_pupper);
    // actual C call
    eleven_out_of_ten_majestic_af(pupper);
    CAMLreturn0();
}

ocaml-bindgen's People

Contributors

leostera avatar omnisci3nce avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.