Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for arbitrary external types #109

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/constructs/operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ class Operation {
requires.merge(parameterConversions.requires);
str += parameterConversions.body;

if (overloads.every(overload => conversions[overload.operation.idlType.idlType])) {
if (
overloads.every(overload => {
const { idlType } = overload.operation.idlType;
return conversions[idlType] || this.ctx._externalTypes.includes(idlType);
})
) {
str += `
return ${callOn}.${implFunc}(${argsSpread});
`;
Expand Down
16 changes: 16 additions & 0 deletions lib/parameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,22 @@ module.exports.generateOverloadConversions = function (ctx, typeOfOp, name, pare
}
}

for (const [importPath, types] of Object.entries(ctx.options.externalTypes)) {
for (const type of types) {
const ts = S.filter(o => isOrIncludes(ctx, o.typeList[d], t => t.idlType === type));
if (ts.length) {
if (!requires.has(type)) {
requires.addRaw(type, `require("${importPath}").${type}`);
}
possibilities.push(`
if (curArg instanceof ${type}) {
${continued(ts[0], i)}
}
`);
}
}
}

const callables = S.filter(o => {
return isOrIncludes(ctx, o.typeList[d], t => ["Function", "VoidFunction"].includes(t.idlType));
});
Expand Down
10 changes: 9 additions & 1 deletion lib/transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@ class Transformer {
this.ctx = new Context({
implSuffix: opts.implSuffix,
options: {
suppressErrors: Boolean(opts.suppressErrors)
suppressErrors: Boolean(opts.suppressErrors),
externalTypes: opts.externalTypes
}
});

this.ctx._externalTypes = [];
if (opts.externalTypes) {
for (const types of Object.values(opts.externalTypes)) {
this.ctx._externalTypes.push(...types);
}
}

this.sources = []; // Absolute paths to the IDL and Impl directories.
this.utilPath = null;
}
Expand Down
46 changes: 46 additions & 0 deletions lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ function generateTypeConversion(ctx, name, idlType, argAttrs = [], parentName, e
} else if (idlType.generic === "FrozenArray") {
// frozen array type
generateFrozenArray();
} else if (ctx._externalTypes.includes(idlType.idlType)) {
// an external type, generate ad-hoc "conversion"
generateExternal(idlType.idlType);
} else if (conversions[idlType.idlType]) {
// string or number type compatible with webidl-conversions
generateGeneric(`conversions["${idlType.idlType}"]`);
Expand Down Expand Up @@ -210,6 +213,17 @@ function generateTypeConversion(ctx, name, idlType, argAttrs = [], parentName, e
output.push(`if (${condition}) {}`);
}

for (const [importPath, types] of Object.entries(ctx.options.externalTypes)) {
for (const type of types) {
if (union[type] || union.object) {
if (!requires.has(type)) {
requires.addRaw(type, `require("${importPath}").${type}`);
}
output.push(`if (${name} instanceof ${type}) {}`);
}
}
}

if (union.callback || union.object) {
output.push(`if (typeof ${name} === "function") {}`);
}
Expand Down Expand Up @@ -352,6 +366,27 @@ function generateTypeConversion(ctx, name, idlType, argAttrs = [], parentName, e
str += `${name} = Object.freeze(${name});`;
}

function generateExternal(type) {
if (!requires.has(type)) {
let importPath;
for (const [path, types] of Object.entries(ctx.options.externalTypes)) {
if (types.includes(type)) {
importPath = path;
break;
}
}
if (importPath) {
requires.addRaw(type, `require("${importPath}").${type}`);
}
}
const article = /^[AEIOU]/.test(type) ? "an" : "a";
str += `
if (!(${name} instanceof ${type})) {
throw new TypeError(${errPrefix} + " is not ${article} ${type} object.");
}
`;
}

function generateGeneric(conversionFn) {
const enforceRange = utils.getExtAttr(extAttrs, "EnforceRange");
const clamp = utils.getExtAttr(extAttrs, "Clamp");
Expand Down Expand Up @@ -410,6 +445,11 @@ function extractUnionInfo(ctx, idlType, errPrefix) {
},
unknown: false
};

for (const type of ctx._externalTypes) {
seen[type] = false;
}

for (const item of idlType.idlType) {
if (item.generic === "sequence" || item.generic === "FrozenArray") {
if (seen.sequenceLike) {
Expand Down Expand Up @@ -437,6 +477,11 @@ function extractUnionInfo(ctx, idlType, errPrefix) {
error(`${item.idlType} is not distinguishable with object type`);
}
seen.ArrayBufferViews.add(item.idlType);
} else if (ctx._externalTypes.includes(item.idlType)) {
if (seen.object) {
error(`${item.idlTypes} is not distinguishable with object type`);
}
seen[item.idlType] = true;
} else if (stringTypes.has(item.idlType) || ctx.enumerations.has(item.idlType)) {
if (seen.string) {
error("There can only be one string type in a union type");
Expand Down Expand Up @@ -579,6 +624,7 @@ function sameType(ctx, type1, type2) {
extracted1.ArrayBuffer !== extracted2.ArrayBuffer &&
JSON.stringify([...extracted1.ArrayBufferViews].sort()) ===
JSON.stringify([...extracted2.ArrayBufferViews].sort()) &&
ctx._externalTypes.every(type => extracted1[type] === extracted2[type]) &&
extracted1.object === extracted2.object &&
sameType(ctx, extracted1.exception, extracted2.exception) &&
sameType(ctx, extracted1.string, extracted2.string) &&
Expand Down
Loading