-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
313 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use dash_middle::interner::sym; | ||
|
||
use crate::throw; | ||
use crate::value::function::native::CallContext; | ||
use crate::value::object::{NamedObject, PropertyKey}; | ||
use crate::value::ops::conversions::ValueConversion; | ||
use crate::value::weakmap::WeakMap; | ||
use crate::value::{Root, Value, ValueContext}; | ||
|
||
use super::receiver_t; | ||
|
||
pub fn constructor(cx: CallContext) -> Result<Value, Value> { | ||
let Some(new_target) = cx.new_target else { | ||
throw!(cx.scope, TypeError, "WeakMap constructor requires new") | ||
}; | ||
|
||
let weakmap = WeakMap::with_obj(NamedObject::instance_for_new_target(new_target, cx.scope)?); | ||
if let Some(iter) = cx.args.first() { | ||
let len = iter.length_of_array_like(cx.scope)?; | ||
|
||
for i in 0..len { | ||
let i = cx.scope.intern_usize(i); | ||
let item = iter | ||
.get_property(cx.scope, PropertyKey::String(i.into())) | ||
.root(cx.scope)?; | ||
let k = item | ||
.get_property(cx.scope, PropertyKey::String(sym::zero.into())) | ||
.root(cx.scope)?; | ||
let v = item | ||
.get_property(cx.scope, PropertyKey::String(sym::one.into())) | ||
.root(cx.scope)?; | ||
weakmap.set(k, v); | ||
} | ||
} | ||
|
||
Ok(Value::object(cx.scope.register(weakmap))) | ||
} | ||
|
||
pub fn set(cx: CallContext) -> Result<Value, Value> { | ||
let k = cx.args.first().unwrap_or_undefined(); | ||
let v = cx.args.get(1).unwrap_or_undefined(); | ||
receiver_t::<WeakMap>(cx.scope, &cx.this, "WeakMap.prototype.set")?.set(k, v); | ||
|
||
Ok(cx.this) | ||
} | ||
|
||
pub fn has(cx: CallContext) -> Result<Value, Value> { | ||
let item = cx.args.first().unwrap_or_undefined(); | ||
Ok(Value::boolean( | ||
receiver_t::<WeakMap>(cx.scope, &cx.this, "WeakMap.prototype.has")?.has(&item), | ||
)) | ||
} | ||
|
||
pub fn get(cx: CallContext) -> Result<Value, Value> { | ||
let item = cx.args.first().unwrap_or_undefined(); | ||
Ok(receiver_t::<WeakMap>(cx.scope, &cx.this, "WeakMap.prototype.get")? | ||
.get(&item) | ||
.unwrap_or_undefined()) | ||
} | ||
|
||
pub fn delete(cx: CallContext) -> Result<Value, Value> { | ||
let item = cx.args.first().unwrap_or_undefined(); | ||
let did_delete = receiver_t::<WeakMap>(cx.scope, &cx.this, "WeakMap.prototype.delete")?.delete(&item); | ||
Ok(Value::boolean(did_delete)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use crate::throw; | ||
use crate::value::function::native::CallContext; | ||
use crate::value::object::{NamedObject, PropertyKey}; | ||
use crate::value::ops::conversions::ValueConversion; | ||
use crate::value::weakset::WeakSet; | ||
use crate::value::{Root, Value, ValueContext}; | ||
|
||
use super::receiver_t; | ||
|
||
pub fn constructor(cx: CallContext) -> Result<Value, Value> { | ||
let Some(new_target) = cx.new_target else { | ||
throw!(cx.scope, TypeError, "WeakSet constructor requires new") | ||
}; | ||
|
||
let weakset = WeakSet::with_obj(NamedObject::instance_for_new_target(new_target, cx.scope)?); | ||
if let Some(iter) = cx.args.first() { | ||
let len = iter.length_of_array_like(cx.scope)?; | ||
|
||
for i in 0..len { | ||
let i = cx.scope.intern_usize(i); | ||
let item = iter | ||
.get_property(cx.scope, PropertyKey::String(i.into())) | ||
.root(cx.scope)?; | ||
|
||
weakset.add(item); | ||
} | ||
} | ||
|
||
Ok(Value::object(cx.scope.register(weakset))) | ||
} | ||
|
||
pub fn add(cx: CallContext) -> Result<Value, Value> { | ||
let item = cx.args.first().unwrap_or_undefined(); | ||
receiver_t::<WeakSet>(cx.scope, &cx.this, "WeakSet.prototype.add")?.add(item); | ||
|
||
Ok(cx.this) | ||
} | ||
|
||
pub fn has(cx: CallContext) -> Result<Value, Value> { | ||
let item = cx.args.first().unwrap_or_undefined(); | ||
Ok(Value::boolean( | ||
receiver_t::<WeakSet>(cx.scope, &cx.this, "WeakSet.prototype.has")?.has(&item), | ||
)) | ||
} | ||
|
||
pub fn delete(cx: CallContext) -> Result<Value, Value> { | ||
let item = cx.args.first().unwrap_or_undefined(); | ||
let did_delete = receiver_t::<WeakSet>(cx.scope, &cx.this, "WeakSet.prototype.delete")?.delete(&item); | ||
|
||
Ok(Value::boolean(did_delete)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use dash_proc_macro::Trace; | ||
|
||
use crate::{delegate, extract}; | ||
|
||
use super::Value; | ||
use super::map::Map; | ||
use super::object::{NamedObject, Object}; | ||
|
||
#[derive(Debug, Trace)] | ||
pub struct WeakMap { | ||
// for now | ||
map: Map, | ||
} | ||
|
||
impl WeakMap { | ||
pub fn with_obj(object: NamedObject) -> Self { | ||
Self { | ||
map: Map::with_obj(object), | ||
} | ||
} | ||
|
||
pub fn null() -> Self { | ||
Self::with_obj(NamedObject::null()) | ||
} | ||
|
||
pub fn set(&self, key: Value, value: Value) { | ||
self.map.set(key, value); | ||
} | ||
|
||
pub fn delete(&self, key: &Value) -> bool { | ||
self.map.delete(key) | ||
} | ||
|
||
pub fn get(&self, key: &Value) -> Option<Value> { | ||
self.map.get(key) | ||
} | ||
|
||
pub fn has(&self, key: &Value) -> bool { | ||
self.map.has(key) | ||
} | ||
} | ||
|
||
impl Object for WeakMap { | ||
delegate!( | ||
map, | ||
get_own_property_descriptor, | ||
get_property, | ||
get_property_descriptor, | ||
set_property, | ||
delete_property, | ||
set_prototype, | ||
get_prototype, | ||
apply, | ||
own_keys | ||
); | ||
|
||
extract!(self); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use dash_proc_macro::Trace; | ||
|
||
use crate::{delegate, extract}; | ||
|
||
use super::Value; | ||
use super::object::{NamedObject, Object}; | ||
use super::set::Set; | ||
|
||
#[derive(Debug, Trace)] | ||
pub struct WeakSet { | ||
// for now | ||
set: Set, | ||
} | ||
|
||
impl WeakSet { | ||
pub fn with_obj(object: NamedObject) -> Self { | ||
Self { | ||
set: Set::with_obj(object), | ||
} | ||
} | ||
|
||
pub fn null() -> Self { | ||
Self::with_obj(NamedObject::null()) | ||
} | ||
|
||
pub fn add(&self, key: Value) { | ||
self.set.add(key); | ||
} | ||
|
||
pub fn delete(&self, key: &Value) -> bool { | ||
self.set.delete(key) | ||
} | ||
|
||
pub fn has(&self, key: &Value) -> bool { | ||
self.set.has(key) | ||
} | ||
} | ||
|
||
impl Object for WeakSet { | ||
delegate!( | ||
set, | ||
get_own_property_descriptor, | ||
get_property, | ||
get_property_descriptor, | ||
set_property, | ||
delete_property, | ||
set_prototype, | ||
get_prototype, | ||
apply, | ||
own_keys | ||
); | ||
|
||
extract!(self); | ||
} |