Skip to content

Instantly share code, notes, and snippets.

@sekia
Created July 26, 2017 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sekia/794d2ac98a085efa01436f56658df12c to your computer and use it in GitHub Desktop.
Save sekia/794d2ac98a085efa01436f56658df12c to your computer and use it in GitHub Desktop.
Since js_of_ocaml cannot transform tail call of function given as an argument into simple loop, event loop with Lwt.bind (>>=) is unimplementable.
let () =
let n = int_of_string Sys.argv.(1) in
let open Lwt.Infix in
let ping = Lwt_mvar.create_empty () in
let rec consumer () =
Lwt_mvar.take ping >>= function
| false -> Lwt.return_unit
| true -> print_endline "ping"; consumer () in
let producer () =
let rec loop i = Lwt_mvar.put ping (i < n) >>= fun () -> loop (i + 1) in
loop 0 in
Lwt.async producer;
Lwt.ignore_result @@ consumer ()
bash-3.2$ ocamlfind ocamlc -package lwt -linkpkg -o actor.byte actor.ml && js_of_ocaml actor.byte
bash-3.2$ ./actor.byte 20000 > /dev/null
bash-3.2$ node ./actor.js 20000 > /dev/null
buffer.js:34
super(arg1, arg2, arg3);
^
RangeError: Maximum call stack size exceeded
at Buffer.Uint8Array (native)
at FastBuffer (buffer.js:34:5)
at fromString (buffer.js:274:11)
at Function.Buffer.from (buffer.js:150:12)
at decodeChunk (_stream_writable.js:315:20)
at writeOrBuffer (_stream_writable.js:325:20)
at SyncWriteStream.Writable.write (_stream_writable.js:274:11)
at Object.gb [as output] (/Users/koichi/toybox/actor/actor.js:299:77)
at aw (/Users/koichi/toybox/actor/actor.js:203:40)
at cy (/Users/koichi/toybox/actor/actor.js:317:88)
at bd (/Users/koichi/toybox/actor/actor.js:319:29)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment