Skip to content

Commit 5d8c561

Browse files
committed
improve effects of caml_float_of_string
1 parent 6debf4e commit 5d8c561

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

jscomp/runtime/caml_format.ml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,6 @@ let caml_format_float fmt x =
570570
end;
571571
finish_formatting f !s
572572

573-
574573
(**
575574
external float_of_string : string -> float = "caml_float_of_string"
576575
pervasives.ml
@@ -584,8 +583,8 @@ let caml_format_float fmt x =
584583
FIXME: arity of float_of_string is not inferred correctly
585584
*)
586585

587-
let float_of_string : string -> (string -> 'a) -> float = [%bs.raw {|
588-
function (s, caml_failwith) {
586+
let float_of_string : string -> exn -> float = fun%raw s exn -> {|
587+
{
589588
var res = +s;
590589
if ((s.length > 0) && (res === res))
591590
return res;
@@ -606,13 +605,18 @@ let float_of_string : string -> (string -> 'a) -> float = [%bs.raw {|
606605
return Infinity;
607606
if (/^-inf(inity)?$/i.test(s))
608607
return -Infinity;
609-
caml_failwith("float_of_string");
608+
throw exn;
610609
}
611610

612611
|}
613-
]
614612

615-
let caml_float_of_string s = float_of_string s caml_failwith
613+
614+
(* let float_of_string (s : string) : float =
615+
let res : float = [%raw{|+s|}] in
616+
if String.length s > 0 && res = res then res
617+
else *)
618+
619+
let caml_float_of_string s = float_of_string s (Failure "float_of_string")
616620

617621
let caml_nativeint_format = caml_format_int
618622
let caml_int32_format = caml_format_int

lib/js/caml_format.js

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
'use strict';
22

3-
var Curry = require("./curry.js");
43
var Caml_int32 = require("./caml_int32.js");
54
var Caml_int64 = require("./caml_int64.js");
65
var Caml_utils = require("./caml_utils.js");
76
var Caml_builtin_exceptions = require("./caml_builtin_exceptions.js");
87

9-
function caml_failwith(s) {
10-
throw [
11-
Caml_builtin_exceptions.failure,
12-
s
13-
];
14-
}
15-
168
function parse_digit(c) {
179
if (c >= 65) {
1810
if (c >= 97) {
@@ -778,8 +770,8 @@ function caml_format_float(fmt, x) {
778770
return finish_formatting(f, s);
779771
}
780772

781-
var float_of_string = (
782-
function (s, caml_failwith) {
773+
var float_of_string = function (s,exn){
774+
{
783775
var res = +s;
784776
if ((s.length > 0) && (res === res))
785777
return res;
@@ -800,13 +792,16 @@ var float_of_string = (
800792
return Infinity;
801793
if (/^-inf(inity)?$/i.test(s))
802794
return -Infinity;
803-
caml_failwith("float_of_string");
795+
throw exn;
804796
}
805797

806-
);
798+
};
807799

808800
function caml_float_of_string(s) {
809-
return Curry._2(float_of_string, s, caml_failwith);
801+
return float_of_string(s, [
802+
Caml_builtin_exceptions.failure,
803+
"float_of_string"
804+
]);
810805
}
811806

812807
var caml_nativeint_format = caml_format_int;
@@ -827,4 +822,4 @@ exports.caml_int_of_string = caml_int_of_string;
827822
exports.caml_int32_of_string = caml_int32_of_string;
828823
exports.caml_int64_of_string = caml_int64_of_string;
829824
exports.caml_nativeint_of_string = caml_nativeint_of_string;
830-
/* float_of_string Not a pure module */
825+
/* Caml_utils Not a pure module */

0 commit comments

Comments
 (0)