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
|
(* Js_of_ocaml compiler
* http://www.ocsigen.org/js_of_ocaml/
* Copyright (C) 2017 Hugo Heuzard
* 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.
*)
(* https://github.com/ocsigen/js_of_ocaml/commit/a1a24b53e3e25af30b30e2e1779991db1055143e *)
let%expect_test _ =
let prog =
{|
let log_success () = print_endline "Success!"
let log_failure = Printf.printf "Failure! %s"
let fun1 () =
let rec odd x = if x = 0 then false else even (x - 1)
and even x = if x = 0 then true else odd (x - 1) in
assert (odd 1 <> even 1);
try
ignore (odd 5000);
log_success ()
with _ -> log_failure "too much recursion"
let () = fun1 ()
|}
in
Util.compile_and_run prog;
[%expect {| Success! |}];
let program = Util.compile_and_parse prog in
Util.print_fun_decl program (Some "fun1");
[%expect
{|
function fun1(param){
function odd$0(counter, x){
if(0 === x) return 0;
var _f_ = x - 1 | 0;
if(counter >= 50) return caml_trampoline_return(even$0, [0, _f_]);
var counter$0 = counter + 1 | 0;
return even$0(counter$0, _f_);
}
function odd(x){return caml_trampoline(odd$0(0, x));}
function even$0(counter, x){
if(0 === x) return 1;
var _e_ = x - 1 | 0;
if(counter >= 50) return caml_trampoline_return(odd$0, [0, _e_]);
var counter$0 = counter + 1 | 0;
return odd$0(counter$0, _e_);
}
function even(x){return caml_trampoline(even$0(0, x));}
var _b_ = even(1);
if(odd(1) === _b_)
throw caml_maybe_attach_backtrace([0, Assert_failure, _a_], 1);
try{odd(5000); var _c_ = log_success(0); return _c_;}
catch(_d_){return caml_call1(log_failure, cst_too_much_recursion);}
}
//end |}]
let%expect_test _ =
let prog =
{|
let log_success () = print_endline "Success!"
let log_failure = Printf.printf "Failure! %s"
let fun1 () =
let rec odd x = if x = 0 then false else even (x - 1)
and even x = if x = 0 then true else odd (x - 1) in
assert (odd 1 <> even 1);
try
ignore (odd 5000);
log_success ()
with _ -> log_failure "too much recursion"
let () = fun1 ()
|}
in
Util.compile_and_run prog;
[%expect {| Success! |}];
let program = Util.compile_and_parse ~flags:[ "--set"; "tc_depth=0" ] prog in
Util.print_fun_decl program (Some "fun1");
[%expect
{|
function fun1(param){
function odd$0(x){
return 0 === x ? 0 : caml_trampoline_return(even$0, [0, x - 1 | 0]);
}
function odd(x){return caml_trampoline(odd$0(x));}
function even$0(x){
return 0 === x ? 1 : caml_trampoline_return(odd$0, [0, x - 1 | 0]);
}
function even(x){return caml_trampoline(even$0(x));}
var _b_ = even(1);
if(odd(1) === _b_)
throw caml_maybe_attach_backtrace([0, Assert_failure, _a_], 1);
try{odd(5000); var _c_ = log_success(0); return _c_;}
catch(_d_){return caml_call1(log_failure, cst_too_much_recursion);}
}
//end |}]
|