Skip to content

Commit 489008a

Browse files
committed
add edge case tests for recurisve modules
1 parent 2c48c7d commit 489008a

File tree

4 files changed

+112
-16
lines changed

4 files changed

+112
-16
lines changed

Diff for: jscomp/runtime/caml_module.ml

+2-6
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,12 @@ type shape =
3535
| Module of shape array
3636
| Value of Caml_obj_extern.t
3737
(* ATTENTION: check across versions *)
38-
38+
module Array = Caml_array_extern
3939
(** Note that we have to provide a drop in replacement, since compiler internally will
4040
spit out ("CamlinternalMod".[init_mod|update_mod] unless we intercept it
4141
in the lambda layer
4242
*)
43-
let init_mod (loc : string * int * int) (shape : shape) =
44-
let module Array = Caml_array_extern in
43+
let init_mod (loc : string * int * int) (shape : shape) =
4544
let undef_module _ = raise (Undefined_recursive_module loc) in
4645
let rec loop (shape : shape) (struct_ : Caml_obj_extern.t array) idx =
4746
match shape with
@@ -67,17 +66,14 @@ let init_mod (loc : string * int * int) (shape : shape) =
6766
loop shape res 0 ;
6867
res.(0)
6968

70-
7169
(* Note the [shape] passed between [init_mod] and [update_mod] is always the same
7270
and we assume [module] is encoded as an array
7371
*)
7472
let update_mod (shape : shape) (o : Caml_obj_extern.t) (n : Caml_obj_extern.t) : unit =
75-
let module Array = Caml_array_extern in
7673
let rec aux (shape : shape) o n parent i =
7774
match shape with
7875
| Function
7976
-> Caml_obj_extern.set_field parent i n
80-
8177
| Lazy
8278
| Class ->
8379
Caml_obj.caml_update_dummy o n

Diff for: jscomp/test/recursive_module.js

+78-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
'use strict';
22

33
var Mt = require("./mt.js");
4+
var Lazy = require("../../lib/js/lazy.js");
45
var Curry = require("../../lib/js/curry.js");
6+
var Caml_obj = require("../../lib/js/caml_obj.js");
57
var Caml_module = require("../../lib/js/caml_module.js");
8+
var CamlinternalLazy = require("../../lib/js/camlinternalLazy.js");
69
var Caml_js_exceptions = require("../../lib/js/caml_js_exceptions.js");
710
var Caml_builtin_exceptions = require("../../lib/js/caml_builtin_exceptions.js");
811

@@ -42,22 +45,88 @@ var Int3 = Caml_module.init_mod([
4245

4346
Caml_module.update_mod([[0]], Int3, Int3);
4447

48+
var Inta = Caml_module.init_mod([
49+
"recursive_module.ml",
50+
23,
51+
6
52+
], [[1]]);
53+
54+
var Intb = Caml_module.init_mod([
55+
"recursive_module.ml",
56+
28,
57+
6
58+
], [[1]]);
59+
60+
var a = Caml_obj.caml_lazy_make((function (param) {
61+
return CamlinternalLazy.force(Intb[/* a */0]);
62+
}));
63+
64+
Caml_module.update_mod([[1]], Inta, /* module */[/* a */a]);
65+
66+
var a$1 = Caml_obj.caml_lazy_make((function (param) {
67+
return CamlinternalLazy.force(Inta[/* a */0]) + 1 | 0;
68+
}));
69+
70+
Caml_module.update_mod([[1]], Intb, /* module */[/* a */a$1]);
71+
4572
var tmp;
4673

74+
try {
75+
tmp = CamlinternalLazy.force(Intb[/* a */0]);
76+
}
77+
catch (exn){
78+
if (exn === Lazy.Undefined) {
79+
tmp = -1;
80+
} else {
81+
throw exn;
82+
}
83+
}
84+
85+
eq("File \"recursive_module.ml\", line 33, characters 3-10", -1, tmp);
86+
87+
var Inta$1 = Caml_module.init_mod([
88+
"recursive_module.ml",
89+
40,
90+
8
91+
], [[1]]);
92+
93+
var Intb$1 = Caml_module.init_mod([
94+
"recursive_module.ml",
95+
45,
96+
8
97+
], [[1]]);
98+
99+
var a$2 = Caml_obj.caml_lazy_make((function (param) {
100+
return CamlinternalLazy.force(Intb$1[/* a */0]) + 1 | 0;
101+
}));
102+
103+
Caml_module.update_mod([[1]], Inta$1, /* module */[/* a */a$2]);
104+
105+
Caml_module.update_mod([[1]], Intb$1, /* module */[/* a */2]);
106+
107+
var A = /* module */[
108+
/* Inta */Inta$1,
109+
/* Intb */Intb$1
110+
];
111+
112+
eq("File \"recursive_module.ml\", line 50, characters 6-13", CamlinternalLazy.force(Inta$1[/* a */0]), 3);
113+
114+
var tmp$1;
115+
47116
try {
48117
Curry._1(Int3[/* u */0], 3);
49-
tmp = 3;
118+
tmp$1 = 3;
50119
}
51120
catch (raw_exn){
52-
var exn = Caml_js_exceptions.internalToOCamlException(raw_exn);
53-
if (exn[0] === Caml_builtin_exceptions.undefined_recursive_module) {
54-
tmp = 4;
121+
var exn$1 = Caml_js_exceptions.internalToOCamlException(raw_exn);
122+
if (exn$1[0] === Caml_builtin_exceptions.undefined_recursive_module) {
123+
tmp$1 = 4;
55124
} else {
56-
throw exn;
125+
throw exn$1;
57126
}
58127
}
59128

60-
eq("File \"recursive_module.ml\", line 24, characters 6-13", 4, tmp);
129+
eq("File \"recursive_module.ml\", line 52, characters 6-13", 4, tmp$1);
61130

62131
Mt.from_pair_suites("Recursive_module", suites[0]);
63132

@@ -66,4 +135,7 @@ exports.test_id = test_id;
66135
exports.eq = eq;
67136
exports.Int32 = Int32;
68137
exports.Int3 = Int3;
138+
exports.Inta = Inta;
139+
exports.Intb = Intb;
140+
exports.A = A;
69141
/* Int32 Not a pure module */

Diff for: jscomp/test/recursive_module.ml

+30-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,36 @@ module rec Int3 : sig
1818
val u : int -> int
1919
end = Int3
2020

21-
22-
21+
module rec Inta : sig
22+
val a : int lazy_t
23+
end = struct
24+
let a = lazy (Lazy.force Intb.a)
25+
end
26+
and Intb : sig
27+
val a : int lazy_t
28+
end = struct
29+
let a = lazy (Lazy.force Inta.a + 1)
30+
end
31+
32+
;;
33+
eq __LOC__
34+
(-1)
35+
(try Lazy.force (Intb.a) with Lazy.Undefined -> -1)
36+
37+
module A = struct
38+
module rec Inta : sig
39+
val a : int lazy_t
40+
end = struct
41+
let a = lazy (Lazy.force Intb.a + 1)
42+
end
43+
and Intb : sig
44+
val a : int lazy_t
45+
end = struct
46+
let a = lazy 2
47+
end
48+
end
49+
50+
;; eq __LOC__ (Lazy.force A.Inta.a) 3
2351
(* expect raise Undefined_recursive_module *)
2452
;; eq __LOC__ 4
2553
(try ignore (Int3.u 3); 3

Diff for: lib/js/caml_module.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function update_mod(shape, o, n) {
7373
Caml_builtin_exceptions.assert_failure,
7474
/* tuple */[
7575
"caml_module.ml",
76-
95,
76+
91,
7777
10
7878
]
7979
];
@@ -82,7 +82,7 @@ function update_mod(shape, o, n) {
8282
Caml_builtin_exceptions.assert_failure,
8383
/* tuple */[
8484
"caml_module.ml",
85-
95,
85+
91,
8686
10
8787
]
8888
];

0 commit comments

Comments
 (0)