Skip to content

Commit

Permalink
Fix Recursive Modules on ocaml < 4.13 (#1485)
Browse files Browse the repository at this point in the history
* Use caml_alloc_dummy_infix where it is needed
* Update changes
* Tests: allow to run some test with OCaml 4.12
* Fix caml_update_dummy

Co-authored-by: Hugo Heuzard <hugo.heuzard@gmail.com>
  • Loading branch information
mlasson and hhugo authored Jul 5, 2023
1 parent 168d44f commit db77ca4
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Dev (2023-??-??) - ??

## Bug fixes
* Runtime: Fix recursive modules on ocaml < 4.13 (#1485)
* Runtime: fix hashing of NaN (#1475)
* Runtime: float rounding should resolve tie away from zero (#1475)
* Runtime: fix Gc.stat, Gc.quick_stat, Gc.get (#1475)
Expand Down
15 changes: 14 additions & 1 deletion compiler/tests-jsoo/dune
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
(library
(name jsoo_testsuite_latest)
(modules test_io test_floats)
(libraries unix compiler-libs.common js_of_ocaml-compiler)
(enabled_if
(>= %{ocaml_version} 4.14))
(inline_tests
(modes js best))
(preprocess
(pps ppx_expect)))

(library
(name jsoo_testsuite)
(libraries unix compiler-libs.common)
(modules
(:standard \ test_io test_floats))
(libraries unix compiler-libs.common js_of_ocaml-compiler)
(foreign_stubs
(language c)
(names bigarray_stubs flush_stubs))
Expand Down
6 changes: 5 additions & 1 deletion compiler/tests-jsoo/test_rec_mod.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*)

open Js_of_ocaml_compiler.Stdlib

(* https://github.com/ocaml/ocaml/pull/9497 *)
(* Original test case from the issue: *)

Expand All @@ -36,7 +38,9 @@ let%expect_test _ =
ignore (IdSet.mem { id = 1 } basic_set : bool)
(* diverge here *)
with e ->
assert (String.ends_with ~suffix:"Undefined recursive module" (Printexc.to_string e))
if String.is_suffix ~suffix:"Undefined recursive module" (Printexc.to_string e)
then ()
else raise e

(* Looping version *)
module rec M1 : sig
Expand Down
6 changes: 4 additions & 2 deletions compiler/tests-jsoo/test_time.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*)

open Js_of_ocaml_compiler.Stdlib

let%expect_test _ =
let f = Unix.time () in
let z, _integral = Float.modf f in
let z, _integral = modf f in
match Float.classify_float f, Float.classify_float z with
| FP_normal, FP_zero -> ()
| _ -> assert false
Expand Down Expand Up @@ -49,7 +51,7 @@ let%expect_test _ =
assert (
String.length s > 0
&& String.for_all
(function
~f:(function
| '0' .. '9' | ' ' -> true
| _ -> false)
s)
Expand Down
12 changes: 8 additions & 4 deletions runtime/internalMod.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

//Provides: caml_CamlinternalMod_init_mod
//Requires: caml_raise_with_arg, caml_global_data
//Requires: caml_raise_with_arg, caml_global_data, caml_alloc_dummy_infix
//If: !effects
//Version: < 4.13
function caml_CamlinternalMod_init_mod(loc,shape) {
Expand All @@ -29,7 +29,9 @@ function caml_CamlinternalMod_init_mod(loc,shape) {
if(typeof shape === "number")
switch(shape){
case 0://function
struct[idx]={fun:undef_module};
var dummy=caml_alloc_dummy_infix();
dummy.fun=undef_module;
struct[idx]=dummy;
break;
case 1://lazy
struct[idx]=[246, undef_module];
Expand Down Expand Up @@ -78,7 +80,7 @@ function caml_CamlinternalMod_update_mod(shape,real,x) {
}

//Provides: caml_CamlinternalMod_init_mod
//Requires: caml_raise_with_arg, caml_global_data
//Requires: caml_raise_with_arg, caml_global_data, caml_alloc_dummy_infix
//If: effects
//Version: < 4.13
function caml_CamlinternalMod_init_mod(loc,shape,cont) {
Expand All @@ -89,7 +91,9 @@ function caml_CamlinternalMod_init_mod(loc,shape,cont) {
if(typeof shape === "number")
switch(shape){
case 0://function
struct[idx]={fun:undef_module};
var dummy=caml_alloc_dummy_infix();
dummy.fun=undef_module;
struct[idx]=dummy;
break;
case 1://lazy
struct[idx]=[246, undef_module];
Expand Down
2 changes: 1 addition & 1 deletion runtime/obj.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

//Provides: caml_update_dummy
function caml_update_dummy (x, y) {
if( typeof y==="function" ) { x.fun = y; return 0; }
if( y.fun ) { x.fun = y.fun; return 0; }
if( typeof y==="function" ) { x.fun = y; return 0; }
var i = y.length; while (i--) x[i] = y[i]; return 0;
}

Expand Down

0 comments on commit db77ca4

Please sign in to comment.