Skip to content

Latest commit

 

History

History
424 lines (355 loc) · 8.73 KB

README.md

File metadata and controls

424 lines (355 loc) · 8.73 KB

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

(* 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);
(* 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;
(* 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;
(* 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);
(* 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);
(* 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();
(* 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();
(* 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];
(* 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

(* 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);
(* 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();
(* 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);
(* 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");
(* 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);
(* 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);
(* 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);

Extensions

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

Expression

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

Function

(* ml *)
let myFunction = [%raw fun a b -> "return a + b;"];
/* re */
let myFunction = [%raw (a, b) => "return a + b;"];
// js
function myFunction (a, b) { return a + b; };

Block

(* ml *)
[%%raw {|
  var a = 1;
  var b = a + 2;
|}]
/* re */
[%%raw {|
  var a = 1;
  var b = a + 2;
|}];
// js
var a = 1;
var b = a + 2;
(* ml *)
[%debugger]
/* re */
[%debugger];
// js
debugger;

JS Objects