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

Ensure promise-typed operations and attributes return promises #200

Merged
merged 2 commits into from
Apr 27, 2020

Conversation

domenic
Copy link
Member

@domenic domenic commented Apr 17, 2020

Closes #79.

This removes support for overloading promise operations and non-promise operations, since that would require a more ambitious approach and doesn't seem to appear on the web.


Thoughts welcome. The strategy of using async for operations and try/catch for attributes makes the output nice but the webidl2js code a bit icky. Threading the async bit through all the operation stuff adds a bit of complexity. And, we might want to abandon that if we do want to support overloads of promise-returning operations vs. non-promise returning ones, since doing that would require an appropriately-scoped try/catch.

@domenic
Copy link
Member Author

domenic commented Apr 21, 2020

I found whatwg/webidl#776 which indicates we don't need to worry about supporting overloads between promise-returning and non-promise-returning operations.

Copy link
Contributor

@ExE-Boss ExE-Boss left a comment

Choose a reason for hiding this comment

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

This should use isAsync, because async is a contextual keyword and a reserved word in modules.

throw new TypeError(\\"Illegal invocation\\");
}

return utils.tryWrapperForImpl(esValue[implSymbol].promiseOperation());
Copy link
Contributor

Choose a reason for hiding this comment

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

This has to generate:

       return utils.tryWrapperForImpl(await esValue[implSymbol].promiseOperation());

Copy link
Member Author

Choose a reason for hiding this comment

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

This and your other unsolicited comments are not so straightforward.

You are suggesting that we incorporate the unwrapping done in #108 here. However, that breaks the ability for the impl method/getter to return the same promise multiple times. That is especially problematic for attributes, where x.attr === x.attr is almost always what the spec intends.

I think we should leave working on the unwrapping to #108 or some continuation of that has better discussion behind it.

throw new TypeError(\\"Illegal invocation\\");
}

return utils.tryWrapperForImpl(esValue[implSymbol][\\"promiseAttribute\\"]);
Copy link
Contributor

Choose a reason for hiding this comment

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

This has to generate:

          return Promise.resolve(esValue[implSymbol]["promiseAttribute"])
            .then(utils.tryWrapperForImpl);

}

static async staticPromiseOperation() {
return utils.tryWrapperForImpl(Impl.implementation.staticPromiseOperation());
Copy link
Contributor

Choose a reason for hiding this comment

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

This has to generate:

       return utils.tryWrapperForImpl(await Impl.implementation.staticPromiseOperation());

try {
const esValue = this !== null && this !== undefined ? this : globalObject;

return Impl.implementation[\\"staticPromiseAttribute\\"];
Copy link
Contributor

Choose a reason for hiding this comment

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

This has to generate:

          return Promise.resolve(Impl.implementation["staticPromiseAttribute"])
            .then(utils.tryWrapperForImpl);

@domenic
Copy link
Member Author

domenic commented Apr 21, 2020

Please do not review pull requests that your review was not requested on. It is very rude to come into someone else's project and critique their code unsolicited.

@jsdom jsdom deleted a comment from ExE-Boss Apr 21, 2020
@jsdom jsdom deleted a comment from ExE-Boss Apr 21, 2020
@jsdom jsdom deleted a comment from ExE-Boss Apr 21, 2020
@jsdom jsdom deleted a comment from ExE-Boss Apr 21, 2020
@jsdom jsdom deleted a comment from ExE-Boss Apr 21, 2020
@jsdom jsdom deleted a comment from ExE-Boss Apr 21, 2020
@jsdom jsdom deleted a comment from ExE-Boss Apr 21, 2020
@jsdom jsdom deleted a comment from ExE-Boss Apr 21, 2020
@jsdom jsdom deleted a comment from ExE-Boss Apr 21, 2020
@jsdom jsdom deleted a comment from ExE-Boss Apr 21, 2020
@jsdom jsdom deleted a comment from ExE-Boss Apr 21, 2020
@jsdom jsdom deleted a comment from ExE-Boss Apr 21, 2020
@jsdom jsdom deleted a comment from ExE-Boss Apr 21, 2020
@jsdom jsdom deleted a comment from ExE-Boss Apr 21, 2020
@jsdom jsdom deleted a comment from ExE-Boss Apr 21, 2020
@jsdom jsdom deleted a comment from ExE-Boss Apr 21, 2020
@jsdom jsdom deleted a comment from ExE-Boss Apr 21, 2020
@jsdom jsdom deleted a comment from ExE-Boss Apr 21, 2020
@@ -25,6 +25,20 @@ class Operation {
return firstOverloadOnInstance;
}

isAsync() {
const firstAsync = this.idls[0].idlType.generic === "Promise";
Copy link
Member

Choose a reason for hiding this comment

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

Linking to whatwg/webidl#776 would be useful.

this.str += `
${name}(${formatArgs(args)}) {${body}}
${isStatic ? "static " : ""}${isAsync ? "async " : ""}${name}(${formatArgs(args)}) {${body}}
Copy link
Member

Choose a reason for hiding this comment

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

Do we actually need the function to be async? I don't see await being used anywhere, and I fear that this difference could in fact be observable by the number of ticks it passed before the promise is rejected with a synchronously thrown error.

Copy link
Contributor

Choose a reason for hiding this comment

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

I talk about that in #79 (comment).

Copy link
Member Author

Choose a reason for hiding this comment

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

It's a convenient way to convert exceptions into rejections. But, it sounds like folks are in favor of instead wrapping everything in try/catch, so I'll do that.

Closes #79.

This removes support for overloading promise operations and non-promise operations, since that would require a more ambitious approach and doesn't seem to appear on the web.
Copy link
Member

@TimothyGu TimothyGu left a comment

Choose a reason for hiding this comment

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

LGTM modulo the comment.

Comment on lines 9 to 10
[Unforgeable] Promise<void> unforgeablePromiseOperation();
[Unforgeable] readonly attribute Promise<void> unforgeablePromiseAttribute;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
[Unforgeable] Promise<void> unforgeablePromiseOperation();
[Unforgeable] readonly attribute Promise<void> unforgeablePromiseAttribute;
[LegacyUnforgeable] Promise<void> unforgeablePromiseOperation();
[LegacyUnforgeable] readonly attribute Promise<void> unforgeablePromiseAttribute;

@domenic domenic merged commit f7520b3 into master Apr 27, 2020
@domenic domenic deleted the promise-returning branch April 27, 2020 23:31
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.

Promise-returning operations should return Promise.reject on invalid argument
3 participants