|
| 1 | +(* Js_of_ocaml compiler |
| 2 | + * http://www.ocsigen.org/js_of_ocaml/ |
| 3 | + * Copyright (C) 2024 Hugo Heuzard |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Lesser General Public License as published by |
| 7 | + * the Free Software Foundation, with linking exception; |
| 8 | + * either version 2.1 of the License, or (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Lesser General Public License |
| 16 | + * along with this program; if not, write to the Free Software |
| 17 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 | + *) |
| 19 | + |
| 20 | +open Util |
| 21 | + |
| 22 | +let%expect_test "let rec" = |
| 23 | + let p = |
| 24 | + {| |
| 25 | + let rec a x = |
| 26 | + (* syntactic function *) |
| 27 | + b x |
| 28 | + and b = |
| 29 | + (* non-syntactic function *) |
| 30 | + let tbl : (int, int) Hashtbl.t = Hashtbl.create 17 in |
| 31 | + fun x -> `T (tbl, c, a 0) |
| 32 | + and c = |
| 33 | + (* block *) |
| 34 | + Some (d, default) |
| 35 | + and d = |
| 36 | + (* 'dynamic' value (not recursive *) |
| 37 | + Array.make 5 0 |
| 38 | + and default = |
| 39 | + (* constant, with (spurious) use |
| 40 | + of a recursive neighbor *) |
| 41 | + let _ = a in |
| 42 | + 42 |
| 43 | + |} |
| 44 | + in |
| 45 | + let p = compile_and_parse p in |
| 46 | + print_program p; |
| 47 | + [%expect |
| 48 | + {| |
| 49 | + (function(globalThis){ |
| 50 | + "use strict"; |
| 51 | + var |
| 52 | + runtime = globalThis.jsoo_runtime, |
| 53 | + caml_update_dummy = runtime.caml_update_dummy; |
| 54 | + function caml_call2(f, a0, a1){ |
| 55 | + return (f.l >= 0 ? f.l : f.l = f.length) === 2 |
| 56 | + ? f(a0, a1) |
| 57 | + : runtime.caml_call_gen(f, [a0, a1]); |
| 58 | + } |
| 59 | + var |
| 60 | + global_data = runtime.caml_get_global_data(), |
| 61 | + Stdlib_Hashtbl = global_data.Stdlib__Hashtbl, |
| 62 | + letrec_function_context = [], |
| 63 | + c = [], |
| 64 | + d = runtime.caml_make_vect(5, 0), |
| 65 | + default$0 = 42; |
| 66 | + function a(x){return b(x);} |
| 67 | + function b(x){ |
| 68 | + var _a_ = b(0); |
| 69 | + return [0, 84, [0, letrec_function_context[1], c, _a_]]; |
| 70 | + } |
| 71 | + var tbl = caml_call2(Stdlib_Hashtbl[1], 0, 17); |
| 72 | + caml_update_dummy(letrec_function_context, [0, tbl]); |
| 73 | + caml_update_dummy(c, [0, [0, d, default$0]]); |
| 74 | + var Test = [0, a, b, c, d, default$0]; |
| 75 | + runtime.caml_register_global(1, Test, "Test"); |
| 76 | + return; |
| 77 | + } |
| 78 | + (globalThis)); |
| 79 | + //end |
| 80 | + |}] |
0 commit comments