Skip to content

Commit

Permalink
Merge pull request #31 from andersfugmann/andersfugmann/melange_compat
Browse files Browse the repository at this point in the history
Lazy backend
  • Loading branch information
andersfugmann authored Apr 25, 2024
2 parents f3fda16 + 33eb771 commit 6a63ef9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/ocaml_protoc_plugin/ocaml_protoc_plugin.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,12 @@ module Service = Service
module Result = Result
module Extensions = Extensions
module Json_options = Json_options


let apply_lazy f =
match Sys.backend_type with
| Native | Bytecode ->
f ()
| Other _ ->
let f = Lazy.from_fun f in
fun x -> (Lazy.force f) x
6 changes: 3 additions & 3 deletions src/plugin/emit.ml
Original file line number Diff line number Diff line change
Expand Up @@ -304,22 +304,22 @@ let rec emit_message ~params ~syntax ~scope ~type_db ~comment_db
Code.emit implementation `None "let spec () = %s" spec_str;

Code.emit implementation `Begin "let to_proto' =";
Code.emit implementation `None "let serialize = Runtime'.Serialize.serialize (spec ()) in";
Code.emit implementation `None "let serialize = Runtime'.apply_lazy (fun () -> Runtime'.Serialize.serialize (spec ())) in";
Code.emit implementation `None "fun writer %s -> serialize writer %s" destructor (String.concat ~sep:" " args);
Code.emit implementation `End "";

Code.emit implementation `None "let to_proto t = let writer = Runtime'.Writer.init () in to_proto' writer t; writer";

Code.emit implementation `Begin "let from_proto_exn =";
Code.emit implementation `None "let constructor %s = %s in" (String.concat ~sep:" " args) destructor;
Code.emit implementation `None "Runtime'.Deserialize.deserialize (spec ()) constructor";
Code.emit implementation `None "Runtime'.apply_lazy (fun () -> Runtime'.Deserialize.deserialize (spec ()) constructor)";
Code.emit implementation `End "let from_proto writer = Runtime'.Result.catch (fun () -> from_proto_exn writer)";
Code.emit implementation `Begin "let to_json options = ";
Code.emit implementation `None "let serialize = Runtime'.Serialize_json.serialize ~message_name:(name ()) (spec ()) options in";
Code.emit implementation `None "fun %s -> serialize %s" destructor (String.concat ~sep:" " args);
Code.emit implementation `EndBegin "let from_json_exn =";
Code.emit implementation `None "let constructor %s = %s in" (String.concat ~sep:" " args) destructor;
Code.emit implementation `None "Runtime'.Deserialize_json.deserialize ~message_name:(name ()) (spec ()) constructor";
Code.emit implementation `None "Runtime'.apply_lazy (fun () -> Runtime'.Deserialize_json.deserialize ~message_name:(name ()) (spec ()) constructor)";
Code.emit implementation `End "let from_json json = Runtime'.Result.catch (fun () -> from_json_exn json)";
| None -> ()
in
Expand Down

0 comments on commit 6a63ef9

Please sign in to comment.