Skip to content

Commit 2024ca2

Browse files
authored
Rename imported symbols to be more descriptive (#179)
1 parent d176871 commit 2024ca2

File tree

6 files changed

+1125
-1126
lines changed

6 files changed

+1125
-1126
lines changed

README.md

+5-6
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ will generate a JavaScript wrapper class file roughly like this:
2626

2727
```js
2828
const conversions = require("webidl-conversions");
29-
const impl = require("./utils.js").implSymbol;
30-
const ctorRegistry = require("./utils.js").ctorRegistrySymbol;
29+
const { implSymbol, ctorRegistrySymbol } = require("./utils.js");
3130

3231
const Impl = require("./SomeInterface-impl.js").implementation;
3332

@@ -56,7 +55,7 @@ class SomeInterface {
5655
context: "Failed to execute 'add' on 'SomeInterface': parameter 2"
5756
});
5857

59-
return this[impl].add(...args);
58+
return this[implSymbol].add(...args);
6059
}
6160
}
6261

@@ -66,13 +65,13 @@ Object.defineProperties(SomeInterface.prototype, {
6665
});
6766

6867
exports.create = (globalObject, constructorArgs = [], privateData = {}) => {
69-
const ctor = globalObject[ctorRegistry].SomeInterface;
68+
const ctor = globalObject[ctorRegistrySymbol].SomeInterface;
7069
const obj = Object.create(ctor.prototype);
71-
obj[impl] = new Impl(constructorArgs, privateData);
70+
obj[implSymbol] = new Impl(constructorArgs, privateData);
7271
return obj;
7372
};
7473

75-
exports.is = obj => obj && obj[impl] instanceof Impl;
74+
exports.is = obj => obj && obj[implSymbol] instanceof Impl;
7675
```
7776

7877
The above is a simplification of the actual generated code, but should give you some idea of what's going on. We bring your attention to a few points:

lib/constructs/attribute.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ class Attribute {
2828
throw new TypeError("Illegal invocation");
2929
}
3030
`;
31-
let getterBody = `return utils.tryWrapperForImpl(esValue[impl]["${this.idl.name}"]);`;
32-
let setterBody = `esValue[impl]["${this.idl.name}"] = V;`;
31+
let getterBody = `return utils.tryWrapperForImpl(esValue[implSymbol]["${this.idl.name}"]);`;
32+
let setterBody = `esValue[implSymbol]["${this.idl.name}"] = V;`;
3333
if (conversions[this.idl.idlType.idlType]) {
34-
getterBody = `return esValue[impl]["${this.idl.name}"];`;
34+
getterBody = `return esValue[implSymbol]["${this.idl.name}"];`;
3535
}
3636

3737
const addMethod = this.static ?
@@ -43,7 +43,7 @@ class Attribute {
4343
getterBody = `return Impl.implementation["${this.idl.name}"];`;
4444
setterBody = `Impl.implementation["${this.idl.name}"] = V;`;
4545
} else if (shouldReflect) {
46-
const processedOutput = this.ctx.invokeProcessReflect(this.idl, "esValue[impl]", { requires });
46+
const processedOutput = this.ctx.invokeProcessReflect(this.idl, "esValue[implSymbol]", { requires });
4747
getterBody = processedOutput.get;
4848
setterBody = processedOutput.set;
4949
}

lib/constructs/interface.js

+32-32
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ class Interface {
413413
value: function next() {
414414
const internal = this[utils.iterInternalSymbol];
415415
const { target, kind, index } = internal;
416-
const values = Array.from(target[impl]);
416+
const values = Array.from(target[implSymbol]);
417417
const len = values.length;
418418
if (index >= len) {
419419
return { value: undefined, done: true };
@@ -492,8 +492,8 @@ class Interface {
492492
}
493493

494494
generateRequires() {
495-
this.requires.addRaw("impl", "utils.implSymbol");
496-
this.requires.addRaw("ctorRegistry", "utils.ctorRegistrySymbol");
495+
this.requires.addRaw("implSymbol", "utils.implSymbol");
496+
this.requires.addRaw("ctorRegistrySymbol", "utils.ctorRegistrySymbol");
497497

498498
if (this.idl.inheritance !== null) {
499499
this.requires.addRelative(this.idl.inheritance);
@@ -509,7 +509,7 @@ class Interface {
509509
generateExport() {
510510
this.str += `
511511
exports.is = function is(obj) {
512-
return utils.isObject(obj) && utils.hasOwn(obj, impl) && obj[impl] instanceof Impl.implementation;
512+
return utils.isObject(obj) && utils.hasOwn(obj, implSymbol) && obj[implSymbol] instanceof Impl.implementation;
513513
};
514514
exports.isImpl = function isImpl(obj) {
515515
return utils.isObject(obj) && obj instanceof Impl.implementation;
@@ -549,10 +549,10 @@ class Interface {
549549
}
550550
if (unsupportedValue) {
551551
const func = this.indexedGetter.name ? `.${this.indexedGetter.name}` : "[utils.indexedGet]";
552-
const value = indexedValue || `${O}[impl]${func}(${index})`;
552+
const value = indexedValue || `${O}[implSymbol]${func}(${index})`;
553553
return `${value} !== ${unsupportedValue}`;
554554
}
555-
return `${O}[impl][utils.supportsPropertyIndex](${index})`;
555+
return `${O}[implSymbol][utils.supportsPropertyIndex](${index})`;
556556
};
557557

558558
const supportsPropertyName = (O, P, namedValue) => {
@@ -562,10 +562,10 @@ class Interface {
562562
}
563563
if (unsupportedValue) {
564564
const func = this.namedGetter.name ? `.${this.namedGetter.name}` : "[utils.namedGet]";
565-
const value = namedValue || `${O}[impl]${func}(${P})`;
565+
const value = namedValue || `${O}[implSymbol]${func}(${P})`;
566566
return `${value} !== ${unsupportedValue}`;
567567
}
568-
return `${O}[impl][utils.supportsPropertyName](${P})`;
568+
return `${O}[implSymbol][utils.supportsPropertyName](${P})`;
569569
};
570570

571571
// "named property visibility algorithm"
@@ -603,14 +603,14 @@ class Interface {
603603
invocation = `
604604
const creating = !(${supportsPropertyIndex(O, "index")});
605605
if (creating) {
606-
${O}[impl][utils.indexedSetNew](index, indexedValue);
606+
${O}[implSymbol][utils.indexedSetNew](index, indexedValue);
607607
} else {
608-
${O}[impl][utils.indexedSetExisting](index, indexedValue);
608+
${O}[implSymbol][utils.indexedSetExisting](index, indexedValue);
609609
}
610610
`;
611611
} else {
612612
invocation = `
613-
${O}[impl].${this.indexedSetter.name}(index, indexedValue);
613+
${O}[implSymbol].${this.indexedSetter.name}(index, indexedValue);
614614
`;
615615
}
616616

@@ -641,14 +641,14 @@ class Interface {
641641
invocation = `
642642
const creating = !(${supportsPropertyName(O, P)});
643643
if (creating) {
644-
${O}[impl][utils.namedSetNew](${P}, namedValue);
644+
${O}[implSymbol][utils.namedSetNew](${P}, namedValue);
645645
} else {
646-
${O}[impl][utils.namedSetExisting](${P}, namedValue);
646+
${O}[implSymbol][utils.namedSetExisting](${P}, namedValue);
647647
}
648648
`;
649649
} else {
650650
invocation = `
651-
${O}[impl].${this.namedSetter.name}(${P}, namedValue);
651+
${O}[implSymbol].${this.namedSetter.name}(${P}, namedValue);
652652
`;
653653
}
654654

@@ -729,14 +729,14 @@ class Interface {
729729
`;
730730
if (this.supportsIndexedProperties) {
731731
this.str += `
732-
for (const key of target[impl][utils.supportedPropertyIndices]) {
732+
for (const key of target[implSymbol][utils.supportedPropertyIndices]) {
733733
keys.add(\`\${key}\`);
734734
}
735735
`;
736736
}
737737
if (this.supportsNamedProperties) {
738738
this.str += `
739-
for (const key of target[impl][utils.supportedPropertyNames]) {
739+
for (const key of target[implSymbol][utils.supportedPropertyNames]) {
740740
if (${namedPropertyVisible("key", "target", true)}) {
741741
keys.add(\`\${key}\`);
742742
}
@@ -769,10 +769,10 @@ class Interface {
769769
let preamble = "";
770770
let condition;
771771
if (utils.getExtAttr(this.indexedGetter.extAttrs, "WebIDL2JSValueAsUnsupported")) {
772-
this.str += `const indexedValue = target[impl]${func}(index);`;
772+
this.str += `const indexedValue = target[implSymbol]${func}(index);`;
773773
condition = supportsPropertyIndex("target", "index", "indexedValue");
774774
} else {
775-
preamble = `const indexedValue = target[impl]${func}(index);`;
775+
preamble = `const indexedValue = target[implSymbol]${func}(index);`;
776776
condition = supportsPropertyIndex("target", "index");
777777
}
778778

@@ -797,13 +797,13 @@ class Interface {
797797
const conditions = [];
798798
if (utils.getExtAttr(this.namedGetter.extAttrs, "WebIDL2JSValueAsUnsupported")) {
799799
this.str += `
800-
const namedValue = target[impl]${func}(P);
800+
const namedValue = target[implSymbol]${func}(P);
801801
`;
802802
conditions.push(supportsPropertyName("target", "index", "namedValue"));
803803
conditions.push(namedPropertyVisible("P", "target", true));
804804
} else {
805805
preamble = `
806-
const namedValue = target[impl]${func}(P);
806+
const namedValue = target[implSymbol]${func}(P);
807807
`;
808808
conditions.push(namedPropertyVisible("P", "target", false));
809809
}
@@ -885,10 +885,10 @@ class Interface {
885885
let preamble = "";
886886
let condition;
887887
if (utils.getExtAttr(this.indexedGetter.extAttrs, "WebIDL2JSValueAsUnsupported")) {
888-
this.str += `const indexedValue = target[impl]${func}(index);`;
888+
this.str += `const indexedValue = target[implSymbol]${func}(index);`;
889889
condition = supportsPropertyIndex("target", "index", "indexedValue");
890890
} else {
891-
preamble = `const indexedValue = target[impl]${func}(index);`;
891+
preamble = `const indexedValue = target[implSymbol]${func}(index);`;
892892
condition = supportsPropertyIndex("target", "index");
893893
}
894894

@@ -1070,11 +1070,11 @@ class Interface {
10701070

10711071
if (this.namedDeleter.idlType.idlType === "bool") {
10721072
invocation = `
1073-
return target[impl]${func}(P);
1073+
return target[implSymbol]${func}(P);
10741074
`;
10751075
} else {
10761076
invocation = `
1077-
target[impl]${func}(P);
1077+
target[implSymbol]${func}(P);
10781078
return true;
10791079
`;
10801080
}
@@ -1111,11 +1111,11 @@ class Interface {
11111111
generateIface() {
11121112
this.str += `
11131113
exports.create = function create(globalObject, constructorArgs, privateData) {
1114-
if (globalObject[ctorRegistry] === undefined) {
1114+
if (globalObject[ctorRegistrySymbol] === undefined) {
11151115
throw new Error('Internal error: invalid global object');
11161116
}
11171117
1118-
const ctor = globalObject[ctorRegistry]["${this.name}"];
1118+
const ctor = globalObject[ctorRegistrySymbol]["${this.name}"];
11191119
if (ctor === undefined) {
11201120
throw new Error('Internal error: constructor ${this.name} is not installed on the passed global object');
11211121
}
@@ -1145,7 +1145,7 @@ class Interface {
11451145
privateData.wrapper = obj;
11461146
11471147
exports._internalSetup(obj, globalObject);
1148-
Object.defineProperty(obj, impl, {
1148+
Object.defineProperty(obj, implSymbol, {
11491149
value: new Impl.implementation(globalObject, constructorArgs, privateData),
11501150
configurable: true
11511151
});
@@ -1171,9 +1171,9 @@ class Interface {
11711171
}
11721172

11731173
this.str += `
1174-
obj[impl][utils.wrapperSymbol] = obj;
1174+
obj[implSymbol][utils.wrapperSymbol] = obj;
11751175
if (Impl.init) {
1176-
Impl.init(obj[impl], privateData);
1176+
Impl.init(obj[implSymbol], privateData);
11771177
}
11781178
return obj;
11791179
};
@@ -1467,10 +1467,10 @@ class Interface {
14671467
this.generateOffInstanceAfterClass();
14681468

14691469
this.str += `
1470-
if (globalObject[ctorRegistry] === undefined) {
1471-
globalObject[ctorRegistry] = Object.create(null);
1470+
if (globalObject[ctorRegistrySymbol] === undefined) {
1471+
globalObject[ctorRegistrySymbol] = Object.create(null);
14721472
}
1473-
globalObject[ctorRegistry][interfaceName] = ${name};
1473+
globalObject[ctorRegistrySymbol][interfaceName] = ${name};
14741474
14751475
Object.defineProperty(globalObject, interfaceName, {
14761476
configurable: true,

lib/constructs/iterable.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ class Iterable {
4747
"as parameter 1 is not a function.");
4848
}
4949
const thisArg = arguments[1];
50-
let pairs = Array.from(this[impl]);
50+
let pairs = Array.from(this[implSymbol]);
5151
let i = 0;
5252
while (i < pairs.length) {
5353
const [key, value] = pairs[i].map(utils.tryWrapperForImpl);
5454
callback.call(thisArg, value, key, this);
55-
pairs = Array.from(this[impl]);
55+
pairs = Array.from(this[implSymbol]);
5656
i++;
5757
}
5858
`);

lib/constructs/operation.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class Operation {
6969
`;
7070
}
7171

72-
const callOn = this.static ? "Impl.implementation" : `esValue[impl]`;
72+
const callOn = this.static ? "Impl.implementation" : `esValue[implSymbol]`;
7373
// In case of stringifiers, use the named implementation function rather than hardcoded "toString".
7474
// All overloads will have the same name, so pick the first one.
7575
const implFunc = this.idls[0].name || this.name;

0 commit comments

Comments
 (0)