BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Open-Source OCaml to JavaScript Compiler BuckleScript Hits 1.0

Open-Source OCaml to JavaScript Compiler BuckleScript Hits 1.0

This item in japanese

Bookmarks

BuckleScript 1.0 brings almost full compatibility with OCaml features and an improved FFI with the aim of avoiding writing unsafe JavaScript stubs. InfoQ has spoken with Bloomberg’s Hongbo Zhang, BuckleScript creator at Bloomberg.

According to Bloomberg, BuckleScript aims to attempt to solve the problem of building large scale systems in JavaScript by providing:

  • OCaml industrial-strength type safety
  • Dead code elimination to improve performance at the function and module level thanks to the sophistication of OCaml type-system
  • Compile-time optimizations to improve runtime performance
  • Support for both JavaScript and native platforms

In comparison with other existing transpilers for JavaScript, BuckleScript aims to provide faster compilation, readable and compact output code that retains the same module structure as the OCaml source.

InfoQ has spoken with Hongbo Zhang to learn more about BuckleScript present and future.

What concerns brought BuckleScript from its initial release to version 1.0?

Mostly stabilizing the design of FFI and bug fixes. Since BuckleScript generates readable JavaScript code, there wasn’t too much work needed to make JavaScript call OCaml functions. We have spent several months polishing the design of FFI, so that it is easy for users to call JavaScript from OCaml. The goal is taking advantages of OCaml’s expressive type system (with some BuckleScript customized attributes) to model JavaScript library directly without writing any stub code.

These are the major highlights:

  1. BuckleScript supports two calling convention: uncurried (exactly the same as JavaScript) and optimized curried convention (used in Functional programming paradigm).

  2. We managed to model JavaScript FFI in two styles: a functional FFI like PureScript, and an object FFI by using OCaml’s expressive object type system. This allowed us to model JavaScript structural typing rigorously.

  3. We also have built-in support tying JavaScript this keyword semantics.

  4. Some unique features in OCaml like polymorphic variants are used in model event listeners. This gives us very strongly type-safe guarantees compared with TypeScript.

You can find more details about BuckleScript FFI in a presentation we gave at a meet-up.

One recent effort aimed to integrate Reason with BuckleScript. Could you explain how would it work and what advantages it would bring to developers?

Reason is a JavaScript-like front-end syntax for OCaml developed at Facebook by Jordan’s team. Since OCaml’s compiler tool chain is very modular, it is very easy for us to switch the front end from vanilla OCaml to Reason. Note that Reason is more than just syntax. The team behind Reason also worked very hard to improve the tooling of OCaml, like build system, IDE etc. We work very hard together. For example, the design of BuckleScript FFI took a lot of feedback from Reason’s team.

What’s in BuckleScript roadmap?

  1. BuckleScript compiles OCaml to JavaScript. So, we will follow up the recent developments of OCaml, and upgrade to the latest compiler. There are plenty of exciting new features recently landed in OCaml that we will benefit from. For example, Flambda optimizations will make our compiler even faster.

  2. We will work with other teams (inside or outside Bloomberg) to provide more bindings (NodeJS, Electron and React) for BuckleScript.

  3. BuckleScript’s compiler is also compiled into JavaScript, which means users can not only run but also write OCaml/Reason anywhere. We will package everything into a single JavaScript file so users can get started quickly without hitting JavaScript fatigue. At the same time, we will also improve our playground to make it a better web IDE.

Rate this Article

Adoption
Style

BT