Skip to content

Replace some assert usage with unreachable in the src/shared/util.js file #14454

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 1 commit into from
Jan 15, 2022
Merged
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
22 changes: 15 additions & 7 deletions src/shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,10 +576,13 @@ class AbortException extends BaseException {
}

function bytesToString(bytes) {
assert(
bytes !== null && typeof bytes === "object" && bytes.length !== undefined,
"Invalid argument for bytesToString"
);
if (
typeof bytes !== "object" ||
bytes === null ||
bytes.length === undefined
) {
unreachable("Invalid argument for bytesToString");
}
const length = bytes.length;
const MAX_ARGUMENT_COUNT = 8192;
if (length < MAX_ARGUMENT_COUNT) {
Expand All @@ -595,7 +598,9 @@ function bytesToString(bytes) {
}

function stringToBytes(str) {
assert(typeof str === "string", "Invalid argument for stringToBytes");
if (typeof str !== "string") {
unreachable("Invalid argument for stringToBytes");
}
const length = str.length;
const bytes = new Uint8Array(length);
for (let i = 0; i < length; ++i) {
Expand All @@ -609,12 +614,15 @@ function stringToBytes(str) {
* @param {Array<any>|Uint8Array|string} arr
* @returns {number}
*/
// eslint-disable-next-line consistent-return
function arrayByteLength(arr) {
if (arr.length !== undefined) {
return arr.length;
}
assert(arr.byteLength !== undefined, "arrayByteLength - invalid argument.");
return arr.byteLength;
if (arr.byteLength !== undefined) {
return arr.byteLength;
}
unreachable("Invalid argument for arrayByteLength");
}

/**
Expand Down