Skip to content

Commit 6b6a214

Browse files
committed
dummy Weak{Map,Set} impls
1 parent ca4668c commit 6b6a214

File tree

9 files changed

+313
-0
lines changed

9 files changed

+313
-0
lines changed

crates/dash_middle/src/interner.rs

+2
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,13 @@ pub mod sym {
251251
Promise,
252252
then,
253253
Set,
254+
WeakSet,
254255
add,
255256
has,
256257
clear,
257258
size,
258259
Map,
260+
WeakMap,
259261
RegExp,
260262
test,
261263
exec,

crates/dash_vm/src/js_std/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ pub mod set;
2222
pub mod string;
2323
pub mod symbol;
2424
pub mod typedarray;
25+
pub mod weakmap;
26+
pub mod weakset;
2527

2628
pub fn receiver_t<'a, T: 'static>(
2729
sc: &mut LocalScope<'_>,

crates/dash_vm/src/js_std/weakmap.rs

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
use dash_middle::interner::sym;
2+
3+
use crate::throw;
4+
use crate::value::function::native::CallContext;
5+
use crate::value::object::{NamedObject, PropertyKey};
6+
use crate::value::ops::conversions::ValueConversion;
7+
use crate::value::weakmap::WeakMap;
8+
use crate::value::{Root, Value, ValueContext};
9+
10+
use super::receiver_t;
11+
12+
pub fn constructor(cx: CallContext) -> Result<Value, Value> {
13+
let Some(new_target) = cx.new_target else {
14+
throw!(cx.scope, TypeError, "WeakMap constructor requires new")
15+
};
16+
17+
let weakmap = WeakMap::with_obj(NamedObject::instance_for_new_target(new_target, cx.scope)?);
18+
if let Some(iter) = cx.args.first() {
19+
let len = iter.length_of_array_like(cx.scope)?;
20+
21+
for i in 0..len {
22+
let i = cx.scope.intern_usize(i);
23+
let item = iter
24+
.get_property(cx.scope, PropertyKey::String(i.into()))
25+
.root(cx.scope)?;
26+
let k = item
27+
.get_property(cx.scope, PropertyKey::String(sym::zero.into()))
28+
.root(cx.scope)?;
29+
let v = item
30+
.get_property(cx.scope, PropertyKey::String(sym::one.into()))
31+
.root(cx.scope)?;
32+
weakmap.set(k, v);
33+
}
34+
}
35+
36+
Ok(Value::object(cx.scope.register(weakmap)))
37+
}
38+
39+
pub fn set(cx: CallContext) -> Result<Value, Value> {
40+
let k = cx.args.first().unwrap_or_undefined();
41+
let v = cx.args.get(1).unwrap_or_undefined();
42+
receiver_t::<WeakMap>(cx.scope, &cx.this, "WeakMap.prototype.set")?.set(k, v);
43+
44+
Ok(cx.this)
45+
}
46+
47+
pub fn has(cx: CallContext) -> Result<Value, Value> {
48+
let item = cx.args.first().unwrap_or_undefined();
49+
Ok(Value::boolean(
50+
receiver_t::<WeakMap>(cx.scope, &cx.this, "WeakMap.prototype.has")?.has(&item),
51+
))
52+
}
53+
54+
pub fn get(cx: CallContext) -> Result<Value, Value> {
55+
let item = cx.args.first().unwrap_or_undefined();
56+
Ok(receiver_t::<WeakMap>(cx.scope, &cx.this, "WeakMap.prototype.get")?
57+
.get(&item)
58+
.unwrap_or_undefined())
59+
}
60+
61+
pub fn delete(cx: CallContext) -> Result<Value, Value> {
62+
let item = cx.args.first().unwrap_or_undefined();
63+
let did_delete = receiver_t::<WeakMap>(cx.scope, &cx.this, "WeakMap.prototype.delete")?.delete(&item);
64+
Ok(Value::boolean(did_delete))
65+
}

crates/dash_vm/src/js_std/weakset.rs

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use crate::throw;
2+
use crate::value::function::native::CallContext;
3+
use crate::value::object::{NamedObject, PropertyKey};
4+
use crate::value::ops::conversions::ValueConversion;
5+
use crate::value::weakset::WeakSet;
6+
use crate::value::{Root, Value, ValueContext};
7+
8+
use super::receiver_t;
9+
10+
pub fn constructor(cx: CallContext) -> Result<Value, Value> {
11+
let Some(new_target) = cx.new_target else {
12+
throw!(cx.scope, TypeError, "WeakSet constructor requires new")
13+
};
14+
15+
let weakset = WeakSet::with_obj(NamedObject::instance_for_new_target(new_target, cx.scope)?);
16+
if let Some(iter) = cx.args.first() {
17+
let len = iter.length_of_array_like(cx.scope)?;
18+
19+
for i in 0..len {
20+
let i = cx.scope.intern_usize(i);
21+
let item = iter
22+
.get_property(cx.scope, PropertyKey::String(i.into()))
23+
.root(cx.scope)?;
24+
25+
weakset.add(item);
26+
}
27+
}
28+
29+
Ok(Value::object(cx.scope.register(weakset)))
30+
}
31+
32+
pub fn add(cx: CallContext) -> Result<Value, Value> {
33+
let item = cx.args.first().unwrap_or_undefined();
34+
receiver_t::<WeakSet>(cx.scope, &cx.this, "WeakSet.prototype.add")?.add(item);
35+
36+
Ok(cx.this)
37+
}
38+
39+
pub fn has(cx: CallContext) -> Result<Value, Value> {
40+
let item = cx.args.first().unwrap_or_undefined();
41+
Ok(Value::boolean(
42+
receiver_t::<WeakSet>(cx.scope, &cx.this, "WeakSet.prototype.has")?.has(&item),
43+
))
44+
}
45+
46+
pub fn delete(cx: CallContext) -> Result<Value, Value> {
47+
let item = cx.args.first().unwrap_or_undefined();
48+
let did_delete = receiver_t::<WeakSet>(cx.scope, &cx.this, "WeakSet.prototype.delete")?.delete(&item);
49+
50+
Ok(Value::boolean(did_delete))
51+
}

crates/dash_vm/src/lib.rs

+55
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,59 @@ impl Vm {
885885
&mut scope,
886886
);
887887

888+
let weakmap_ctor = register(
889+
scope.statics.weakmap_constructor,
890+
function_proto,
891+
function_ctor,
892+
[],
893+
[],
894+
[],
895+
Some((sym::WeakMap, scope.statics.weakmap_prototype)),
896+
&mut scope
897+
);
898+
899+
register(
900+
scope.statics.weakmap_prototype,
901+
object_proto,
902+
weakmap_ctor,
903+
[
904+
(sym::set, scope.statics.weakmap_set),
905+
(sym::has, scope.statics.weakmap_has),
906+
(sym::get, scope.statics.weakmap_get),
907+
(sym::delete, scope.statics.weakmap_delete),
908+
],
909+
[],
910+
[],
911+
None,
912+
&mut scope
913+
);
914+
915+
let weakset_ctor = register(
916+
scope.statics.weakset_constructor,
917+
function_proto,
918+
function_ctor,
919+
[],
920+
[],
921+
[],
922+
Some((sym::WeakSet, scope.statics.weakset_prototype)),
923+
&mut scope
924+
);
925+
926+
register(
927+
scope.statics.weakset_prototype,
928+
object_proto,
929+
weakset_ctor,
930+
[
931+
(sym::add, scope.statics.weakset_add),
932+
(sym::has, scope.statics.weakset_has),
933+
(sym::delete, scope.statics.weakset_delete),
934+
],
935+
[],
936+
[],
937+
None,
938+
&mut scope
939+
);
940+
888941
let regexp_ctor = register(
889942
scope.statics.regexp_ctor,
890943
function_proto,
@@ -1152,6 +1205,8 @@ impl Vm {
11521205
(sym::Object, object_ctor),
11531206
(sym::Set, set_ctor),
11541207
(sym::Map, map_ctor),
1208+
(sym::WeakMap, weakmap_ctor),
1209+
(sym::WeakSet, weakset_ctor),
11551210
(sym::console, console),
11561211
(sym::Math, math),
11571212
(sym::Number, number_ctor),

crates/dash_vm/src/statics.rs

+24
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use crate::value::function::{Function, FunctionKind};
88
use crate::value::map::Map;
99
use crate::value::regex::RegExp;
1010
use crate::value::set::Set;
11+
use crate::value::weakmap::WeakMap;
12+
use crate::value::weakset::WeakSet;
1113
use dash_middle::interner::{self, sym};
1214

1315
use super::value::array::{Array, ArrayIterator};
@@ -246,6 +248,17 @@ pub struct Statics {
246248
pub map_delete: ObjectId,
247249
pub map_clear: ObjectId,
248250
pub map_size: ObjectId,
251+
pub weakmap_constructor: ObjectId,
252+
pub weakmap_prototype: ObjectId,
253+
pub weakmap_set: ObjectId,
254+
pub weakmap_has: ObjectId,
255+
pub weakmap_get: ObjectId,
256+
pub weakmap_delete: ObjectId,
257+
pub weakset_constructor: ObjectId,
258+
pub weakset_prototype: ObjectId,
259+
pub weakset_add: ObjectId,
260+
pub weakset_has: ObjectId,
261+
pub weakset_delete: ObjectId,
249262
pub regexp_ctor: ObjectId,
250263
pub regexp_prototype: ObjectId,
251264
pub regexp_test: ObjectId,
@@ -525,6 +538,17 @@ impl Statics {
525538
date_get_time: function(gc, sym::getTime, js_std::date::get_time),
526539
json_ctor: function(gc, sym::JSON, js_std::json::constructor),
527540
json_parse: function(gc, sym::parse, js_std::json::parse),
541+
weakmap_constructor: function(gc, sym::WeakMap, js_std::weakmap::constructor),
542+
weakmap_prototype: builtin_object(gc, WeakMap::null()),
543+
weakmap_set: function(gc, sym::set, js_std::weakmap::set),
544+
weakmap_has: function(gc, sym::has, js_std::weakmap::has),
545+
weakmap_get: function(gc, sym::get, js_std::weakmap::get),
546+
weakmap_delete: function(gc, sym::delete, js_std::weakmap::delete),
547+
weakset_constructor: function(gc, sym::WeakSet, js_std::weakset::constructor),
548+
weakset_prototype: builtin_object(gc, WeakSet::null()),
549+
weakset_add: function(gc, sym::add, js_std::weakset::add),
550+
weakset_has: function(gc, sym::has, js_std::weakset::has),
551+
weakset_delete: function(gc, sym::delete, js_std::weakset::delete),
528552
}
529553
}
530554
}

crates/dash_vm/src/value/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ pub mod promise;
1414
pub mod regex;
1515
pub mod set;
1616
pub mod typedarray;
17+
pub mod weakmap;
18+
pub mod weakset;
1719

1820
use std::any::TypeId;
1921
use std::ops::ControlFlow;

crates/dash_vm/src/value/weakmap.rs

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
use dash_proc_macro::Trace;
2+
3+
use crate::{delegate, extract};
4+
5+
use super::Value;
6+
use super::map::Map;
7+
use super::object::{NamedObject, Object};
8+
9+
#[derive(Debug, Trace)]
10+
pub struct WeakMap {
11+
// for now
12+
map: Map,
13+
}
14+
15+
impl WeakMap {
16+
pub fn with_obj(object: NamedObject) -> Self {
17+
Self {
18+
map: Map::with_obj(object),
19+
}
20+
}
21+
22+
pub fn null() -> Self {
23+
Self::with_obj(NamedObject::null())
24+
}
25+
26+
pub fn set(&self, key: Value, value: Value) {
27+
self.map.set(key, value);
28+
}
29+
30+
pub fn delete(&self, key: &Value) -> bool {
31+
self.map.delete(key)
32+
}
33+
34+
pub fn get(&self, key: &Value) -> Option<Value> {
35+
self.map.get(key)
36+
}
37+
38+
pub fn has(&self, key: &Value) -> bool {
39+
self.map.has(key)
40+
}
41+
}
42+
43+
impl Object for WeakMap {
44+
delegate!(
45+
map,
46+
get_own_property_descriptor,
47+
get_property,
48+
get_property_descriptor,
49+
set_property,
50+
delete_property,
51+
set_prototype,
52+
get_prototype,
53+
apply,
54+
own_keys
55+
);
56+
57+
extract!(self);
58+
}

crates/dash_vm/src/value/weakset.rs

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use dash_proc_macro::Trace;
2+
3+
use crate::{delegate, extract};
4+
5+
use super::Value;
6+
use super::object::{NamedObject, Object};
7+
use super::set::Set;
8+
9+
#[derive(Debug, Trace)]
10+
pub struct WeakSet {
11+
// for now
12+
set: Set,
13+
}
14+
15+
impl WeakSet {
16+
pub fn with_obj(object: NamedObject) -> Self {
17+
Self {
18+
set: Set::with_obj(object),
19+
}
20+
}
21+
22+
pub fn null() -> Self {
23+
Self::with_obj(NamedObject::null())
24+
}
25+
26+
pub fn add(&self, key: Value) {
27+
self.set.add(key);
28+
}
29+
30+
pub fn delete(&self, key: &Value) -> bool {
31+
self.set.delete(key)
32+
}
33+
34+
pub fn has(&self, key: &Value) -> bool {
35+
self.set.has(key)
36+
}
37+
}
38+
39+
impl Object for WeakSet {
40+
delegate!(
41+
set,
42+
get_own_property_descriptor,
43+
get_property,
44+
get_property_descriptor,
45+
set_property,
46+
delete_property,
47+
set_prototype,
48+
get_prototype,
49+
apply,
50+
own_keys
51+
);
52+
53+
extract!(self);
54+
}

0 commit comments

Comments
 (0)