Skip to content

Commit

Permalink
improve map conformance without losing perf
Browse files Browse the repository at this point in the history
  • Loading branch information
neeldug committed Jul 29, 2021
1 parent 461069c commit 9f0080d
Showing 1 changed file with 87 additions and 4 deletions.
91 changes: 87 additions & 4 deletions boa/src/builtins/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ use crate::{
object::{ConstructorBuilder, FunctionBuilder, ObjectData, PROTOTYPE},
property::{Attribute, DataDescriptor},
symbol::WellKnownSymbols,
BoaProfiler, Context, Result, Value,
BoaProfiler, Context, JsBigInt, Result, Value,
};
use ordered_map::OrderedMap;

pub mod map_iterator;
use map_iterator::{MapIterationKind, MapIterator};

use self::ordered_map::MapLock;
use num_traits::Zero;

pub mod ordered_map;
#[cfg(test)]
Expand Down Expand Up @@ -246,6 +247,30 @@ impl Map {

let size = if let Some(object) = this.as_object() {
if let Some(map) = object.borrow_mut().as_map_mut() {
let key = match &key {
Value::Rational(r) => {
if r.is_zero() {
Value::Rational(0f64)
} else {
key
}
}
Value::Integer(i) => {
if i.is_zero() {
Value::Integer(0)
} else {
key
}
}
Value::BigInt(b) => {
if b.is_zero() {
Value::BigInt(JsBigInt::from(0))
} else {
key
}
}
_ => key,
};
map.insert(key, value);
map.len()
} else {
Expand All @@ -270,6 +295,15 @@ impl Map {
/// [spec]: https://tc39.es/ecma262/#sec-map.prototype.delete
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/delete
pub(crate) fn delete(this: &Value, args: &[Value], context: &mut Context) -> Result<Value> {
// 1. Let M be the this value.
// 2. Perform ? RequireInternalSlot(M, [[MapData]]).
{
let obj = this.as_object().unwrap_or_default();
let obj = obj.borrow();
obj.as_map_ref().ok_or_else(|| {
context.construct_type_error("Map.prototype.clear called with invalid value")
})?;
}
let undefined = Value::Undefined;
let key = match args.len() {
0 => &undefined,
Expand Down Expand Up @@ -307,10 +341,40 @@ impl Map {
_ => &args[0],
};

let temp_v;

let key = match key {
Value::Rational(r) => {
if r.is_zero() {
temp_v = Value::Rational(0f64);
&temp_v
} else {
key
}
}
Value::Integer(i) => {
if i.is_zero() {
temp_v = Value::Integer(0);
&temp_v
} else {
key
}
}
Value::BigInt(b) => {
if b.is_zero() {
temp_v = Value::BigInt(JsBigInt::from(0));
&temp_v
} else {
key
}
}
_ => key,
};

if let Value::Object(ref object) = this {
let object = object.borrow();
if let Some(map) = object.as_map_ref() {
return Ok(if let Some(result) = map.get(key) {
return Ok(if let Some(result) = map.get(&key) {
result.clone()
} else {
Value::Undefined
Expand All @@ -331,12 +395,25 @@ impl Map {
///
/// [spec]: https://tc39.es/ecma262/#sec-map.prototype.clear
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/clear
pub(crate) fn clear(this: &Value, _: &[Value], _: &mut Context) -> Result<Value> {
pub(crate) fn clear(this: &Value, _: &[Value], context: &mut Context) -> Result<Value> {
// 1. Let M be the this value.
// 2. Perform ? RequireInternalSlot(M, [[MapData]]).
{
let obj = this.as_object().unwrap_or_default();
let obj = obj.borrow();
obj.as_map_ref().ok_or_else(|| {
context.construct_type_error("Map.prototype.clear called with invalid value")
})?;
}
// 3. Let entries be the List that is M.[[MapData]].
// 4. For each Record { [[Key]], [[Value]] } p of entries, do
// a. Set p.[[Key]] to empty.
// b. Set p.[[Value]] to empty.
this.set_data(ObjectData::Map(OrderedMap::new()));

Self::set_size(this, 0);

Ok(Value::Undefined)
Ok(Value::undefined())
}

/// `Map.prototype.has( key )`
Expand Down Expand Up @@ -382,6 +459,12 @@ impl Map {
}

let callback_arg = &args[0];

if !callback_arg.is_function() {
let name = callback_arg.to_string(context)?;
return context.throw_type_error(format!("{} is not a function", name));
}

let this_arg = args.get(1).cloned().unwrap_or_else(Value::undefined);

let mut index = 0;
Expand Down

0 comments on commit 9f0080d

Please sign in to comment.