Skip to content

Add Stdlib Bool and Char modules + Better deprecation msg in Pervasives #7361

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

Merged
merged 4 commits into from
Mar 24, 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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
- Deprecate unsafe host-specific bindings from stdlib. https://github.com/rescript-lang/rescript/pull/7334
- Make unsafe function names consistent in Stdlib.String. https://github.com/rescript-lang/rescript/pull/7337
- `rescript` package does not trigger `postinstall` script anymore. https://github.com/rescript-lang/rescript/pull/7350
- Add Stdlib Bool and Char modules and improve Pervasives deprecation messages. https://github.com/rescript-lang/rescript/pull/7361

#### :bug: Bug fix

Expand Down
9 changes: 8 additions & 1 deletion analysis/reanalyze/src/ExnLib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ let raisesLibTable : (Name.t, Exceptions.t) Hashtbl.t =
]
in
let stdlibBigInt = [("fromStringExn", [jsExnError])] in
let stdlibBool = [("fromStringExn", [invalidArgument])] in
let stdlibChar = [("fromIntExn", [invalidArgument])] in
let stdlibError = [("raise", [jsExnError])] in
let stdlibExn =
[
Expand Down Expand Up @@ -140,7 +142,8 @@ let raisesLibTable : (Name.t, Exceptions.t) Hashtbl.t =
("Belt_SetInt", beltSet);
("Belt_SetString", beltSet);
("BigInt", stdlibBigInt);
("Char", [("chr", [invalidArgument])]);
("Bool", stdlibBool);
("Char", stdlibChar);
("Error", stdlibError);
("Exn", stdlibExn);
("Js.Json", [("parseExn", [jsExnError])]);
Expand All @@ -159,6 +162,10 @@ let raisesLibTable : (Name.t, Exceptions.t) Hashtbl.t =
("Stdlib", stdlib);
("Stdlib_BigInt", stdlibBigInt);
("Stdlib.BigInt", stdlibBigInt);
("Stdlib_Bool", stdlibBool);
("Stdlib.Bool", stdlibBool);
("Stdlib_Char", stdlibChar);
("Stdlib.Char", stdlibChar);
("Stdlib_Error", stdlibError);
("Stdlib.Error", stdlibError);
("Stdlib_Exn", stdlibExn);
Expand Down
8 changes: 7 additions & 1 deletion lib/es6/Stdlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function assertEqual(a, b) {
RE_EXN_ID: "Assert_failure",
_1: [
"Stdlib.res",
117,
119,
4
],
Error: new Error()
Expand All @@ -27,6 +27,10 @@ let $$Array;

let $$BigInt;

let Bool;

let Char;

let Console;

let $$DataView;
Expand Down Expand Up @@ -118,6 +122,8 @@ export {
IntervalId,
$$Array,
$$BigInt,
Bool,
Char,
Console,
$$DataView,
$$Date,
Expand Down
43 changes: 43 additions & 0 deletions lib/es6/Stdlib_Bool.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@



function toString(b) {
if (b) {
return "true";
} else {
return "false";
}
}

function fromString(s) {
switch (s) {
case "false" :
return false;
case "true" :
return true;
default:
return;
}
}

function fromStringExn(param) {
switch (param) {
case "false" :
return false;
case "true" :
return true;
default:
throw {
RE_EXN_ID: "Invalid_argument",
_1: "Bool.fromStringExn: value is neither \"true\" nor \"false\"",
Error: new Error()
};
}
}

export {
toString,
fromString,
fromStringExn,
}
/* No side effect */
37 changes: 27 additions & 10 deletions lib/es6/Char.js → lib/es6/Stdlib_Char.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@



function fromIntExn(n) {
if (n < 0 || n > 255) {
throw {
RE_EXN_ID: "Invalid_argument",
_1: "`Char.fromIntExn` expects an integer between 0 and 255",
Error: new Error()
};
}
return n;
}

function fromInt(n) {
if (n < 0 || n > 255) {
return;
} else {
return n;
}
}

function escaped(param) {
let exit = 0;
if (param >= 40) {
Expand Down Expand Up @@ -54,35 +73,33 @@ function escaped(param) {
}
}

function lowercase_ascii(c) {
function toLowerCaseAscii(c) {
if (c >= /* 'A' */65 && c <= /* 'Z' */90) {
return c + 32 | 0;
} else {
return c;
}
}

function uppercase_ascii(c) {
function toUpperCaseAscii(c) {
if (c >= /* 'a' */97 && c <= /* 'z' */122) {
return c - 32 | 0;
} else {
return c;
}
}

function compare(c1, c2) {
return c1 - c2 | 0;
}
let lowercase_ascii = toLowerCaseAscii;

function equal(c1, c2) {
return (c1 - c2 | 0) === 0;
}
let uppercase_ascii = toUpperCaseAscii;

export {
escaped,
lowercase_ascii,
uppercase_ascii,
compare,
equal,
toLowerCaseAscii,
toUpperCaseAscii,
fromIntExn,
fromInt,
}
/* No side effect */
8 changes: 7 additions & 1 deletion lib/js/Stdlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function assertEqual(a, b) {
RE_EXN_ID: "Assert_failure",
_1: [
"Stdlib.res",
117,
119,
4
],
Error: new Error()
Expand All @@ -27,6 +27,10 @@ let $$Array;

let $$BigInt;

let Bool;

let Char;

let Console;

let $$DataView;
Expand Down Expand Up @@ -117,6 +121,8 @@ exports.TimeoutId = TimeoutId;
exports.IntervalId = IntervalId;
exports.$$Array = $$Array;
exports.$$BigInt = $$BigInt;
exports.Bool = Bool;
exports.Char = Char;
exports.Console = Console;
exports.$$DataView = $$DataView;
exports.$$Date = $$Date;
Expand Down
41 changes: 41 additions & 0 deletions lib/js/Stdlib_Bool.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';


function toString(b) {
if (b) {
return "true";
} else {
return "false";
}
}

function fromString(s) {
switch (s) {
case "false" :
return false;
case "true" :
return true;
default:
return;
}
}

function fromStringExn(param) {
switch (param) {
case "false" :
return false;
case "true" :
return true;
default:
throw {
RE_EXN_ID: "Invalid_argument",
_1: "Bool.fromStringExn: value is neither \"true\" nor \"false\"",
Error: new Error()
};
}
}

exports.toString = toString;
exports.fromString = fromString;
exports.fromStringExn = fromStringExn;
/* No side effect */
37 changes: 27 additions & 10 deletions lib/js/Char.js → lib/js/Stdlib_Char.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
'use strict';


function fromIntExn(n) {
if (n < 0 || n > 255) {
throw {
RE_EXN_ID: "Invalid_argument",
_1: "`Char.fromIntExn` expects an integer between 0 and 255",
Error: new Error()
};
}
return n;
}

function fromInt(n) {
if (n < 0 || n > 255) {
return;
} else {
return n;
}
}

function escaped(param) {
let exit = 0;
if (param >= 40) {
Expand Down Expand Up @@ -54,33 +73,31 @@ function escaped(param) {
}
}

function lowercase_ascii(c) {
function toLowerCaseAscii(c) {
if (c >= /* 'A' */65 && c <= /* 'Z' */90) {
return c + 32 | 0;
} else {
return c;
}
}

function uppercase_ascii(c) {
function toUpperCaseAscii(c) {
if (c >= /* 'a' */97 && c <= /* 'z' */122) {
return c - 32 | 0;
} else {
return c;
}
}

function compare(c1, c2) {
return c1 - c2 | 0;
}
let lowercase_ascii = toLowerCaseAscii;

function equal(c1, c2) {
return (c1 - c2 | 0) === 0;
}
let uppercase_ascii = toUpperCaseAscii;

exports.escaped = escaped;
exports.lowercase_ascii = lowercase_ascii;
exports.uppercase_ascii = uppercase_ascii;
exports.compare = compare;
exports.equal = equal;
exports.toLowerCaseAscii = toLowerCaseAscii;
exports.toUpperCaseAscii = toUpperCaseAscii;
exports.fromIntExn = fromIntExn;
exports.fromInt = fromInt;
/* No side effect */
24 changes: 16 additions & 8 deletions packages/artifacts.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ lib/es6/Belt_internalMapString.js
lib/es6/Belt_internalSetBuckets.js
lib/es6/Belt_internalSetInt.js
lib/es6/Belt_internalSetString.js
lib/es6/Char.js
lib/es6/Dom.js
lib/es6/Dom_storage.js
lib/es6/Dom_storage2.js
Expand Down Expand Up @@ -149,6 +148,8 @@ lib/es6/Stdlib_AsyncIterator.js
lib/es6/Stdlib_BigInt.js
lib/es6/Stdlib_BigInt64Array.js
lib/es6/Stdlib_BigUint64Array.js
lib/es6/Stdlib_Bool.js
lib/es6/Stdlib_Char.js
lib/es6/Stdlib_Console.js
lib/es6/Stdlib_DataView.js
lib/es6/Stdlib_Date.js
Expand Down Expand Up @@ -243,7 +244,6 @@ lib/js/Belt_internalMapString.js
lib/js/Belt_internalSetBuckets.js
lib/js/Belt_internalSetInt.js
lib/js/Belt_internalSetString.js
lib/js/Char.js
lib/js/Dom.js
lib/js/Dom_storage.js
lib/js/Dom_storage2.js
Expand Down Expand Up @@ -320,6 +320,8 @@ lib/js/Stdlib_AsyncIterator.js
lib/js/Stdlib_BigInt.js
lib/js/Stdlib_BigInt64Array.js
lib/js/Stdlib_BigUint64Array.js
lib/js/Stdlib_Bool.js
lib/js/Stdlib_Char.js
lib/js/Stdlib_Console.js
lib/js/Stdlib_DataView.js
lib/js/Stdlib_Date.js
Expand Down Expand Up @@ -619,12 +621,6 @@ lib/ocaml/Belt_internalSetString.cmi
lib/ocaml/Belt_internalSetString.cmj
lib/ocaml/Belt_internalSetString.cmt
lib/ocaml/Belt_internalSetString.res
lib/ocaml/Char.cmi
lib/ocaml/Char.cmj
lib/ocaml/Char.cmt
lib/ocaml/Char.cmti
lib/ocaml/Char.res
lib/ocaml/Char.resi
lib/ocaml/Dom.cmi
lib/ocaml/Dom.cmj
lib/ocaml/Dom.cmt
Expand Down Expand Up @@ -967,6 +963,18 @@ lib/ocaml/Stdlib_BigUint64Array.cmi
lib/ocaml/Stdlib_BigUint64Array.cmj
lib/ocaml/Stdlib_BigUint64Array.cmt
lib/ocaml/Stdlib_BigUint64Array.res
lib/ocaml/Stdlib_Bool.cmi
lib/ocaml/Stdlib_Bool.cmj
lib/ocaml/Stdlib_Bool.cmt
lib/ocaml/Stdlib_Bool.cmti
lib/ocaml/Stdlib_Bool.res
lib/ocaml/Stdlib_Bool.resi
lib/ocaml/Stdlib_Char.cmi
lib/ocaml/Stdlib_Char.cmj
lib/ocaml/Stdlib_Char.cmt
lib/ocaml/Stdlib_Char.cmti
lib/ocaml/Stdlib_Char.res
lib/ocaml/Stdlib_Char.resi
lib/ocaml/Stdlib_Console.cmi
lib/ocaml/Stdlib_Console.cmj
lib/ocaml/Stdlib_Console.cmt
Expand Down
Loading