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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
(* Js_of_ocaml compiler
* http://www.ocsigen.org/js_of_ocaml/
* Copyright (C) 2019 Ty Overby
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, with linking exception;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*)
open Util
module M1 = struct
let code =
{|
let f_prime g = try g 1 2 with e -> raise e
let f g = f_prime g 3 4
(* [g] will be unknown as long as [f] is not inlined. *)
let g () = f (fun a b c d -> print_int (a + b + c + d))
let h () = f (fun a b c d -> print_int (a + b + c + d))
let () = g (); print_newline ()
let () = h (); print_newline ()
(* using [f] multiple time should prevent inlining to trigger. *)
let k a b c d = a + b + c + d |> print_int
let l = k 1 2
let m = k 3 4
let () = l 5 6; print_newline ()
let () = m 8 10; print_newline ()
|}
let%expect_test "executed code" =
compile_and_run code;
[%expect {|
10
10
14
25 |}]
let%expect_test "generated code" =
let generated = compile_and_parse code in
print_fun_decl generated (Some "f");
print_fun_decl generated (Some "f_prime");
print_fun_decl generated (Some "g");
print_fun_decl generated (Some "h");
print_fun_decl generated (Some "k");
print_fun_decl generated (Some "l");
print_fun_decl generated (Some "m");
print_fun_decl generated (Some "caml_call1");
print_fun_decl generated (Some "caml_call2");
[%expect
{|
function f(g){return caml_call2(f_prime(g), 3, 4);}
//end
function f_prime(g){
try{var _i_ = caml_call2(g, 1, 2); return _i_;}
catch(e$0){
var e = caml_wrap_exception(e$0);
throw caml_maybe_attach_backtrace(e, 0);
}
}
//end
function g(param){
return f
(function(a, b, c, d){
return caml_call1(Stdlib[44], ((a + b | 0) + c | 0) + d | 0);
});
}
//end
function h(param){
return f
(function(a, b, c, d){
return caml_call1(Stdlib[44], ((a + b | 0) + c | 0) + d | 0);
});
}
//end
function k(a, b, c, d){
return caml_call1(Stdlib[44], ((a + b | 0) + c | 0) + d | 0);
}
//end
function l(_g_, _h_){return k(_b_, _a_, _g_, _h_);}
//end
function m(_e_, _f_){return k(_d_, _c_, _e_, _f_);}
//end
function caml_call1(f, a0){
return (f.l >= 0 ? f.l : f.l = f.length) === 1
? f(a0)
: runtime.caml_call_gen(f, [a0]);
}
//end
function caml_call2(f, a0, a1){
return (f.l >= 0 ? f.l : f.l = f.length) === 2
? f(a0, a1)
: runtime.caml_call_gen(f, [a0, a1]);
}
//end
|}]
end
module M2 = struct
let code =
{|
let f _ a b c d e (_f: int -> int -> int -> int -> int -> unit -> unit) =
print_int (a + b + c + d + e);
print_newline ();;
let f_prime f = f true ;;
let f_prime_prime f = f false;;
let g _a _b _c _d _e _f = failwith "printed g!" ;;
let () = f_prime f 1 2 3 4 5 g ;;
let () = f_prime f 2 3 4 5 6 g ;;
let () = f_prime_prime f 1 2 3 4 5 g ;;
let () = f_prime_prime f 2 3 4 5 6 g ;;
|}
let%expect_test "generated code" =
let generated = compile_and_parse code in
print_fun_decl generated (Some "f");
print_fun_decl generated (Some "f_prime");
print_fun_decl generated (Some "f_prime_prime");
print_fun_decl generated (Some "g");
[%expect
{|
function f(param, a, b, c, d, e, f){
caml_call1(Stdlib[44], (((a + b | 0) + c | 0) + d | 0) + e | 0);
return caml_call1(Stdlib[47], 0);
}
//end
function f_prime(f){return caml_call1(f, 1);}
//end
function f_prime_prime(f){return caml_call1(f, 0);}
//end
function g(a, b, c, d, e, f){return caml_call1(Stdlib[2], cst_printed_g);}
//end |}]
let%expect_test _ =
compile_and_run code;
[%expect {|
15
20
15
20 |}]
end
|