Skip to content

Commit

Permalink
support dynamic getters in classes
Browse files Browse the repository at this point in the history
  • Loading branch information
y21 committed May 5, 2024
1 parent d35d6c7 commit 25d269b
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 16 deletions.
4 changes: 3 additions & 1 deletion crates/dash_compiler/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,9 @@ impl<'cx, 'interner> InstructionBuilder<'cx, 'interner> {
for member in constants.into_iter().rev() {
let kind_id = CompilerObjectMemberKind::from(&member) as u8;
match member {
ObjectMemberKind::Dynamic(..) => self.write(kind_id),
ObjectMemberKind::Dynamic(_)
| ObjectMemberKind::DynamicGetter(_)
| ObjectMemberKind::DynamicSetter(_) => self.write(kind_id),
ObjectMemberKind::Static(name) => compile_object_member_kind(self, span, name, kind_id)?,
ObjectMemberKind::Spread => self.write(kind_id),
ObjectMemberKind::Getter(name) | ObjectMemberKind::Setter(name) => {
Expand Down
27 changes: 13 additions & 14 deletions crates/dash_compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2359,12 +2359,17 @@ fn compile_object_members(
for (member, value) in iter {
ib.accept_expr(value)?;

if let ObjectMemberKind::Dynamic(expr) = member {
// TODO: no clone needed, the `expr` is not needed in ib.build_objlit
members.push(ObjectMemberKind::Dynamic(expr.clone()));
ib.accept_expr(expr)?;
} else {
members.push(member);
let mut push_and_accept = |ct: fn(Expr) -> ObjectMemberKind, expr: Expr| {
// TODO: no clone really needed, the `expr` is not needed in ib.build_objlit
members.push(ct(expr.clone()));
ib.accept_expr(expr)
};

match member {
ObjectMemberKind::DynamicGetter(expr) => push_and_accept(ObjectMemberKind::DynamicGetter, expr)?,
ObjectMemberKind::DynamicSetter(expr) => push_and_accept(ObjectMemberKind::DynamicSetter, expr)?,
ObjectMemberKind::Dynamic(expr) => push_and_accept(ObjectMemberKind::Dynamic, expr)?,
_ => members.push(member),
}
}

Expand Down Expand Up @@ -2396,16 +2401,10 @@ fn compile_class_members(
}),
),
(ClassMemberKey::Computed(key), ClassMemberValue::Getter(value)) => {
let Some(ident) = key.kind.as_identifier() else {
panic!("FIXME")
};
(ObjectMemberKind::Getter(ident), mk_fn(value))
(ObjectMemberKind::DynamicGetter(key), mk_fn(value))
}
(ClassMemberKey::Computed(key), ClassMemberValue::Setter(value)) => {
let Some(ident) = key.kind.as_identifier() else {
panic!("FIXME")
};
(ObjectMemberKind::Setter(ident), mk_fn(value))
(ObjectMemberKind::DynamicSetter(key), mk_fn(value))
}
(ClassMemberKey::Named(key), ClassMemberValue::Method(value)) => {
(ObjectMemberKind::Static(key), mk_fn(value))
Expand Down
2 changes: 2 additions & 0 deletions crates/dash_decompiler/src/decompiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ impl<'interner, 'buf> FunctionDecompiler<'interner, 'buf> {
ObjectMemberKind::Spread => {
props.push(String::from("<spread>"));
}
ObjectMemberKind::DynamicGetter => props.push(String::from("get <dynamic>")),
ObjectMemberKind::DynamicSetter => props.push(String::from("set <dynamic>")),
}
}
let props = props.iter().map(|v| v as &dyn fmt::Display).collect::<Vec<_>>();
Expand Down
4 changes: 4 additions & 0 deletions crates/dash_middle/src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ pub enum StaticImportKind {
#[derive(FromRepr)]
pub enum ObjectMemberKind {
Getter,
DynamicGetter,
Setter,
DynamicSetter,
Static,
Dynamic,
Spread,
Expand All @@ -138,7 +140,9 @@ impl From<&ParserObjectMemberKind> for ObjectMemberKind {
fn from(v: &ParserObjectMemberKind) -> Self {
match v {
ParserObjectMemberKind::Dynamic(..) => Self::Dynamic,
ParserObjectMemberKind::DynamicGetter(..) => Self::DynamicGetter,
ParserObjectMemberKind::Getter(..) => Self::Getter,
ParserObjectMemberKind::DynamicSetter(..) => Self::DynamicSetter,
ParserObjectMemberKind::Setter(..) => Self::Setter,
ParserObjectMemberKind::Static(..) => Self::Static,
ParserObjectMemberKind::Spread => Self::Spread,
Expand Down
6 changes: 5 additions & 1 deletion crates/dash_middle/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,9 @@ impl fmt::Display for ArrayLiteral {
#[derive(Debug, Clone)]
pub enum ObjectMemberKind {
Getter(Symbol),
DynamicGetter(Expr),
Setter(Symbol),
DynamicSetter(Expr),
Static(Symbol),
Spread,
Dynamic(Expr),
Expand All @@ -290,7 +292,9 @@ impl fmt::Display for ObjectMemberKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Getter(name) => write!(f, "get {name}"),
Self::Setter(name) => write!(f, "set {name}"),
Self::DynamicGetter(expr) => write!(f, "get [{expr}]"),
Self::Setter(name) => write!(f, "set [{name}]"),
Self::DynamicSetter(expr) => write!(f, "set [{expr}]"),
Self::Static(name) => write!(f, "{name}"),
Self::Dynamic(expr) => write!(f, "[{expr}]"),
Self::Spread => f.write_str("...<expression unavailable>"), // TODO: figure out a way to display it here
Expand Down
3 changes: 3 additions & 0 deletions crates/dash_parser/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,9 @@ impl<'a, 'interner> Parser<'a, 'interner> {
},
));
}
ObjectMemberKind::DynamicGetter(_) | ObjectMemberKind::DynamicSetter(_) => {
unreachable!("never created")
}
}
}
let rbrace_span = self.previous()?.span;
Expand Down
18 changes: 18 additions & 0 deletions crates/dash_vm/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,24 @@ mod extract {
value: PropertyValue::static_default(value),
}
}
ObjectMemberKind::DynamicGetter => {
let key = extract(cx);
let value = extract::<Object>(cx).0;

Self::Getter {
key: PropertyKey::from_value(cx.scope, key)?,
value,
}
}
ObjectMemberKind::DynamicSetter => {
let key = extract(cx);
let value = extract::<Object>(cx).0;

Self::Setter {
key: PropertyKey::from_value(cx.scope, key)?,
value,
}
}
ObjectMemberKind::Spread => Self::Spread(extract(cx)),
})
}
Expand Down

0 comments on commit 25d269b

Please sign in to comment.