Skip to content

Commit

Permalink
Implement delete in the vm
Browse files Browse the repository at this point in the history
  • Loading branch information
raskad committed Oct 7, 2021
1 parent 69d9f62 commit a5b7873
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 13 deletions.
30 changes: 26 additions & 4 deletions boa/src/bytecompiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,29 @@ impl ByteCompiler {
UnaryOp::DecrementPre => todo!(),
UnaryOp::IncrementPost => todo!(),
UnaryOp::DecrementPost => todo!(),
UnaryOp::Delete => todo!(),
UnaryOp::Delete => match unary.target() {
Node::GetConstField(ref get_const_field) => {
self.emit_push_literal(Literal::String(get_const_field.field().into()));
self.compile_expr(get_const_field.obj(), true);
self.emit(Opcode::DeletePropertyByValue, &[]);
None
}
Node::GetField(ref get_field) => {
self.compile_expr(get_field.field(), true);
self.compile_expr(get_field.obj(), true);
self.emit(Opcode::DeletePropertyByValue, &[]);
None
}
// TODO: implement delete on references.
Node::Identifier(_) => {
self.emit(Opcode::PushFalse, &[]);
None
}
_ => {
self.emit(Opcode::PushTrue, &[]);
None
}
},
UnaryOp::Minus => Some(Opcode::Neg),
UnaryOp::Plus => Some(Opcode::Pos),
UnaryOp::Not => Some(Opcode::LogicalNot),
Expand All @@ -382,10 +404,10 @@ impl ByteCompiler {
if let Some(opcode) = opcode {
self.compile_expr(unary.target(), true);
self.emit(opcode, &[]);
}

if !use_expr {
self.emit(Opcode::Pop, &[]);
}
if !use_expr {
self.emit(Opcode::Pop, &[]);
}
}
Node::BinOp(binary) => {
Expand Down
11 changes: 2 additions & 9 deletions boa/src/syntax/ast/node/operator/unary_op/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,9 @@ impl Executable for UnaryOp {
.__delete__(&field.to_property_key(context)?, context)?;
return Ok(JsValue::new(res));
}
// TODO: implement delete on references.
Node::Identifier(_) => JsValue::new(false),
Node::ArrayDecl(_)
| Node::Block(_)
| Node::Const(_)
| Node::FunctionDecl(_)
| Node::FunctionExpr(_)
| Node::New(_)
| Node::Object(_)
| Node::UnaryOp(_) => JsValue::new(true),
_ => return context.throw_syntax_error(format!("wrong delete argument {}", self)),
_ => JsValue::new(true),
},
op::UnaryOp::TypeOf => JsValue::new(self.target().run(context)?.type_of()),
})
Expand Down
1 change: 1 addition & 0 deletions boa/src/vm/code_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ impl CodeBlock {
| Opcode::Neg
| Opcode::GetPropertyByValue
| Opcode::SetPropertyByValue
| Opcode::DeletePropertyByValue
| Opcode::ToBoolean
| Opcode::Throw
| Opcode::This
Expand Down
8 changes: 8 additions & 0 deletions boa/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,14 @@ impl Context {
let key = key.to_property_key(self)?;
object.set(key, value, true, self)?;
}
Opcode::DeletePropertyByValue => {
let object = self.vm.pop();
let key = self.vm.pop();
let result = object
.to_object(self)?
.__delete__(&key.to_property_key(self)?, self)?;
self.vm.push(result);
}
Opcode::Throw => {
let value = self.vm.pop();
return Err(value);
Expand Down
10 changes: 10 additions & 0 deletions boa/src/vm/opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,15 @@ pub enum Opcode {
/// Stack: value, key, object **=>**
SetPropertyByValue,

/// Deletes a property by value of an object.
///
/// Like `delete object[key]`
///
/// Operands:
///
/// Stack: key, object **=>**
DeletePropertyByValue,

/// Unconditional jump to address.
///
/// Operands: address: `u32`
Expand Down Expand Up @@ -605,6 +614,7 @@ impl Opcode {
Opcode::GetPropertyByValue => "GetPropertyByValue",
Opcode::SetPropertyByName => "SetPropertyByName",
Opcode::SetPropertyByValue => "SetPropertyByValue",
Opcode::DeletePropertyByValue => "DeletePropertyByValue",
Opcode::Jump => "Jump",
Opcode::JumpIfFalse => "JumpIfFalse",
Opcode::JumpIfTrue => "JumpIfTrue",
Expand Down

0 comments on commit a5b7873

Please sign in to comment.