0

Seeing as both the OCaml compiler and js_of_ocaml are written in OCaml, it seems reasonable (to me) that I should be able to write a wrapper that uses:

  1. the OCaml compilerlibs to compile OCaml source into, say Instruct.instruction list (using Bytegen.compile_implementation), and
  2. js_of_ocaml to compile the instruction list into JavaScript, thereby bypassing the Parse_bytecode.

I would therefore keep all data in memory, avoiding writing and then reading the cmo file(s). I expect I could write the cmo to a buffer and have js_of_ocaml process that, but it seems at best inefficient.

Is this realistic/has it been done before, or am I just trying to join the two components in the wrong place/way?

user207421
  • 305,947
  • 44
  • 307
  • 483
ngunn
  • 1
  • The tag `compiler` should be applied to questions concerning the programming of compilers or for questions about the detailed inner workings of compilers. Don't use `compiler` for questions about options and settings for a particular compiler, use the name of the compiler you are interested in instead. – user207421 Nov 14 '13 at 00:31
  • There is no tag for one of the compilers (ocamlc). I used the tag of the other compiler involved (js_of_ocaml). Also, I referenced the inner workings of both compilers (Instruct and Parse_bytecode). My question is how I programmatically glue the two compilers together. – ngunn Nov 14 '13 at 08:45

2 Answers2

0

I'm almost sure that http://edit.ocamlpro.com/ uses this approach when compiling source code to OCaml byte code online in browser.

source: https://github.com/pcouderc/ocp-webedit

Kakadu
  • 2,837
  • 19
  • 29
  • src/toplevel-src/toplevelw.ml implies compiler+interpreter execute within JavaScript (bytecode is interpreted). But, src/main-src/mytoplevel includes an Eval (str) (implying some compilation to JavaScript). Then, mycompile appears to compile only to bytecode. There doesn't seem to be any tight integration here. Otherwise, if edit.ocamlpro is similar to the toplevel in js_of_ocaml, it compiles to bytecode and interprets that in a JavaScript compiled program. No JavaScript compilation necessary. – ngunn Nov 14 '13 at 09:36
  • I can't get whole point of your comment but ocp-webedit allows to download generated bytecode to machine, and it doesn't perform it on separated server, hence it generates bytecode in Javascript, hence it uses compiler-libs on client side. What am I missing? – Kakadu Nov 14 '13 at 10:25
  • Yes, it uses compilerlibs on the client side to generate bytecode. That bytecode is executed in the OCaml VM (see toploop). The VM is itself compiled from bytecode to JavaScript and executed in the browser's JavaScript VM. What would be equivalent is execution of js_of_ocaml in the browser to skip the OCaml VM step. – ngunn Nov 14 '13 at 19:33
0

They say it is easier to maintain.

https://www.lri.fr/~conchon/TER/2012/3/js_of_ocaml.pdf

Virtual machines provide a very stable API. ... Thus, there is no need to modify the compiler at each release of the language to support the latest features

zakki
  • 2,283
  • 14
  • 20
  • Thanks for the ref and quote, which closes the door on the approach. Shame because `js_of_ocaml/instr.ml` looks like it converts bytecode back to instructions while `ocamlc/emitcode.ml` converts instructions to bytecode and it is hard to see why divergence between instruction sets would be ease maintenance. – ngunn Nov 14 '13 at 19:48
  • js_of_ocaml has to check consistency between cmo files, and link these files? – zakki Nov 15 '13 at 02:57