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

Allow BindingPattern in function parameters #1601

Closed
raskad opened this issue Sep 29, 2021 · 8 comments
Closed

Allow BindingPattern in function parameters #1601

raskad opened this issue Sep 29, 2021 · 8 comments
Assignees
Labels
E-Medium Medium difficulty problem enhancement New feature or request good first issue Good for newcomers Hacktoberfest Hacktoberfest 2021 - https://hacktoberfest.digitalocean.com parser Issues surrounding the parser
Milestone

Comments

@raskad
Copy link
Member

raskad commented Sep 29, 2021

ECMASCript feature
Currently we only allow BindingIdentifiers in the FormalParameters of functions. BindingPatterns should be also allowed.

ECMAScript specification

Example code
This code should work and give the expected result:

function foo({ a, b }) {
  console.log(a, b)
}
let o = {
  a: "1",
  b: "2"
}
foo(o)
// prints `1 2`

Good starting points would be the FormalParameter parsing in https://github.com/boa-dev/boa/blob/master/boa/src/syntax/parser/function/mod.rs and the BindingPattern parsing in https://github.com/boa-dev/boa/blob/master/boa/src/syntax/parser/statement/mod.rs

@raskad raskad added enhancement New feature or request good first issue Good for newcomers Hacktoberfest Hacktoberfest 2021 - https://hacktoberfest.digitalocean.com parser Issues surrounding the parser E-Medium Medium difficulty problem labels Sep 29, 2021
@am-a-man
Copy link
Contributor

am-a-man commented Oct 1, 2021

Hey,
I would like to work on this, can you please assign this to me?

@jedel1043
Copy link
Member

@am-a-man Hey :)
We've noticed you asked for multiple issues at the same time, and we very much appreciate that!

However, seeing as you're a first contributor, we cannot assign you multiple tasks at the same time. This is partly because we wouldn't want to saturate a newcomer with too many tasks and partly because we would like to give a chance to some other people to get involved themselves with the project and possibly become regular contributors.

Nonetheless, we would absolutely love if you contributed to Boa, so please tell us which one of the issues you would like to work on, and we will assign it to you :D

@am-a-man
Copy link
Contributor

am-a-man commented Oct 1, 2021

this issue and #1600 are quite similar, but assign me this issue and I'll check afterwards if it's still unassigned I'll request again

@jedel1043
Copy link
Member

@am-a-man Assigned. Let us know if you need mentoring or if you have any questions ☺

@am-a-man
Copy link
Contributor

am-a-man commented Oct 1, 2021

Yeah, it would be great if you could help me to get started.

@raskad
Copy link
Member Author

raskad commented Oct 1, 2021

The best place to start for this would be in the file https://github.com/boa-dev/boa/blob/master/boa/src/syntax/parser/function/mod.rs. There we have the struct FormalParameter. That is the parser for one formal parameter as defined by the spec here. The parsing itself is defined in the parse method in the impl<R> TokenParser<R> for FormalParameter block. As you can see there is already a TODO at there.

When you compare the spec to the parser implementation, you will notice that we currently only implement one possible parsing path; you can follow in the spec: FormalParameter -> BindingElement -> SingleNameBinding. The SingleNameBinding contains a BindingIdentifier and optionally an Initializer. Both are parsed via the corresponding parser structs in our code. As you can see we currently do not expect a BindingPattern so the parsing would just fail because { and [ cannot be parsed as a BindingIdentifier.

We already have a working parser for BindingPattern in this file: https://github.com/boa-dev/boa/blob/master/boa/src/syntax/parser/statement/mod.rs.

The first step would be to check if the next token is the start of a BindingPattern (e.g. { or [ ) and parse that pattern with the existing parser (I would ignore the output first, just parse it). You can check if that works by running cargo run on the project and trying some code, for example the one from the issue.

Next you probably have to adjust the output type of the parser. You can see the the parser returns a node::FormalParameter which is just a FormalParameter AST node. I guess that that node currently only accounts for SingleNameBindings, so you would have to make some changes there to make it work with the BindingPatterns.

I hope that helps :)

@am-a-man
Copy link
Contributor

@raskad hey, I allowed BindingPattern as function parameter by defining FormalParameter as

pub struct FormalParameter {

    declaration: Declaration,
    is_rest_param: bool,

}

it works as it should in nightly as well as when I comment #[doc = concat!("JavaScript `", $name, "` built-in implementation.")]
from the following file https://github.com/boa-dev/boa/blob/master/boa/src/builtins/typed_array/mod.rs but on running cargo run -- test.js
following error keeps coming up:

error[E0658]: arbitrary expressions in key-value attributes are unstable
    --> boa\src\builtins\typed_array\mod.rs:40:17
     |
40   |         #[doc = concat!("JavaScript `", $name, "` built-in implementation.")]
     |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
3352 | typed_array!(Int8Array, "Int8Array", typed_int8_array_object);
     | -------------------------------------------------------------- in this macro invocation
     |
     = note: see issue #78835 <https://github.com/rust-lang/rust/issues/78835> for more information
     = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0658]: arbitrary expressions in key-value attributes are unstable
    --> boa\src\builtins\typed_array\mod.rs:40:17
     |
40   |         #[doc = concat!("JavaScript `", $name, "` built-in implementation.")]
     |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
3353 | typed_array!(Uint8Array, "Uint8Array", typed_uint8_array_object);
     | ----------------------------------------------------------------- in this macro invocation
     |
     = note: see issue #78835 <https://github.com/rust-lang/rust/issues/78835> for more information
     = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0658]: arbitrary expressions in key-value attributes are unstable
    --> boa\src\builtins\typed_array\mod.rs:40:17
     |
40   |           #[doc = concat!("JavaScript `", $name, "` built-in implementation.")]
     |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
3354 | / typed_array!(
3355 | |     Uint8ClampedArray,
3356 | |     "Uint8ClampedArray",
3357 | |     typed_uint8clamped_array_object
3358 | | );
     | |__- in this macro invocation
     |
     = note: see issue #78835 <https://github.com/rust-lang/rust/issues/78835> for more information
     = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0658]: arbitrary expressions in key-value attributes are unstable
    --> boa\src\builtins\typed_array\mod.rs:40:17
     |
40   |         #[doc = concat!("JavaScript `", $name, "` built-in implementation.")]
     |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
3359 | typed_array!(Int16Array, "Int16Array", typed_int16_array_object);
     | ----------------------------------------------------------------- in this macro invocation
     |
     = note: see issue #78835 <https://github.com/rust-lang/rust/issues/78835> for more information
     = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0658]: arbitrary expressions in key-value attributes are unstable
    --> boa\src\builtins\typed_array\mod.rs:40:17
     |
40   |         #[doc = concat!("JavaScript `", $name, "` built-in implementation.")]
     |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
3360 | typed_array!(Uint16Array, "Uint16Array", typed_uint16_array_object);
     | -------------------------------------------------------------------- in this macro invocation
     |
     = note: see issue #78835 <https://github.com/rust-lang/rust/issues/78835> for more information
     = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0658]: arbitrary expressions in key-value attributes are unstable
    --> boa\src\builtins\typed_array\mod.rs:40:17
     |
40   |         #[doc = concat!("JavaScript `", $name, "` built-in implementation.")]
     |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
3361 | typed_array!(Int32Array, "Int32Array", typed_int32_array_object);
     | ----------------------------------------------------------------- in this macro invocation
     |
     = note: see issue #78835 <https://github.com/rust-lang/rust/issues/78835> for more information
     = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0658]: arbitrary expressions in key-value attributes are unstable
    --> boa\src\builtins\typed_array\mod.rs:40:17
     |
40   |         #[doc = concat!("JavaScript `", $name, "` built-in implementation.")]
     |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
3362 | typed_array!(Uint32Array, "Uint32Array", typed_uint32_array_object);
     | -------------------------------------------------------------------- in this macro invocation
     |
     = note: see issue #78835 <https://github.com/rust-lang/rust/issues/78835> for more information
     = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0658]: arbitrary expressions in key-value attributes are unstable
    --> boa\src\builtins\typed_array\mod.rs:40:17
     |
40   |         #[doc = concat!("JavaScript `", $name, "` built-in implementation.")]
     |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
3363 | typed_array!(BigInt64Array, "BigInt64Array", typed_bigint64_array_object);
     | -------------------------------------------------------------------------- in this macro invocation
     |
     = note: see issue #78835 <https://github.com/rust-lang/rust/issues/78835> for more information
     = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0658]: arbitrary expressions in key-value attributes are unstable
    --> boa\src\builtins\typed_array\mod.rs:40:17
     |
40   |           #[doc = concat!("JavaScript `", $name, "` built-in implementation.")]
     |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
3364 | / typed_array!(
3365 | |     BigUint64Array,
3366 | |     "BigUint64Array",
3367 | |     typed_biguint64_array_object
3368 | | );
     | |__- in this macro invocation
     |
     = note: see issue #78835 <https://github.com/rust-lang/rust/issues/78835> for more information
     = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0658]: arbitrary expressions in key-value attributes are unstable
    --> boa\src\builtins\typed_array\mod.rs:40:17
     |
40   |         #[doc = concat!("JavaScript `", $name, "` built-in implementation.")]
     |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
3369 | typed_array!(Float32Array, "Float32Array", typed_float32_array_object);
     | ----------------------------------------------------------------------- in this macro invocation
     |
     = note: see issue #78835 <https://github.com/rust-lang/rust/issues/78835> for more information
     = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0658]: arbitrary expressions in key-value attributes are unstable
    --> boa\src\builtins\typed_array\mod.rs:40:17
     |
40   |         #[doc = concat!("JavaScript `", $name, "` built-in implementation.")]
     |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
3370 | typed_array!(Float64Array, "Float64Array", typed_float64_array_object);
     | ----------------------------------------------------------------------- in this macro invocation
     |
     = note: see issue #78835 <https://github.com/rust-lang/rust/issues/78835> for more information
     = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

https://github.com/boa-dev/boa/blob/master/boa/src/builtins/typed_array/mod.rs

Can you suggest what could be wrong?

@jedel1043
Copy link
Member

@am-a-man Update your rustc. The feature was stabilized on 1.54

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-Medium Medium difficulty problem enhancement New feature or request good first issue Good for newcomers Hacktoberfest Hacktoberfest 2021 - https://hacktoberfest.digitalocean.com parser Issues surrounding the parser
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants