Skip to content

Commit b73f7bc

Browse files
author
Hongbo Zhang
committed
more class tests and bug fix for #251
1 parent f152b97 commit b73f7bc

File tree

7 files changed

+129
-4
lines changed

7 files changed

+129
-4
lines changed

jscomp/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<!-- <script src="./async.js"></script> -->
1111
<script src="./require1k.js"></script>
1212
<script>
13-
R('./test/class_test', function(err,v){
13+
R('./test/class7_test', function(err,v){
1414
window.v = v
1515
})
1616
</script>

jscomp/runtime/caml_builtin_exceptions.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
var id = [0];
66

77
function caml_set_oo_id(b) {
8-
b[2] = id[0];
8+
b[1] = id[0];
99
id[0] += 1;
1010
return b;
1111
}

jscomp/runtime/caml_builtin_exceptions.ml

+11-2
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,18 @@ let undefined_recursive_module = 248, "Undefined_recursive_module", -11
4242
let id = ref 0n
4343

4444

45+
(* see #251
46+
{[
47+
CAMLprim value caml_set_oo_id (value obj) {
48+
Field(obj, 1) = oo_last_id;
49+
oo_last_id += 2;
50+
return obj;
51+
}
52+
53+
]}*)
4554
let caml_set_oo_id (b : exception_block) =
46-
Obj.set_field (Obj.repr b) 2 (Obj.repr !id);
47-
id := Nativeint.add !id 1n;
55+
Obj.set_field (Obj.repr b) 1 (Obj.repr !id);
56+
id := Nativeint.add !id 1n;
4857
b
4958

5059
let get_id () =

jscomp/test/.depend

+4
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ class5_test.cmo : mt.cmi ../stdlib/list.cmi
7272
class5_test.cmx : mt.cmx ../stdlib/list.cmx
7373
class6_test.cmo : mt.cmi
7474
class6_test.cmx : mt.cmx
75+
class7_test.cmo : ../stdlib/oo.cmi mt.cmi
76+
class7_test.cmx : ../stdlib/oo.cmx mt.cmx
7577
class_test.cmo : mt.cmi
7678
class_test.cmx : mt.cmx
7779
class_type_ffi_test.cmo : ../lib/js.cmo
@@ -598,6 +600,8 @@ class5_test.cmo : mt.cmi ../stdlib/list.cmi
598600
class5_test.cmj : mt.cmj ../stdlib/list.cmj
599601
class6_test.cmo : mt.cmi
600602
class6_test.cmj : mt.cmj
603+
class7_test.cmo : ../stdlib/oo.cmi mt.cmi
604+
class7_test.cmj : ../stdlib/oo.cmj mt.cmj
601605
class_test.cmo : mt.cmi
602606
class_test.cmj : mt.cmj
603607
class_type_ffi_test.cmo : ../lib/js.cmo

jscomp/test/class7_test.js

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Generated CODE, PLEASE EDIT WITH CARE
2+
'use strict';
3+
4+
var Mt = require("./mt");
5+
var CamlinternalOO = require("../stdlib/camlinternalOO");
6+
var Caml_curry = require("../runtime/caml_curry");
7+
var Oo = require("../stdlib/oo");
8+
9+
var shared = [
10+
"move",
11+
"get_x"
12+
];
13+
14+
var suites = [/* [] */0];
15+
16+
var test_id = [0];
17+
18+
function eq(loc, x, y) {
19+
test_id[0] = test_id[0] + 1 | 0;
20+
suites[0] = /* :: */[
21+
/* tuple */[
22+
loc + (" id " + test_id[0]),
23+
function () {
24+
return /* Eq */{
25+
0: x,
26+
1: y,
27+
length: 2,
28+
tag: 0
29+
};
30+
}
31+
],
32+
suites[0]
33+
];
34+
return /* () */0;
35+
}
36+
37+
function point_init($$class) {
38+
var ids = CamlinternalOO.new_methods_variables($$class, shared, ["x"]);
39+
var move = ids[0];
40+
var get_x = ids[1];
41+
var x = ids[2];
42+
CamlinternalOO.set_methods($$class, /* array */[
43+
get_x,
44+
function (self$neg1) {
45+
return self$neg1[x];
46+
},
47+
move,
48+
function (self$neg1, d) {
49+
self$neg1[x] = self$neg1[x] + d | 0;
50+
return /* () */0;
51+
}
52+
]);
53+
return function (_, self, x_init) {
54+
var self$1 = CamlinternalOO.create_object_opt(self, $$class);
55+
self$1[x] = x_init;
56+
return self$1;
57+
};
58+
}
59+
60+
var point = CamlinternalOO.make_class(shared, point_init);
61+
62+
var p = Caml_curry.app2(point[0], 0, 55);
63+
64+
var q = Oo.copy(p);
65+
66+
if (q.tag === 248) {
67+
Caml_curry.js2(-933174511, 1, q, 7);
68+
}
69+
else {
70+
Caml_curry.app1(q.move.bind(q), 7);
71+
}
72+
73+
eq('File "class7_test.ml", line 22, characters 5-12', /* tuple */[
74+
55,
75+
62
76+
], /* tuple */[
77+
p.tag === 248 ? Caml_curry.js1(291546447, 2, p) : p.get_x,
78+
q.tag === 248 ? Caml_curry.js1(291546447, 3, q) : q.get_x
79+
]);
80+
81+
Mt.from_pair_suites("class7_test.ml", suites[0]);
82+
83+
exports.suites = suites;
84+
exports.test_id = test_id;
85+
exports.eq = eq;
86+
exports.point = point;
87+
/* point Not a pure module */

jscomp/test/class7_test.ml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
let suites : Mt.pair_suites ref = ref []
2+
let test_id = ref 0
3+
let eq loc x y =
4+
incr test_id ;
5+
suites :=
6+
(loc ^" id " ^ (string_of_int !test_id), (fun _ -> Mt.Eq(x,y))) :: !suites
7+
8+
class point = fun x_init ->
9+
object
10+
val mutable x = x_init
11+
method get_x = x
12+
method move d = x <- x + d
13+
end
14+
15+
let () =
16+
let p = new point 55 in
17+
(* TODO: see why [field 1] is missing
18+
*)
19+
(* let () = [%bs.debug] in *)
20+
let q = Oo.copy p in
21+
q # move 7;
22+
eq __LOC__ (55, 62) (p#get_x, q # get_x )
23+
24+
let () = Mt.from_pair_suites __FILE__ !suites

jscomp/test/test.mllib

+1
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,5 @@ class3_test
236236
class4_test
237237
class5_test
238238
class6_test
239+
class7_test
239240

0 commit comments

Comments
 (0)