Skip to content

Commit fdb0c75

Browse files
committed
fix: Use correct [ImplicitThis] behaviour
1 parent f365d33 commit fdb0c75

File tree

3 files changed

+975
-556
lines changed

3 files changed

+975
-556
lines changed

lib/constructs/attribute.js

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,15 @@ class Attribute {
2323

2424
const onInstance = utils.isOnInstance(this.idl, this.interface.idl);
2525

26-
let objName = `this`;
27-
if (onInstance) { // we're in a setup method
28-
objName = `obj`;
29-
}
30-
let brandCheck = this.interface.isGlobal ? `
31-
if (this !== null && this !== undefined && !exports.is(this)) {
32-
throw new TypeError("Illegal invocation");
33-
}
34-
` : `
35-
if (!this || !exports.is(this)) {
26+
let brandCheck = `
27+
if (!exports.is(esValue)) {
3628
throw new TypeError("Illegal invocation");
3729
}
3830
`;
39-
let getterBody = `return utils.tryWrapperForImpl(${objName}[impl]["${this.idl.name}"]);`;
40-
let setterBody = `${objName}[impl]["${this.idl.name}"] = V;`;
31+
let getterBody = `return utils.tryWrapperForImpl(esValue[impl]["${this.idl.name}"]);`;
32+
let setterBody = `esValue[impl]["${this.idl.name}"] = V;`;
4133
if (conversions[this.idl.idlType.idlType]) {
42-
getterBody = `return ${objName}[impl]["${this.idl.name}"];`;
34+
getterBody = `return esValue[impl]["${this.idl.name}"];`;
4335
}
4436

4537
const addMethod = this.static ?
@@ -55,8 +47,8 @@ class Attribute {
5547
throw new Error("Unknown reflector type: " + this.idl.idlType.idlType);
5648
}
5749
const attrName = shouldReflect.rhs && shouldReflect.rhs.value.replace(/_/g, "-") || this.idl.name;
58-
getterBody = reflector[this.idl.idlType.idlType].get(objName, attrName.toLowerCase());
59-
setterBody = reflector[this.idl.idlType.idlType].set(objName, attrName.toLowerCase());
50+
getterBody = reflector[this.idl.idlType.idlType].get("esValue", attrName.toLowerCase());
51+
setterBody = reflector[this.idl.idlType.idlType].set("esValue", attrName.toLowerCase());
6052
}
6153

6254
if (utils.getExtAttr(this.idl.extAttrs, "LenientThis")) {
@@ -75,6 +67,7 @@ class Attribute {
7567
}
7668

7769
addMethod(this.idl.name, [], `
70+
const esValue = this !== null && this !== undefined ? this : globalObject;
7871
${brandCheck}
7972
${getterBody}
8073
`, "get", { configurable });
@@ -99,19 +92,22 @@ class Attribute {
9992
}
10093

10194
addMethod(this.idl.name, ["V"], `
95+
const esValue = this !== null && this !== undefined ? this : globalObject;
10296
${brandCheck}
10397
${idlConversion}
10498
${setterBody}
10599
`, "set", { configurable });
106100
} else if (utils.getExtAttr(this.idl.extAttrs, "PutForwards")) {
107101
addMethod(this.idl.name, ["V"], `
102+
const esValue = this !== null && this !== undefined ? this : globalObject;
108103
${brandCheck}
109104
this.${this.idl.name}.${utils.getExtAttr(this.idl.extAttrs, "PutForwards").rhs.value} = V;
110105
`, "set", { configurable });
111106
} else if (utils.getExtAttr(this.idl.extAttrs, "Replaceable")) {
112107
addMethod(this.idl.name, ["V"], `
108+
const esValue = this !== null && this !== undefined ? this : globalObject;
113109
${brandCheck}
114-
Object.defineProperty(this, "${this.idl.name}", {
110+
Object.defineProperty(esValue, "${this.idl.name}", {
115111
configurable: true,
116112
enumerable: true,
117113
value: V,

lib/constructs/operation.js

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,15 @@ class Operation {
6161
const argNames = minOp.nameList;
6262

6363
if (!this.static) {
64-
if (this.interface.isGlobal) {
65-
str += `
66-
if (this !== null && this !== undefined && !exports.is(this)) {
67-
throw new TypeError("Illegal invocation");
68-
}
69-
`;
70-
} else {
71-
str += `
72-
if (!this || !exports.is(this)) {
73-
throw new TypeError("Illegal invocation");
74-
}
75-
`;
76-
}
64+
str += `
65+
const esValue = this !== null && this !== undefined ? this : globalObject;
66+
if (!exports.is(esValue)) {
67+
throw new TypeError("Illegal invocation");
68+
}
69+
`;
7770
}
7871

79-
const callOn = this.static ? "Impl.implementation" : `${this.interface.isGlobal ? "obj" : "this"}[impl]`;
72+
const callOn = this.static ? "Impl.implementation" : `esValue[impl]`;
8073
// In case of stringifiers, use the named implementation function rather than hardcoded "toString".
8174
// All overloads will have the same name, so pick the first one.
8275
const implFunc = this.idls[0].name || this.name;

0 commit comments

Comments
 (0)