DISQUS

Skydeck Blog: ocamljs, OCaml to Javascript compiler

  • Massimiliano · 2 years ago
    Wow! I just downloaded it and this seems a top quality work.

    Did you have access to some documentation on the internals of the ocaml compiler or did you just read the source code?

    In case you had access to docs, could you please tell me what you had to do to obtain it?
    If you just read the source would you like to write some documents (a tutorial or a blog post or whatever you like) explaining a bit the internals of the compiler?

    I'm working on a snippet manager to ease the distribution and reuse of ocaml code and to reduce the size of the binary by linking only those pieces of libraries that are used in your application and not the whole module. We already have a working prototype implementation using ocamldoc but we need to access the internals of the compiler to get a better tool. Our snippet manager will be released as free software.
    If you are interested I can provide you further details.
  • Jake · 2 years ago
    I just read the source code; it is very clean. However, there is some documentation on the internals in the OCaml-Java project; see

    http://cadmium.x9c.fr/downloads.html

    Your snippet manager would be useful in ocamljs since minimizing the resulting Javascript is important for web apps.
  • Issac Trotts · 1 year ago
    Nice stuff, thanks very much for implementing it. In the interest of making it even nicer, here's a bug report. It looks like there is a bug with support for closures, that could be fixed using the JS 'with' construct. For details, search for "(with" in http://blogs.bl0rg.net/netzstaub/2005/03/09/jav...

    let foo() =
    let a = ref [] in
    for i = 1 to 5 do
    a := (fun x -> x + i) :: !a;
    done;
    Array.of_list !a;;

    let foo_test() =
    let a = foo() in
    assert ((a.(0) 3) == 4);
    assert ((a.(1) 3) == 5);;

    foo_test();;
  • Jake · 1 year ago
    Issac, thanks for the bug report. I will try to get a fix released soon.
  • Florian Hars · 1 year ago
    "with" in javascript is absolutely evil. The code should compile to someting like

    for (var i$60 = 1; i$60
  • Florian Hars · 1 year ago
    I made you a bug report but your submission form has eated it.
  • Florian Hars · 1 year ago
    Another try:

    The code should compile to someting like

    for (var i$60 = 1; i$60 <= 5; i$60++) (function() { var i$60$ = i$60; a$59 = $(_f(function (x$61) { return x$61 + i$60$; }), a$59)})();

    instead of

    for (var i$60 = 1; i$60 <= 5; i$60++) a$59 = $(_f(function (x$61) { return x$61 + i$60; }), a$59);

    Only functions, not blocks, are scopes in javascript.
    The assers in Isaacs code should obviously be

    assert ((a.(0) 3) == 8);
    assert ((a.(1) 3) == 7);;

    And print_int in ocamljs adds a newline that ocaml proper doesn't add.
  • Jake · 1 year ago
    Thanks Florian. I am working on a new release of ocamljs (see the Google Code link above) and will make sure this is fixed.