Skip to content

Commit 566ca51

Browse files
hhugovouillon
andcommitted
Runtime: reimplement weak/ephemeron
Co-authored-by: Jérôme Vouillon <jerome.vouillon@gmail.com>
1 parent 59f4693 commit 566ca51

File tree

3 files changed

+270
-88
lines changed

3 files changed

+270
-88
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* Runtime: backtraces are really expensive, they need to be be explicitly
2222
requested at compile time (--enable with-js-error) or at startup (OCAMLRUNPARAM=b=1)
2323
* Runtime: allow dynlink of precompiled js with separate compilation (#1676)
24+
* Runtime: reimplement the runtime of weak and ephemeron (#1707)
2425
* Lib: Modify Typed_array API for compatibility with WebAssembly
2526
* Toplevel: no longer set globals for toplevel initialization
2627

compiler/tests-jsoo/test_weak.ml

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
(* Js_of_ocaml tests
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+
let%expect_test _ =
21+
let k1 = Some 2 in
22+
let k2 = Some 3 in
23+
let k3 = Some 4 in
24+
let d = k1, k2, k3 in
25+
let e = Ephemeron.Kn.make [| k1; k2; k3 |] d in
26+
(match Ephemeron.Kn.query e [| k1; k2; k3 |] with
27+
| None -> print_endline "none"
28+
| Some d' ->
29+
assert (d = d');
30+
print_endline "found");
31+
[%expect {| found |}]
32+
33+
let%expect_test _ =
34+
let module K = struct
35+
type t = int option
36+
37+
let equal (a : t) (b : t) = a = b
38+
39+
let hash (x : t) = Hashtbl.hash x
40+
end in
41+
let module T = Ephemeron.Kn.Make (K) in
42+
let f y =
43+
let k1 = Some 2 in
44+
let k2 = Some 3 in
45+
let k3 = Some y in
46+
let d = k1, k2, k3 in
47+
let t = T.create 10 in
48+
T.add t [| k1; k2; k3 |] d;
49+
T.add t [| k2; k3; k1 |] d;
50+
T.add t [| k3; k1; k2 |] d;
51+
match T.find_opt t [| k1; k2; k3 |] with
52+
| None -> print_endline "none"
53+
| Some d' ->
54+
assert (d = d');
55+
print_endline "found"
56+
in
57+
f 3;
58+
f 2;
59+
[%expect {|
60+
found
61+
found
62+
|}]
63+
64+
let copy_eq a b =
65+
if a == b
66+
then false
67+
else
68+
let a = Obj.repr a in
69+
let b = Obj.repr b in
70+
if Obj.size a <> Obj.size b || Obj.tag a <> Obj.tag b
71+
then false
72+
else
73+
let exception False in
74+
try
75+
for i = 0 to Obj.size a - 1 do
76+
if Obj.field a i != Obj.field b i then raise False
77+
done;
78+
true
79+
with False -> false
80+
81+
let bool x = Printf.printf "%b" x
82+
83+
let%expect_test _ =
84+
let module E = Obj.Ephemeron in
85+
let ki = Obj.repr None in
86+
let k1 = Obj.repr (Some 2) in
87+
let k2 = Obj.repr (Some 43) in
88+
let e = E.create 10 in
89+
let e2 = E.create 3 in
90+
Printf.printf "%d\n" (E.length e);
91+
[%expect {| 10 |}];
92+
E.set_key e 1 ki;
93+
E.set_key e 2 k1;
94+
E.set_key e 3 k2;
95+
bool (Option.get (E.get_key e 2) == k1);
96+
[%expect {| true |}];
97+
bool (Option.get (E.get_key_copy e 2) == k1);
98+
[%expect {| false |}];
99+
bool (copy_eq (Option.get (E.get_key_copy e 2)) k1);
100+
[%expect {| true |}];
101+
bool (Option.get (E.get_key e 1) == ki);
102+
[%expect {| true |}];
103+
bool (Option.get (E.get_key_copy e 1) == ki);
104+
[%expect {| true |}];
105+
bool (copy_eq (Option.get (E.get_key_copy e 1)) ki);
106+
[%expect {| false |}];
107+
bool (E.check_key e 0);
108+
[%expect {| false |}];
109+
bool (E.check_key e 2);
110+
[%expect {| true |}];
111+
bool (E.check_key e 3);
112+
[%expect {| true |}];
113+
E.unset_key e 3;
114+
bool (E.check_key e 3);
115+
[%expect {| false |}];
116+
117+
bool (E.check_data e);
118+
[%expect {| false |}];
119+
E.set_data e k1;
120+
bool (E.check_data e);
121+
[%expect {| true |}];
122+
123+
bool (Option.get (E.get_data e) == k1);
124+
[%expect {| true |}];
125+
bool (Option.get (E.get_data_copy e) == k1);
126+
[%expect {| false |}];
127+
bool (copy_eq (Option.get (E.get_data_copy e)) k1);
128+
[%expect {| true |}];
129+
130+
E.set_data e ki;
131+
bool (Option.get (E.get_data e) == ki);
132+
[%expect {| true |}];
133+
bool (Option.get (E.get_data_copy e) == ki);
134+
[%expect {| true |}];
135+
bool (copy_eq (Option.get (E.get_data_copy e)) ki);
136+
[%expect {| false |}];
137+
138+
bool (E.check_data e2);
139+
[%expect {| false |}];
140+
E.blit_data e e2;
141+
bool (E.check_data e2);
142+
[%expect {| true |}];
143+
144+
E.blit_key e 1 e2 0 3;
145+
bool (E.check_key e2 0);
146+
[%expect {| true |}];
147+
bool (E.check_key e2 1);
148+
[%expect {| true |}];
149+
bool (E.check_key e2 2);
150+
[%expect {| false |}];
151+
152+
E.unset_data e;
153+
bool (E.check_data e);
154+
[%expect {| false |}]

0 commit comments

Comments
 (0)