Skip to content

Commit a2fe4f9

Browse files
committed
Make Obj.dup work on floats and boxed integers
1 parent 70aac7f commit a2fe4f9

File tree

7 files changed

+64
-19
lines changed

7 files changed

+64
-19
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* Runtime: remove polyfill for Map to simplify MlObjectTable implementation (#1846)
99
* Runtime: refactor caml_xmlhttprequest_create implementation (#1846)
1010
* Runtime: update constant imports to use `node:fs` module (#1850)
11+
* Runtime: make Obj.dup work with floats and boxed numbers (#1871)
1112

1213
## Bug fixes
1314
* Runtime: fix path normalization (#1848)

compiler/lib/generate.ml

-2
Original file line numberDiff line numberDiff line change
@@ -1159,8 +1159,6 @@ let _ =
11591159
register_tern_prim "caml_array_unsafe_set" (fun cx cy cz _ ->
11601160
J.EBin (J.Eq, Mlvalue.Array.field cx cy, cz));
11611161
register_un_prim "caml_alloc_dummy" `Pure (fun _ _ -> J.array []);
1162-
register_un_prim "caml_obj_dup" ~need_loc:true `Mutable (fun cx loc ->
1163-
J.call (J.dot cx (Utf8_string.of_string_exn "slice")) [] loc);
11641162
register_un_prim "caml_int_of_float" `Pure (fun cx _loc -> to_int cx);
11651163
register_un_math_prim "caml_abs_float" "abs";
11661164
register_un_math_prim "caml_acos_float" "acos";

compiler/tests-compiler/obj.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,5 @@ let%expect_test "static eval of string get" =
5858
//end
5959
function my_new_block(x, l){return runtime.caml_obj_block(x + 1 | 0, 3);}
6060
//end
61-
function my_dup(t){return [0, t, 0].slice();}
61+
function my_dup(t){return runtime.caml_obj_dup([0, t, 0]);}
6262
//end |}]

compiler/tests-full/stdlib.cma.expected.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -22432,13 +22432,13 @@
2243222432
/*<<printexc.ml:301:39>>*/ }
2243322433
var
2243422434
errors =
22435-
/*<<printexc.ml:20:29>>*/ [0,
22436-
cst$4,
22437-
"(Cannot print locations:\n bytecode executable program file not found)",
22438-
"(Cannot print locations:\n bytecode executable program file appears to be corrupt)",
22439-
"(Cannot print locations:\n bytecode executable program file has wrong magic number)",
22440-
"(Cannot print locations:\n bytecode executable program file cannot be opened;\n -- too many open files. Try running with OCAMLRUNPARAM=b=2)"].slice
22441-
(),
22435+
/*<<printexc.ml:20:29>>*/ runtime.caml_obj_dup
22436+
([0,
22437+
cst$4,
22438+
"(Cannot print locations:\n bytecode executable program file not found)",
22439+
"(Cannot print locations:\n bytecode executable program file appears to be corrupt)",
22440+
"(Cannot print locations:\n bytecode executable program file has wrong magic number)",
22441+
"(Cannot print locations:\n bytecode executable program file cannot be opened;\n -- too many open files. Try running with OCAMLRUNPARAM=b=2)"]),
2244222442
_o_ =
2244322443
[0,
2244422444
[11, cst_Fatal_error_exception, [2, 0, [12, 10, 0]]],
@@ -31106,7 +31106,7 @@
3110631106
Stdlib_List = global_data.Stdlib__List,
3110731107
Stdlib_Map = global_data.Stdlib__Map;
3110831108
function copy(o){
31109-
var o$0 = /*<<camlinternalOO.ml:23:19>>*/ o.slice();
31109+
var o$0 = /*<<camlinternalOO.ml:23:19>>*/ runtime.caml_obj_dup(o);
3111031110
/*<<camlinternalOO.ml:24:2>>*/ return caml_set_oo_id(o$0) /*<<camlinternalOO.ml:24:10>>*/ ;
3111131111
}
3111231112
var params = /*<<?>>*/ [0, 1, 1, 1, 3, 16];

compiler/tests-jsoo/test_obj.ml

+50-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ let%expect_test "is_int" =
4545
(* https://github.com/ocsigen/js_of_ocaml/issues/666 *)
4646
(* https://github.com/ocsigen/js_of_ocaml/pull/725 *)
4747

48-
let%expect_test "dup" =
48+
let%expect_test "dup string/bytes" =
4949
let magic = "abcd" in
5050
let js_string_enabled =
5151
match Sys.backend_type with
@@ -71,6 +71,55 @@ let%expect_test "dup" =
7171
true
7272
|}]
7373

74+
let%expect_test "dup tuple" =
75+
let x = 1, "a", 2.1 in
76+
let x' = Obj.obj (Obj.dup (Obj.repr x)) in
77+
print_bool (x = x');
78+
print_bool (x != x');
79+
[%expect {|
80+
true
81+
true
82+
|}]
83+
84+
let%expect_test "dup numbers" =
85+
let js =
86+
match Sys.backend_type with
87+
| Other "js_of_ocaml" -> true
88+
| _ -> false
89+
in
90+
let x = 2.1 in
91+
let x' = Obj.obj (Obj.dup (Obj.repr x)) in
92+
print_bool (x = x');
93+
print_bool ((not js) = (x != x'));
94+
[%expect {|
95+
true
96+
true
97+
|}];
98+
let x = 21L in
99+
let x' = Obj.obj (Obj.dup (Obj.repr x)) in
100+
print_bool (x = x');
101+
print_bool (x != x');
102+
[%expect {|
103+
true
104+
true
105+
|}];
106+
let x = 21l in
107+
let x' = Obj.obj (Obj.dup (Obj.repr x)) in
108+
print_bool (x = x');
109+
print_bool ((not js) = (x != x'));
110+
[%expect {|
111+
true
112+
true
113+
|}];
114+
let x = 21n in
115+
let x' = Obj.obj (Obj.dup (Obj.repr x)) in
116+
print_bool (x = x');
117+
print_bool ((not js) = (x != x'));
118+
[%expect {|
119+
true
120+
true
121+
|}]
122+
74123
let%expect_test "sameness" =
75124
(* FIXME: Jsoo returns the wrong opposite result for cases below.
76125
Would be fixed by GH#1410 *)

runtime/js/int64.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class MlInt64 {
3030
this.caml_custom = "_j";
3131
}
3232

33-
copy() {
33+
slice() {
3434
return new MlInt64(this.lo, this.mi, this.hi);
3535
}
3636

@@ -179,8 +179,8 @@ class MlInt64 {
179179

180180
udivmod(x) {
181181
var offset = 0;
182-
var modulus = this.copy();
183-
var divisor = x.copy();
182+
var modulus = this.slice();
183+
var divisor = x.slice();
184184
var quotient = new MlInt64(0, 0, 0);
185185
while (modulus.ucompare(divisor) > 0) {
186186
offset++;

runtime/js/obj.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,7 @@ function caml_obj_with_tag(tag, x) {
8181

8282
//Provides: caml_obj_dup mutable (mutable)
8383
function caml_obj_dup(x) {
84-
var l = x.length;
85-
var a = new Array(l);
86-
for (var i = 0; i < l; i++) a[i] = x[i];
87-
return a;
84+
return typeof x === "number" ? x : x.slice();
8885
}
8986

9087
//Provides: caml_obj_truncate (mutable, const)

0 commit comments

Comments
 (0)