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

Initial implementation for increment/decrement in VM #1621

Merged
merged 1 commit into from
Oct 14, 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
46 changes: 42 additions & 4 deletions boa/src/bytecompiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,48 @@ impl ByteCompiler {
}
Node::UnaryOp(unary) => {
let opcode = match unary.op() {
UnaryOp::IncrementPre => todo!(),
UnaryOp::DecrementPre => todo!(),
UnaryOp::IncrementPost => todo!(),
UnaryOp::DecrementPost => todo!(),
UnaryOp::IncrementPre => {
self.compile_expr(unary.target(), true);
self.emit(Opcode::Inc, &[]);

let access = self.compile_access(unary.target());
self.access_set(access, None, use_expr);
None
}
UnaryOp::DecrementPre => {
self.compile_expr(unary.target(), true);
self.emit(Opcode::Dec, &[]);

let access = self.compile_access(unary.target());
self.access_set(access, None, use_expr);
None
}
UnaryOp::IncrementPost => {
self.compile_expr(unary.target(), true);
self.emit(Opcode::Dup, &[]);
self.emit(Opcode::Inc, &[]);
let access = self.compile_access(unary.target());
self.access_set(access, None, false);

if !use_expr {
self.emit(Opcode::Pop, &[]);
}

None
}
UnaryOp::DecrementPost => {
self.compile_expr(unary.target(), true);
self.emit(Opcode::Dup, &[]);
self.emit(Opcode::Dec, &[]);
let access = self.compile_access(unary.target());
self.access_set(access, None, false);

if !use_expr {
self.emit(Opcode::Pop, &[]);
}

None
}
UnaryOp::Delete => match unary.target() {
Node::GetConstField(ref get_const_field) => {
let index = self.get_or_insert_name(get_const_field.field());
Expand Down
2 changes: 2 additions & 0 deletions boa/src/vm/code_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ impl CodeBlock {
| Opcode::LogicalNot
| Opcode::Pos
| Opcode::Neg
| Opcode::Inc
| Opcode::Dec
| Opcode::GetPropertyByValue
| Opcode::SetPropertyByValue
| Opcode::DeletePropertyByValue
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 @@ -236,6 +236,14 @@ impl Context {
let value = self.vm.pop().neg(self)?;
self.vm.push(value);
}
Opcode::Inc => {
let value = self.vm.pop().add(&JsValue::Integer(1), self)?;
self.vm.push(value);
}
Opcode::Dec => {
let value = self.vm.pop().sub(&JsValue::Integer(1), self)?;
self.vm.push(value);
}
Opcode::LogicalNot => {
let value = self.vm.pop();
self.vm.push(!value.to_boolean());
Expand Down
16 changes: 16 additions & 0 deletions boa/src/vm/opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,20 @@ pub enum Opcode {
/// Stack: value **=>** (-value)
Neg,

/// Unary `++` operator.
///
/// Operands:
///
/// Stack: value **=>** (value + 1)
Inc,

/// Unary `--` operator.
///
/// Operands:
///
/// Stack: value **=>** (value - 1)
Dec,

/// Declate `var` type variable.
///
/// Operands: name_index: `u32`
Expand Down Expand Up @@ -613,6 +627,8 @@ impl Opcode {
Opcode::Coalesce => "Coalesce",
Opcode::Pos => "Pos",
Opcode::Neg => "Neg",
Opcode::Inc => "Inc",
Opcode::Dec => "Dec",
Opcode::DefVar => "DefVar",
Opcode::DefLet => "DefLet",
Opcode::DefConst => "DefConst",
Expand Down