Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy backend #31

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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