Skip to content

refactor: Rename imported symbols #179

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

Merged
merged 3 commits into from
Mar 25, 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
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ will generate a JavaScript wrapper class file roughly like this:

```js
const conversions = require("webidl-conversions");
const impl = require("./utils.js").implSymbol;
const ctorRegistry = require("./utils.js").ctorRegistrySymbol;
const { implSymbol, ctorRegistrySymbol } = require("./utils.js");

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

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

return this[impl].add(...args);
return this[implSymbol].add(...args);
}
}

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

exports.create = (globalObject, constructorArgs = [], privateData = {}) => {
const ctor = globalObject[ctorRegistry].SomeInterface;
const ctor = globalObject[ctorRegistrySymbol].SomeInterface;
const obj = Object.create(ctor.prototype);
obj[impl] = new Impl(constructorArgs, privateData);
obj[implSymbol] = new Impl(constructorArgs, privateData);
return obj;
};

exports.is = obj => obj && obj[impl] instanceof Impl;
exports.is = obj => obj && obj[implSymbol] instanceof Impl;
```

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:
Expand Down
8 changes: 4 additions & 4 deletions lib/constructs/attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ class Attribute {
throw new TypeError("Illegal invocation");
}
`;
let getterBody = `return utils.tryWrapperForImpl(esValue[impl]["${this.idl.name}"]);`;
let setterBody = `esValue[impl]["${this.idl.name}"] = V;`;
let getterBody = `return utils.tryWrapperForImpl(esValue[implSymbol]["${this.idl.name}"]);`;
let setterBody = `esValue[implSymbol]["${this.idl.name}"] = V;`;
if (conversions[this.idl.idlType.idlType]) {
getterBody = `return esValue[impl]["${this.idl.name}"];`;
getterBody = `return esValue[implSymbol]["${this.idl.name}"];`;
}

const addMethod = this.static ?
Expand All @@ -43,7 +43,7 @@ class Attribute {
getterBody = `return Impl.implementation["${this.idl.name}"];`;
setterBody = `Impl.implementation["${this.idl.name}"] = V;`;
} else if (shouldReflect) {
const processedOutput = this.ctx.invokeProcessReflect(this.idl, "esValue[impl]", { requires });
const processedOutput = this.ctx.invokeProcessReflect(this.idl, "esValue[implSymbol]", { requires });
getterBody = processedOutput.get;
setterBody = processedOutput.set;
}
Expand Down
64 changes: 32 additions & 32 deletions lib/constructs/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ class Interface {
value: function next() {
const internal = this[utils.iterInternalSymbol];
const { target, kind, index } = internal;
const values = Array.from(target[impl]);
const values = Array.from(target[implSymbol]);
const len = values.length;
if (index >= len) {
return { value: undefined, done: true };
Expand Down Expand Up @@ -492,8 +492,8 @@ class Interface {
}

generateRequires() {
this.requires.addRaw("impl", "utils.implSymbol");
this.requires.addRaw("ctorRegistry", "utils.ctorRegistrySymbol");
this.requires.addRaw("implSymbol", "utils.implSymbol");
this.requires.addRaw("ctorRegistrySymbol", "utils.ctorRegistrySymbol");

if (this.idl.inheritance !== null) {
this.requires.addRelative(this.idl.inheritance);
Expand All @@ -509,7 +509,7 @@ class Interface {
generateExport() {
this.str += `
exports.is = function is(obj) {
return utils.isObject(obj) && utils.hasOwn(obj, impl) && obj[impl] instanceof Impl.implementation;
return utils.isObject(obj) && utils.hasOwn(obj, implSymbol) && obj[implSymbol] instanceof Impl.implementation;
};
exports.isImpl = function isImpl(obj) {
return utils.isObject(obj) && obj instanceof Impl.implementation;
Expand Down Expand Up @@ -549,10 +549,10 @@ class Interface {
}
if (unsupportedValue) {
const func = this.indexedGetter.name ? `.${this.indexedGetter.name}` : "[utils.indexedGet]";
const value = indexedValue || `${O}[impl]${func}(${index})`;
const value = indexedValue || `${O}[implSymbol]${func}(${index})`;
return `${value} !== ${unsupportedValue}`;
}
return `${O}[impl][utils.supportsPropertyIndex](${index})`;
return `${O}[implSymbol][utils.supportsPropertyIndex](${index})`;
};

const supportsPropertyName = (O, P, namedValue) => {
Expand All @@ -562,10 +562,10 @@ class Interface {
}
if (unsupportedValue) {
const func = this.namedGetter.name ? `.${this.namedGetter.name}` : "[utils.namedGet]";
const value = namedValue || `${O}[impl]${func}(${P})`;
const value = namedValue || `${O}[implSymbol]${func}(${P})`;
return `${value} !== ${unsupportedValue}`;
}
return `${O}[impl][utils.supportsPropertyName](${P})`;
return `${O}[implSymbol][utils.supportsPropertyName](${P})`;
};

// "named property visibility algorithm"
Expand Down Expand Up @@ -603,14 +603,14 @@ class Interface {
invocation = `
const creating = !(${supportsPropertyIndex(O, "index")});
if (creating) {
${O}[impl][utils.indexedSetNew](index, indexedValue);
${O}[implSymbol][utils.indexedSetNew](index, indexedValue);
} else {
${O}[impl][utils.indexedSetExisting](index, indexedValue);
${O}[implSymbol][utils.indexedSetExisting](index, indexedValue);
}
`;
} else {
invocation = `
${O}[impl].${this.indexedSetter.name}(index, indexedValue);
${O}[implSymbol].${this.indexedSetter.name}(index, indexedValue);
`;
}

Expand Down Expand Up @@ -641,14 +641,14 @@ class Interface {
invocation = `
const creating = !(${supportsPropertyName(O, P)});
if (creating) {
${O}[impl][utils.namedSetNew](${P}, namedValue);
${O}[implSymbol][utils.namedSetNew](${P}, namedValue);
} else {
${O}[impl][utils.namedSetExisting](${P}, namedValue);
${O}[implSymbol][utils.namedSetExisting](${P}, namedValue);
}
`;
} else {
invocation = `
${O}[impl].${this.namedSetter.name}(${P}, namedValue);
${O}[implSymbol].${this.namedSetter.name}(${P}, namedValue);
`;
}

Expand Down Expand Up @@ -729,14 +729,14 @@ class Interface {
`;
if (this.supportsIndexedProperties) {
this.str += `
for (const key of target[impl][utils.supportedPropertyIndices]) {
for (const key of target[implSymbol][utils.supportedPropertyIndices]) {
keys.add(\`\${key}\`);
}
`;
}
if (this.supportsNamedProperties) {
this.str += `
for (const key of target[impl][utils.supportedPropertyNames]) {
for (const key of target[implSymbol][utils.supportedPropertyNames]) {
if (${namedPropertyVisible("key", "target", true)}) {
keys.add(\`\${key}\`);
}
Expand Down Expand Up @@ -769,10 +769,10 @@ class Interface {
let preamble = "";
let condition;
if (utils.getExtAttr(this.indexedGetter.extAttrs, "WebIDL2JSValueAsUnsupported")) {
this.str += `const indexedValue = target[impl]${func}(index);`;
this.str += `const indexedValue = target[implSymbol]${func}(index);`;
condition = supportsPropertyIndex("target", "index", "indexedValue");
} else {
preamble = `const indexedValue = target[impl]${func}(index);`;
preamble = `const indexedValue = target[implSymbol]${func}(index);`;
condition = supportsPropertyIndex("target", "index");
}

Expand All @@ -797,13 +797,13 @@ class Interface {
const conditions = [];
if (utils.getExtAttr(this.namedGetter.extAttrs, "WebIDL2JSValueAsUnsupported")) {
this.str += `
const namedValue = target[impl]${func}(P);
const namedValue = target[implSymbol]${func}(P);
`;
conditions.push(supportsPropertyName("target", "index", "namedValue"));
conditions.push(namedPropertyVisible("P", "target", true));
} else {
preamble = `
const namedValue = target[impl]${func}(P);
const namedValue = target[implSymbol]${func}(P);
`;
conditions.push(namedPropertyVisible("P", "target", false));
}
Expand Down Expand Up @@ -885,10 +885,10 @@ class Interface {
let preamble = "";
let condition;
if (utils.getExtAttr(this.indexedGetter.extAttrs, "WebIDL2JSValueAsUnsupported")) {
this.str += `const indexedValue = target[impl]${func}(index);`;
this.str += `const indexedValue = target[implSymbol]${func}(index);`;
condition = supportsPropertyIndex("target", "index", "indexedValue");
} else {
preamble = `const indexedValue = target[impl]${func}(index);`;
preamble = `const indexedValue = target[implSymbol]${func}(index);`;
condition = supportsPropertyIndex("target", "index");
}

Expand Down Expand Up @@ -1070,11 +1070,11 @@ class Interface {

if (this.namedDeleter.idlType.idlType === "bool") {
invocation = `
return target[impl]${func}(P);
return target[implSymbol]${func}(P);
`;
} else {
invocation = `
target[impl]${func}(P);
target[implSymbol]${func}(P);
return true;
`;
}
Expand Down Expand Up @@ -1111,11 +1111,11 @@ class Interface {
generateIface() {
this.str += `
exports.create = function create(globalObject, constructorArgs, privateData) {
if (globalObject[ctorRegistry] === undefined) {
if (globalObject[ctorRegistrySymbol] === undefined) {
throw new Error('Internal error: invalid global object');
}

const ctor = globalObject[ctorRegistry]["${this.name}"];
const ctor = globalObject[ctorRegistrySymbol]["${this.name}"];
if (ctor === undefined) {
throw new Error('Internal error: constructor ${this.name} is not installed on the passed global object');
}
Expand Down Expand Up @@ -1145,7 +1145,7 @@ class Interface {
privateData.wrapper = obj;

exports._internalSetup(obj, globalObject);
Object.defineProperty(obj, impl, {
Object.defineProperty(obj, implSymbol, {
value: new Impl.implementation(globalObject, constructorArgs, privateData),
configurable: true
});
Expand All @@ -1171,9 +1171,9 @@ class Interface {
}

this.str += `
obj[impl][utils.wrapperSymbol] = obj;
obj[implSymbol][utils.wrapperSymbol] = obj;
if (Impl.init) {
Impl.init(obj[impl], privateData);
Impl.init(obj[implSymbol], privateData);
}
return obj;
};
Expand Down Expand Up @@ -1467,10 +1467,10 @@ class Interface {
this.generateOffInstanceAfterClass();

this.str += `
if (globalObject[ctorRegistry] === undefined) {
globalObject[ctorRegistry] = Object.create(null);
if (globalObject[ctorRegistrySymbol] === undefined) {
globalObject[ctorRegistrySymbol] = Object.create(null);
}
globalObject[ctorRegistry][interfaceName] = ${name};
globalObject[ctorRegistrySymbol][interfaceName] = ${name};

Object.defineProperty(globalObject, interfaceName, {
configurable: true,
Expand Down
4 changes: 2 additions & 2 deletions lib/constructs/iterable.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ class Iterable {
"as parameter 1 is not a function.");
}
const thisArg = arguments[1];
let pairs = Array.from(this[impl]);
let pairs = Array.from(this[implSymbol]);
let i = 0;
while (i < pairs.length) {
const [key, value] = pairs[i].map(utils.tryWrapperForImpl);
callback.call(thisArg, value, key, this);
pairs = Array.from(this[impl]);
pairs = Array.from(this[implSymbol]);
i++;
}
`);
Expand Down
2 changes: 1 addition & 1 deletion lib/constructs/operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class Operation {
`;
}

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