6

I'm quite new to OCaml and to the js_of_ocaml compiler in particular.

Did someone manage to compile the application which uses Jane Street Core with js_of_ocaml? I get lots of "missing primitives" warnings during the compilation, and then when I try to run it with node they turn into an actual exceptions.

I understand that those are primitives which can't be ported from OCaml to JS and that their implementation should depend on the application, but for core there is literally thousands of them, whereas my program only uses output to stdout.

On a side note, I had trouble even compiling a simple "hello world" project, as IO functions weren't implemented in JS. Is there a "standard" JS file somewhere which could be used for this purpose? e.g. replacing caml_ml_output_char with console.log and other things, so that modules can be compiled to something useful without writing any custom javascript?

Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
George Karpenkov
  • 2,094
  • 1
  • 16
  • 36

3 Answers3

3

Yes, it is possible to compile Core_kernel with js_of_ocaml, OCamlPro did a version of Try-OCaml with it. It requires to patch a few of its dependencies (sexplib, ounit, etc.) and to use the latest version of js_of_ocaml from the repository, that includes a bigarray implementation.

Fabrice Le Fessant
  • 4,222
  • 23
  • 36
3

as of today, core_kernel version 0.9, it works.

after compile to bytecode.

run js_of_ocaml

js_of_ocaml +weak.js +nat.js +base/runtime.js +core_kernel/runtime.js +bin_prot/runtime.js main.bc

or add the js_of_ocaml in jbuilder directly.

(js_of_ocaml (
    (flags (+weak.js +nat.js +base/runtime.js +core_kernel/runtime.js +bin_prot/runtime.js))))
echo
  • 2,666
  • 1
  • 25
  • 17
1

Yeah, missing primitives are the issue when using Core with js_of_ocaml. There is core_kernel library. It is a subset of Core which contains basic functions. UNIX-related functions from core are not included to Core_kernel. If I remember correctly, the main reason for extracting Core_kernel from Core is your issue.

Update. I have failed. It seems that developers have tried to allow using Core_kernel with js_of_ocaml but without success. It seems that you can't do it now. They are waiting for OCaml namespaces.

Kakadu
  • 2,837
  • 19
  • 29
  • Thanks for the answer! So what should I do? Open `core_kernel` instead? I'm quite confused as to why missing primitives should matter with core --- if I'm not using them in my code, couldn't they be thrown away safely? – George Karpenkov Feb 05 '14 at 14:48
  • Please do not use backticks for emphasis. They are for code. See http://stackoverflow.com/editing-help#code – Pascal Cuoq Feb 05 '14 at 15:58
  • @PascalCuoq But it is code, isn't it? I think of library name as code. – George Karpenkov Feb 05 '14 at 16:09
  • @cheshire Please only use backticks for sequences of characters that you would type as part of an OCaml program. This is how people use them and it is less ambiguous if everyone uses them the same way. Referring to the module `Core_kernel` is acceptable but to the library `core_kernel` is not. – Pascal Cuoq Feb 05 '14 at 16:20
  • what is the status now? – echo May 27 '17 at 19:22