Skip to content

Commit

Permalink
Fixed a bunch of test errors, still some to fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Razican committed May 14, 2020
1 parent fe83bf3 commit e0d6b85
Show file tree
Hide file tree
Showing 32 changed files with 1,061 additions and 527 deletions.
11 changes: 2 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion boa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ rand = "0.7.3"
num-traits = "0.2.11"
regex = "1.3.7"
rustc-hash = "1.1.0"
# once_cell = "1.3.1"

# Optional Dependencies
serde = { version = "1.0.110", features = ["derive"], optional = true }
once_cell = "1.3.1"

[dev-dependencies]
criterion = "0.3.2"
Expand Down
6 changes: 3 additions & 3 deletions boa/src/exec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,17 @@ impl Interpreter {
}

/// Retrieves the `Realm` of this executor.
pub fn realm(&self) -> &Realm {
pub(crate) fn realm(&self) -> &Realm {
&self.realm
}

/// Retrieves the `Realm` of this executor as a mutable reference.
pub fn realm_mut(&mut self) -> &mut Realm {
pub(crate) fn realm_mut(&mut self) -> &mut Realm {
&mut self.realm
}

/// Run an expression.
pub fn exec(&mut self, node: &Node) -> ResultValue {
pub(crate) fn exec(&mut self, node: &Node) -> ResultValue {
node.run(self)
}

Expand Down
13 changes: 7 additions & 6 deletions boa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,21 @@ pub mod environment;
pub mod exec;
pub mod realm;
pub mod syntax;
use crate::{
builtins::value::ResultValue,

use crate::{builtins::value::ResultValue, syntax::ast::node::StatementList};
pub use crate::{
exec::{Executable, Interpreter},
realm::Realm,
syntax::{ast::node::StatementList, lexer::Lexer, parser::Parser},
syntax::{lexer::Lexer, parser::Parser},
};

fn parser_expr(src: &str) -> Result<StatementList, String> {
let mut lexer = Lexer::new(src);
lexer.lex().map_err(|e| format!("SyntaxError: {}", e))?;
lexer.lex().map_err(|e| format!("Syntax Error: {}", e))?;
let tokens = lexer.tokens;
Parser::new(&tokens)
.parse_all()
.map_err(|e| format!("ParsingError: {}", e))
.map_err(|e| format!("Parsing Error: {}", e))
}

/// Execute the code using an existing Interpreter
Expand All @@ -63,7 +64,7 @@ pub fn forward(engine: &mut Interpreter, src: &str) -> String {
Err(e) => return e,
};
expr.run(engine)
.map_or_else(|v| v.to_string(), |e| format!("Error: {}", e))
.map_or_else(|e| format!("Error: {}", e), |v| v.to_string())
}

/// Execute the code using an existing Interpreter.
Expand Down
14 changes: 7 additions & 7 deletions boa/src/syntax/ast/node/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ impl Block {
self.statements.statements()
}

/// Gets the lexically declared names.
///
/// More information:
/// <https://tc39.es/ecma262/#sec-block-static-semantics-lexicallydeclarednames>
pub(crate) fn lexically_declared_names(&self) -> &[Box<str>] {
self.statements.lexically_declared_names()
}
// /// Gets the lexically declared names.
// ///
// /// More information:
// /// <https://tc39.es/ecma262/#sec-block-static-semantics-lexicallydeclarednames>
// pub(crate) fn lexically_declared_names(&self) -> &[Box<str>] {
// self.statements.lexically_declared_names()
// }

/// Implements the display formatting with indentation.
pub(super) fn display(&self, f: &mut fmt::Formatter<'_>, indentation: usize) -> fmt::Result {
Expand Down
6 changes: 3 additions & 3 deletions boa/src/syntax/ast/node/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl fmt::Display for VarDecl {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.name, f)?;
if let Some(ref init) = self.init {
write!(f, " = {}", init);
write!(f, " = {}", init)?;
}
Ok(())
}
Expand Down Expand Up @@ -245,13 +245,13 @@ impl FunctionDecl {
pub(super) fn display(&self, f: &mut fmt::Formatter<'_>, indentation: usize) -> fmt::Result {
f.write_str("function")?;
if let Some(ref name) = self.name {
write!(f, " {}", name);
write!(f, " {}", name)?;
}
f.write_str("(")?;
join_nodes(f, &self.parameters)?;
f.write_str(") {{")?;

self.body.display(f, indentation + 1);
self.body.display(f, indentation + 1)?;

writeln!(f, "}}")
}
Expand Down
19 changes: 8 additions & 11 deletions boa/src/syntax/ast/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use crate::syntax::ast::{
op::{Operator, UnaryOp},
};
use gc::{Finalize, Trace};
use once_cell::sync::OnceCell;
use std::{
cmp::Ordering,
fmt::{self, Display},
Expand Down Expand Up @@ -722,17 +721,15 @@ impl Node {
Self::WhileLoop(condition.into(), body.into())
}

/// Gets the lexically declared names.
///
/// More information:
/// <https://tc39.es/ecma262/#sec-block-static-semantics-lexicallydeclarednames>
pub(crate) fn lexically_declared_names(&self) -> &[Box<str>] {
static LIST: OnceCell<Box<[Box<str>]>> = OnceCell::new();
// /// Gets the lexically declared names.
// ///
// /// More information:
// /// <https://tc39.es/ecma262/#sec-block-static-semantics-lexicallydeclarednames>
// pub(crate) fn lexically_declared_names(&self) -> &[Box<str>] {
// static LIST: OnceCell<Box<[Box<str>]>> = OnceCell::new();

LIST.get_or_init(|| match *self {
_ => unimplemented!(),
})
}
// LIST.get_or_init(|| unimplemented!())
// }

/// Implements the display formatting with indentation.
fn display(&self, f: &mut fmt::Formatter<'_>, indentation: usize) -> fmt::Result {
Expand Down
33 changes: 16 additions & 17 deletions boa/src/syntax/ast/node/statement_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
use super::Node;
use gc::{Finalize, Trace};
use once_cell::sync::OnceCell;
use std::fmt;

#[cfg(feature = "serde")]
Expand All @@ -29,23 +28,23 @@ impl StatementList {
&self.statements
}

/// Gets the lexically declared names.
///
/// More information:
/// <https://tc39.es/ecma262/#sec-block-static-semantics-lexicallydeclarednames>
pub(crate) fn lexically_declared_names(&self) -> &[Box<str>] {
static LIST: OnceCell<Box<[Box<str>]>> = OnceCell::new();
// /// Gets the lexically declared names.
// ///
// /// More information:
// /// <https://tc39.es/ecma262/#sec-block-static-semantics-lexicallydeclarednames>
// pub(crate) fn lexically_declared_names(&self) -> &[Box<str>] {
// static LIST: OnceCell<Box<[Box<str>]>> = OnceCell::new();

LIST.get_or_init(|| {
self.statements
.iter()
.map(|node| node.lexically_declared_names())
.flatten()
.cloned()
.collect::<Vec<_>>()
.into_boxed_slice()
})
}
// LIST.get_or_init(|| {
// self.statements
// .iter()
// .map(|node| node.lexically_declared_names())
// .flatten()
// .cloned()
// .collect::<Vec<_>>()
// .into_boxed_slice()
// })
// }

/// Implements the display formatting with indentation.
pub(super) fn display(&self, f: &mut fmt::Formatter<'_>, indentation: usize) -> fmt::Result {
Expand Down
10 changes: 5 additions & 5 deletions boa/src/syntax/ast/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,9 @@ mod tests {
assert!(span_ac.contains(b));

let span_ab = Span::new(a, b);
let span_dc = Span::new(d, c);
let span_cd = Span::new(c, d);

assert!(!span_ab.contains(span_dc));
assert!(!span_ab.contains(span_cd));
assert!(span_ab.contains(b));

let span_ad = Span::new(a, d);
Expand Down Expand Up @@ -269,9 +269,9 @@ mod tests {
let d = Position::new(12, 5);

let span_ab = Span::new(a, b);
let span_dc = Span::new(d, c);
let span_cd = Span::new(c, d);

assert!(span_ab < span_dc);
assert!(span_dc > span_ab);
assert!(span_ab < span_cd);
assert!(span_cd > span_ab);
}
}
19 changes: 10 additions & 9 deletions boa/src/syntax/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use std::{
/// If the next value is not an assignment operation it will pattern match the provided values and return the corresponding token.
macro_rules! vop {
($this:ident, $assign_op:expr, $op:expr) => ({
let preview = $this.preview_next().ok_or_else(|| LexerError::new("Could not preview next value"))?;
let preview = $this.preview_next().ok_or_else(|| LexerError::new("could not preview next value"))?;
match preview {
'=' => {
$this.next();
Expand All @@ -33,7 +33,7 @@ macro_rules! vop {
}
});
($this:ident, $assign_op:expr, $op:expr, {$($case:pat => $block:expr), +}) => ({
let preview = $this.preview_next().ok_or_else(|| LexerError::new("Could not preview next value"))?;
let preview = $this.preview_next().ok_or_else(|| LexerError::new("could not preview next value"))?;
match preview {
'=' => {
$this.next();
Expand All @@ -49,7 +49,7 @@ macro_rules! vop {
}
});
($this:ident, $op:expr, {$($case:pat => $block:expr),+}) => {
let preview = $this.preview_next().ok_or_else(|| LexerError::new("Could not preview next value"))?;
let preview = $this.preview_next().ok_or_else(|| LexerError::new("could not preview next value"))?;
match preview {
$($case => {
$this.next()?;
Expand Down Expand Up @@ -136,7 +136,7 @@ impl<'a> Lexer<'a> {
pub fn new(buffer: &'a str) -> Lexer<'a> {
Lexer {
tokens: Vec::new(),
position: Position::new(1, 1),
position: Position::new(1, 0),
buffer: buffer.chars().peekable(),
}
}
Expand Down Expand Up @@ -177,13 +177,13 @@ impl<'a> Lexer<'a> {

/// Changes the current position by advancing to the next line.
fn next_line(&mut self) {
let pos = Position::new(self.position.line_number() + 1, 1);
let pos = Position::new(self.position.line_number() + 1, 0);
self.position = pos;
}

/// Changes the current position by advancing the given number of lines.
fn move_lines(&mut self, lines: u64) {
let pos = Position::new(self.position.line_number() + lines, 1);
let pos = Position::new(self.position.line_number() + lines, 0);
self.position = pos;
}

Expand Down Expand Up @@ -508,6 +508,7 @@ impl<'a> Lexer<'a> {
if self.preview_next().is_none() {
return Err(LexerError::new("Unterminated String"));
}
let escape_pos = self.position;
let escape = self.next();
if escape != '\n' {
let escaped_ch = match escape {
Expand Down Expand Up @@ -597,7 +598,7 @@ impl<'a> Lexer<'a> {
}
'\'' | '"' | '\\' => escape,
ch => {
let details = format!("{}: Invalid escape `{}`", self.position, ch);
let details = format!("invalid escape sequence `{}` at line {}, column {}", escape_pos.line_number(), escape_pos.column_number(), ch);
return Err(LexerError { details });
}
};
Expand Down Expand Up @@ -683,7 +684,7 @@ impl<'a> Lexer<'a> {
let mut lines = 0;
loop {
if self.preview_next().is_none() {
return Err(LexerError::new("Unterminated Multiline Comment"));
return Err(LexerError::new("unterminated multiline comment"));
}
match self.next() {
'*' => {
Expand Down Expand Up @@ -827,7 +828,7 @@ impl<'a> Lexer<'a> {
// Unicode Space_Seperator category (minus \u{0020} and \u{00A0} which are allready stated above)
'\u{1680}' | '\u{2000}'..='\u{200A}' | '\u{202F}' | '\u{205F}' | '\u{3000}' => (),
_ => {
let details = format!("{}: Unexpected '{}'", self.position, ch);
let details = format!("Unexpected '{}' at line {}, column {}", start_pos.line_number(), start_pos.column_number(), ch);
return Err(LexerError { details });
},
}
Expand Down
Loading

0 comments on commit e0d6b85

Please sign in to comment.