Browse Source

Respect Rust 1.53 (#1352)

* 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
pull/1346/head
João Borges 3 years ago committed by GitHub
parent
commit
78b926e98f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 28
      boa/src/builtins/array/tests.rs
  2. 9
      boa/src/builtins/bigint/mod.rs
  3. 16
      boa/src/builtins/date/mod.rs
  4. 3
      boa/src/builtins/number/mod.rs
  5. 7
      boa/src/syntax/lexer/string.rs
  6. 22
      boa/src/syntax/parser/cursor/buffered_lexer/mod.rs
  7. 6
      boa/src/value/operations.rs

28
boa/src/builtins/array/tests.rs

@ -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]

9
boa/src/builtins/bigint/mod.rs

@ -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 {}

16
boa/src/builtins/date/mod.rs

@ -217,7 +217,7 @@ 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))
}
@ -225,7 +225,7 @@ impl Date {
/// 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))
}
@ -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())
@ -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()
}
@ -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())
@ -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()
}
@ -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())
@ -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())

3
boa/src/builtins/number/mod.rs

@ -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.
@ -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;
}

7
boa/src/syntax/lexer/string.rs

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

22
boa/src/syntax/parser/cursor/buffered_lexer/mod.rs

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

6
boa/src/value/operations.rs

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

Loading…
Cancel
Save