Skip to content

Commit 244d283

Browse files
committed
refactor: Move named property visibility algorithm to utils.js
1 parent 838642d commit 244d283

File tree

3 files changed

+57
-39
lines changed

3 files changed

+57
-39
lines changed

lib/constructs/interface.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ class Interface {
625625
if (this.overrideBuiltins) {
626626
conditions.push(`!utils.hasOwn(${O}, ${P})`);
627627
} else {
628-
conditions.push(`!(${P} in ${O})`);
628+
conditions.push(`utils.isNamedPropertyVisible(${P}, ${O})`);
629629
}
630630
return conditions.join(" && ");
631631
}
@@ -835,7 +835,7 @@ class Interface {
835835
const func = this.namedGetter.name ? `.${this.namedGetter.name}` : "[utils.namedGet]";
836836
const enumerable = !utils.getExtAttr(this.idl.extAttrs, "LegacyUnenumerableNamedProperties");
837837
let preamble = "";
838-
const conditions = [];
838+
const conditions = ["!ignoreNamedProps"];
839839
if (utils.getExtAttr(this.namedGetter.extAttrs, "WebIDL2JSValueAsUnsupported")) {
840840
this.str += `
841841
const namedValue = target[impl]${func}(P);
@@ -848,7 +848,6 @@ class Interface {
848848
`;
849849
conditions.push(this.namedPropertyVisible("P", "target", false));
850850
}
851-
conditions.push("!ignoreNamedProps");
852851
this.str += `
853852
if (${conditions.join(" && ")}) {
854853
${preamble}
@@ -1136,10 +1135,9 @@ class Interface {
11361135
this.str += `
11371136
11381137
const NamedPropertiesObject = new Proxy(
1139-
Object.create(
1140-
globalObject.${idl.inheritance ? idl.inheritance : "Object"}.prototype,
1141-
{ [Symbol.toStringTag]: { value: "${name}Properties", configurable: true } }
1142-
),
1138+
Object.create(globalObject.${idl.inheritance ? idl.inheritance : "Object"}.prototype, {
1139+
[Symbol.toStringTag]: { value: "${name}Properties", configurable: true },
1140+
}),
11431141
{
11441142
`;
11451143

@@ -1260,6 +1258,7 @@ class Interface {
12601258
`;
12611259

12621260
this.str += `
1261+
utils.registerNamedPropertiesObject(NamedPropertiesObject);
12631262
Object.setPrototypeOf(${name}.prototype, NamedPropertiesObject);
12641263
`;
12651264
}

lib/output/utils.js

+17
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,21 @@ function isArrayBuffer(value) {
7474
}
7575
}
7676

77+
const namedPropertiesObjects = new WeakSet();
78+
function isNamedPropertyVisible(P, O) {
79+
while (O !== null) {
80+
if (!namedPropertiesObjects.has(O) && hasOwn(O, P)) {
81+
return false;
82+
}
83+
O = Object.getPrototypeOf(O);
84+
}
85+
return true;
86+
}
87+
88+
function registerNamedPropertiesObject(obj) {
89+
namedPropertiesObjects.add(obj);
90+
}
91+
7792
const supportsPropertyIndex = Symbol("supports property index");
7893
const supportedPropertyIndices = Symbol("supported property indices");
7994
const supportsPropertyName = Symbol("supports property name");
@@ -101,6 +116,8 @@ module.exports = exports = {
101116
IteratorPrototype,
102117
isArrayBuffer,
103118
isArrayIndexPropName,
119+
registerNamedPropertiesObject,
120+
isNamedPropertyVisible,
104121
supportsPropertyIndex,
105122
supportedPropertyIndices,
106123
supportsPropertyName,

test/__snapshots__/test.js.snap

+34-32
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ const proxyHandler = {
465465
const keys = new Set();
466466

467467
for (const key of target[impl][utils.supportedPropertyNames]) {
468-
if (!(key in target)) {
468+
if (utils.isNamedPropertyVisible(key, target)) {
469469
keys.add(\`\${key}\`);
470470
}
471471
}
@@ -482,7 +482,7 @@ const proxyHandler = {
482482
}
483483
let ignoreNamedProps = false;
484484

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

488488
return {
@@ -594,7 +594,7 @@ const proxyHandler = {
594594
return Reflect.deleteProperty(target, P);
595595
}
596596

597-
if (target[impl][utils.supportsPropertyName](P) && !(P in target)) {
597+
if (target[impl][utils.supportsPropertyName](P) && utils.isNamedPropertyVisible(P, target)) {
598598
CEReactions.preSteps(globalObject);
599599
try {
600600
target[impl][utils.namedDelete](P);
@@ -3398,7 +3398,7 @@ const proxyHandler = {
33983398
const keys = new Set();
33993399

34003400
for (const key of target[impl][utils.supportedPropertyNames]) {
3401-
if (!(key in target)) {
3401+
if (utils.isNamedPropertyVisible(key, target)) {
34023402
keys.add(\`\${key}\`);
34033403
}
34043404
}
@@ -3415,7 +3415,7 @@ const proxyHandler = {
34153415
}
34163416
let ignoreNamedProps = false;
34173417

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

34213421
return {
@@ -3507,7 +3507,7 @@ const proxyHandler = {
35073507
return Reflect.deleteProperty(target, P);
35083508
}
35093509

3510-
if (target[impl][utils.supportsPropertyName](P) && !(P in target)) {
3510+
if (target[impl][utils.supportsPropertyName](P) && utils.isNamedPropertyVisible(P, target)) {
35113511
target[impl].removeItem(P);
35123512
return true;
35133513
}
@@ -5907,7 +5907,7 @@ const proxyHandler = {
59075907
}
59085908

59095909
for (const key of target[impl][utils.supportedPropertyNames]) {
5910-
if (!(key in target)) {
5910+
if (utils.isNamedPropertyVisible(key, target)) {
59115911
keys.add(\`\${key}\`);
59125912
}
59135913
}
@@ -5940,7 +5940,7 @@ const proxyHandler = {
59405940

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

5943-
if (namedValue !== null && !(P in target) && !ignoreNamedProps) {
5943+
if (!ignoreNamedProps && namedValue !== null && utils.isNamedPropertyVisible(P, target)) {
59445944
return {
59455945
writable: false,
59465946
enumerable: false,
@@ -6035,7 +6035,7 @@ const proxyHandler = {
60356035
return !(target[impl].item(index) !== undefined);
60366036
}
60376037

6038-
if (target[impl].namedItem(P) !== null && !(P in target)) {
6038+
if (target[impl].namedItem(P) !== null && utils.isNamedPropertyVisible(P, target)) {
60396039
return false;
60406040
}
60416041

@@ -6219,7 +6219,7 @@ const proxyHandler = {
62196219
}
62206220

62216221
for (const key of target[impl][utils.supportedPropertyNames]) {
6222-
if (!(key in target)) {
6222+
if (utils.isNamedPropertyVisible(key, target)) {
62236223
keys.add(\`\${key}\`);
62246224
}
62256225
}
@@ -6252,7 +6252,7 @@ const proxyHandler = {
62526252

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

6255-
if (namedValue !== null && !(P in target) && !ignoreNamedProps) {
6255+
if (!ignoreNamedProps && namedValue !== null && utils.isNamedPropertyVisible(P, target)) {
62566256
return {
62576257
writable: true,
62586258
enumerable: true,
@@ -6376,7 +6376,7 @@ const proxyHandler = {
63766376
return !(target[impl].item(index) !== undefined);
63776377
}
63786378

6379-
if (target[impl].namedItem(P) !== null && !(P in target)) {
6379+
if (target[impl].namedItem(P) !== null && utils.isNamedPropertyVisible(P, target)) {
63806380
return false;
63816381
}
63826382

@@ -6947,7 +6947,7 @@ const proxyHandler = {
69476947
const keys = new Set();
69486948

69496949
for (const key of target[impl][utils.supportedPropertyNames]) {
6950-
if (!(key in target)) {
6950+
if (utils.isNamedPropertyVisible(key, target)) {
69516951
keys.add(\`\${key}\`);
69526952
}
69536953
}
@@ -6964,7 +6964,7 @@ const proxyHandler = {
69646964
}
69656965
let ignoreNamedProps = false;
69666966

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

69706970
return {
@@ -7068,7 +7068,7 @@ const proxyHandler = {
70687068
return Reflect.deleteProperty(target, P);
70697069
}
70707070

7071-
if (target[impl][utils.supportsPropertyName](P) && !(P in target)) {
7071+
if (target[impl][utils.supportsPropertyName](P) && utils.isNamedPropertyVisible(P, target)) {
70727072
return false;
70737073
}
70747074

@@ -7646,7 +7646,7 @@ exports.install = function install(globalObject) {
76467646
return Reflect.getOwnPropertyDescriptor(target, P);
76477647
}
76487648

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

76527652
return {
@@ -7678,6 +7678,7 @@ exports.install = function install(globalObject) {
76787678
}
76797679
);
76807680

7681+
utils.registerNamedPropertiesObject(NamedPropertiesObject);
76817682
Object.setPrototypeOf(Window.prototype, NamedPropertiesObject);
76827683

76837684
if (globalObject[ctorRegistry] === undefined) {
@@ -8256,7 +8257,7 @@ const proxyHandler = {
82568257
const keys = new Set();
82578258

82588259
for (const key of target[impl][utils.supportedPropertyNames]) {
8259-
if (!(key in target)) {
8260+
if (utils.isNamedPropertyVisible(key, target)) {
82608261
keys.add(\`\${key}\`);
82618262
}
82628263
}
@@ -8273,7 +8274,7 @@ const proxyHandler = {
82738274
}
82748275
let ignoreNamedProps = false;
82758276

8276-
if (target[impl][utils.supportsPropertyName](P) && !(P in target) && !ignoreNamedProps) {
8277+
if (!ignoreNamedProps && target[impl][utils.supportsPropertyName](P) && utils.isNamedPropertyVisible(P, target)) {
82778278
const namedValue = target[impl][utils.namedGet](P);
82788279

82798280
return {
@@ -8375,7 +8376,7 @@ const proxyHandler = {
83758376
return Reflect.deleteProperty(target, P);
83768377
}
83778378

8378-
if (target[impl][utils.supportsPropertyName](P) && !(P in target)) {
8379+
if (target[impl][utils.supportsPropertyName](P) && utils.isNamedPropertyVisible(P, target)) {
83798380
target[impl][utils.namedDelete](P);
83808381
return true;
83818382
}
@@ -11173,7 +11174,7 @@ const proxyHandler = {
1117311174
const keys = new Set();
1117411175

1117511176
for (const key of target[impl][utils.supportedPropertyNames]) {
11176-
if (!(key in target)) {
11177+
if (utils.isNamedPropertyVisible(key, target)) {
1117711178
keys.add(\`\${key}\`);
1117811179
}
1117911180
}
@@ -11190,7 +11191,7 @@ const proxyHandler = {
1119011191
}
1119111192
let ignoreNamedProps = false;
1119211193

11193-
if (target[impl][utils.supportsPropertyName](P) && !(P in target) && !ignoreNamedProps) {
11194+
if (!ignoreNamedProps && target[impl][utils.supportsPropertyName](P) && utils.isNamedPropertyVisible(P, target)) {
1119411195
const namedValue = target[impl].getItem(P);
1119511196

1119611197
return {
@@ -11282,7 +11283,7 @@ const proxyHandler = {
1128211283
return Reflect.deleteProperty(target, P);
1128311284
}
1128411285

11285-
if (target[impl][utils.supportsPropertyName](P) && !(P in target)) {
11286+
if (target[impl][utils.supportsPropertyName](P) && utils.isNamedPropertyVisible(P, target)) {
1128611287
target[impl].removeItem(P);
1128711288
return true;
1128811289
}
@@ -13682,7 +13683,7 @@ const proxyHandler = {
1368213683
}
1368313684

1368413685
for (const key of target[impl][utils.supportedPropertyNames]) {
13685-
if (!(key in target)) {
13686+
if (utils.isNamedPropertyVisible(key, target)) {
1368613687
keys.add(\`\${key}\`);
1368713688
}
1368813689
}
@@ -13715,7 +13716,7 @@ const proxyHandler = {
1371513716

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

13718-
if (namedValue !== null && !(P in target) && !ignoreNamedProps) {
13719+
if (!ignoreNamedProps && namedValue !== null && utils.isNamedPropertyVisible(P, target)) {
1371913720
return {
1372013721
writable: false,
1372113722
enumerable: false,
@@ -13810,7 +13811,7 @@ const proxyHandler = {
1381013811
return !(target[impl].item(index) !== undefined);
1381113812
}
1381213813

13813-
if (target[impl].namedItem(P) !== null && !(P in target)) {
13814+
if (target[impl].namedItem(P) !== null && utils.isNamedPropertyVisible(P, target)) {
1381413815
return false;
1381513816
}
1381613817

@@ -13994,7 +13995,7 @@ const proxyHandler = {
1399413995
}
1399513996

1399613997
for (const key of target[impl][utils.supportedPropertyNames]) {
13997-
if (!(key in target)) {
13998+
if (utils.isNamedPropertyVisible(key, target)) {
1399813999
keys.add(\`\${key}\`);
1399914000
}
1400014001
}
@@ -14027,7 +14028,7 @@ const proxyHandler = {
1402714028

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

14030-
if (namedValue !== null && !(P in target) && !ignoreNamedProps) {
14031+
if (!ignoreNamedProps && namedValue !== null && utils.isNamedPropertyVisible(P, target)) {
1403114032
return {
1403214033
writable: true,
1403314034
enumerable: true,
@@ -14151,7 +14152,7 @@ const proxyHandler = {
1415114152
return !(target[impl].item(index) !== undefined);
1415214153
}
1415314154

14154-
if (target[impl].namedItem(P) !== null && !(P in target)) {
14155+
if (target[impl].namedItem(P) !== null && utils.isNamedPropertyVisible(P, target)) {
1415514156
return false;
1415614157
}
1415714158

@@ -14722,7 +14723,7 @@ const proxyHandler = {
1472214723
const keys = new Set();
1472314724

1472414725
for (const key of target[impl][utils.supportedPropertyNames]) {
14725-
if (!(key in target)) {
14726+
if (utils.isNamedPropertyVisible(key, target)) {
1472614727
keys.add(\`\${key}\`);
1472714728
}
1472814729
}
@@ -14739,7 +14740,7 @@ const proxyHandler = {
1473914740
}
1474014741
let ignoreNamedProps = false;
1474114742

14742-
if (target[impl][utils.supportsPropertyName](P) && !(P in target) && !ignoreNamedProps) {
14743+
if (!ignoreNamedProps && target[impl][utils.supportsPropertyName](P) && utils.isNamedPropertyVisible(P, target)) {
1474314744
const namedValue = target[impl][utils.namedGet](P);
1474414745

1474514746
return {
@@ -14843,7 +14844,7 @@ const proxyHandler = {
1484314844
return Reflect.deleteProperty(target, P);
1484414845
}
1484514846

14846-
if (target[impl][utils.supportsPropertyName](P) && !(P in target)) {
14847+
if (target[impl][utils.supportsPropertyName](P) && utils.isNamedPropertyVisible(P, target)) {
1484714848
return false;
1484814849
}
1484914850

@@ -15421,7 +15422,7 @@ exports.install = function install(globalObject) {
1542115422
return Reflect.getOwnPropertyDescriptor(target, P);
1542215423
}
1542315424

15424-
if (object[impl][utils.supportsPropertyName](P) && !(P in object)) {
15425+
if (object[impl][utils.supportsPropertyName](P) && utils.isNamedPropertyVisible(P, object)) {
1542515426
const namedValue = object[impl][utils.namedGet](P);
1542615427

1542715428
return {
@@ -15453,6 +15454,7 @@ exports.install = function install(globalObject) {
1545315454
}
1545415455
);
1545515456

15457+
utils.registerNamedPropertiesObject(NamedPropertiesObject);
1545615458
Object.setPrototypeOf(Window.prototype, NamedPropertiesObject);
1545715459

1545815460
if (globalObject[ctorRegistry] === undefined) {

0 commit comments

Comments
 (0)