Skip to content

Implement “internally create a new object implementing the interface” #239

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

Merged
merged 1 commit into from
Mar 20, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,11 @@ Creates a new instance of the wrapper class and corresponding implementation cla

This is useful inside implementation class files, where it is easiest to only deal with impls, not wrappers.

#### `new(globalObject)`
#### `new(globalObject, newTarget)`

Creates a new instance of the wrapper class and corresponding implementation class, but without invoking the implementation class constructor logic. Then returns the implementation class.

This corresponds to the [Web IDL "new" algorithm](https://heycam.github.io/webidl/#new), and is useful when implementing specifications that initialize objects in different ways than their constructors do.
This corresponds to the [WebIDL "create a new object implementing the interface"](https://heycam.github.io/webidl/#new) and ["internally create a new object implementing the interface"](https://heycam.github.io/webidl/#internally-create-a-new-object-implementing-the-interface) algorithms, and is useful when implementing specifications that initialize objects in different ways than their constructors do.

#### `setup(obj, globalObject, constructorArgs, privateData)`

Expand Down
18 changes: 13 additions & 5 deletions lib/constructs/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -1206,9 +1206,17 @@ class Interface {

generateIface() {
this.str += `
function makeWrapper(globalObject) {
const ctor = globalObject[ctorRegistrySymbol]["${this.name}"];
return Object.create(ctor.prototype);
function makeWrapper(globalObject, newTarget) {
let proto;
if (newTarget !== undefined) {
proto = newTarget.prototype;
}

if (!utils.isObject(proto)) {
proto = globalObject[ctorRegistrySymbol]["${this.name}"].prototype;
}

return Object.create(proto);
}
`;

Expand Down Expand Up @@ -1276,8 +1284,8 @@ class Interface {
return wrapper;
};

exports.new = globalObject => {
${this.isLegacyPlatformObj ? "let" : "const"} wrapper = makeWrapper(globalObject);
exports.new = (globalObject, newTarget) => {
${this.isLegacyPlatformObj ? "let" : "const"} wrapper = makeWrapper(globalObject, newTarget);

exports._internalSetup(wrapper, globalObject);
Object.defineProperty(wrapper, implSymbol, {
Expand Down
Loading