Skip to content

Commit

Permalink
Add support for records
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyGu committed Feb 6, 2017
1 parent 9ff8b6c commit e13e645
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
39 changes: 38 additions & 1 deletion lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,43 @@ module.exports.generateTypeConversion = function (name, idlType, argAttrs, custo
}
${name} = V;
}`;
} else if (idlType.generic === "record") {
if (!handleNullable || !idlType.nullable) {
str += `
if (${name} === null && ${name} === undefined) {
${name} = new Map;
} else {`;
}

str += `
if (typeof ${name} !== "object") {
throw new TypeError("The value provided is not an object");
} else {
const result = new Map;
const keys = Object.keys(${name});
for (let key of keys) {
let value = ${name}[key];
let typedKey = key;
let typedValue = value;`;

str += exports.generateTypeConversion('typedKey', idlType.idlType[0], [], customTypes).body;

const conv = exports.generateTypeConversion('typedValue', idlType.idlType[1], [], customTypes, {
handleNullable: true
});
Object.assign(requires, conv.requires);
str += conv.body;

str += `
result.set(typedKey, typedValue);
}
${name} = result;
}`;

if (!handleNullable || !idlType.nullable) {
str += `
}`;
}
} else if (conversions[idlType.idlType] || customTypes.has(idlType.idlType)) {
const enforceRange = utils.getExtAttr(argAttrs, "EnforceRange");
const clamp = utils.getExtAttr(argAttrs, "Clamp");
Expand Down Expand Up @@ -84,5 +121,5 @@ module.exports.generateTypeConversion = function (name, idlType, argAttrs, custo
};

module.exports.canHandleType = function (idlType, customTypes) {
return idlType.generic === "sequence" || conversions[idlType.idlType] || customTypes.has(idlType.idlType);
return idlType.generic === "sequence" || idlType.generic === "record" || conversions[idlType.idlType] || customTypes.has(idlType.idlType);
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"co": "^4.6.0",
"pn": "^1.0.0",
"webidl-conversions": "^3.0.0",
"webidl2": "^2.0.11"
"webidl2": "^2.2.2"
},
"devDependencies": {},
"scripts": {
Expand Down

0 comments on commit e13e645

Please sign in to comment.