HomeCoinsAeternity (AE)Erlscripten. Transpiling Erlang to Javascript. Yes… | by æternity blockchain | Feb,...

Erlscripten. Transpiling Erlang to Javascript. Yes… | by æternity blockchain | Feb, 2021

- Advertisement -

Here at æternity, we’ve envisioned for a long time that running major parts of a blockchain protocol in the context of modern web frameworks would mean a huge leap for the adoption and scalability of distributed ledger technology. What if you could run an ad-hoc blockchain node in your browser, or use state channels to quickly make safe payments using your phone?

As a solid foundation had to be laid for a reliable infrastructure first, that was not conceivable as of yet. Until now.

Erlscripten (sic not Emscripten!) is a source-to-source transpiler capable of converting most Erlang codebases into semantically equivalent Purescript projects. Purescript is a strongly typed language heavily inspired by Haskell and which compiles down to Javascript. Taking Purescript as an intermediary step, Erlscripten allows you to take your existing Erlang application and easily ship it out to Javascript users — your Erlang code can now run safely in the browser — enabling code sharing between an Erlang backend and the frontend. It is highly interoperable with Javascript — Javascript can easily call the transpiled code and then make use of the results — you can easily map Erlang constructs and types to readily available JS constructs.

It’s zero hassle — you point Erlscripten to an existing Rebar3 Erlang project and in result get a compatible spago project ready to be used in existing Purescript/Javascript projects. While at it, we transpiled the Erlang runtime (ERTS) that allows you to run arbitrary transpiled Erlang code:

Besides bringing the components of the æternity blockchain to more platforms and web frameworks, there are two novel use cases for Erlscripten:

1 — Code reuse between Erlang backends and Javascript frontends;

2 — The creation of a new paradigm for developing frontend applications.

æternity is a blockchain platform primarily written in Erlang. In the upcoming era of Hyperchains the need for code distribution between frontend and backend applications is high. Therefore we need to move some parts of æternity infrastructure to the clients’ side in order to provide a more reliable and decentralized system. It doesn’t make sense to rewrite our code in Javascript/Rust manually as this would require a lengthy rewrite and then the need to maintain and sync 2 codebases. The high need of interoperability between Erlang and Javascript is where Erlscripten was born. We can transpile what we need and distribute it to the browser where it’s needed. One side effect of Erlscripten is that it will soon be possible to write Frontend applications directly in Erlang — Erlang’s actor based programming maps really nicely to existing reactive frameworks.

We’re not the first ones with the need of Erlang/Javascript interoperability — Elixirscript is a Elixir to Javascript transpiler with similar goals as our project. The main downside of Elixirscript is the lack of OTP support (Erlang’s main framework for building failsafe applications) — it works on a really small subset of the Elixir language and they need to rewrite the Elixir standard library in Javascript for it to work. In contrast we are OTP compatible — we transpiled the real Erlang standard library from OTP and distributed it open-source: https://github.com/erlscripten/erlps-stdlib (Erlang Solutions).

To demonstrate our point we were capable of taking the compiler of our smart contract language (https://github.com/aeternity/aesophia) and converting it to be runnable in the browser: https://github.com/erlscripten/erlps-aesophia

We tested the transpiled project and it works! Demonstrating our point that we have support for advanced OTP constructs including ETS, Erlang’s in-memory database.

Notably, Lumen has similar goals to our project, after their project is done it will be much faster than Erlscripten and probably more capable — the downside is that Lumen is in WIP phase and it will take a long time for Lumen to be ready whereas Erlscripten is now and here! Ready to be used!

  • Majority of Erlang expression
  • Arbitrary arity functions
  • Pattern matching
  • Records (via tuples)
  • Binaries
  • Lambdas
  • Tail recursion
  • Exceptions
  • Process dictionaries
  • Code server, module loading
  • Imports and exports
  • Compatibility utilities
  • Common errors (function_clause, case_clause, badarity, etc.)
  • Bitstrings
  • Leaking variable scopes
  • Basic erlang process emulation
  • NIFs (will be done through C to WASM compilation)

The original PureScript turned out not to suffice our use case perfectly. For instance, we had some issues regarding handling tail call optimization which started several discussions and contributions on their GitHub:

Some of the problems have been already fixed due to our reports, but some of them haven’t been accepted as they involved some overhead on the performance in the general case. Because of this we have forked the compiler and started adjusting it for our needs ourselves. Our works mainly involve the optimization tweaks and updating the backend to EcmaScript6 (instead of 5). For instance, we have implemented a much more aggressive tail call optimizer, increased strictness of AST traversals and completely rewritten the code generation for pattern match alternatives.

During this work we have spotted some performance bugs and started working on fixing them on our own. One of our patches has already been approved and awaits merging:

While we are perfectly aware that neither Erlang nor PureScript are the most common sight to see, we invite you to take a look: Here are some examples of how Erlscripten transforms existing Erlang code into PureScript. The generated code is obviously larger as we need to take care of the type system (PureScript is typed while Erlang is not) and tackle some syntactic sugars. Furthermore, we need to emulate the default error handling and exceptions that are being solved much differently in Erlang in order to preserve compatibility.

Find the code example here:

Find the code example here:

Erlang to PureScript transpiler > https://github.com/erlscripten/erlscripten

Get in touch:

GitHub | Forum | Reddit | Twitter | YouTube| LinkedIn

Read the original article here

- Advertisement -
Mr Bitcointe
Mr Bitcointehttps://www.bitcointe.com/
“Fact You Need To Know About Cryptocurrency - The first Bitcoin purchase was for pizza.” ― Mohsin Jameel

Most Popular

Bitcoin (BTC) $ 58,318.00
Ethereum (ETH) $ 1,818.24
Tether (USDT) $ 0.998747
Bitcoin Cash (BCH) $ 517.96
Litecoin (LTC) $ 191.50
EOS (EOS) $ 4.22
OKB (OKB) $ 16.13
Tezos (XTZ) $ 4.45
LEO Token (LEO) $ 2.08
Cardano (ADA) $ 1.19
Monero (XMR) $ 260.15
Stellar (XLM) $ 0.395884
Chainlink (LINK) $ 27.27
Huobi Token (HT) $ 15.43
TRON (TRX) $ 0.072746
USD Coin (USDC) $ 1.00
Dash (DASH) $ 211.65
NEO (NEO) $ 45.02
IOTA (MIOTA) $ 1.47
NEM (XEM) $ 0.364084
Zcash (ZEC) $ 150.51
Maker (MKR) $ 2,076.16
Paxos Standard (PAX) $ 1.00
Ethereum Classic (ETC) $ 13.17
VeChain (VET) $ 0.086628
TrueUSD (TUSD) $ 1.00
FTX Token (FTT) $ 39.35
KuCoin Token (KCS) $ 5.04
Waves (WAVES) $ 11.66