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

webassembly/objpyproxy: Implement JS iterator protocol for Py iterables. #14435

Merged

Conversation

dpgeorge
Copy link
Member

@dpgeorge dpgeorge commented May 6, 2024

This allows using JavaScript for..of on Python iterables.

Copy link

codecov bot commented May 6, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.39%. Comparing base (50b43fe) to head (ac70b1e).

❗ Current head ac70b1e differs from pull request most recent head c056840. Consider uploading reports for the commit c056840 to get more accurate results

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #14435   +/-   ##
=======================================
  Coverage   98.39%   98.39%           
=======================================
  Files         161      161           
  Lines       21204    21204           
=======================================
  Hits        20864    20864           
  Misses        340      340           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Signed-off-by: Damien George <damien@micropython.org>
@dpgeorge dpgeorge force-pushed the webassembly-implement-js-iterator branch from 2ec5a87 to cb71a7d Compare May 6, 2024 04:54
Copy link

github-actions bot commented May 6, 2024

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:    +0 +0.000% PYBV10
     mimxrt:    +0 +0.000% TEENSY40
        rp2:    +0 +0.000% RPI_PICO
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS

@dpgeorge dpgeorge force-pushed the webassembly-implement-js-iterator branch from 68e3f38 to ac70b1e Compare May 6, 2024 05:58
["number"],
[target._ref],
);
return () => ({

Choose a reason for hiding this comment

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

nit: this "screams" for a generator to me ... or better, that's usually how iterators are implemented in JS:

return function* () {
    while (true) {
        const value = Module._malloc(3 * 4);
        const valid = Module.ccall(
            "proxy_c_to_js_iternext",
            "number",
            ["number", "pointer"],
            [iter_ref, value],
        );
        if (valid) {
            yield proxy_convert_mp_to_js_obj_jsside_with_free(value);
        }
        else {
            Module._free(value);
            break;
        }
    }
};

Maybe it doesn't save much typing but at least it grants that the shape of the object is consistent, that is { done: true, value: void 0 } or { done: false, value: ... }.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks! I was hoping you would tell me the "right" way to do this 😄

Now updated as suggested.

This allows using JavaScript for..of on Python iterables.

Signed-off-by: Damien George <damien@micropython.org>
@dpgeorge dpgeorge force-pushed the webassembly-implement-js-iterator branch from ac70b1e to c056840 Compare May 6, 2024 14:21
@WebReflection
Copy link

virtually approved 👍

please remember to push incremental updates on npm for quicker feedback in case something ain't fully right (but to me this looks right) 🙏

@dpgeorge dpgeorge merged commit c056840 into micropython:master May 7, 2024
23 of 24 checks passed
@dpgeorge dpgeorge deleted the webassembly-implement-js-iterator branch May 7, 2024 00:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants