Skip to content

"Stack overflow" exception in JavaScript execution mode (with Js_of_ocaml) #138

@pbaudin

Description

@pbaudin

Within the JavaScript mode the compilation of the recursive functions is not optimized (c.f. http://ocsigen.org/js_of_ocaml/3.7.0/manual/tailcall).

There is the Javascript translation of your Ppx_deriving_yojson_runtime.map_bind function :

 function map_bind(f,acc,xs)
     {if(xs)
       {var
         xs$0=xs[2],
         x=xs[1],
         _b_=function(x){return map_bind(f,[0,x,acc],xs$0)};
        return symbol_bind(caml_call1(f,x),_b_)}
      return [0,caml_call1(Stdlib_list[9],acc)]}

So, the conversion of a Yojson.Safe.t data into a long list of elements (~10.000) could raise a Stack overflow exception.

Notice that kind of implementation (without acc) solves the problem:

let save_map_bind f xs =
  let exception Error_map_bind of string in
  try
    Result.Ok (List.rev (List.rev_map
                                     (fun x -> match f x with
                                      | Result.Ok x -> x
                                      | Result.Error s -> raise (Error_map_bind s)) xs))
  with Error_map_bind s -> Result.Error s

Of course, the proposed implementation can certainly be improved since the type of that save_map_bind function is less generic than your map_bind function (but it seems to me that the proposed implementation is compliant with the .mli interface except about the removed accumulator).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions