Skip to content

Commit

Permalink
refactor(interface): Use default export for implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ExE-Boss committed Oct 22, 2019
1 parent 1d5b0b4 commit 3dc3ff3
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 128 deletions.
4 changes: 2 additions & 2 deletions lib/constructs/attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ class Attribute {

if (this.static) {
brandCheck = "";
getterBody = `return Impl.implementation["${this.idl.name}"];`;
setterBody = `Impl.implementation["${this.idl.name}"] = V;`;
getterBody = `return Impl.default["${this.idl.name}"];`;
setterBody = `Impl.default["${this.idl.name}"] = V;`;
} else if (shouldReflect) {
if (!reflector[this.idl.idlType.idlType]) {
throw new Error("Unknown reflector type: " + this.idl.idlType.idlType);
Expand Down
6 changes: 3 additions & 3 deletions lib/constructs/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ class Interface {
_mixedIntoPredicates: [],
is(obj) {
if (obj) {
if (utils.hasOwn(obj, impl) && obj[impl] instanceof Impl.implementation) {
if (utils.hasOwn(obj, impl) && obj[impl] instanceof Impl.default) {
return true;
}
for (const isMixedInto of module.exports._mixedIntoPredicates) {
Expand All @@ -541,7 +541,7 @@ class Interface {
},
isImpl(obj) {
if (obj) {
if (obj instanceof Impl.implementation) {
if (obj instanceof Impl.default) {
return true;
}
Expand Down Expand Up @@ -1162,7 +1162,7 @@ class Interface {
this._internalSetup(obj);
Object.defineProperty(obj, impl, {
value: new Impl.implementation(constructorArgs, privateData),
value: new Impl.default(constructorArgs, privateData),
configurable: true
});
`;
Expand Down
2 changes: 1 addition & 1 deletion lib/constructs/operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Operation {
`;
}

const callOn = this.static ? "Impl.implementation" : "this[impl]";
const callOn = this.static ? "Impl.default" : "this[impl]";
// 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
47 changes: 46 additions & 1 deletion lib/output/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,50 @@ const namedSetNew = Symbol("named property set new");
const namedSetExisting = Symbol("named property set existing");
const namedDelete = Symbol("named property delete");

/**
* Used to cache importStar results.
*/
const importStarCache = new WeakMap();

/**
* Mostly copied from TypeScript and Babel.
*
* @template T
* @param {T} obj
* @return {T extends { default: any } ? T : { default: T } & { [K in keyof T]: T[K]; }}
*/
function importStar(obj) {
if (obj && obj.__esModule) {
return obj;
}

if (obj === null || (typeof obj !== "object" && typeof obj !== "function")) {
return { default: obj };
}

if (importStarCache.has(obj)) {
return importStarCache.get(obj);
}

const newObj = {};

for (const key in obj) {
if (hasOwn(obj, key) && key !== "default") {
Object.defineProperty(
newObj,
key,
Object.getOwnPropertyDescriptor(obj, key)
);
}
}

newObj.default = obj;

importStarCache.set(obj, newObj);

return newObj;
}

module.exports = exports = {
isObject,
hasOwn,
Expand All @@ -97,5 +141,6 @@ module.exports = exports = {
namedGet,
namedSetNew,
namedSetExisting,
namedDelete
namedDelete,
importStar
};
2 changes: 1 addition & 1 deletion lib/transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ class Transformer {
const conversions = require("webidl-conversions");
const utils = require("${relativeUtils}");
${source}
const Impl = require("${implFile}.js");
const Impl = utils.importStar(require("${implFile}.js"));
`;

source = this._prettify(source);
Expand Down
Loading

0 comments on commit 3dc3ff3

Please sign in to comment.