Skip to content

Commit 978c979

Browse files
authored
Add Stdlib Bool and Char modules + Better deprecation msg in Pervasives (#7361)
* add Stdlib Bool and Char modules * point deprecated functions to Stdlib alternatives * update Changelog * update artifacts.txt
1 parent cba4fe3 commit 978c979

21 files changed

+437
-157
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
- Deprecate unsafe host-specific bindings from stdlib. https://github.com/rescript-lang/rescript/pull/7334
4242
- Make unsafe function names consistent in Stdlib.String. https://github.com/rescript-lang/rescript/pull/7337
4343
- `rescript` package does not trigger `postinstall` script anymore. https://github.com/rescript-lang/rescript/pull/7350
44+
- Add Stdlib Bool and Char modules and improve Pervasives deprecation messages. https://github.com/rescript-lang/rescript/pull/7361
4445

4546
#### :bug: Bug fix
4647

analysis/reanalyze/src/ExnLib.ml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ let raisesLibTable : (Name.t, Exceptions.t) Hashtbl.t =
5151
]
5252
in
5353
let stdlibBigInt = [("fromStringExn", [jsExnError])] in
54+
let stdlibBool = [("fromStringExn", [invalidArgument])] in
55+
let stdlibChar = [("fromIntExn", [invalidArgument])] in
5456
let stdlibError = [("raise", [jsExnError])] in
5557
let stdlibExn =
5658
[
@@ -140,7 +142,8 @@ let raisesLibTable : (Name.t, Exceptions.t) Hashtbl.t =
140142
("Belt_SetInt", beltSet);
141143
("Belt_SetString", beltSet);
142144
("BigInt", stdlibBigInt);
143-
("Char", [("chr", [invalidArgument])]);
145+
("Bool", stdlibBool);
146+
("Char", stdlibChar);
144147
("Error", stdlibError);
145148
("Exn", stdlibExn);
146149
("Js.Json", [("parseExn", [jsExnError])]);
@@ -159,6 +162,10 @@ let raisesLibTable : (Name.t, Exceptions.t) Hashtbl.t =
159162
("Stdlib", stdlib);
160163
("Stdlib_BigInt", stdlibBigInt);
161164
("Stdlib.BigInt", stdlibBigInt);
165+
("Stdlib_Bool", stdlibBool);
166+
("Stdlib.Bool", stdlibBool);
167+
("Stdlib_Char", stdlibChar);
168+
("Stdlib.Char", stdlibChar);
162169
("Stdlib_Error", stdlibError);
163170
("Stdlib.Error", stdlibError);
164171
("Stdlib_Exn", stdlibExn);

lib/es6/Stdlib.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function assertEqual(a, b) {
1212
RE_EXN_ID: "Assert_failure",
1313
_1: [
1414
"Stdlib.res",
15-
117,
15+
119,
1616
4
1717
],
1818
Error: new Error()
@@ -27,6 +27,10 @@ let $$Array;
2727

2828
let $$BigInt;
2929

30+
let Bool;
31+
32+
let Char;
33+
3034
let Console;
3135

3236
let $$DataView;
@@ -118,6 +122,8 @@ export {
118122
IntervalId,
119123
$$Array,
120124
$$BigInt,
125+
Bool,
126+
Char,
121127
Console,
122128
$$DataView,
123129
$$Date,

lib/es6/Stdlib_Bool.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
3+
4+
function toString(b) {
5+
if (b) {
6+
return "true";
7+
} else {
8+
return "false";
9+
}
10+
}
11+
12+
function fromString(s) {
13+
switch (s) {
14+
case "false" :
15+
return false;
16+
case "true" :
17+
return true;
18+
default:
19+
return;
20+
}
21+
}
22+
23+
function fromStringExn(param) {
24+
switch (param) {
25+
case "false" :
26+
return false;
27+
case "true" :
28+
return true;
29+
default:
30+
throw {
31+
RE_EXN_ID: "Invalid_argument",
32+
_1: "Bool.fromStringExn: value is neither \"true\" nor \"false\"",
33+
Error: new Error()
34+
};
35+
}
36+
}
37+
38+
export {
39+
toString,
40+
fromString,
41+
fromStringExn,
42+
}
43+
/* No side effect */

lib/es6/Char.js renamed to lib/es6/Stdlib_Char.js

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11

22

33

4+
function fromIntExn(n) {
5+
if (n < 0 || n > 255) {
6+
throw {
7+
RE_EXN_ID: "Invalid_argument",
8+
_1: "`Char.fromIntExn` expects an integer between 0 and 255",
9+
Error: new Error()
10+
};
11+
}
12+
return n;
13+
}
14+
15+
function fromInt(n) {
16+
if (n < 0 || n > 255) {
17+
return;
18+
} else {
19+
return n;
20+
}
21+
}
22+
423
function escaped(param) {
524
let exit = 0;
625
if (param >= 40) {
@@ -54,35 +73,33 @@ function escaped(param) {
5473
}
5574
}
5675

57-
function lowercase_ascii(c) {
76+
function toLowerCaseAscii(c) {
5877
if (c >= /* 'A' */65 && c <= /* 'Z' */90) {
5978
return c + 32 | 0;
6079
} else {
6180
return c;
6281
}
6382
}
6483

65-
function uppercase_ascii(c) {
84+
function toUpperCaseAscii(c) {
6685
if (c >= /* 'a' */97 && c <= /* 'z' */122) {
6786
return c - 32 | 0;
6887
} else {
6988
return c;
7089
}
7190
}
7291

73-
function compare(c1, c2) {
74-
return c1 - c2 | 0;
75-
}
92+
let lowercase_ascii = toLowerCaseAscii;
7693

77-
function equal(c1, c2) {
78-
return (c1 - c2 | 0) === 0;
79-
}
94+
let uppercase_ascii = toUpperCaseAscii;
8095

8196
export {
8297
escaped,
8398
lowercase_ascii,
8499
uppercase_ascii,
85-
compare,
86-
equal,
100+
toLowerCaseAscii,
101+
toUpperCaseAscii,
102+
fromIntExn,
103+
fromInt,
87104
}
88105
/* No side effect */

lib/js/Stdlib.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function assertEqual(a, b) {
1212
RE_EXN_ID: "Assert_failure",
1313
_1: [
1414
"Stdlib.res",
15-
117,
15+
119,
1616
4
1717
],
1818
Error: new Error()
@@ -27,6 +27,10 @@ let $$Array;
2727

2828
let $$BigInt;
2929

30+
let Bool;
31+
32+
let Char;
33+
3034
let Console;
3135

3236
let $$DataView;
@@ -117,6 +121,8 @@ exports.TimeoutId = TimeoutId;
117121
exports.IntervalId = IntervalId;
118122
exports.$$Array = $$Array;
119123
exports.$$BigInt = $$BigInt;
124+
exports.Bool = Bool;
125+
exports.Char = Char;
120126
exports.Console = Console;
121127
exports.$$DataView = $$DataView;
122128
exports.$$Date = $$Date;

lib/js/Stdlib_Bool.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict';
2+
3+
4+
function toString(b) {
5+
if (b) {
6+
return "true";
7+
} else {
8+
return "false";
9+
}
10+
}
11+
12+
function fromString(s) {
13+
switch (s) {
14+
case "false" :
15+
return false;
16+
case "true" :
17+
return true;
18+
default:
19+
return;
20+
}
21+
}
22+
23+
function fromStringExn(param) {
24+
switch (param) {
25+
case "false" :
26+
return false;
27+
case "true" :
28+
return true;
29+
default:
30+
throw {
31+
RE_EXN_ID: "Invalid_argument",
32+
_1: "Bool.fromStringExn: value is neither \"true\" nor \"false\"",
33+
Error: new Error()
34+
};
35+
}
36+
}
37+
38+
exports.toString = toString;
39+
exports.fromString = fromString;
40+
exports.fromStringExn = fromStringExn;
41+
/* No side effect */

lib/js/Char.js renamed to lib/js/Stdlib_Char.js

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
'use strict';
22

33

4+
function fromIntExn(n) {
5+
if (n < 0 || n > 255) {
6+
throw {
7+
RE_EXN_ID: "Invalid_argument",
8+
_1: "`Char.fromIntExn` expects an integer between 0 and 255",
9+
Error: new Error()
10+
};
11+
}
12+
return n;
13+
}
14+
15+
function fromInt(n) {
16+
if (n < 0 || n > 255) {
17+
return;
18+
} else {
19+
return n;
20+
}
21+
}
22+
423
function escaped(param) {
524
let exit = 0;
625
if (param >= 40) {
@@ -54,33 +73,31 @@ function escaped(param) {
5473
}
5574
}
5675

57-
function lowercase_ascii(c) {
76+
function toLowerCaseAscii(c) {
5877
if (c >= /* 'A' */65 && c <= /* 'Z' */90) {
5978
return c + 32 | 0;
6079
} else {
6180
return c;
6281
}
6382
}
6483

65-
function uppercase_ascii(c) {
84+
function toUpperCaseAscii(c) {
6685
if (c >= /* 'a' */97 && c <= /* 'z' */122) {
6786
return c - 32 | 0;
6887
} else {
6988
return c;
7089
}
7190
}
7291

73-
function compare(c1, c2) {
74-
return c1 - c2 | 0;
75-
}
92+
let lowercase_ascii = toLowerCaseAscii;
7693

77-
function equal(c1, c2) {
78-
return (c1 - c2 | 0) === 0;
79-
}
94+
let uppercase_ascii = toUpperCaseAscii;
8095

8196
exports.escaped = escaped;
8297
exports.lowercase_ascii = lowercase_ascii;
8398
exports.uppercase_ascii = uppercase_ascii;
84-
exports.compare = compare;
85-
exports.equal = equal;
99+
exports.toLowerCaseAscii = toLowerCaseAscii;
100+
exports.toUpperCaseAscii = toUpperCaseAscii;
101+
exports.fromIntExn = fromIntExn;
102+
exports.fromInt = fromInt;
86103
/* No side effect */

packages/artifacts.txt

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ lib/es6/Belt_internalMapString.js
7272
lib/es6/Belt_internalSetBuckets.js
7373
lib/es6/Belt_internalSetInt.js
7474
lib/es6/Belt_internalSetString.js
75-
lib/es6/Char.js
7675
lib/es6/Dom.js
7776
lib/es6/Dom_storage.js
7877
lib/es6/Dom_storage2.js
@@ -149,6 +148,8 @@ lib/es6/Stdlib_AsyncIterator.js
149148
lib/es6/Stdlib_BigInt.js
150149
lib/es6/Stdlib_BigInt64Array.js
151150
lib/es6/Stdlib_BigUint64Array.js
151+
lib/es6/Stdlib_Bool.js
152+
lib/es6/Stdlib_Char.js
152153
lib/es6/Stdlib_Console.js
153154
lib/es6/Stdlib_DataView.js
154155
lib/es6/Stdlib_Date.js
@@ -243,7 +244,6 @@ lib/js/Belt_internalMapString.js
243244
lib/js/Belt_internalSetBuckets.js
244245
lib/js/Belt_internalSetInt.js
245246
lib/js/Belt_internalSetString.js
246-
lib/js/Char.js
247247
lib/js/Dom.js
248248
lib/js/Dom_storage.js
249249
lib/js/Dom_storage2.js
@@ -320,6 +320,8 @@ lib/js/Stdlib_AsyncIterator.js
320320
lib/js/Stdlib_BigInt.js
321321
lib/js/Stdlib_BigInt64Array.js
322322
lib/js/Stdlib_BigUint64Array.js
323+
lib/js/Stdlib_Bool.js
324+
lib/js/Stdlib_Char.js
323325
lib/js/Stdlib_Console.js
324326
lib/js/Stdlib_DataView.js
325327
lib/js/Stdlib_Date.js
@@ -619,12 +621,6 @@ lib/ocaml/Belt_internalSetString.cmi
619621
lib/ocaml/Belt_internalSetString.cmj
620622
lib/ocaml/Belt_internalSetString.cmt
621623
lib/ocaml/Belt_internalSetString.res
622-
lib/ocaml/Char.cmi
623-
lib/ocaml/Char.cmj
624-
lib/ocaml/Char.cmt
625-
lib/ocaml/Char.cmti
626-
lib/ocaml/Char.res
627-
lib/ocaml/Char.resi
628624
lib/ocaml/Dom.cmi
629625
lib/ocaml/Dom.cmj
630626
lib/ocaml/Dom.cmt
@@ -967,6 +963,18 @@ lib/ocaml/Stdlib_BigUint64Array.cmi
967963
lib/ocaml/Stdlib_BigUint64Array.cmj
968964
lib/ocaml/Stdlib_BigUint64Array.cmt
969965
lib/ocaml/Stdlib_BigUint64Array.res
966+
lib/ocaml/Stdlib_Bool.cmi
967+
lib/ocaml/Stdlib_Bool.cmj
968+
lib/ocaml/Stdlib_Bool.cmt
969+
lib/ocaml/Stdlib_Bool.cmti
970+
lib/ocaml/Stdlib_Bool.res
971+
lib/ocaml/Stdlib_Bool.resi
972+
lib/ocaml/Stdlib_Char.cmi
973+
lib/ocaml/Stdlib_Char.cmj
974+
lib/ocaml/Stdlib_Char.cmt
975+
lib/ocaml/Stdlib_Char.cmti
976+
lib/ocaml/Stdlib_Char.res
977+
lib/ocaml/Stdlib_Char.resi
970978
lib/ocaml/Stdlib_Console.cmi
971979
lib/ocaml/Stdlib_Console.cmj
972980
lib/ocaml/Stdlib_Console.cmt

0 commit comments

Comments
 (0)