Skip to content

Commit

Permalink
Add support for typedefs
Browse files Browse the repository at this point in the history
Treat it as a first-class citizen like interfaces and dictionaries.
  • Loading branch information
TimothyGu committed Feb 6, 2017
1 parent e13e645 commit a40aded
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
40 changes: 40 additions & 0 deletions lib/constructs/typedef.js
Original file line number Diff line number Diff line change
@@ -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;
}
}
17 changes: 13 additions & 4 deletions lib/transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 + "'");
Expand Down Expand Up @@ -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"), "");
}
}

Expand Down Expand Up @@ -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, '/');
Expand Down

0 comments on commit a40aded

Please sign in to comment.