Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

とくに新出のトランスパイラだと、バインディングが少ないので、いかにして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 */
plaid
CXプラットフォーム「KARTE」の開発・運営、EC特化型メディア「Shopping Tribe」の企画・運営、CX特化型メディア「XD(クロスディー)」の企画・運営
https://plaid.co.jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away