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

Create async iterators and iterator result objects in the correct realm #247

Merged
merged 20 commits into from
Sep 12, 2021
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions lib/constructs/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,13 +424,15 @@ class Interface {
}

if (iterable.isAsync) {
this.requires.addRaw("newObjectInRealm", "utils.newObjectInRealm");
this.str += `
ctorRegistry["${this.name} AsyncIterator"] = Object.create(utils.AsyncIteratorPrototype, {
[Symbol.toStringTag]: {
value: "${this.name} AsyncIterator",
configurable: true
}
});
ctorRegistry["${this.name} AsyncIterator"] =
Object.create(ctorRegistry["%AsyncIteratorPrototype%"], {
[Symbol.toStringTag]: {
value: "${this.name} AsyncIterator",
configurable: true
}
});
utils.define(ctorRegistry["${this.name} AsyncIterator"], {
next() {
const internal = this && this[utils.iterInternalSymbol];
Expand All @@ -440,7 +442,7 @@ class Interface {
const nextSteps = () => {
if (internal.isFinished) {
return Promise.resolve({ value: undefined, done: true });
return Promise.resolve(newObjectInRealm(globalObject, { value: undefined, done: true }));
}
const nextPromise = internal.target[implSymbol][utils.asyncIteratorNext](this);
Expand All @@ -449,15 +451,15 @@ class Interface {
internal.ongoingPromise = null;
if (next === utils.asyncIteratorEOI) {
internal.isFinished = true;
return { value: undefined, done: true };
return newObjectInRealm(globalObject, { value: undefined, done: true });
}`;
if (iterable.isPair) {
this.str += `
return utils.iteratorResult(next.map(utils.tryWrapperForImpl), kind);
return newObjectInRealm(globalObject, utils.iteratorResult(next.map(utils.tryWrapperForImpl), kind));
`;
} else {
this.str += `
return { value: utils.tryWrapperForImpl(next), done: false };
return newObjectInRealm(globalObject, { value: utils.tryWrapperForImpl(next), done: false });
`;
}
this.str += `
Expand Down Expand Up @@ -487,7 +489,7 @@ class Interface {
const returnSteps = () => {
if (internal.isFinished) {
return Promise.resolve({ value, done: true });
return Promise.resolve(newObjectInRealm(globalObject, { value, done: true }));
}
internal.isFinished = true;
Expand All @@ -497,14 +499,15 @@ class Interface {
const returnPromise = internal.ongoingPromise ?
internal.ongoingPromise.then(returnSteps, returnSteps) :
returnSteps();
return returnPromise.then(() => ({ value, done: true }));
return returnPromise.then(() => newObjectInRealm(globalObject, { value, done: true }));
}
`;
}
this.str += `
});
`;
} else if (iterable.isPair) {
this.requires.addRaw("newObjectInRealm", "utils.newObjectInRealm");
this.str += `
ctorRegistry["${this.name} Iterator"] =
Object.create(ctorRegistry["%IteratorPrototype%"], {
Expand All @@ -526,12 +529,12 @@ class Interface {
const values = Array.from(target[implSymbol]);
const len = values.length;
if (index >= len) {
return { value: undefined, done: true };
return newObjectInRealm(globalObject, { value: undefined, done: true });
}
const pair = values[index];
internal.index = index + 1;
return utils.iteratorResult(pair.map(utils.tryWrapperForImpl), kind);
return newObjectInRealm(globalObject, utils.iteratorResult(pair.map(utils.tryWrapperForImpl), kind));
}
}
);
Expand Down
26 changes: 23 additions & 3 deletions lib/output/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ function define(target, source) {
}
}

function newObjectInRealm(globalObject, object) {
const ctorRegistry = initCtorRegistry(globalObject);
return Object.defineProperties(
Object.create(ctorRegistry["%Object.prototype%"]),
Object.getOwnPropertyDescriptors(object)
);
}

const wrapperSymbol = Symbol("wrapper");
const implSymbol = Symbol("impl");
const sameObjectCaches = Symbol("SameObject caches");
Expand All @@ -26,6 +34,9 @@ const ctorRegistrySymbol = Symbol.for("[webidl2js] constructor registry");
// This only contains the intrinsic names that are referenced from the `ctorRegistry`:
const intrinsicConstructors = ["Array"];

const IteratorPrototype = Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]()));
const AsyncIteratorPrototype = Object.getPrototypeOf(Object.getPrototypeOf(async function* () {}).prototype);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These exports could be removed now; should they be?

Copy link
Member

Choose a reason for hiding this comment

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

I think removing them would technically be a major bump. Maybe add a comment here saying we should remove them when we bump?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense. It's worth noting though that there's already a bit of incompatibility between the main branch and the last release; interfaces generated after #234 expect initCtorRegistry to be the only way of initializing the ctorRegistry, while code generated before #234 naturally doesn't call initCtorRegistry, as it didn't exist. Installing pre-#234 interfaces before post-#234 interfaces causes the latter to throw, because the ctorRegistry was created but %IteratorPrototype% was not captured. There's a workaround (calling initCtorRegistry manually before installing interfaces), which I've been using in local testing, but it's not documented.

I sort of assumed that the next release would be major for this reason, though admittedly that would probably be more of a pain for jsdom than just using the workaround. I'm not terribly clear on what the semver guarantees mean for codegen projects, much less for domexception and other projects whose public interface is only part codegen.


function initCtorRegistry(globalObject) {
if (hasOwn(globalObject, ctorRegistrySymbol)) {
return globalObject[ctorRegistrySymbol];
Expand All @@ -36,11 +47,21 @@ function initCtorRegistry(globalObject) {
ctorRegistry[`%${intrinsic}%`] = globalObject[intrinsic];
}

// TODO: Also capture `%AsyncIteratorPrototype%`
ctorRegistry["%Object.prototype%"] = globalObject.Object.prototype;
ctorRegistry["%IteratorPrototype%"] = Object.getPrototypeOf(
Object.getPrototypeOf(new ctorRegistry["%Array%"]()[Symbol.iterator]())
);
ninevra marked this conversation as resolved.
Show resolved Hide resolved

try {
ctorRegistry["%AsyncIteratorPrototype%"] = Object.getPrototypeOf(
Object.getPrototypeOf(
globalObject.eval("(async function* () {})").prototype
)
);
} catch {
ctorRegistry["%AsyncIteratorPrototype%"] = AsyncIteratorPrototype;
}

globalObject[ctorRegistrySymbol] = ctorRegistry;
return ctorRegistry;
}
Expand Down Expand Up @@ -77,8 +98,6 @@ function tryImplForWrapper(wrapper) {
}

const iterInternalSymbol = Symbol("internal");
const IteratorPrototype = Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]()));
const AsyncIteratorPrototype = Object.getPrototypeOf(Object.getPrototypeOf(async function* () {}).prototype);

function isArrayIndexPropName(P) {
if (typeof P !== "string") {
Expand Down Expand Up @@ -143,6 +162,7 @@ module.exports = exports = {
isObject,
hasOwn,
define,
newObjectInRealm,
wrapperSymbol,
implSymbol,
getSameObject,
Expand Down
Loading