diff --git a/.vscode/launch.json b/.vscode/launch.json index 0f6c2f58ab..9ce9b302a6 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -1,29 +1,27 @@ { - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "type": "lldb", - "request": "launch", - "name": "Launch", - "args": [], - "program": "./target/debug/bin", - "cwd": "${workspaceRoot}", - "stopOnEntry": false, - "sourceLanguages": [ - "rust" - ] - }, - { - "name": "(Windows) Launch", - "type": "cppvsdbg", - "request": "launch", - "program": "${workspaceFolder}/target/debug/bin.exe", - "stopAtEntry": false, - "cwd": "${workspaceFolder}", - "environment": [], - } - ] -} \ No newline at end of file + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "lldb", + "request": "launch", + "name": "Launch", + "args": [], + "program": "./target/debug/boa", + "cwd": "${workspaceRoot}", + "stopOnEntry": false, + "sourceLanguages": ["rust"] + }, + { + "name": "(Windows) Launch", + "type": "cppvsdbg", + "request": "launch", + "program": "${workspaceFolder}/target/debug/boa.exe", + "stopAtEntry": false, + "cwd": "${workspaceFolder}", + "environment": [] + } + ] +} diff --git a/src/lib/syntax/parser.rs b/src/lib/syntax/parser.rs index 0a37da60eb..d7782827ff 100644 --- a/src/lib/syntax/parser.rs +++ b/src/lib/syntax/parser.rs @@ -530,11 +530,37 @@ impl Parser { } }; self.pos += 1; - self.expect( - TokenData::Punctuator(Punctuator::Colon), - "object declaration", - )?; - let value = self.parse()?; + let value = match self.get_token(self.pos)?.data { + TokenData::Punctuator(Punctuator::Colon) => { + self.pos += 1; + self.parse()? + } + TokenData::Punctuator(Punctuator::OpenParen) => { + self.pos += 1; + self.expect( + TokenData::Punctuator(Punctuator::CloseParen), + "Method Block", + )?; + self.pos += 1; // { + let expr = self.parse()?; + self.pos += 1; + mk!( + self, + ExprDef::FunctionDecl(None, vec!(), Box::new(expr)), + token + ) + } + _ => { + return Err(ParseError::Expected( + vec![ + TokenData::Punctuator(Punctuator::Colon), + TokenData::Punctuator(Punctuator::OpenParen), + ], + tk, + "object declaration", + )) + } + }; map.insert(name, value); self.pos += 1; } @@ -554,6 +580,13 @@ impl Parser { self.pos += 1; mk!(self, ExprDef::Block(exprs), token) } + // Empty Block + TokenData::Punctuator(Punctuator::CloseBlock) + if self.get_token(self.pos.wrapping_sub(2))?.data + == TokenData::Punctuator(Punctuator::OpenBlock) => + { + mk!(self, ExprDef::Block(vec!()), token) + } TokenData::Punctuator(Punctuator::Sub) => mk!( self, ExprDef::UnaryOp(UnaryOp::Minus, Box::new(self.parse()?)) @@ -889,6 +922,32 @@ mod tests { ))))], ); } + #[test] + fn check_object() { + // Testing short function syntax + let mut object_properties: BTreeMap = BTreeMap::new(); + object_properties.insert( + String::from("a"), + Expr::new(ExprDef::Const(Const::Bool(true))), + ); + object_properties.insert( + String::from("b"), + Expr::new(ExprDef::FunctionDecl( + None, + vec![], + Box::new(Expr::new(ExprDef::Block(vec![]))), + )), + ); + + check_parser( + "{ + a: true, + b() {} + }; + ", + &[Expr::new(ExprDef::ObjectDecl(Box::new(object_properties)))], + ); + } #[test] fn check_array() { diff --git a/tests/js/test.js b/tests/js/test.js index ae107b0f1e..ac0f9d87ba 100644 --- a/tests/js/test.js +++ b/tests/js/test.js @@ -1,6 +1,8 @@ -/// -// Use this file to test your changes to boa. -/// +let a = { + a: true, + b() { + return 2; + } +}; -let a = Boolean(0); -typeof a; +console.log(a["b"]());