diff --git a/lib/constructs/typedef.js b/lib/constructs/typedef.js new file mode 100644 index 00000000..1488aae9 --- /dev/null +++ b/lib/constructs/typedef.js @@ -0,0 +1,40 @@ +"use strict"; + +const Types = require("../types.js"); +const utils = require("../utils"); + +module.exports = class Typedef { + constructor(idl, opts) { + this.idl = idl; + this.name = idl.name; + this.opts = opts; + this.str = ""; + } + + generate() { + this.str += ` +module.exports = { + convert(value) {`; + + const conv = Types.generateTypeConversion("value", this.idl.idlType, [], this.opts.customTypes, { + handleNullable: true + }); + for (let key in conv.requires) { + this.str = `const ${key} = ${conv.requires[key]};\n` + this.str; + } + this.str += conv.body; + + this.str += ` + return value; + } +};`; + + return this.str; + } + + toString() { + this.str = ""; + this.generate(); + return this.str; + } +} diff --git a/lib/transformer.js b/lib/transformer.js index cfba41d8..375888c4 100644 --- a/lib/transformer.js +++ b/lib/transformer.js @@ -8,6 +8,7 @@ const webidl = require("webidl2"); const Interface = require("./constructs/interface"); const Dictionary = require("./constructs/dictionary"); +const Typedef = require("./constructs/typedef"); class Transformer { constructor(opts) { @@ -77,6 +78,7 @@ class Transformer { const interfaces = this._interfaces = Object.create(null); const dictionaries = this._dictionaries = Object.create(null); + const typedefs = this._typedefs = Object.create(null); const customTypes = this._customTypes = new Set(); // first we're gathering all full interfaces and ignore partial ones @@ -107,6 +109,11 @@ class Transformer { dictionaries[obj.name] = obj; customTypes.add(instruction.name); break; + case "typedef": + obj = new Typedef(instruction, { customTypes }); + typedefs[obj.name] = obj; + customTypes.add(instruction.name); + break; default: if (!this.options.suppressErrors) { throw new Error("Can't convert type '" + instruction.type + "'"); @@ -162,12 +169,14 @@ 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)); + 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 { + } 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"), ""); } } @@ -196,9 +205,9 @@ const Impl = require("${implFile}.js");\n`; yield fs.writeFile(path.join(outputDir, obj.name + ".js"), source); } - keys = Object.keys(this._dictionaries); + keys = Object.keys(this._dictionaries).concat(Object.keys(this._typedefs)); for (let i = 0; i < keys.length; ++i) { - const obj = this._dictionaries[keys[i]]; + const obj = this._dictionaries[keys[i]] || this._typedefs[keys[i]]; let source = obj.toString(); let relativeUtils = path.relative(outputDir, this.options.utilPath).replace(/\\/g, '/');