Running Ocaml in the Browser

O’Browser is an Objective Caml bytecode interpreter written in Javascript. It is capable of running a stripped down version of OCaml’s standard library. Some example applications can be seen here.

The implementation of the runtime counts around 6000 lines of Javascript code, plus another 7000 lines of OCaml code for the standard library. That’s quite a lot when compared against my 4000 lines of Ruby code, including inline Javascript code of the Ruby core classes, of my RubyJS Ruby to Javascript compiler. But it’s definitively a great way to run little OCaml applications within the browser, for those that use OCaml. Unfortunately, I never made it to write any larger application in OCaml, I only used SML (MLTon) in a more extensive way.

Before I started to work on RubyJS (that’s now around 2 years ago), I also thought about compiling OCaml down to Javascript. My idea was to emit Javascript code instead of running OCaml bytecode within Javascript, and I think together with OCaml’s object model, this approach could be a viable alternative to Google’s Java to Javascript compiler when it comes to performance and static typing. RubyJS can’t simply give you all those static type guarantees and due to it’s dynamic typing system of Ruby, where everything is a method call, it can’t be all that fast. But then, we all use Ruby or a similar dynamic language and it is just fast enough, so why stick with Java?