No description, website, or topics provided.
Switch branches/tags
Nothing to show
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
README.md Add Reason versions (#2) Apr 14, 2018

README.md

BuckleScript FFI cheatsheet

NOTE: The translations given here might not be literally correct. The output will in some instances have been simplified slightly to improve clarity.

Basic

bs.val

(* ml *)
external f : 'a -> 'b -> 'ret = "" [@@bs.val]

let ret = f a b
/* re */
[@bs.val] external f : ('a, 'b) => 'ret = "";

let ret = f(a, b);
// js
var ret = f(a, b);

bs.get

(* ml *)
external f : 'self -> 'ret = "" [@@bs.get]

let ret = f self
/* re */
[@bs.get] external f : 'self => 'ret = "";

let ret = f(self);
// js
var ret = self.f;

bs.set

(* ml *)
external f : 'self -> 'a -> 'ret = "" [@@bs.set]

let ret = f self a
/* re */
[@bs.set] external f : ('self, 'a) => 'ret = "";

let ret = f(self, a);
// js
var ret = self.f = a;

bs.send

(* ml *)
external f : 'self -> 'a -> 'b -> 'ret = "" [@@bs.send]

let ret = f self a b
/* re */
[@bs.send] external f : ('self, 'a, 'b) => 'ret = "";

let ret = f(self, a, b);
// js
var ret = self.f(a, b);

bs.send.pipe

(* ml *)
external f : 'a -> 'b -> 'ret = "" [@@bs.send.pipe: 'self]

let ret = f a b self
(* or *)
let ret = self |> f a b
/* re */
[@bs.send.pipe: 'self] external f : ('a, 'b) => 'ret = "";

let ret = f(a, b, self);
/* or */
let ret = self |> f(a, b);
// js
var ret = self.f(a, b);

bs.new

(* ml *)
external f : unit -> 'ret = "" [@@bs.new]

let ret = f ()
/* re */
[@bs.new] external f : unit => 'ret = "";

let ret = f();
// js
var ret = new f();

bs.module

(* ml *)
external f : unit -> unit = "" [@@bs.module "m"]

let _ = f ()
/* re */
[@bs.module "m"] external f : unit => unit = "";

f();
// js
var m = require('m');
m.f();

bs.get_index

(* ml *)
external f : 'self -> 'key -> 'ret = "" [@@bs.get_index]

let ret = f self key
/* re */
[@bs.get_index] external f : ('self, 'key) => 'ret = "";

let ret = f(self, key);
// js
var ret = self[key];

bs.set_index

(* ml *)
external f : 'self -> 'key -> 'a -> 'ret = "" [@@bs.set_index]

let ret = f self key a
/* re */
[@bs.set_index] external f : ('self, 'key, 'a) => 'ret = "";

let ret = f(self, key, a);
// js
var ret = self[key] = a;

Advanced

bs.splice

(* ml *)
external f : 'a -> 'b array -> 'ret = "" [@@bs.val] [@@bs.splice]

let ret = f a [|b1; b2; b3|]
/* re */
[@bs.val] [@bs.splice] external f : ('a, array('b)) => 'ret = "";

let ret = f(a, [|b1, b2, b3|]);
// js
var ret = f(a, b1, b2, b3);

bs.scope

(* ml *)
external f : unit -> unit = "" [@@bs.val] [@@bs.scope "a", "b"]

let _ = f ()
/* re */
[@bs.val] [@bs.scope ("a", "b")] external f : unit => unit = "";

f();
// js
a.b.f();

bs.return

bs.obj

bs.as

(* ml *)
external f : 'a -> (_ [@bs.as "b"]) -> 'c -> unit = "" [@@bs.val]

let _ = f a c
/* re */
[@bs.val] external f : ('a, [@bs.as "b"] _, 'c) => unit = "";

f(a, c);
// js
f(a, "b", c);

bs.string

(* ml *)
external f : ([`a] [@bs.string]) -> unit = "" [@@bs.val]

let _ = f `a
/* re */
[@bs.val] external f : ([@bs.string] [`a]) => unit = "";

f(`a);
// js
f("a");

bs.int

(* ml *)
external f : ([`a] [@bs.int]) -> unit = "" [@@bs.val]

let _ = f `a
/* re */
[@bs.val] external f : ([@bs.int] [`a]) => unit = "";

f(`a);
// js
f(0);

bs.unwrap

(* ml *)
external f : ([`int of int] [@bs.unwrap]) -> unit = "" [@@bs.val]

let _ = f (`int 42)
/* re */
[@bs.val] external f : ([@bs.unwrap] [`int(int)]) => unit = "";

f(`int(42));
// js
f(42);

bs.ignore

(* ml *)
external f : 'a -> ('b [@bs.ignore]) -> 'c -> unit = "" [@@bs.val]

let _ = f a b c
/* re */
[@bs.val] external f : ('a, [@bs.ignore] 'b, 'c) => unit = "";

f(a, b, c);
// js
f(a, c);

bs

bs.uncurry

bs.this

bs.open

Extensions

bs.re

(* ml *)
let re = [%re "/na/gi"]
/* re */
let re = [%re "/na/gi"];
// js
var re = /na/gi;

bs.obj

(* ml *)
let obj = [%obj { property = "value" }]
/* re */
let obj = [%obj { property: "value" }];
/* or */
let obj = { "property": "value" };
// js
var obj = {
  property: "value"
};

bs.raw

(* ml *)
let raw = [%raw "1 + 2 == 3"]
/* re */
let raw = [%raw "1 + 2 == 3"];
// js
var raw = (1 + 2 == 3);

bs.debugger

(* ml *)
[%debugger]
/* re */
[%debugger];
// js
debugger;

bs.external

bs.node

JS Objects

Js.t

##

class type ... end [@bs]

#=

bs.get

bs.set