Skip to content

Commit

Permalink
Implement “internally create a new object implementing the interface”
Browse files Browse the repository at this point in the history
  • Loading branch information
ExE-Boss committed Mar 20, 2021
1 parent b5ed275 commit 970cf26
Show file tree
Hide file tree
Showing 3 changed files with 1,055 additions and 407 deletions.
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

0 comments on commit 970cf26

Please sign in to comment.