summaryrefslogtreecommitdiff
path: root/js/src
diff options
context:
space:
mode:
authorMoonchild <moonchild@palemoon.org>2023-10-19 10:17:11 +0200
committerMoonchild <moonchild@palemoon.org>2023-10-19 10:17:11 +0200
commit6ad6cc9c2d245be3e0ed7526b09f26b8cd5854a7 (patch)
treeda3aad64364f599c0f08e094b6b358c145288794 /js/src
parent60b4d804bd8f29582df32db7e862e1e91b221e69 (diff)
downloaduxp-6ad6cc9c2d245be3e0ed7526b09f26b8cd5854a7.tar.gz
Issue #2350 - Part 0: Add a helper function DefaultNumberOption
This is the abstract operation that converts an option value to a number value, checking whether it is in the allowed range, and filling in a fallback value if necessary.
Diffstat (limited to 'js/src')
-rw-r--r--js/src/builtin/intl/CommonFunctions.js46
1 files changed, 46 insertions, 0 deletions
diff --git a/js/src/builtin/intl/CommonFunctions.js b/js/src/builtin/intl/CommonFunctions.js
index effabf020f..80a35f1851 100644
--- a/js/src/builtin/intl/CommonFunctions.js
+++ b/js/src/builtin/intl/CommonFunctions.js
@@ -647,6 +647,52 @@ function GetOption(options, property, type, values, fallback) {
return fallback;
}
+
+/**
+ * The abstract operation DefaultNumberOption converts value to a Number value,
+ * checks whether it is in the allowed range, and fills in a fallback value if
+ * necessary.
+ *
+ * Spec: ECMAScript Internationalization API Specification, 9.2.11.
+ */
+function DefaultNumberOption(value, minimum, maximum, fallback) {
+ assert(
+ typeof minimum === "number" && (minimum | 0) === minimum,
+ "DefaultNumberOption"
+ );
+ assert(
+ typeof maximum === "number" && (maximum | 0) === maximum,
+ "DefaultNumberOption"
+ );
+ assert(
+ fallback === undefined ||
+ (typeof fallback === "number" && (fallback | 0) === fallback),
+ "DefaultNumberOption"
+ );
+ assert(
+ fallback === undefined || (minimum <= fallback && fallback <= maximum),
+ "DefaultNumberOption"
+ );
+
+ // Step 1.
+ if (value === undefined) {
+ return fallback;
+ }
+
+ // Step 2.
+ value = ToNumber(value);
+
+ // Step 3.
+ if (Number_isNaN(value) || value < minimum || value > maximum) {
+ ThrowRangeError(JSMSG_INVALID_DIGITS_VALUE, value);
+ }
+
+ // Step 4.
+ // Apply bitwise-or to convert -0 to +0 per ES2017, 5.2 and to ensure the
+ // result is an int32 value.
+ return std_Math_floor(value) | 0;
+}
+
/**
* Extracts a property value from the provided options object, converts it to a
* Number value, checks whether it is in the allowed range, and fills in a