boa icon indicating copy to clipboard operation
boa copied to clipboard

Duplicate code in Boa

Open 0x7D2B opened this issue 3 years ago • 1 comments

I've been coming across a lot of duplicate or very similar code in Boa. I think a lot of it can be combined to simplify things. Some examples:

Function parsers

There are four very similar parsers for function declarations and statements, one for each combination of async/non-async function expression/declaration:

  • https://github.com/boa-dev/boa/blob/586a68970b1b5698183983fec9c3333bd0c45cbf/boa/src/syntax/parser/expression/primary/function_expression/mod.rs#L45
  • https://github.com/boa-dev/boa/blob/586a68970b1b5698183983fec9c3333bd0c45cbf/boa/src/syntax/parser/expression/primary/async_function_expression/mod.rs#L50
  • https://github.com/boa-dev/boa/blob/586a68970b1b5698183983fec9c3333bd0c45cbf/boa/src/syntax/parser/statement/declaration/hoistable/function_decl/mod.rs#L51
  • https://github.com/boa-dev/boa/blob/586a68970b1b5698183983fec9c3333bd0c45cbf/boa/src/syntax/parser/statement/declaration/hoistable/async_function_decl/mod.rs#L51

For reference, here's also the arrow function parser:

  • https://github.com/boa-dev/boa/blob/e2f9ecd593a74c372aa3bdda5903c1c66322dedd/boa/src/syntax/parser/expression/assignment/arrow_function.rs#L71

Function AST nodes

  • https://github.com/boa-dev/boa/blob/39764b9e006ee51523edd3af55afe1833fda3c40/boa/src/syntax/ast/node/declaration/function_decl/mod.rs#L40
  • https://github.com/boa-dev/boa/blob/39764b9e006ee51523edd3af55afe1833fda3c40/boa/src/syntax/ast/node/declaration/function_expr/mod.rs#L37
  • https://github.com/boa-dev/boa/blob/39764b9e006ee51523edd3af55afe1833fda3c40/boa/src/syntax/ast/node/declaration/async_function_decl/mod.rs#L30
  • https://github.com/boa-dev/boa/blob/39764b9e006ee51523edd3af55afe1833fda3c40/boa/src/syntax/ast/node/declaration/async_function_expr/mod.rs#L31
  • https://github.com/boa-dev/boa/blob/39764b9e006ee51523edd3af55afe1833fda3c40/boa/src/syntax/ast/node/declaration/arrow_function_decl/mod.rs#L33

Declarative and function environment records

Declarative and function environments are almost identical in their implementation. Fixed in #1156.

  • https://github.com/boa-dev/boa/blob/39764b9e006ee51523edd3af55afe1833fda3c40/boa/src/environment/declarative_environment_record.rs#L40
  • https://github.com/boa-dev/boa/blob/39764b9e006ee51523edd3af55afe1833fda3c40/boa/src/environment/function_environment_record.rs#L96

Loops

While and Do-While loops:

  • https://github.com/boa-dev/boa/blob/39764b9e006ee51523edd3af55afe1833fda3c40/boa/src/syntax/ast/node/iteration/while_loop/mod.rs
  • https://github.com/boa-dev/boa/blob/39764b9e006ee51523edd3af55afe1833fda3c40/boa/src/syntax/ast/node/iteration/do_while_loop/mod.rs

For-In and For-Of loops:

  • https://github.com/boa-dev/boa/blob/39764b9e006ee51523edd3af55afe1833fda3c40/boa/src/syntax/ast/node/iteration/for_in_loop/mod.rs#L94
  • https://github.com/boa-dev/boa/blob/39764b9e006ee51523edd3af55afe1833fda3c40/boa/src/syntax/ast/node/iteration/for_of_loop/mod.rs#L84

Fixed issues

Fixed

ConstDeclList, LetDeclList, VarDeclList and ConstDecl, LetDecl, VarDecl (Fixed in #1181)

Six declaration container objects which are almost identical.

  • https://github.com/boa-dev/boa/blob/7b103a5c0bf29150a9bace87b17b39c9e0e7750d/boa/src/syntax/ast/node/declaration/const_decl_list/mod.rs
  • https://github.com/boa-dev/boa/blob/7b103a5c0bf29150a9bace87b17b39c9e0e7750d/boa/src/syntax/ast/node/declaration/let_decl_list/mod.rs
  • https://github.com/boa-dev/boa/blob/7b103a5c0bf29150a9bace87b17b39c9e0e7750d/boa/src/syntax/ast/node/declaration/var_decl_list/mod.rs

Function calls and object constructors (Fixed in #1273)

This already resulted in an issue where one of these implementations had a bug while the other had it fixed.

  • https://github.com/boa-dev/boa/blob/39764b9e006ee51523edd3af55afe1833fda3c40/boa/src/object/gcobject.rs#L118
  • https://github.com/boa-dev/boa/blob/39764b9e006ee51523edd3af55afe1833fda3c40/boa/src/object/gcobject.rs#L214

0x7D2B avatar Mar 22 '21 04:03 0x7D2B

#1273 fixes the function call and constructor one.

0x7D2B avatar May 23 '21 06:05 0x7D2B

Closing since most of the deduplication was removed when we created the VM interpreter.

jedel1043 avatar Apr 14 '24 14:04 jedel1043