Skip to content

Commit 9375a62

Browse files
committed
Runtime: fix #1630
1 parent 999bf16 commit 9375a62

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

compiler/tests-jsoo/test_ints.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,13 @@ let%expect_test _ =
4646
[%expect {| -123 |}];
4747
Printf.printf "%d\n" (int_of_string "-0U123");
4848
[%expect {| -123 |}]
49+
50+
let%expect_test _ =
51+
Printf.printf "%ld\n" (Int32.of_string "-0u2147483648");
52+
[%expect {| -2147483648 |}];
53+
Printf.printf "%ld\n" (Int32.of_string "-0u2147483648");
54+
[%expect {| -2147483648 |}];
55+
Printf.printf "%ld\n" (Int32.of_string "0u2147483648");
56+
[%expect {| -2147483648 |}];
57+
Printf.printf "%ld\n" (Int32.of_string "0u2147483648");
58+
[%expect {| -2147483648 |}]

runtime/ints.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function caml_format_int(fmt, i) {
3434
//Provides: caml_parse_sign_and_base
3535
//Requires: caml_string_unsafe_get, caml_ml_string_length
3636
function caml_parse_sign_and_base (s) {
37-
var i = 0, len = caml_ml_string_length(s), base = 10, sign = 1;
37+
var i = 0, len = caml_ml_string_length(s), base = 10, sign = 1, signedness = 1;
3838
if (len > 0) {
3939
switch (caml_string_unsafe_get(s,i)) {
4040
case 45: i++; sign = -1; break;
@@ -43,12 +43,12 @@ function caml_parse_sign_and_base (s) {
4343
}
4444
if (i + 1 < len && caml_string_unsafe_get(s, i) == 48)
4545
switch (caml_string_unsafe_get(s, i + 1)) {
46-
case 120: case 88: base = 16; i += 2; break;
47-
case 111: case 79: base = 8; i += 2; break;
48-
case 98: case 66: base = 2; i += 2; break;
49-
case 117: case 85: i += 2; break;
46+
case 120: case 88: signedness = 0; base = 16; i += 2; break;
47+
case 111: case 79: signedness = 0; base = 8; i += 2; break;
48+
case 98: case 66: signedness = 0; base = 2; i += 2; break;
49+
case 117: case 85: signedness = 0; i += 2; break;
5050
}
51-
return [i, sign, base];
51+
return [i, sign, base, signedness];
5252
}
5353

5454
//Provides: caml_parse_digit
@@ -64,7 +64,7 @@ function caml_parse_digit(c) {
6464
//Requires: caml_parse_sign_and_base, caml_parse_digit, caml_failwith
6565
function caml_int_of_string (s) {
6666
var r = caml_parse_sign_and_base (s);
67-
var i = r[0], sign = r[1], base = r[2];
67+
var i = r[0], sign = r[1], base = r[2], signedness = r[3];
6868
var len = caml_ml_string_length(s);
6969
var threshold = -1 >>> 0;
7070
var c = (i < len)?caml_string_unsafe_get(s, i):0;
@@ -84,7 +84,7 @@ function caml_int_of_string (s) {
8484
// hence any value of 'res' (less than 'threshold') is acceptable.
8585
// But we have to convert the result back to a signed integer.
8686
res = sign * res;
87-
if ((base == 10) && ((res | 0) != res))
87+
if (signedness && ((res | 0) != res))
8888
/* Signed representation expected, allow -2^(nbits-1) to 2^(nbits-1) - 1 */
8989
caml_failwith("int_of_string");
9090
return res | 0;

0 commit comments

Comments
 (0)