Skip to content

Commit

Permalink
create abstract_operations module for BigInt
Browse files Browse the repository at this point in the history
  • Loading branch information
sno2 authored and aapoalas committed Oct 30, 2023
1 parent 9786674 commit 0ab696f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
1 change: 1 addition & 0 deletions nova_vm/src/ecmascript/abstract_operations.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub(crate) mod bigint;
pub(crate) mod operations_on_objects;
pub(crate) mod testing_and_comparison;
pub(crate) mod type_conversion;
22 changes: 22 additions & 0 deletions nova_vm/src/ecmascript/abstract_operations/bigint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use crate::{
ecmascript::{execution::Agent, types::BigInt},
heap::GetHeapData,
};

/// ### [6.1.6.2.12 BigInt::lessThan ( x, y )](https://tc39.es/ecma262/#sec-numeric-types-bigint-lessThan)
///
/// The abstract operation BigInt::lessThan takes arguments x (a BigInt) and
/// y (a BigInt) and returns a Boolean. It performs the following steps when
/// called:
pub(crate) fn less_than(agent: &mut Agent, x: BigInt, y: BigInt) -> bool {
// 1. If ℝ(x) < ℝ(y), return true; otherwise return false.
match (x, y) {
(BigInt::BigInt(_), BigInt::SmallBigInt(_)) => false,
(BigInt::SmallBigInt(_), BigInt::BigInt(_)) => true,
(BigInt::BigInt(b1), BigInt::BigInt(b2)) => {
let (b1, b2) = (agent.heap.get(b1), agent.heap.get(b2));
b1.data < b2.data
}
(BigInt::SmallBigInt(b1), BigInt::SmallBigInt(b2)) => b1.into_i64() < b2.into_i64(),
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! ## [7.2 Testing and Comparison Operations](https://tc39.es/ecma262/#sec-testing-and-comparison-operations)
use crate::ecmascript::{
abstract_operations::bigint,
execution::{agent::JsError, Agent, JsResult},
types::{Number, Value},
};
Expand Down Expand Up @@ -249,7 +250,7 @@ pub(crate) fn is_less_than<const LEFT_FIRST: bool>(
// 2. Return BigInt::lessThan(nx, ny).
let nx = nx.to_bigint(agent)?;
let ny = ny.to_bigint(agent)?;
return Ok(Some(nx.less_than(agent, ny)));
return Ok(Some(bigint::less_than(agent, nx, ny)));
}
}

Expand Down
26 changes: 1 addition & 25 deletions nova_vm/src/ecmascript/types/language/bigint.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
mod data;

use super::value::{BIGINT_DISCRIMINANT, SMALL_BIGINT_DISCRIMINANT};
use crate::{
ecmascript::execution::Agent,
heap::{indexes::BigIntIndex, GetHeapData},
SmallInteger,
};
use crate::{heap::indexes::BigIntIndex, SmallInteger};

pub use data::BigIntHeapData;

Expand All @@ -15,23 +11,3 @@ pub enum BigInt {
BigInt(BigIntIndex) = BIGINT_DISCRIMINANT,
SmallBigInt(SmallInteger) = SMALL_BIGINT_DISCRIMINANT,
}

impl BigInt {
/// ### [6.1.6.2.12 BigInt::lessThan ( x, y )](https://tc39.es/ecma262/#sec-numeric-types-bigint-lessThan)
///
/// The abstract operation BigInt::lessThan takes arguments x (a BigInt) and
/// y (a BigInt) and returns a Boolean. It performs the following steps when
/// called:
pub(crate) fn less_than(self, agent: &mut Agent, y: BigInt) -> bool {
// 1. If ℝ(x) < ℝ(y), return true; otherwise return false.
match (self, y) {
(BigInt::BigInt(_), BigInt::SmallBigInt(_)) => false,
(BigInt::SmallBigInt(_), BigInt::BigInt(_)) => true,
(BigInt::BigInt(b1), BigInt::BigInt(b2)) => {
let (b1, b2) = (agent.heap.get(b1), agent.heap.get(b2));
b1.data < b2.data
}
(BigInt::SmallBigInt(b1), BigInt::SmallBigInt(b2)) => b1.into_i64() < b2.into_i64(),
}
}
}

0 comments on commit 0ab696f

Please sign in to comment.