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 moving NativeObject variables into closures as external captures #1523

Merged
merged 4 commits into from
Sep 19, 2021

Conversation

jedel1043
Copy link
Member

@jedel1043 jedel1043 commented Aug 28, 2021

This Pull Request follows from #1518.

It changes the following:

  • Allows moving any NativeObject + Clone type as a parameter into any closure
  • Adds test for the new feature

This unblocks the implementation of arguments exotic objects.
The new test is a good example of how this new feature works:

let js_function = FunctionBuilder::closure_with_captures(
        &mut context,
        |_, _, context, captures| {
            println!("Called `createMessage`");
            // We obtain the `name` property of `captures.object`
            let name = captures.object.get("name", context)?;

            // We create a new message from our captured variable.
            let message = JsString::concat_array(&[
                "message from `",
                name.to_string(context)?.as_str(),
                "`: ",
                captures.greeting.as_str(),
            ]);

            println!("{}", message);

            // We convert `message` into `Jsvalue` to be able to return it.
            Ok(message.into())
        },
        // Here is where we move `clone_variable` into the closure.
        clone_variable,
    )

@jedel1043 jedel1043 changed the title Allow capturing NativeObject variables in closures Allow capturing NativeObject variables in closures as external captures Aug 28, 2021
@jedel1043 jedel1043 changed the title Allow capturing NativeObject variables in closures as external captures Allow moving NativeObject variables into closures as external captures Aug 28, 2021
@jedel1043 jedel1043 marked this pull request as ready for review August 28, 2021 23:44
@jedel1043 jedel1043 requested review from Razican and HalidOdat and removed request for Razican August 28, 2021 23:44
@jedel1043 jedel1043 added the enhancement New feature or request label Aug 29, 2021
@jedel1043 jedel1043 added this to the v0.13.0 milestone Aug 29, 2021
@jedel1043 jedel1043 added the API label Aug 29, 2021
@Razican Razican requested review from RageKnify and raskad August 29, 2021 07:22
Copy link
Member

@raskad raskad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.
Maybe you could add the test (or something similar) to the examples? NativeObject is pretty abstract and it may not be clear what you can do with closure_with_captures without an example.

@jedel1043
Copy link
Member Author

Looks good to me.
Maybe you could add the test (or something similar) to the examples? NativeObject is pretty abstract and it may not be clear what you can do with closure_with_captures without an example.

Okay, let me add an example

@jedel1043 jedel1043 requested a review from raskad August 29, 2021 14:44
@jedel1043
Copy link
Member Author

Looks good to me.
Maybe you could add the test (or something similar) to the examples? NativeObject is pretty abstract and it may not be clear what you can do with closure_with_captures without an example.

Added an example inside closures.rs. Also slightly modified the API to automatically downcast to the passed type. Let me know if you like the new changes.

Copy link
Member

@raskad raskad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, the automatic downcast looks really nice from an API consumer perspective. We only have to make sure that the downcast never fails, otherwise the error is really confusing.

The example looks great!

@raskad
Copy link
Member

raskad commented Aug 29, 2021

I just noticed that there are some lints in this latest version. You may want to fix those.

@jedel1043
Copy link
Member Author

Rebased

Copy link
Member

@HalidOdat HalidOdat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! :)

@jedel1043 jedel1043 merged commit 25ac4cc into boa-dev:master Sep 19, 2021
@jedel1043 jedel1043 deleted the capture_closures branch September 19, 2021 16:42
@raskad raskad mentioned this pull request Sep 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants