mirror of https://github.com/boa-dev/boa.git
Browse Source
The ECMAScript 2022 specification removes the `toInteger` method, and replaces it with `toIntegerOrInfinity`, which is arguably better for us since the `JsValue::toInteger` returns an `f64`, which is pretty confusing at times. This pull request removes the `JsValue::to_integer` method, replaces all its calls by `JsValue::to_integer_or_infinity` or others per the spec and documents several methods from the `string` builtin.pull/1889/head
jedel1043
3 years ago
11 changed files with 744 additions and 479 deletions
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,69 @@
|
||||
use std::cmp::Ordering; |
||||
|
||||
/// Represents the result of `ToIntegerOrInfinity` operation
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] |
||||
pub enum IntegerOrInfinity { |
||||
PositiveInfinity, |
||||
Integer(i64), |
||||
NegativeInfinity, |
||||
} |
||||
|
||||
impl IntegerOrInfinity { |
||||
/// Clamps an `IntegerOrInfinity` between two `i64`, effectively converting
|
||||
/// it to an i64.
|
||||
pub fn clamp_finite(self, min: i64, max: i64) -> i64 { |
||||
assert!(min <= max); |
||||
|
||||
match self { |
||||
IntegerOrInfinity::Integer(i) => i.clamp(min, max), |
||||
IntegerOrInfinity::PositiveInfinity => max, |
||||
IntegerOrInfinity::NegativeInfinity => min, |
||||
} |
||||
} |
||||
|
||||
/// Gets the wrapped `i64` if the variant is an `Integer`.
|
||||
pub fn as_integer(self) -> Option<i64> { |
||||
match self { |
||||
IntegerOrInfinity::Integer(i) => Some(i), |
||||
_ => None, |
||||
} |
||||
} |
||||
} |
||||
|
||||
impl PartialEq<i64> for IntegerOrInfinity { |
||||
fn eq(&self, other: &i64) -> bool { |
||||
match self { |
||||
IntegerOrInfinity::Integer(i) => i == other, |
||||
_ => false, |
||||
} |
||||
} |
||||
} |
||||
|
||||
impl PartialEq<IntegerOrInfinity> for i64 { |
||||
fn eq(&self, other: &IntegerOrInfinity) -> bool { |
||||
match other { |
||||
IntegerOrInfinity::Integer(i) => i == other, |
||||
_ => false, |
||||
} |
||||
} |
||||
} |
||||
|
||||
impl PartialOrd<i64> for IntegerOrInfinity { |
||||
fn partial_cmp(&self, other: &i64) -> Option<Ordering> { |
||||
match self { |
||||
IntegerOrInfinity::PositiveInfinity => Some(Ordering::Greater), |
||||
IntegerOrInfinity::Integer(i) => i.partial_cmp(other), |
||||
IntegerOrInfinity::NegativeInfinity => Some(Ordering::Less), |
||||
} |
||||
} |
||||
} |
||||
|
||||
impl PartialOrd<IntegerOrInfinity> for i64 { |
||||
fn partial_cmp(&self, other: &IntegerOrInfinity) -> Option<Ordering> { |
||||
match other { |
||||
IntegerOrInfinity::PositiveInfinity => Some(Ordering::Less), |
||||
IntegerOrInfinity::Integer(i) => self.partial_cmp(i), |
||||
IntegerOrInfinity::NegativeInfinity => Some(Ordering::Greater), |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue