Browse Source

Initial implementation for increment/decrement in VM (#1621)

- Added two new opcodes Inc and Dec to the VM for increment and decrement.
- Implementated compilation for Post/Pre Increment and Decrement operators to VM operations.
- Implemented running the Inc and Dec opcodes on the VM.
pull/1672/head
Abhishek C Sharma 3 years ago committed by GitHub
parent
commit
c809de76ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 46
      boa/src/bytecompiler.rs
  2. 2
      boa/src/vm/code_block.rs
  3. 8
      boa/src/vm/mod.rs
  4. 16
      boa/src/vm/opcode.rs

46
boa/src/bytecompiler.rs

@ -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());

2
boa/src/vm/code_block.rs

@ -206,6 +206,8 @@ impl CodeBlock {
| Opcode::LogicalNot
| Opcode::Pos
| Opcode::Neg
| Opcode::Inc
| Opcode::Dec
| Opcode::GetPropertyByValue
| Opcode::SetPropertyByValue
| Opcode::DeletePropertyByValue

8
boa/src/vm/mod.rs

@ -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());

16
boa/src/vm/opcode.rs

@ -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`
@ -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",

Loading…
Cancel
Save