LoginSignup
6
3

More than 5 years have passed since last update.

facebook/reason と BuckleScript で JavaScript の関数を呼び出すパターン

Posted at

とくに新出のトランスパイラだと、バインディングが少ないので、いかにしてFFI叩くかが勝負になる

参考: https://bloomberg.github.io/bucklescript/Manual.html

main.re
let add a b => a + b;
Js.log "this is console.log";
Js.log(add 3 4);

/* function call */
external imul : int => int => int = "Math.imul" [@@bs.val];
Js.log(imul 3 4);

/* function call with unit */
external now : unit => int = "Date.now" [@@bs.val];
Js.log(now());

/* new */
external create_date: unit => string = "Date" [@@bs.new];
Js.log(create_date());

/* value */
external dirname : string = "__dirname" [@@bs.val];
Js.log dirname;

/* require */
external resolve_path : string => string => string = "resolve" [@@bs.val] [@@bs.module "path"];
Js.log(resolve_path "a" "b");

コンパイル後


// Generated by BUCKLESCRIPT VERSION 1.5.2, PLEASE EDIT WITH CARE
'use strict';

var Path = require("path");

function add(a, b) {
  return a + b | 0;
}

console.log("this is console.log");

console.log(7);

console.log(Math.imul(3, 4));

console.log(Date.now());

console.log(new Date());

console.log(__dirname);

console.log(Path.resolve("a", "b"));

exports.add = add;
/*  Not a pure module */
6
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3