package js_of_ocaml-compiler

  1. Overview
  2. Docs
Compiler from OCaml bytecode to JavaScript

Install

Dune Dependency

Authors

Maintainers

Sources

js_of_ocaml-4.1.0.tbz
sha256=91793f29a5af3deaba3f7d35ceab11e739cfe0a5cc30556f6201461a6d4236ef
sha512=43f91c1f37939dfc894558a23a8ca163e6f316db442bdd0a5909bd095f5b0eb596d0943883452f4b78641da71222a775476877ea9ba0500583700d1fb35e04a5

doc/src/js_of_ocaml-compiler.dynlink/js_of_ocaml_compiler_dynlink.ml.html

Source file js_of_ocaml_compiler_dynlink.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
open Js_of_ocaml_compiler.Stdlib
open Js_of_ocaml_compiler
module J = Jsoo_runtime.Js

let split_primitives p =
  let len = String.length p in
  let rec split beg cur =
    if cur >= len
    then []
    else if Char.equal p.[cur] '\000'
    then String.sub p ~pos:beg ~len:(cur - beg) :: split (cur + 1) (cur + 1)
    else split beg (cur + 1)
  in
  Array.of_list (split 0 0)

let () =
  let global = J.pure_js_expr "globalThis" in
  (* this needs to stay synchronized with toplevel.js *)
  let toplevel_compile (s : bytes array) (debug : Instruct.debug_event list array) :
      unit -> J.t =
    let s = String.concat ~sep:"" (List.map ~f:Bytes.to_string (Array.to_list s)) in
    let prims = split_primitives (Symtable.data_primitive_names ()) in
    let output_program = Driver.from_string ~prims ~debug s in
    let b = Buffer.create 100 in
    output_program (Pretty_print.to_buffer b);
    Format.(pp_print_flush std_formatter ());
    Format.(pp_print_flush err_formatter ());
    flush stdout;
    flush stderr;
    let js = Buffer.contents b in
    let res : string -> unit -> J.t =
      Obj.magic (J.get global (J.string "toplevelEval"))
    in
    res (js : string)
  in
  let toplevel_eval (x : string) : unit -> J.t =
    let f : J.t -> J.t = J.eval_string x in
    fun () ->
      let res = f global in
      Format.(pp_print_flush std_formatter ());
      Format.(pp_print_flush err_formatter ());
      flush stdout;
      flush stderr;
      res
  in
  let toplevel_reloc (name : J.t) : int =
    let name = J.to_string name in
    Js_of_ocaml_compiler.Ocaml_compiler.Symtable.reloc_ident name
  in
  J.set global (J.string "toplevelCompile") (Obj.magic toplevel_compile) (*XXX HACK!*);
  J.set global (J.string "toplevelEval") (Obj.magic toplevel_eval);
  J.set global (J.string "toplevelReloc") (Obj.magic toplevel_reloc)
OCaml

Innovation. Community. Security.