Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Obj.dup work on floats and boxed integers #1871

Merged
merged 1 commit into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Runtime: remove polyfill for Map to simplify MlObjectTable implementation (#1846)
* Runtime: refactor caml_xmlhttprequest_create implementation (#1846)
* Runtime: update constant imports to use `node:fs` module (#1850)
* Runtime: make Obj.dup work with floats and boxed numbers (#1871)

## Bug fixes
* Runtime: fix path normalization (#1848)
Expand Down
2 changes: 0 additions & 2 deletions compiler/lib/generate.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1159,8 +1159,6 @@ let _ =
register_tern_prim "caml_array_unsafe_set" (fun cx cy cz _ ->
J.EBin (J.Eq, Mlvalue.Array.field cx cy, cz));
register_un_prim "caml_alloc_dummy" `Pure (fun _ _ -> J.array []);
register_un_prim "caml_obj_dup" ~need_loc:true `Mutable (fun cx loc ->
J.call (J.dot cx (Utf8_string.of_string_exn "slice")) [] loc);
register_un_prim "caml_int_of_float" `Pure (fun cx _loc -> to_int cx);
register_un_math_prim "caml_abs_float" "abs";
register_un_math_prim "caml_acos_float" "acos";
Expand Down
2 changes: 1 addition & 1 deletion compiler/tests-compiler/obj.ml
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ let%expect_test "static eval of string get" =
//end
function my_new_block(x, l){return runtime.caml_obj_block(x + 1 | 0, 3);}
//end
function my_dup(t){return [0, t, 0].slice();}
function my_dup(t){return runtime.caml_obj_dup([0, t, 0]);}
//end |}]
16 changes: 8 additions & 8 deletions compiler/tests-full/stdlib.cma.expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -22432,13 +22432,13 @@
/*<<printexc.ml:301:39>>*/ }
var
errors =
/*<<printexc.ml:20:29>>*/ [0,
cst$4,
"(Cannot print locations:\n bytecode executable program file not found)",
"(Cannot print locations:\n bytecode executable program file appears to be corrupt)",
"(Cannot print locations:\n bytecode executable program file has wrong magic number)",
"(Cannot print locations:\n bytecode executable program file cannot be opened;\n -- too many open files. Try running with OCAMLRUNPARAM=b=2)"].slice
(),
/*<<printexc.ml:20:29>>*/ runtime.caml_obj_dup
([0,
cst$4,
"(Cannot print locations:\n bytecode executable program file not found)",
"(Cannot print locations:\n bytecode executable program file appears to be corrupt)",
"(Cannot print locations:\n bytecode executable program file has wrong magic number)",
"(Cannot print locations:\n bytecode executable program file cannot be opened;\n -- too many open files. Try running with OCAMLRUNPARAM=b=2)"]),
_o_ =
[0,
[11, cst_Fatal_error_exception, [2, 0, [12, 10, 0]]],
Expand Down Expand Up @@ -31106,7 +31106,7 @@
Stdlib_List = global_data.Stdlib__List,
Stdlib_Map = global_data.Stdlib__Map;
function copy(o){
var o$0 = /*<<camlinternalOO.ml:23:19>>*/ o.slice();
var o$0 = /*<<camlinternalOO.ml:23:19>>*/ runtime.caml_obj_dup(o);
/*<<camlinternalOO.ml:24:2>>*/ return caml_set_oo_id(o$0) /*<<camlinternalOO.ml:24:10>>*/ ;
}
var params = /*<<?>>*/ [0, 1, 1, 1, 3, 16];
Expand Down
51 changes: 50 additions & 1 deletion compiler/tests-jsoo/test_obj.ml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ let%expect_test "is_int" =
(* https://github.com/ocsigen/js_of_ocaml/issues/666 *)
(* https://github.com/ocsigen/js_of_ocaml/pull/725 *)

let%expect_test "dup" =
let%expect_test "dup string/bytes" =
let magic = "abcd" in
let js_string_enabled =
match Sys.backend_type with
Expand All @@ -71,6 +71,55 @@ let%expect_test "dup" =
true
|}]

let%expect_test "dup tuple" =
let x = 1, "a", 2.1 in
let x' = Obj.obj (Obj.dup (Obj.repr x)) in
print_bool (x = x');
print_bool (x != x');
[%expect {|
true
true
|}]

let%expect_test "dup numbers" =
let js =
match Sys.backend_type with
| Other "js_of_ocaml" -> true
| _ -> false
in
let x = 2.1 in
let x' = Obj.obj (Obj.dup (Obj.repr x)) in
print_bool (x = x');
print_bool ((not js) = (x != x'));
[%expect {|
true
true
|}];
let x = 21L in
let x' = Obj.obj (Obj.dup (Obj.repr x)) in
print_bool (x = x');
print_bool (x != x');
[%expect {|
true
true
|}];
let x = 21l in
let x' = Obj.obj (Obj.dup (Obj.repr x)) in
print_bool (x = x');
print_bool ((not js) = (x != x'));
[%expect {|
true
true
|}];
let x = 21n in
let x' = Obj.obj (Obj.dup (Obj.repr x)) in
print_bool (x = x');
print_bool ((not js) = (x != x'));
[%expect {|
true
true
|}]

let%expect_test "sameness" =
(* FIXME: Jsoo returns the wrong opposite result for cases below.
Would be fixed by GH#1410 *)
Expand Down
6 changes: 3 additions & 3 deletions runtime/js/int64.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class MlInt64 {
this.caml_custom = "_j";
}

copy() {
slice() {
return new MlInt64(this.lo, this.mi, this.hi);
}

Expand Down Expand Up @@ -179,8 +179,8 @@ class MlInt64 {

udivmod(x) {
var offset = 0;
var modulus = this.copy();
var divisor = x.copy();
var modulus = this.slice();
var divisor = x.slice();
var quotient = new MlInt64(0, 0, 0);
while (modulus.ucompare(divisor) > 0) {
offset++;
Expand Down
5 changes: 1 addition & 4 deletions runtime/js/obj.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@ function caml_obj_with_tag(tag, x) {

//Provides: caml_obj_dup mutable (mutable)
function caml_obj_dup(x) {
var l = x.length;
var a = new Array(l);
for (var i = 0; i < l; i++) a[i] = x[i];
return a;
return typeof x === "number" ? x : x.slice();
}

//Provides: caml_obj_truncate (mutable, const)
Expand Down
Loading