Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 14, 2021 10:20 pm GMT

Erlscripten: Transpiling Erlang to Javascript. Yes, you read it right.

It is safe to say that Erlang as a language and framework is the most reliable choice out there to build highly stable applications, running the likes of Whatsapp and Discord. Now, there is a transpiler introducing concepts of Erlang to web technology, which would be near impossible to implement manually using Javascript.

image

Here at ternity, weve 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 is here.

image

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.

Its 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:

https://github.com/erlscripten/erlps-core

Purpose

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 doesnt 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 its needed. One side effect of Erlscripten is that it will soon be possible to write Frontend applications directly in Erlang Erlangs actor based programming maps really nicely to existing reactive frameworks.

Similar projects

Were 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 (Erlangs 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, Erlangs 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!

Status quo

What is supported

  • 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.)

What is partially supported

What is under development

  • Bitstrings
  • Leaking variable scopes
  • Basic erlang process emulation
  • NIFs (will be done through C to WASM compilation)

Works on the PureScript compiler

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 havent 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:

Examples:

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.

Factorial:

image

Find the code example here:

https://github.com/erlscripten/erlscripten/blob/main/examples/factorial.erl

Continuation based list reversal via catamorphic transformation:

image

Find the code example here:

https://github.com/erlscripten/erlscripten/blob/main/examples/list_reverse.erl

Run Erlang in the browser!

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

Original article from :
https://blog.aeternity.com/erlscripten-92c815786987


Original Link: https://dev.to/omarsaadoun/erlscripten-49gg

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To