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

Implement emscripten_promise_any in promise.h #19153

Merged
merged 3 commits into from
Apr 12, 2023
Merged

Implement emscripten_promise_any in promise.h #19153

merged 3 commits into from
Apr 12, 2023

Conversation

tlively
Copy link
Member

@tlively tlively commented Apr 10, 2023

Similar to emscripten_promise_all and emscripten_promise_all_settled, this
function propagates the first of its input promises to be fulfilled or is
rejected with a list of reasons if its inputs are rejected.

@tlively
Copy link
Member Author

tlively commented Apr 10, 2023


typedef struct promise_any_state {
size_t size;
em_promise_t in[3];
Copy link
Member

Choose a reason for hiding this comment

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

What are these 3 constants?

Copy link
Member Author

Choose a reason for hiding this comment

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

These are the (up to) 3 input promises. They're not actually needed after the initial call to emscripten_promise_any, but it's convenient to bundle all the input, output, and expected state together in a single struct.

Copy link
Member

Choose a reason for hiding this comment

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

I'm confused, I don't see any mention of a limit of 3 on input promises in promise.h? The methods all take a num_promises without a mention of a limit in the comments.

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, this is just used in this test file, which arbitrarily chooses to call the API with three promises.

Copy link
Member

Choose a reason for hiding this comment

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

Oh ok, sorry, that's what I was missing...

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, this C API is a little funky because 100% of the implementation is in JS. There's no .c implementation file for the API at all.

Base automatically changed from promise-all-settled to main April 11, 2023 14:52
Similar to `emscripten_promise_all` and `emscripten_promise_all_settled`, this
function propagates the first of its input promises to be fulfilled or is
rejected with a list of reasons if its inputs are rejected.
@tlively
Copy link
Member Author

tlively commented Apr 11, 2023

The problem here is that the Node we ship with emsdk (and use on CI) does not include Promise.any. I'd like to keep this function for users who do have it available on their systems, though. What's the best workaround @sbc100?

@sbc100
Copy link
Collaborator

sbc100 commented Apr 11, 2023

The problem here is that the Node we ship with emsdk (and use on CI) does not include Promise.any. I'd like to keep this function for users who do have it available on their systems, though. What's the best workaround @sbc100?

By completely coincidence I just updated that version: emscripten-core/emsdk#829 !!!

However, I guess you should put some kind of node version check around your use of Promise.any? If it can't be pollyfilled then I guess you would need to add an assert in there? If you do add a polyfill then you should also add this test to test-node-compat to test it on ancient version of node.

@@ -221,6 +221,9 @@ mergeInto(LibraryManager.library, {
var promises = idsToPromises(idBuf, size);
#if RUNTIME_DEBUG
dbg('emscripten_promise_any: ' + promises);
#endif
#if ASSERTIONS
assert(typeof Promise.any !== 'undefined', "Promise.any does not exist");
Copy link
Collaborator

Choose a reason for hiding this comment

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

We could do something more like this.. but its probably not worth it?:

#if ENV_MAY_BE_NODE
if (ENV_IS_NODE && nodeIsOld) {
  abort("helpful message")
}
#endif

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, just having this assertion seems more general. For example it would work in old browsers that don't support Promise.any as well. I'd be happy to make the assertion message more useful, but I think the current message at least delivers the most important piece of information.

@tlively tlively merged commit 3c5bac9 into main Apr 12, 2023
@tlively tlively deleted the promise-any branch April 12, 2023 03:31
@sbc100
Copy link
Collaborator

sbc100 commented Apr 13, 2023

This caused the auto-roller to fail: https://chromium-review.googlesource.com/c/emscripten-releases/+/4420909

I think we should revisit and find a way to make this test fail to link with bumping -sMIN_NODE_VERSION

@sbc100
Copy link
Collaborator

sbc100 commented Apr 13, 2023

I'm working on a fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants