Skip to content

Commit

Permalink
Fix ECMAScript to IDL Promise<T> type conversion
Browse files Browse the repository at this point in the history
This fixes a number of bugs:

* Rejected promises would be converted to fulfilled promises.
* Timing would not be correct due to using .then()

This removes the type conversion based on T that was previously performed, since that does not happen in the Web IDL specification until the time of the "react" algorithm at https://heycam.github.io/webidl/#dfn-perform-steps-once-promise-is-settled (which, for example, is never called when a Promise<T> is used as a return type).
  • Loading branch information
ExE-Boss authored May 5, 2020
1 parent 7d45dfc commit 2aab61c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 64 deletions.
19 changes: 1 addition & 18 deletions lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,24 +328,7 @@ function generateTypeConversion(ctx, name, idlType, argAttrs = [], parentName, e
}

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);
`;
str += `${name} = new Promise(resolve => resolve(${name}));`;
}

function generateFrozenArray() {
Expand Down
52 changes: 6 additions & 46 deletions test/__snapshots__/test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,7 @@ exports.convert = function convert(value, { context = \\"The provided value\\" }

let callResult = Reflect.apply(X, thisArg, []);

callResult = Promise.resolve(callResult).then(
value => {
value = conversions[\\"any\\"](value, { context: context + \\" promise value\\" });

return value;
},
reason => reason
);

callResult = new Promise(resolve => resolve(callResult));
return callResult;
} catch (err) {
return Promise.reject(err);
Expand Down Expand Up @@ -3695,10 +3687,7 @@ exports.install = (globalObject, globalNames) => {
const args = [];
{
let curArg = arguments[0];
curArg = Promise.resolve(curArg).then(
value => {},
reason => reason
);
curArg = new Promise(resolve => resolve(curArg));
args.push(curArg);
}
return esValue[implSymbol].voidPromiseConsumer(...args);
Expand All @@ -3720,16 +3709,7 @@ exports.install = (globalObject, globalNames) => {
const args = [];
{
let curArg = arguments[0];
curArg = Promise.resolve(curArg).then(
value => {
value = conversions[\\"double\\"](value, {
context: \\"Failed to execute 'promiseConsumer' on 'PromiseTypes': parameter 1\\" + \\" promise value\\"
});

return value;
},
reason => reason
);
curArg = new Promise(resolve => resolve(curArg));
args.push(curArg);
}
return esValue[implSymbol].promiseConsumer(...args);
Expand Down Expand Up @@ -8885,15 +8865,7 @@ exports.convert = function convert(value, { context = \\"The provided value\\" }

let callResult = Reflect.apply(X, thisArg, []);

callResult = Promise.resolve(callResult).then(
value => {
value = conversions[\\"any\\"](value, { context: context + \\" promise value\\" });

return value;
},
reason => reason
);

callResult = new Promise(resolve => resolve(callResult));
return callResult;
} catch (err) {
return Promise.reject(err);
Expand Down Expand Up @@ -12511,10 +12483,7 @@ exports.install = (globalObject, globalNames) => {
const args = [];
{
let curArg = arguments[0];
curArg = Promise.resolve(curArg).then(
value => {},
reason => reason
);
curArg = new Promise(resolve => resolve(curArg));
args.push(curArg);
}
return esValue[implSymbol].voidPromiseConsumer(...args);
Expand All @@ -12536,16 +12505,7 @@ exports.install = (globalObject, globalNames) => {
const args = [];
{
let curArg = arguments[0];
curArg = Promise.resolve(curArg).then(
value => {
value = conversions[\\"double\\"](value, {
context: \\"Failed to execute 'promiseConsumer' on 'PromiseTypes': parameter 1\\" + \\" promise value\\"
});

return value;
},
reason => reason
);
curArg = new Promise(resolve => resolve(curArg));
args.push(curArg);
}
return esValue[implSymbol].promiseConsumer(...args);
Expand Down

0 comments on commit 2aab61c

Please sign in to comment.