# Racket to Javascript compiler

**URL:** <https://racket.discourse.group/t/racket-to-javascript-compiler/1018>\
**Category:** RacketScript\
**Tags:** racketscript\
**Created:** [May 21, 2022, 3:35pm UTC](https://racket.discourse.group/t/racket-to-javascript-compiler/1018 "2022-05-21T15:35:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gus-massa](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/gus-massa/32/507_2.png) [@gus-massa](https://racket.discourse.group/u/gus-massa)\
**Post date:** [May 21, 2022, 3:35pm UTC](https://racket.discourse.group/t/racket-to-javascript-compiler/1018/1 "2022-05-21T15:35:08Z")

</div>

What are the difference between:

- RacketScript: [The RacketScript Language and Compiler](https://docs.racket-lang.org/racketscript/index.html)

- Whalesong: [Whalesong: a Racket to JavaScript compiler](https://www.hashcollision.org/whalesong/)

Are both active? Which one has more features?

Is it possible to compile a rkt file to a js file that has no dependencies?

---

<div class="post-metadata">

**Author:** ![dannypsnl](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/dannypsnl/32/917_2.png) [@dannypsnl](https://racket.discourse.group/u/dannypsnl)\
**Post date:** [May 21, 2022, 6:46pm UTC](https://racket.discourse.group/t/racket-to-javascript-compiler/1018/2 "2022-05-21T18:46:18Z")

</div>

lastest commit of them

- Racketscript: 27 days ago
- Whalesong: 30 Oct 2014, fork is 14 May 2017

You would more likely want to use Racketscript, but I already don't remember Racketscript's compiled JS file has dependency or not.

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [May 21, 2022, 7:10pm UTC](https://racket.discourse.group/t/racket-to-javascript-compiler/1018/3 "2022-05-21T19:10:58Z")

</div>

Whalesong is stuck on version 6.2 of Racket.

Here is an explanation from one of the issues:

 ![image](https://global.discourse-cdn.com/free1/uploads/racket/original/1X/3fa31996add0714706dfc1fcc08991735f3ce49e.png)

I recommend looking at RacketScript.

---

<div class="post-metadata">

**Author:** ![stchang](https://avatars.discourse-cdn.com/v4/letter/s/74df32/32.png) [@stchang](https://racket.discourse.group/u/stchang)\
**Post date:** [May 23, 2022, 8:13pm UTC](https://racket.discourse.group/t/racket-to-javascript-compiler/1018/4 "2022-05-23T20:13:31Z")

</div>

Hi, The descriptions below are to the best of my knowledge. Please correct any misunderstandings.

**Whalesong** is a semantics-preserving implementation of Racket. Thus, features like tail calls, continuations, and the numeric tower all behave exactly like Racket.

- Implementation-wise, Whalesong calls the Racket compiler to first compile to bytecode, and then to JS. To support Racket semantics, the runtime maintains its own stack and uses trampolines to implement tail calls and other control operators, and is thus somewhat "heavyweight".

- Usage-wise, Whalesong has performance tradeoffs that would be expected from such an approach. Another tradeoff is that existing tools in the JS ecosystem may not work with WhaleSong generated JS since it is not idiomatic. These tradeoffs are acceptable since Whalesong is primarily used for smaller programs in a teaching context, e.g., see [wescheme.org](http://wescheme.org).

**RacketScript** follows in the steps of other "-Script"-style languages, like ClojureScript or ReScript, i.e., it's not Racket.

- Implementation-wise, RacketScript directly translates (fully expanded) Racket to approximate JavaScript counterparts. For example, Racket functions become JS functions. Same with numbers and most arithmetic. Thus the output is more readable and compatible with the existing JS ecosystem.

- Usage-wise, as mentioned, RacketScript compiled Racket requires a runtime but all dependencies can be combined into a single file using the `webpack` compile target option (which uses the `node` `webpack` command --- so `node` is required to be installed).

---

<div class="post-metadata">

**Author:** ![gus-massa](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/gus-massa/32/507_2.png) [@gus-massa](https://racket.discourse.group/u/gus-massa)\
**Post date:** [May 24, 2022, 2:24pm UTC](https://racket.discourse.group/t/racket-to-javascript-compiler/1018/5 "2022-05-24T14:24:21Z")

</div>

How hard is to make Whalesong use the fully expanded version of the program instead of the BC-bytecode? IIRC they were quite similar. The problem is that it would be even slower becasue the BC-bytecode has already many optimizations (like constant propagation and folding), and the trampoline will confuse the JS compiler and destroy any hope of inlining or loop unroling, and the BC-bycode has safe for space marks, and ...

How hard is to make a fully expanded program to BC-bytecode compiler? Perhaps schemified code to BC-bytecode? [My guess is that cp0 adds too man subte assumptions about Chez Scheme, that it will be necesary to rewrite a lot of the internal parts of Whalesong.]

I think both are possible, but they require a lot of work.

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [May 25, 2022, 2:13pm UTC](https://racket.discourse.group/t/racket-to-javascript-compiler/1018/6 "2022-05-25T14:13:34Z")

</div>

> How hard is to make Whalesong use the fully expanded version of the program instead of the BC-bytecode? IIRC they were quite similar. The problem is that it would be even slower becasue the BC-bytecode has already many optimizations (like constant propagation and folding), and the trampoline will confuse the JS compiler and destroy any hope of inlining or loop unroling, and the BC-bycode has safe for space marks, and ...

It's definitely possible - and it would fix the current problem (that Whalesong is dependent on the bytecodes used in the BC compiler). The current compiler is written in C [1].  
The nice thing about this approach is, that the exising Whalesong runtime can be reused.  
However, as you point out, there will be no synergy between such a compiler and the one in Racket CS.

The new compiler in Racket CS produces linklets as an intermediary step towards outputing Chez Scheme code. I think, it would be to look at compiling linklets to JavaScript.

> I think both are possible, but they require a lot of work.  
> I agree.

[1] [https://github.com/racket/racket/blob/master/racket/src/bc/src/compile.c](https://github.com/racket/racket/blob/master/racket/src/bc/src/compile.c)
