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 support for callback functions #194

Closed

Conversation

ExE-Boss
Copy link
Contributor

@ExE-Boss ExE-Boss commented Apr 16, 2020

This implements callback functions similarly to how callback interfaces were implemented in #172.

I would’ve preferred if this were included in WebIDL2JS v16.0.

Depends on:


Supersedes and closes #123

review?(@domenic, @TimothyGu, @Sebmaster)

Co-authored-by: Timothy Gu <timothygu99@gmail.com>
Copy link
Member

@domenic domenic left a comment

Choose a reason for hiding this comment

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

Thanks for jumping on this!

test/cases/URLCallback.webidl Show resolved Hide resolved
const thisArg = utils.tryWrapperForImpl(this);
let callResult;

callResult = Reflect.apply(value, thisArg, []);
Copy link
Member

Choose a reason for hiding this comment

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

Optional, but if it's easy, it would be nice to remove the unused callResult variable from this case (and construct).

test/__snapshots__/test.js.snap Outdated Show resolved Hide resolved
test/__snapshots__/test.js.snap Show resolved Hide resolved
Co-authored-by: Domenic Denicola <d@domenic.me>
@TimothyGu
Copy link
Member

Be sure to test this on jsdom before making a release. What blocked my other patch was that existing jsdom code didn't like this change.

@ExE-Boss
Copy link
Contributor Author

I’ll begin work on the JSDOM side of this once jsdom/jsdom#2884 is merged.

@ExE-Boss ExE-Boss requested a review from domenic April 29, 2020 09:25
}
}

CallbackFunction.prototype.type = "callback";
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
CallbackFunction.prototype.type = "callback";
CallbackFunction.prototype.type = "callback function";

Change others as appropriate.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

lib/types.js Outdated Show resolved Hide resolved
lib/types.js Outdated Show resolved Hide resolved
Copy link
Member

@domenic domenic left a comment

Choose a reason for hiding this comment

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

I stress-tested this against whatwg/streams#1035 and found some bugs with the promise conversion code.


return value;
},
reason => reason
Copy link
Member

Choose a reason for hiding this comment

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

This is incorrect.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This code is generated by:

webidl2js/lib/types.js

Lines 330 to 349 in 7d45dfc

function generatePromise() {
let handler;
if (idlType.idlType[0].idlType === "void") {
// Do nothing.
handler = "";
} else {
const conv = generateTypeConversion(ctx, "value", idlType.idlType[0], [], parentName,
`${errPrefix} + " promise value"`);
requires.merge(conv.requires);
handler = `
${conv.body}
return value;
`;
}
str += `
${name} = Promise.resolve(${name}).then(value => {
${handler}
}, reason => reason);
`;
}

Changing that is outside the scope of this PR.

try {
callResult = Reflect.apply(value, thisArg, []);

callResult = Promise.resolve(callResult).then(
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't implement the semantics described in https://heycam.github.io/webidl/#es-invoking-callback-functions + https://heycam.github.io/webidl/#es-promise . For that the correct code would be something more like

return new Promise(resolve => resolve(callResult))

Note that Promise.resolve() is inappropriate because it returns its argument in some cases, which is never true for converting to a Web IDL promise type. And note how converting to a Web IDL Promise<T> type ignores the T.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This code is generated by:

webidl2js/lib/types.js

Lines 330 to 349 in 7d45dfc

function generatePromise() {
let handler;
if (idlType.idlType[0].idlType === "void") {
// Do nothing.
handler = "";
} else {
const conv = generateTypeConversion(ctx, "value", idlType.idlType[0], [], parentName,
`${errPrefix} + " promise value"`);
requires.merge(conv.requires);
handler = `
${conv.body}
return value;
`;
}
str += `
${name} = Promise.resolve(${name}).then(value => {
${handler}
}, reason => reason);
`;
}

Changing that is outside the scope of this PR.

Copy link
Member

Choose a reason for hiding this comment

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

Well, I'm not willing to merge this PR if it generates incorrect code that is unusable for consumers of webidl2js, so I guess fixing that is a dependency of this PR then.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in #219.

@ExE-Boss ExE-Boss requested review from domenic and TimothyGu May 1, 2020 17:00
@domenic
Copy link
Member

domenic commented May 5, 2020

I'm unable to push commits to this branch again, despite it seemingly being a normal fork :(. I'll merge directly into master from my local copy.

@domenic
Copy link
Member

domenic commented May 5, 2020

Merged as 4a6e558!

@domenic domenic closed this May 5, 2020
@ExE-Boss ExE-Boss deleted the feat/constructs/callback-function branch May 5, 2020 19:51
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