Skip to content

Commit

Permalink
refactor: Move named property visibility algorithm to utils.js
Browse files Browse the repository at this point in the history
  • Loading branch information
ExE-Boss committed Feb 13, 2020
1 parent 838642d commit 30091e8
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 39 deletions.
13 changes: 6 additions & 7 deletions lib/constructs/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ class Interface {
if (this.overrideBuiltins) {
conditions.push(`!utils.hasOwn(${O}, ${P})`);
} else {
conditions.push(`!(${P} in ${O})`);
conditions.push(`utils.isNamedPropertyVisible(${P}, ${O})`);
}
return conditions.join(" && ");
}
Expand Down Expand Up @@ -835,7 +835,7 @@ class Interface {
const func = this.namedGetter.name ? `.${this.namedGetter.name}` : "[utils.namedGet]";
const enumerable = !utils.getExtAttr(this.idl.extAttrs, "LegacyUnenumerableNamedProperties");
let preamble = "";
const conditions = [];
const conditions = ["!ignoreNamedProps"];
if (utils.getExtAttr(this.namedGetter.extAttrs, "WebIDL2JSValueAsUnsupported")) {
this.str += `
const namedValue = target[impl]${func}(P);
Expand All @@ -848,7 +848,6 @@ class Interface {
`;
conditions.push(this.namedPropertyVisible("P", "target", false));
}
conditions.push("!ignoreNamedProps");
this.str += `
if (${conditions.join(" && ")}) {
${preamble}
Expand Down Expand Up @@ -1136,10 +1135,9 @@ class Interface {
this.str += `
const NamedPropertiesObject = new Proxy(
Object.create(
globalObject.${idl.inheritance ? idl.inheritance : "Object"}.prototype,
{ [Symbol.toStringTag]: { value: "${name}Properties", configurable: true } }
),
Object.create(globalObject.${idl.inheritance ? idl.inheritance : "Object"}.prototype, {
[Symbol.toStringTag]: { value: "${name}Properties", configurable: true },
}),
{
`;

Expand Down Expand Up @@ -1260,6 +1258,7 @@ class Interface {
`;

this.str += `
utils.registerNamedPropertiesObject(NamedPropertiesObject);
Object.setPrototypeOf(${name}.prototype, NamedPropertiesObject);
`;
}
Expand Down
17 changes: 17 additions & 0 deletions lib/output/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ function isArrayBuffer(value) {
}
}

const namedPropertiesObjects = new WeakSet();
function registerNamedPropertiesObject(obj) {
namedPropertiesObjects.add(obj);
}

function isNamedPropertyVisible(P, O) {
while (O !== null) {
if (!namedPropertiesObjects.has(O) && hasOwn(O, P)) {
return false;
}
O = Object.getPrototypeOf(O);
}
return true;
}

const supportsPropertyIndex = Symbol("supports property index");
const supportedPropertyIndices = Symbol("supported property indices");
const supportsPropertyName = Symbol("supports property name");
Expand Down Expand Up @@ -101,6 +116,8 @@ module.exports = exports = {
IteratorPrototype,
isArrayBuffer,
isArrayIndexPropName,
registerNamedPropertiesObject,
isNamedPropertyVisible,
supportsPropertyIndex,
supportedPropertyIndices,
supportsPropertyName,
Expand Down
66 changes: 34 additions & 32 deletions test/__snapshots__/test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ const proxyHandler = {
const keys = new Set();

for (const key of target[impl][utils.supportedPropertyNames]) {
if (!(key in target)) {
if (utils.isNamedPropertyVisible(key, target)) {
keys.add(\`\${key}\`);
}
}
Expand All @@ -482,7 +482,7 @@ const proxyHandler = {
}
let ignoreNamedProps = false;

if (target[impl][utils.supportsPropertyName](P) && !(P in target) && !ignoreNamedProps) {
if (!ignoreNamedProps && target[impl][utils.supportsPropertyName](P) && utils.isNamedPropertyVisible(P, target)) {
const namedValue = target[impl][utils.namedGet](P);

return {
Expand Down Expand Up @@ -594,7 +594,7 @@ const proxyHandler = {
return Reflect.deleteProperty(target, P);
}

if (target[impl][utils.supportsPropertyName](P) && !(P in target)) {
if (target[impl][utils.supportsPropertyName](P) && utils.isNamedPropertyVisible(P, target)) {
CEReactions.preSteps(globalObject);
try {
target[impl][utils.namedDelete](P);
Expand Down Expand Up @@ -3398,7 +3398,7 @@ const proxyHandler = {
const keys = new Set();

for (const key of target[impl][utils.supportedPropertyNames]) {
if (!(key in target)) {
if (utils.isNamedPropertyVisible(key, target)) {
keys.add(\`\${key}\`);
}
}
Expand All @@ -3415,7 +3415,7 @@ const proxyHandler = {
}
let ignoreNamedProps = false;

if (target[impl][utils.supportsPropertyName](P) && !(P in target) && !ignoreNamedProps) {
if (!ignoreNamedProps && target[impl][utils.supportsPropertyName](P) && utils.isNamedPropertyVisible(P, target)) {
const namedValue = target[impl].getItem(P);

return {
Expand Down Expand Up @@ -3507,7 +3507,7 @@ const proxyHandler = {
return Reflect.deleteProperty(target, P);
}

if (target[impl][utils.supportsPropertyName](P) && !(P in target)) {
if (target[impl][utils.supportsPropertyName](P) && utils.isNamedPropertyVisible(P, target)) {
target[impl].removeItem(P);
return true;
}
Expand Down Expand Up @@ -5907,7 +5907,7 @@ const proxyHandler = {
}

for (const key of target[impl][utils.supportedPropertyNames]) {
if (!(key in target)) {
if (utils.isNamedPropertyVisible(key, target)) {
keys.add(\`\${key}\`);
}
}
Expand Down Expand Up @@ -5940,7 +5940,7 @@ const proxyHandler = {

const namedValue = target[impl].namedItem(P);

if (namedValue !== null && !(P in target) && !ignoreNamedProps) {
if (!ignoreNamedProps && namedValue !== null && utils.isNamedPropertyVisible(P, target)) {
return {
writable: false,
enumerable: false,
Expand Down Expand Up @@ -6035,7 +6035,7 @@ const proxyHandler = {
return !(target[impl].item(index) !== undefined);
}

if (target[impl].namedItem(P) !== null && !(P in target)) {
if (target[impl].namedItem(P) !== null && utils.isNamedPropertyVisible(P, target)) {
return false;
}

Expand Down Expand Up @@ -6219,7 +6219,7 @@ const proxyHandler = {
}

for (const key of target[impl][utils.supportedPropertyNames]) {
if (!(key in target)) {
if (utils.isNamedPropertyVisible(key, target)) {
keys.add(\`\${key}\`);
}
}
Expand Down Expand Up @@ -6252,7 +6252,7 @@ const proxyHandler = {

const namedValue = target[impl].namedItem(P);

if (namedValue !== null && !(P in target) && !ignoreNamedProps) {
if (!ignoreNamedProps && namedValue !== null && utils.isNamedPropertyVisible(P, target)) {
return {
writable: true,
enumerable: true,
Expand Down Expand Up @@ -6376,7 +6376,7 @@ const proxyHandler = {
return !(target[impl].item(index) !== undefined);
}

if (target[impl].namedItem(P) !== null && !(P in target)) {
if (target[impl].namedItem(P) !== null && utils.isNamedPropertyVisible(P, target)) {
return false;
}

Expand Down Expand Up @@ -6947,7 +6947,7 @@ const proxyHandler = {
const keys = new Set();

for (const key of target[impl][utils.supportedPropertyNames]) {
if (!(key in target)) {
if (utils.isNamedPropertyVisible(key, target)) {
keys.add(\`\${key}\`);
}
}
Expand All @@ -6964,7 +6964,7 @@ const proxyHandler = {
}
let ignoreNamedProps = false;

if (target[impl][utils.supportsPropertyName](P) && !(P in target) && !ignoreNamedProps) {
if (!ignoreNamedProps && target[impl][utils.supportsPropertyName](P) && utils.isNamedPropertyVisible(P, target)) {
const namedValue = target[impl][utils.namedGet](P);

return {
Expand Down Expand Up @@ -7068,7 +7068,7 @@ const proxyHandler = {
return Reflect.deleteProperty(target, P);
}

if (target[impl][utils.supportsPropertyName](P) && !(P in target)) {
if (target[impl][utils.supportsPropertyName](P) && utils.isNamedPropertyVisible(P, target)) {
return false;
}

Expand Down Expand Up @@ -7646,7 +7646,7 @@ exports.install = function install(globalObject) {
return Reflect.getOwnPropertyDescriptor(target, P);
}

if (object[impl][utils.supportsPropertyName](P) && !(P in object)) {
if (object[impl][utils.supportsPropertyName](P) && utils.isNamedPropertyVisible(P, object)) {
const namedValue = object[impl][utils.namedGet](P);

return {
Expand Down Expand Up @@ -7678,6 +7678,7 @@ exports.install = function install(globalObject) {
}
);

utils.registerNamedPropertiesObject(NamedPropertiesObject);
Object.setPrototypeOf(Window.prototype, NamedPropertiesObject);

if (globalObject[ctorRegistry] === undefined) {
Expand Down Expand Up @@ -8256,7 +8257,7 @@ const proxyHandler = {
const keys = new Set();

for (const key of target[impl][utils.supportedPropertyNames]) {
if (!(key in target)) {
if (utils.isNamedPropertyVisible(key, target)) {
keys.add(\`\${key}\`);
}
}
Expand All @@ -8273,7 +8274,7 @@ const proxyHandler = {
}
let ignoreNamedProps = false;

if (target[impl][utils.supportsPropertyName](P) && !(P in target) && !ignoreNamedProps) {
if (!ignoreNamedProps && target[impl][utils.supportsPropertyName](P) && utils.isNamedPropertyVisible(P, target)) {
const namedValue = target[impl][utils.namedGet](P);

return {
Expand Down Expand Up @@ -8375,7 +8376,7 @@ const proxyHandler = {
return Reflect.deleteProperty(target, P);
}

if (target[impl][utils.supportsPropertyName](P) && !(P in target)) {
if (target[impl][utils.supportsPropertyName](P) && utils.isNamedPropertyVisible(P, target)) {
target[impl][utils.namedDelete](P);
return true;
}
Expand Down Expand Up @@ -11173,7 +11174,7 @@ const proxyHandler = {
const keys = new Set();

for (const key of target[impl][utils.supportedPropertyNames]) {
if (!(key in target)) {
if (utils.isNamedPropertyVisible(key, target)) {
keys.add(\`\${key}\`);
}
}
Expand All @@ -11190,7 +11191,7 @@ const proxyHandler = {
}
let ignoreNamedProps = false;

if (target[impl][utils.supportsPropertyName](P) && !(P in target) && !ignoreNamedProps) {
if (!ignoreNamedProps && target[impl][utils.supportsPropertyName](P) && utils.isNamedPropertyVisible(P, target)) {
const namedValue = target[impl].getItem(P);

return {
Expand Down Expand Up @@ -11282,7 +11283,7 @@ const proxyHandler = {
return Reflect.deleteProperty(target, P);
}

if (target[impl][utils.supportsPropertyName](P) && !(P in target)) {
if (target[impl][utils.supportsPropertyName](P) && utils.isNamedPropertyVisible(P, target)) {
target[impl].removeItem(P);
return true;
}
Expand Down Expand Up @@ -13682,7 +13683,7 @@ const proxyHandler = {
}

for (const key of target[impl][utils.supportedPropertyNames]) {
if (!(key in target)) {
if (utils.isNamedPropertyVisible(key, target)) {
keys.add(\`\${key}\`);
}
}
Expand Down Expand Up @@ -13715,7 +13716,7 @@ const proxyHandler = {

const namedValue = target[impl].namedItem(P);

if (namedValue !== null && !(P in target) && !ignoreNamedProps) {
if (!ignoreNamedProps && namedValue !== null && utils.isNamedPropertyVisible(P, target)) {
return {
writable: false,
enumerable: false,
Expand Down Expand Up @@ -13810,7 +13811,7 @@ const proxyHandler = {
return !(target[impl].item(index) !== undefined);
}

if (target[impl].namedItem(P) !== null && !(P in target)) {
if (target[impl].namedItem(P) !== null && utils.isNamedPropertyVisible(P, target)) {
return false;
}

Expand Down Expand Up @@ -13994,7 +13995,7 @@ const proxyHandler = {
}

for (const key of target[impl][utils.supportedPropertyNames]) {
if (!(key in target)) {
if (utils.isNamedPropertyVisible(key, target)) {
keys.add(\`\${key}\`);
}
}
Expand Down Expand Up @@ -14027,7 +14028,7 @@ const proxyHandler = {

const namedValue = target[impl].namedItem(P);

if (namedValue !== null && !(P in target) && !ignoreNamedProps) {
if (!ignoreNamedProps && namedValue !== null && utils.isNamedPropertyVisible(P, target)) {
return {
writable: true,
enumerable: true,
Expand Down Expand Up @@ -14151,7 +14152,7 @@ const proxyHandler = {
return !(target[impl].item(index) !== undefined);
}

if (target[impl].namedItem(P) !== null && !(P in target)) {
if (target[impl].namedItem(P) !== null && utils.isNamedPropertyVisible(P, target)) {
return false;
}

Expand Down Expand Up @@ -14722,7 +14723,7 @@ const proxyHandler = {
const keys = new Set();

for (const key of target[impl][utils.supportedPropertyNames]) {
if (!(key in target)) {
if (utils.isNamedPropertyVisible(key, target)) {
keys.add(\`\${key}\`);
}
}
Expand All @@ -14739,7 +14740,7 @@ const proxyHandler = {
}
let ignoreNamedProps = false;

if (target[impl][utils.supportsPropertyName](P) && !(P in target) && !ignoreNamedProps) {
if (!ignoreNamedProps && target[impl][utils.supportsPropertyName](P) && utils.isNamedPropertyVisible(P, target)) {
const namedValue = target[impl][utils.namedGet](P);

return {
Expand Down Expand Up @@ -14843,7 +14844,7 @@ const proxyHandler = {
return Reflect.deleteProperty(target, P);
}

if (target[impl][utils.supportsPropertyName](P) && !(P in target)) {
if (target[impl][utils.supportsPropertyName](P) && utils.isNamedPropertyVisible(P, target)) {
return false;
}

Expand Down Expand Up @@ -15421,7 +15422,7 @@ exports.install = function install(globalObject) {
return Reflect.getOwnPropertyDescriptor(target, P);
}

if (object[impl][utils.supportsPropertyName](P) && !(P in object)) {
if (object[impl][utils.supportsPropertyName](P) && utils.isNamedPropertyVisible(P, object)) {
const namedValue = object[impl][utils.namedGet](P);

return {
Expand Down Expand Up @@ -15453,6 +15454,7 @@ exports.install = function install(globalObject) {
}
);

utils.registerNamedPropertiesObject(NamedPropertiesObject);
Object.setPrototypeOf(Window.prototype, NamedPropertiesObject);

if (globalObject[ctorRegistry] === undefined) {
Expand Down

0 comments on commit 30091e8

Please sign in to comment.