Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(boa): in operator #350

Merged
merged 5 commits into from
May 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
remove over-generalized helper for expression!
Add a test for props up the prototype chain. It is currently failing,
but should be passing for this to be complete.
  • Loading branch information
brian-gavin committed May 1, 2020
commit fd4bb5f4b81acc6b850b1d3a9efe34f1aab56337
10 changes: 10 additions & 0 deletions boa/src/exec/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,16 @@ mod in_operator {
assert_eq!(exec(p_in_o), String::from("true"));
}

#[test]
fn test_p_in_property_chain() {
let p_in_o = r#"
var o = {};
var p = 'toString';
p in o
"#;
assert_eq!(exec(p_in_o), String::from("true"));
}

#[test]
fn test_p_not_in_o() {
let p_not_in_o = r#"
Expand Down
36 changes: 13 additions & 23 deletions boa/src/syntax/parser/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ mod update;
use self::assignment::ExponentiationExpression;
pub(super) use self::{assignment::AssignmentExpression, primary::Initializer};
use super::{AllowAwait, AllowIn, AllowYield, Cursor, ParseResult, TokenParser};
use crate::syntax::ast::{
keyword::Keyword, node::Node, op::BinOp, punc::Punctuator, token::TokenKind,
};
use std::{convert::TryInto, fmt::Debug};
use crate::syntax::ast::{keyword::Keyword, node::Node, punc::Punctuator, token::TokenKind};

// For use in the expression! macro to allow for both Punctuator and Keyword parameters.
// Always returns false.
Expand All @@ -47,23 +44,6 @@ impl PartialEq<Punctuator> for Keyword {
///
/// Those exressions are divided by the punctuators passed as the third parameter.
macro_rules! expression { ($name:ident, $lower:ident, [$( $op:path ),*], [$( $low_param:ident ),*] ) => {
impl $name {
// Helper method to build a BinOp in the parse method
fn build_op<O, L>(&self, op: O, lhs: L, cursor: &mut Cursor<'_>) -> ParseResult
where
O: TryInto<BinOp>,
<O as TryInto<BinOp>>::Error: Debug, // for call to `expect`
L: Into<Box<Node>>
{
let _ = cursor.next().expect("token disappeared");
Ok(Node::bin_op(
op.try_into().expect("Could not get binary operation."),
lhs,
$lower::new($( self.$low_param ),*).parse(cursor)?
))
}
}

impl TokenParser for $name {
type Output = Node;

Expand All @@ -72,10 +52,20 @@ macro_rules! expression { ($name:ident, $lower:ident, [$( $op:path ),*], [$( $lo
while let Some(tok) = cursor.peek(0) {
match tok.kind {
TokenKind::Punctuator(op) if $( op == $op )||* => {
lhs = self.build_op(op, lhs, cursor)?;
let _ = cursor.next().expect("token disappeared");
lhs = Node::bin_op(
op.as_binop().expect("Could not get binary operation."),
lhs,
$lower::new($( self.$low_param ),*).parse(cursor)?
)
}
TokenKind::Keyword(op) if $( op == $op )||* => {
lhs = self.build_op(op, lhs, cursor)?;
let _ = cursor.next().expect("token disappeared");
lhs = Node::bin_op(
op.as_binop().expect("Could not get binary operation."),
lhs,
$lower::new($( self.$low_param ),*).parse(cursor)?
)
}
_ => break
}
Expand Down