Skip to content
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

Use lock for map iteration #1366

Merged
merged 4 commits into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
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
21 changes: 13 additions & 8 deletions boa/src/builtins/map/map_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use crate::{
};
use gc::{Finalize, Trace};

use super::{ordered_map::MapLock, Map};

#[derive(Debug, Clone, Finalize, Trace)]
pub enum MapIterationKind {
Key,
Expand All @@ -25,18 +27,21 @@ pub struct MapIterator {
iterated_map: Value,
map_next_index: usize,
map_iteration_kind: MapIterationKind,
lock: MapLock,
}

impl MapIterator {
pub(crate) const NAME: &'static str = "MapIterator";

/// Constructs a new `MapIterator`, that will iterate over `map`, starting at index 0
fn new(map: Value, kind: MapIterationKind) -> Self {
MapIterator {
fn new(map: Value, kind: MapIterationKind, context: &mut Context) -> Result<Self> {
let lock = Map::lock(&map, context)?;
Ok(MapIterator {
iterated_map: map,
map_next_index: 0,
map_iteration_kind: kind,
}
lock,
})
}

/// Abstract operation CreateMapIterator( map, kind )
Expand All @@ -48,17 +53,17 @@ impl MapIterator {
///
/// [spec]: https://www.ecma-international.org/ecma-262/11.0/index.html#sec-createmapiterator
pub(crate) fn create_map_iterator(
context: &Context,
context: &mut Context,
map: Value,
kind: MapIterationKind,
) -> Value {
) -> Result<Value> {
let map_iterator = Value::new_object(context);
map_iterator.set_data(ObjectData::MapIterator(Self::new(map, kind)));
map_iterator.set_data(ObjectData::MapIterator(Self::new(map, kind, context)?));
map_iterator
.as_object()
.expect("map iterator object")
.set_prototype_instance(context.iterator_prototypes().map_iterator().into());
map_iterator
Ok(map_iterator)
}

/// %MapIteratorPrototype%.next( )
Expand All @@ -83,7 +88,7 @@ impl MapIterator {

if let Value::Object(ref object) = m {
if let Some(entries) = object.borrow().as_map_ref() {
let num_entries = entries.len();
let num_entries = entries.full_len();
while index < num_entries {
let e = entries.get_index(index);
index += 1;
Expand Down
48 changes: 28 additions & 20 deletions boa/src/builtins/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ use ordered_map::OrderedMap;
pub mod map_iterator;
use map_iterator::{MapIterationKind, MapIterator};

use self::ordered_map::MapLock;

pub mod ordered_map;
#[cfg(test)]
mod tests;

#[derive(Debug, Clone)]
pub(crate) struct Map(OrderedMap<Value, Value>);
pub(crate) struct Map(OrderedMap<Value>);

impl BuiltIn for Map {
const NAME: &'static str = "Map";
Expand Down Expand Up @@ -198,11 +200,7 @@ impl Map {
/// [spec]: https://www.ecma-international.org/ecma-262/11.0/index.html#sec-map.prototype.entries
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/entries
pub(crate) fn entries(this: &Value, _: &[Value], context: &mut Context) -> Result<Value> {
Ok(MapIterator::create_map_iterator(
context,
this.clone(),
MapIterationKind::KeyAndValue,
))
MapIterator::create_map_iterator(context, this.clone(), MapIterationKind::KeyAndValue)
}

/// `Map.prototype.keys()`
Expand All @@ -216,11 +214,7 @@ impl Map {
/// [spec]: https://tc39.es/ecma262/#sec-map.prototype.keys
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/keys
pub(crate) fn keys(this: &Value, _: &[Value], context: &mut Context) -> Result<Value> {
Ok(MapIterator::create_map_iterator(
context,
this.clone(),
MapIterationKind::Key,
))
MapIterator::create_map_iterator(context, this.clone(), MapIterationKind::Key)
}

/// Helper function to set the size property.
Expand Down Expand Up @@ -392,7 +386,9 @@ impl Map {

let mut index = 0;

while index < Map::get_size(this, context)? {
let lock = Map::lock(this, context)?;

while index < Map::get_full_len(this, context)? {
let arguments = if let Value::Object(ref object) = this {
let object = object.borrow();
if let Some(map) = object.as_map_ref() {
Expand All @@ -415,15 +411,31 @@ impl Map {
index += 1;
}

drop(lock);

Ok(Value::Undefined)
}

/// Helper function to get the size of the map.
fn get_size(map: &Value, context: &mut Context) -> Result<usize> {
/// Helper function to get the full size of the map.
fn get_full_len(map: &Value, context: &mut Context) -> Result<usize> {
if let Value::Object(ref object) = map {
let object = object.borrow();
if let Some(map) = object.as_map_ref() {
Ok(map.len())
Ok(map.full_len())
} else {
Err(context.construct_type_error("'this' is not a Map"))
}
} else {
Err(context.construct_type_error("'this' is not a Map"))
}
}

/// Helper function to lock the map.
fn lock(map: &Value, context: &mut Context) -> Result<MapLock> {
if let Value::Object(ref object) = map {
let mut map = object.borrow_mut();
if let Some(map) = map.as_map_mut() {
Ok(map.lock(object.clone()))
} else {
Err(context.construct_type_error("'this' is not a Map"))
}
Expand All @@ -443,11 +455,7 @@ impl Map {
/// [spec]: https://tc39.es/ecma262/#sec-map.prototype.values
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/values
pub(crate) fn values(this: &Value, _: &[Value], context: &mut Context) -> Result<Value> {
Ok(MapIterator::create_map_iterator(
context,
this.clone(),
MapIterationKind::Value,
))
MapIterator::create_map_iterator(context, this.clone(), MapIterationKind::Value)
}

/// Helper function to get a key-value pair from an array.
Expand Down
Loading