Skip to content

Commit

Permalink
Respect Rust 1.53 (#1352)
Browse files Browse the repository at this point in the history
* Style: Don't repeat code at end of if

Respecting lint clippy::branches_sharing_code

* Refactor: Introduce BigInt::is_zero, reduce allocations

* Refactor: Use self for "converting" date methods

Respecting the wrong_self_conversion lint

* Style: rustfmt

* Test: Prepare Array.prototype.concat test
  • Loading branch information
RageKnify authored Jun 20, 2021
1 parent f673cbc commit 78b926e
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 43 deletions.
28 changes: 14 additions & 14 deletions boa/src/builtins/array/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,42 +118,42 @@ fn of() {
}

#[ignore]
#[test]
fn concat() {
//TODO: array display formatter
let mut context = Context::new();
let init = r#"
var empty = new Array();
var one = new Array(1);
var empty = [];
var one = [1];
"#;
context.eval(init).unwrap();
// Empty ++ Empty
let ee = context
.eval("empty.concat(empty)")
.unwrap()
.to_string(&mut context)
.unwrap();
.display()
.to_string();
assert_eq!(ee, "[]");
// Empty ++ NonEmpty
let en = context
.eval("empty.concat(one)")
.unwrap()
.to_string(&mut context)
.unwrap();
assert_eq!(en, "[a]");
.display()
.to_string();
assert_eq!(en, "[ 1 ]");
// NonEmpty ++ Empty
let ne = context
.eval("one.concat(empty)")
.unwrap()
.to_string(&mut context)
.unwrap();
assert_eq!(ne, "a.b.c");
.display()
.to_string();
assert_eq!(ne, "[ 1 ]");
// NonEmpty ++ NonEmpty
let nn = context
.eval("one.concat(one)")
.unwrap()
.to_string(&mut context)
.unwrap();
assert_eq!(nn, "a.b.c");
.display()
.to_string();
assert_eq!(nn, "[ 1, 1 ]");
}

#[test]
Expand Down
9 changes: 9 additions & 0 deletions boa/src/builtins/bigint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,15 @@ impl BigInt {
bits,
))
}

/// helper function for checking if the BigInt represents 0
///
/// creating BigInts requires an allocation and for a few operations we need to know if the
/// inner value is 0, this solves that situation
pub(crate) fn is_zero(&self) -> bool {
use num_traits::Zero;
self.0.is_zero()
}
}

impl Finalize for BigInt {}
Expand Down
16 changes: 8 additions & 8 deletions boa/src/builtins/date/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,15 @@ impl Date {
/// Converts the `Date` to a local `DateTime`.
///
/// If the `Date` is invalid (i.e. NAN), this function will return `None`.
pub fn to_local(&self) -> Option<DateTime<Local>> {
pub fn to_local(self) -> Option<DateTime<Local>> {
self.0
.map(|utc| Local::now().timezone().from_utc_datetime(&utc))
}

/// Converts the `Date` to a UTC `DateTime`.
///
/// If the `Date` is invalid (i.e. NAN), this function will return `None`.
pub fn to_utc(&self) -> Option<DateTime<Utc>> {
pub fn to_utc(self) -> Option<DateTime<Utc>> {
self.0
.map(|utc| Utc::now().timezone().from_utc_datetime(&utc))
}
Expand Down Expand Up @@ -1203,7 +1203,7 @@ impl Date {
///
/// [spec]: https://tc39.es/ecma262/#sec-date.prototype.todatestring
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toDateString
pub fn to_date_string(&self) -> String {
pub fn to_date_string(self) -> String {
self.to_local()
.map(|date_time| date_time.format("%a %b %d %Y").to_string())
.unwrap_or_else(|| "Invalid Date".to_string())
Expand All @@ -1219,7 +1219,7 @@ impl Date {
///
/// [spec]: https://tc39.es/ecma262/#sec-date.prototype.togmtstring
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toGMTString
pub fn to_gmt_string(&self) -> String {
pub fn to_gmt_string(self) -> String {
self.to_utc_string()
}

Expand All @@ -1235,7 +1235,7 @@ impl Date {
/// [iso8601]: http://en.wikipedia.org/wiki/ISO_8601
/// [spec]: https://tc39.es/ecma262/#sec-date.prototype.toisostring
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString
pub fn to_iso_string(&self) -> String {
pub fn to_iso_string(self) -> String {
self.to_utc()
// RFC 3389 uses +0.00 for UTC, where JS expects Z, so we can't use the built-in chrono function.
.map(|f| f.format("%Y-%m-%dT%H:%M:%S.%3fZ").to_string())
Expand All @@ -1252,7 +1252,7 @@ impl Date {
///
/// [spec]: https://tc39.es/ecma262/#sec-date.prototype.tojson
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toJSON
pub fn to_json(&self) -> String {
pub fn to_json(self) -> String {
self.to_iso_string()
}

Expand All @@ -1267,7 +1267,7 @@ impl Date {
///
/// [spec]: https://tc39.es/ecma262/#sec-date.prototype.totimestring
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toTimeString
pub fn to_time_string(&self) -> String {
pub fn to_time_string(self) -> String {
self.to_local()
.map(|date_time| date_time.format("%H:%M:%S GMT%:z").to_string())
.unwrap_or_else(|| "Invalid Date".to_string())
Expand All @@ -1283,7 +1283,7 @@ impl Date {
///
/// [spec]: https://tc39.es/ecma262/#sec-date.prototype.toutcstring
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toUTCString
pub fn to_utc_string(&self) -> String {
pub fn to_utc_string(self) -> String {
self.to_utc()
.map(|date_time| date_time.format("%a, %d %b %Y %H:%M:%S GMT").to_string())
.unwrap_or_else(|| "Invalid Date".to_string())
Expand Down
3 changes: 1 addition & 2 deletions boa/src/builtins/number/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,6 @@ impl Number {
// CHECK_EQ('.', buffer[fraction_cursor]);
// Carry over to the integer part.
integer += 1.;
break;
} else {
let c: u8 = frac_buf[fraction_cursor];
// Reconstruct digit.
Expand All @@ -575,8 +574,8 @@ impl Number {
frac_buf[fraction_cursor] =
std::char::from_digit(digit_0 + 1, radix as u32).unwrap() as u8;
fraction_cursor += 1;
break;
}
break;
}
break;
}
Expand Down
7 changes: 6 additions & 1 deletion boa/src/syntax/lexer/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,12 @@ impl StringLiteral {
let _timer = BoaProfiler::global()
.start_event("StringLiteral - escape sequence", "Lexing");

if let Some(escape_value) = Self::take_escape_sequence_or_line_continuation(cursor, ch_start_pos, is_strict_mode, false)? {
if let Some(escape_value) = Self::take_escape_sequence_or_line_continuation(
cursor,
ch_start_pos,
is_strict_mode,
false,
)? {
buf.push_code_point(escape_value);
}
}
Expand Down
22 changes: 7 additions & 15 deletions boa/src/syntax/parser/cursor/buffered_lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,18 +163,15 @@ where
}

if let Some(ref token) = self.peeked[self.read_index] {
let tok = if !skip_line_terminators || token.kind() != &TokenKind::LineTerminator {
self.peeked[self.read_index].take()
} else {
if skip_line_terminators && token.kind() == &TokenKind::LineTerminator {
// We only store 1 contiguous line terminator, so if the one at `self.read_index`
// was a line terminator, we know that the next won't be one.
self.read_index = (self.read_index + 1) % PEEK_BUF_SIZE;
if self.read_index == self.write_index {
self.fill()?;
}

self.peeked[self.read_index].take()
};
}
let tok = self.peeked[self.read_index].take();
self.read_index = (self.read_index + 1) % PEEK_BUF_SIZE;

Ok(tok)
Expand Down Expand Up @@ -220,21 +217,16 @@ where
}

if let Some(ref token) = self.peeked[read_index] {
if !skip_line_terminators || token.kind() != &TokenKind::LineTerminator {
if count == skip_n {
break self.peeked[read_index].as_ref();
}
} else {
if skip_line_terminators && token.kind() == &TokenKind::LineTerminator {
read_index = (read_index + 1) % PEEK_BUF_SIZE;
// We only store 1 contiguous line terminator, so if the one at `self.read_index`
// was a line terminator, we know that the next won't be one.
if read_index == self.write_index {
self.fill()?;
}

if count == skip_n {
break self.peeked[read_index].as_ref();
}
}
if count == skip_n {
break self.peeked[read_index].as_ref();
}
} else {
break None;
Expand Down
6 changes: 3 additions & 3 deletions boa/src/value/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl Value {
(Self::Rational(x), Self::Integer(y)) => Self::rational(x / f64::from(*y)),

(Self::BigInt(ref a), Self::BigInt(ref b)) => {
if *b.as_inner() == BigInt::from(0) {
if b.as_inner().is_zero() {
return context.throw_range_error("BigInt division by zero");
}
Self::bigint(a.as_inner().clone() / b.as_inner().clone())
Expand All @@ -120,7 +120,7 @@ impl Value {
(_, _) => match (self.to_numeric(context)?, other.to_numeric(context)?) {
(Numeric::Number(a), Numeric::Number(b)) => Self::rational(a / b),
(Numeric::BigInt(ref a), Numeric::BigInt(ref b)) => {
if *b.as_inner() == BigInt::from(0) {
if b.as_inner().is_zero() {
return context.throw_range_error("BigInt division by zero");
}
Self::bigint(a.as_inner().clone() / b.as_inner().clone())
Expand Down Expand Up @@ -150,7 +150,7 @@ impl Value {
(Self::Rational(x), Self::Integer(y)) => Self::rational(x % f64::from(*y)),

(Self::BigInt(ref a), Self::BigInt(ref b)) => {
if *b.as_inner() == BigInt::from(0) {
if b.as_inner().is_zero() {
return context.throw_range_error("BigInt division by zero");
}
Self::bigint(a.as_inner().clone() % b.as_inner().clone())
Expand Down

0 comments on commit 78b926e

Please sign in to comment.