diff --git a/lib/transformer.js b/lib/transformer.js index 375888c4..2d90c268 100644 --- a/lib/transformer.js +++ b/lib/transformer.js @@ -76,9 +76,9 @@ class Transformer { impl: content.impl })); - const interfaces = this._interfaces = Object.create(null); - const dictionaries = this._dictionaries = Object.create(null); - const typedefs = this._typedefs = Object.create(null); + const interfaces = this._interfaces = new Map(); + const dictionaries = this._dictionaries = new Map(); + const typedefs = this._typedefs = new Map(); const customTypes = this._customTypes = new Set(); // first we're gathering all full interfaces and ignore partial ones @@ -96,7 +96,7 @@ class Transformer { implSuffix: this.options.implSuffix, customTypes }); - interfaces[obj.name] = obj; + interfaces.set(obj.name, obj); break; case "implements": break; // handled later @@ -106,13 +106,13 @@ class Transformer { } obj = new Dictionary(instruction, { customTypes }); - dictionaries[obj.name] = obj; - customTypes.add(instruction.name); + dictionaries.set(obj.name, obj); + customTypes.add(obj.name); break; case "typedef": obj = new Typedef(instruction, { customTypes }); - typedefs[obj.name] = obj; - customTypes.add(instruction.name); + typedefs.set(obj.name, obj); + customTypes.add(obj.name); break; default: if (!this.options.suppressErrors) { @@ -133,31 +133,34 @@ class Transformer { break; } - if (this.options.suppressErrors && !interfaces[instruction.name]) { + const iface = interfaces.get(instruction.name); + if (this.options.suppressErrors && !iface) { break; } - oldMembers = interfaces[instruction.name].idl.members; + oldMembers = iface.idl.members; oldMembers.push.apply(oldMembers, instruction.members); - extAttrs = interfaces[instruction.name].idl.extAttrs; + extAttrs = iface.idl.extAttrs; extAttrs.push.apply(extAttrs, instruction.extAttrs); break; case "dictionary": if (!instruction.partial) { break; } - if (this.options.suppressErrors && !dictionaries[instruction.name]) { + const dict = dictionaries.get(instruction.name); + if (this.options.suppressErrors && !dict) { break; } - oldMembers = dictionaries[instruction.name].idl.members; + oldMembers = dict.idl.members; oldMembers.push.apply(oldMembers, instruction.members); - extAttrs = dictionaries[instruction.name].idl.extAttrs; + extAttrs = dict.idl.extAttrs; extAttrs.push.apply(extAttrs, instruction.extAttrs); break; case "implements": - if (this.options.suppressErrors && !interfaces[instruction.target]) { + const iface = interfaces.get(instruction.target); + if (this.options.suppressErrors && !iface) { break; } - interfaces[instruction.target].implements(instruction.implements); + iface.implements(instruction.implements); break; } } @@ -169,20 +172,23 @@ class Transformer { yield fs.writeFile(this.options.utilPath, utilsText); let interfaceStub = yield fs.readFile(__dirname + "/output/interfaceStub.js"); - let keys = Object.keys(this._interfaces).concat(Object.keys(this._dictionaries)).concat(Object.keys(this._typedefs)); - for (let key of keys) { - if (this._interfaces[key]) { - yield fs.writeFile(path.join(outputDir, this._interfaces[key].name + ".js"), interfaceStub); - } else if (this._dictionaries[key]) { - yield fs.writeFile(path.join(outputDir, this._dictionaries[key].name + ".js"), ""); - } else { - yield fs.writeFile(path.join(outputDir, this._typedefs[key].name + ".js"), ""); - } + for (const key of this._interfaces.keys()) { + yield fs.writeFile(path.join(outputDir, key + ".js"), interfaceStub); + } + // TODO: use spread syntax to merge the two following blocks + for (const key of this._dictionaries.keys()) { + yield fs.writeFile(path.join(outputDir, key + ".js"), ""); + } + for (const key of this._typedefs.keys()) { + yield fs.writeFile(path.join(outputDir, key + ".js"), ""); + } + + let relativeUtils = path.relative(outputDir, this.options.utilPath).replace(/\\/g, '/'); + if (relativeUtils[0] !== ".") { + relativeUtils = "./" + relativeUtils; } - keys = Object.keys(this._interfaces); - for (let i = 0; i < keys.length; ++i) { - const obj = this._interfaces[keys[i]]; + for (const obj of this._interfaces.values()) { let source = obj.toString(); const implDir = path.relative(outputDir, obj.opts.implDir).replace(/\\/g, "/"); // fix windows file paths @@ -191,11 +197,6 @@ class Transformer { implFile = "./" + implFile; } - let relativeUtils = path.relative(outputDir, this.options.utilPath).replace(/\\/g, '/'); - if (relativeUtils[0] !== ".") { - relativeUtils = "./" + relativeUtils; - } - source = `"use strict"; const conversions = require("webidl-conversions"); @@ -205,16 +206,10 @@ const Impl = require("${implFile}.js");\n`; yield fs.writeFile(path.join(outputDir, obj.name + ".js"), source); } - keys = Object.keys(this._dictionaries).concat(Object.keys(this._typedefs)); - for (let i = 0; i < keys.length; ++i) { - const obj = this._dictionaries[keys[i]] || this._typedefs[keys[i]]; + // TODO: use spread syntax to simplify + for (const obj of Array.from(this._dictionaries.values()).concat(Array.from(this._typedefs.values()))) { let source = obj.toString(); - let relativeUtils = path.relative(outputDir, this.options.utilPath).replace(/\\/g, '/'); - if (relativeUtils[0] !== ".") { - relativeUtils = "./" + relativeUtils; - } - source = `"use strict"; const conversions = require("webidl-conversions");