Skip to content

Commit 970cf26

Browse files
committed
Implement “internally create a new object implementing the interface”
1 parent b5ed275 commit 970cf26

File tree

3 files changed

+1055
-407
lines changed

3 files changed

+1055
-407
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,11 @@ Creates a new instance of the wrapper class and corresponding implementation cla
282282

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

285-
#### `new(globalObject)`
285+
#### `new(globalObject, newTarget)`
286286

287287
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.
288288

289-
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.
289+
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.
290290

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

lib/constructs/interface.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -1206,9 +1206,17 @@ class Interface {
12061206

12071207
generateIface() {
12081208
this.str += `
1209-
function makeWrapper(globalObject) {
1210-
const ctor = globalObject[ctorRegistrySymbol]["${this.name}"];
1211-
return Object.create(ctor.prototype);
1209+
function makeWrapper(globalObject, newTarget) {
1210+
let proto;
1211+
if (newTarget !== undefined) {
1212+
proto = newTarget.prototype;
1213+
}
1214+
1215+
if (!utils.isObject(proto)) {
1216+
proto = globalObject[ctorRegistrySymbol]["${this.name}"].prototype;
1217+
}
1218+
1219+
return Object.create(proto);
12121220
}
12131221
`;
12141222

@@ -1276,8 +1284,8 @@ class Interface {
12761284
return wrapper;
12771285
};
12781286
1279-
exports.new = globalObject => {
1280-
${this.isLegacyPlatformObj ? "let" : "const"} wrapper = makeWrapper(globalObject);
1287+
exports.new = (globalObject, newTarget) => {
1288+
${this.isLegacyPlatformObj ? "let" : "const"} wrapper = makeWrapper(globalObject, newTarget);
12811289
12821290
exports._internalSetup(wrapper, globalObject);
12831291
Object.defineProperty(wrapper, implSymbol, {

0 commit comments

Comments
 (0)