Skip to content

Commit

Permalink
Return Integer for inc/dec of an integer
Browse files Browse the repository at this point in the history
  • Loading branch information
tunz committed Feb 21, 2023
1 parent f8b6820 commit 05f4fee
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 20 deletions.
40 changes: 40 additions & 0 deletions boa_engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,20 @@ fn unary_pre() {
--a === 4;
"#;
assert_eq!(&exec(execs_before_dec), "true");

let i32_limit_inc = r#"
let a = 2147483647;
++a;
a;
"#;
assert_eq!(&exec(i32_limit_inc), "2147483648");

let i32_limit_dec = r#"
let a = -2147483648;
--a;
a;
"#;
assert_eq!(&exec(i32_limit_dec), "-2147483649");
}

#[test]
Expand Down Expand Up @@ -683,6 +697,32 @@ fn unary_post() {
a-- === 5;
"#;
assert_eq!(&exec(execs_after_dec), "true");

let i32_limit_inc = r#"
let a = 2147483647;
a++;
a;
"#;
assert_eq!(&exec(i32_limit_inc), "2147483648");

let i32_limit_dec = r#"
let a = -2147483648;
a--;
a;
"#;
assert_eq!(&exec(i32_limit_dec), "-2147483649");

let to_numeric_inc = r#"
let a = {[Symbol.toPrimitive]() { return 123; }};
a++
"#;
assert_eq!(&exec(to_numeric_inc), "123");

let to_numeric_dec = r#"
let a = {[Symbol.toPrimitive]() { return 123; }};
a--
"#;
assert_eq!(&exec(to_numeric_dec), "123");
}

#[test]
Expand Down
33 changes: 23 additions & 10 deletions boa_engine/src/vm/opcode/unary_ops/decrement.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
value::Numeric,
value::{JsValue, Numeric},
vm::{opcode::Operation, ShouldExit},
Context, JsBigInt, JsResult,
};
Expand All @@ -17,11 +17,16 @@ impl Operation for Dec {

fn execute(context: &mut Context<'_>) -> JsResult<ShouldExit> {
let value = context.vm.pop();
match value.to_numeric(context)? {
Numeric::Number(number) => context.vm.push(number - 1f64),
Numeric::BigInt(bigint) => {
context.vm.push(JsBigInt::sub(&bigint, &JsBigInt::one()));
match value {
JsValue::Integer(number) if number > i32::MIN => {
context.vm.push(number - 1);
}
_ => match value.to_numeric(context)? {
Numeric::Number(number) => context.vm.push(number - 1f64),
Numeric::BigInt(bigint) => {
context.vm.push(JsBigInt::sub(&bigint, &JsBigInt::one()));
}
},
}
Ok(ShouldExit::False)
}
Expand All @@ -40,14 +45,22 @@ impl Operation for DecPost {

fn execute(context: &mut Context<'_>) -> JsResult<ShouldExit> {
let value = context.vm.pop();
let value = value.to_numeric(context)?;
match value {
Numeric::Number(number) => context.vm.push(number - 1f64),
Numeric::BigInt(ref bigint) => {
context.vm.push(JsBigInt::sub(bigint, &JsBigInt::one()));
JsValue::Integer(number) if number > i32::MIN => {
context.vm.push(number - 1);
context.vm.push(value);
}
_ => {
let value = value.to_numeric(context)?;
match value {
Numeric::Number(number) => context.vm.push(number - 1f64),
Numeric::BigInt(ref bigint) => {
context.vm.push(JsBigInt::sub(bigint, &JsBigInt::one()));
}
}
context.vm.push(value);
}
}
context.vm.push(value);
Ok(ShouldExit::False)
}
}
33 changes: 23 additions & 10 deletions boa_engine/src/vm/opcode/unary_ops/increment.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
value::Numeric,
value::{JsValue, Numeric},
vm::{opcode::Operation, ShouldExit},
Context, JsBigInt, JsResult,
};
Expand All @@ -17,11 +17,16 @@ impl Operation for Inc {

fn execute(context: &mut Context<'_>) -> JsResult<ShouldExit> {
let value = context.vm.pop();
match value.to_numeric(context)? {
Numeric::Number(number) => context.vm.push(number + 1f64),
Numeric::BigInt(bigint) => {
context.vm.push(JsBigInt::add(&bigint, &JsBigInt::one()));
match value {
JsValue::Integer(number) if number < i32::MAX => {
context.vm.push(number + 1);
}
_ => match value.to_numeric(context)? {
Numeric::Number(number) => context.vm.push(number + 1f64),
Numeric::BigInt(bigint) => {
context.vm.push(JsBigInt::add(&bigint, &JsBigInt::one()));
}
},
}
Ok(ShouldExit::False)
}
Expand All @@ -40,14 +45,22 @@ impl Operation for IncPost {

fn execute(context: &mut Context<'_>) -> JsResult<ShouldExit> {
let value = context.vm.pop();
let value = value.to_numeric(context)?;
match value {
Numeric::Number(number) => context.vm.push(number + 1f64),
Numeric::BigInt(ref bigint) => {
context.vm.push(JsBigInt::add(bigint, &JsBigInt::one()));
JsValue::Integer(number) if number < i32::MAX => {
context.vm.push(number + 1);
context.vm.push(value);
}
_ => {
let value = value.to_numeric(context)?;
match value {
Numeric::Number(number) => context.vm.push(number + 1f64),
Numeric::BigInt(ref bigint) => {
context.vm.push(JsBigInt::add(bigint, &JsBigInt::one()));
}
}
context.vm.push(value);
}
}
context.vm.push(value);
Ok(ShouldExit::False)
}
}

0 comments on commit 05f4fee

Please sign in to comment.