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

Implement [LegacyLenientSetter] and fix [LegacyLenientThis] #210

Merged
merged 1 commit into from
Apr 26, 2020
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ webidl2js is implementing an ever-growing subset of the Web IDL specification. S
- `[EnforceRange]`
- `[Exposed]`
- `[LegacyLenientThis]`
- `[LegacyLenientSetter]`
- `[LegacyNoInterfaceObject]`
- `[LegacyNullToEmptyString]`
- `[LegacyOverrideBuiltins]`
Expand All @@ -494,7 +495,6 @@ Notable missing features include:
- `[Default]` (for `toJSON()` operations)
- `[Global]`'s various consequences, including the named properties object and `[[SetPrototypeOf]]`
- `[LegacyFactoryFunction]`
- `[LegacyLenientSetter]`
- `[LegacyNamespace]`
- `[LegacyTreatNonObjectAsNull]`
- `[SecureContext]`
Expand Down
70 changes: 51 additions & 19 deletions lib/constructs/attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,16 @@ class Attribute {
setterBody = processedOutput.set;
}

if (utils.getExtAttr(this.idl.extAttrs, "LegacyLenientThis")) {
brandCheck = "";
const replaceable = utils.getExtAttr(this.idl.extAttrs, "Replaceable");
const legacyLenientSetter = utils.getExtAttr(this.idl.extAttrs, "LegacyLenientSetter");
const legacyLenientThis = utils.getExtAttr(this.idl.extAttrs, "LegacyLenientThis");

if (legacyLenientThis) {
brandCheck = `
if (!exports.is(esValue)) {
return;
}
`;
}

if (sameObject) {
Expand Down Expand Up @@ -94,23 +102,47 @@ class Attribute {
${idlConversion}
${setterBody}
`, "set", { configurable });
} else if (utils.getExtAttr(this.idl.extAttrs, "PutForwards")) {
addMethod(this.idl.name, ["V"], `
const esValue = this !== null && this !== undefined ? this : globalObject;
${brandCheck}
this.${this.idl.name}.${utils.getExtAttr(this.idl.extAttrs, "PutForwards").rhs.value} = V;
`, "set", { configurable });
} else if (utils.getExtAttr(this.idl.extAttrs, "Replaceable")) {
addMethod(this.idl.name, ["V"], `
const esValue = this !== null && this !== undefined ? this : globalObject;
${brandCheck}
Object.defineProperty(esValue, "${this.idl.name}", {
configurable: true,
enumerable: true,
value: V,
writable: true
});
`, "set", { configurable });
} else {
const putForwards = utils.getExtAttr(this.idl.extAttrs, "PutForwards");

setterBody = "";
if (replaceable) {
if (legacyLenientThis) {
brandCheck = "";
}

setterBody = `
Object.defineProperty(esValue, "${this.idl.name}", {
configurable: true,
enumerable: true,
value: V,
writable: true
});
`;
} else if (putForwards) {
setterBody = `
const Q = esValue["${this.idl.name}"];
if (!utils.isObject(Q)) {
throw new TypeError("Property '${this.idl.name}' is not an object");
}
`;

// WebIDL calls the `Set` abstract operation with a `Throw` value of `false`:
setterBody += `Reflect.set(Q, "${putForwards.rhs.value}", V);`;
}

if (setterBody) {
addMethod(this.idl.name, ["V"], `
const esValue = this !== null && this !== undefined ? this : globalObject;
${brandCheck}
${setterBody}
`, "set", { configurable });
} else if (legacyLenientSetter) {
addMethod(this.idl.name, ["V"], legacyLenientThis ? "" : `
const esValue = this !== null && this !== undefined ? this : globalObject;
${brandCheck}
`, "set", { configurable });
}
}

if (!this.static && this.idl.special === "stringifier") {
Expand Down
Loading