Skip to content

Commit

Permalink
Optimized .to_property_key()
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat committed Aug 12, 2020
1 parent 548b295 commit ff818d4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
11 changes: 11 additions & 0 deletions boa/src/builtins/property/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,17 @@ impl From<i32> for PropertyKey {
}
}

impl From<f64> for PropertyKey {
fn from(value: f64) -> Self {
use num_traits::cast::FromPrimitive;
if let Some(index) = u32::from_f64(value) {
return PropertyKey::Index(index);
}

PropertyKey::String(ryu_js::Buffer::new().format(value).into())
}
}

impl PartialEq<&str> for PropertyKey {
fn eq(&self, other: &&str) -> bool {
match self {
Expand Down
22 changes: 15 additions & 7 deletions boa/src/exec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,13 +510,21 @@ impl Interpreter {
/// https://tc39.es/ecma262/#sec-topropertykey
#[allow(clippy::wrong_self_convention)]
pub(crate) fn to_property_key(&mut self, value: &Value) -> Result<PropertyKey, Value> {
let key = self.to_primitive(value, PreferredType::String)?;
if let Value::Symbol(ref symbol) = key {
Ok(PropertyKey::from(symbol.clone()))
} else {
let string = self.to_string(&key)?;
Ok(PropertyKey::from(string))
}
Ok(match *value {
// Fast path:
Value::Integer(integer) => integer.into(),
Value::Rational(rational) => rational.into(),
Value::String(ref string) => string.clone().into(),
Value::Symbol(ref symbol) => symbol.clone().into(),
// Slow path:
_ => match self.to_primitive(value, PreferredType::String)? {
Value::Integer(integer) => integer.into(),
Value::Rational(rational) => rational.into(),
Value::String(ref string) => string.clone().into(),
Value::Symbol(ref symbol) => symbol.clone().into(),
primitive => self.to_string(&primitive)?.into(),
},
})
}

/// https://tc39.es/ecma262/#sec-hasproperty
Expand Down

0 comments on commit ff818d4

Please sign in to comment.