Skip to content

Commit 8803c7d

Browse files
hhugovouillon
authored andcommitted
Tests: recursive value compilation
1 parent a5731a0 commit 8803c7d

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

compiler/tests-compiler/dune.inc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,21 @@
659659
(preprocess
660660
(pps ppx_expect)))
661661

662+
(library
663+
;; compiler/tests-compiler/rec.ml
664+
(name rec_15)
665+
(enabled_if %{env:js-enabled=})
666+
(modules rec)
667+
(libraries js_of_ocaml_compiler unix str jsoo_compiler_expect_tests_helper)
668+
(inline_tests
669+
(enabled_if true)
670+
(deps
671+
(file %{project_root}/compiler/bin-js_of_ocaml/js_of_ocaml.exe)
672+
(file %{project_root}/compiler/bin-jsoo_minify/jsoo_minify.exe)))
673+
(flags (:standard -open Jsoo_compiler_expect_tests_helper))
674+
(preprocess
675+
(pps ppx_expect)))
676+
662677
(library
663678
;; compiler/tests-compiler/scopes.ml
664679
(name scopes_15)

compiler/tests-compiler/rec.ml

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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_call1(f, a0){
55+
return (f.l >= 0 ? f.l : f.l = f.length) === 1
56+
? f(a0)
57+
: runtime.caml_call_gen(f, [a0]);
58+
}
59+
function caml_call2(f, a0, a1){
60+
return (f.l >= 0 ? f.l : f.l = f.length) === 2
61+
? f(a0, a1)
62+
: runtime.caml_call_gen(f, [a0, a1]);
63+
}
64+
var
65+
global_data = runtime.caml_get_global_data(),
66+
Stdlib_Hashtbl = global_data.Stdlib__Hashtbl,
67+
a = function _d_(_c_){return _d_.fun(_c_);},
68+
b = function _b_(_a_){return _b_.fun(_a_);},
69+
c = [],
70+
d = runtime.caml_make_vect(5, 0),
71+
default$0 = 42;
72+
caml_update_dummy(a, function(x){return caml_call1(b, x);});
73+
var tbl = caml_call2(Stdlib_Hashtbl[1], 0, 17);
74+
caml_update_dummy
75+
(b, function(x){return [0, 84, [0, tbl, c, caml_call1(a, 0)]];});
76+
caml_update_dummy(c, [0, [0, d, default$0]]);
77+
var Test = [0, a, b, c, d, default$0];
78+
runtime.caml_register_global(1, Test, "Test");
79+
return;
80+
}
81+
(globalThis));
82+
//end |}]

0 commit comments

Comments
 (0)