Skip to content

Commit

Permalink
Runtime: re-support marshaling floats
Browse files Browse the repository at this point in the history
  • Loading branch information
hhugo authored and vouillon committed Jun 7, 2024
1 parent cb665ae commit f37d274
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
24 changes: 24 additions & 0 deletions compiler/tests-jsoo/test_marshal.ml
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,27 @@ let%expect_test "test sharing of string" =
Printf.printf "%S" (Marshal.to_string obj []);
[%expect
{| "\132\149\166\190\000\000\000\016\000\000\000\004\000\000\000\012\000\000\000\011\160\160'AString\004\001\160\004\003@" |}]

let%expect_test "test float" =
let s = 3.14 in
let p = s, s in
let obj = [ p; p ] in
let r1 = Marshal.to_string s [ No_sharing ] in
Printf.printf "%S\n" r1;
Printf.printf "%f" (Marshal.from_string r1 0);
[%expect
{|
"\132\149\166\190\000\000\000\t\000\000\000\000\000\000\000\003\000\000\000\002\012\031\133\235Q\184\030\t@"
3.140000 |}];
let r2 = Marshal.to_string obj [] in
Printf.printf "%S\n" r2;
let a, b, c, d =
match Marshal.from_string r2 0 with
| [ (a, b); (c, d) ] -> a, b, c, d
| _ -> assert false
in
Printf.printf "%f %f %f %f" a b c d;
[%expect
{|
"\132\149\166\190\000\000\000\017\000\000\000\004\000\000\000\012\000\000\000\011\160\160\012\031\133\235Q\184\030\t@\004\001\160\004\003@"
3.140000 3.140000 3.140000 3.140000 |}]
20 changes: 8 additions & 12 deletions runtime/marshal.js
Original file line number Diff line number Diff line change
Expand Up @@ -730,18 +730,14 @@ var caml_output_val = function (){
} else {
if (v != (v|0)){
var type_of_v = typeof v;
//
// If a float happens to be an integer it is serialized as an integer
// (Js_of_ocaml cannot tell whether the type of an integer number is
// float or integer.) This can result in unexpected crashes when
// unmarshalling using the standard runtime. It seems better to
// systematically fail on marshalling.
//
// if(type_of_v != "number")
caml_failwith("output_value: abstract value ("+type_of_v+")");
// var t = caml_int64_to_bytes(caml_int64_bits_of_float(v));
// writer.write (8, 0x0B /*cst.CODE_DOUBLE_BIG*/);
// for(var i = 0; i<8; i++){writer.write(8,t[i])}
if(type_of_v != "number")
caml_failwith("output_value: abstract value ("+type_of_v+")");
if (memo(v)) return;
var t = caml_int64_to_bytes(caml_int64_bits_of_float(v));
writer.write (8, 0x0C /*cst.CODE_DOUBLE_LITTLE*/);
for(var i = 0; i<8; i++){writer.write(8,t[7 - i])}
writer.size_32 += 3
writer.size_64 += 2
}
else if (v >= 0 && v < 0x40) {
writer.write (8, 0X40 /*cst.PREFIX_SMALL_INT*/ + v);
Expand Down

0 comments on commit f37d274

Please sign in to comment.