Comparing version 3.0.2 to 4.0.0
18
index.js
@@ -70,3 +70,3 @@ "use strict"; | ||
extAttrs = interfaces[idl[i].name].idl.extAttrs; | ||
extAttrs.push.apply(oldMembers, idl[i].extAttrs); | ||
extAttrs.push.apply(extAttrs, idl[i].extAttrs); | ||
break; | ||
@@ -83,3 +83,3 @@ case "dictionary": | ||
extAttrs = dictionaries[idl[i].name].idl.extAttrs; | ||
extAttrs.push.apply(oldMembers, idl[i].extAttrs); | ||
extAttrs.push.apply(extAttrs, idl[i].extAttrs); | ||
break; | ||
@@ -98,6 +98,10 @@ case "implements": | ||
let interfaceStub = fs.readFileSync(__dirname + "/lib/output/interfaceStub.js"); | ||
let keys = Object.keys(interfaces).concat(Object.keys(dictionaries)); | ||
for (let key of keys) { | ||
const obj = interfaces[key] || dictionaries[key]; | ||
fs.writeFileSync(path.join(outputDir, obj.name + ".js"), ""); | ||
if (interfaces[key]) { | ||
fs.writeFileSync(path.join(outputDir, interfaces[key].name + ".js"), interfaceStub); | ||
} else { | ||
fs.writeFileSync(path.join(outputDir, dictionaries[key].name + ".js"), ""); | ||
} | ||
} | ||
@@ -116,3 +120,3 @@ | ||
let relativeUtils = path.relative(outputDir, opts.utilPath).replace(/\\/g, '/'); | ||
if (relativeUtils[0] != ".") { | ||
if (relativeUtils[0] !== ".") { | ||
relativeUtils = "./" + relativeUtils; | ||
@@ -124,4 +128,4 @@ } | ||
const conversions = require("webidl-conversions"); | ||
const utils = require("${relativeUtils}"); | ||
const Impl = require("${implFile}.js");\n\n` + source; | ||
const utils = require("${relativeUtils}");\n${source} | ||
const Impl = require("${implFile}.js");\n`; | ||
@@ -128,0 +132,0 @@ fs.writeFileSync(path.join(outputDir, obj.name + ".js"), source); |
@@ -29,4 +29,4 @@ "use strict"; | ||
let getterBody = `return ${objName}[impl].${this.idl.name};`; | ||
let setterBody = `${objName}[impl].${this.idl.name} = V;`; | ||
let getterBody = `return utils.tryWrapperForImpl(${objName}[impl].${this.idl.name});`; | ||
let setterBody = `${objName}[impl].${this.idl.name} = utils.tryImplForWrapper(V);`; | ||
if (this.idl.static) { | ||
@@ -39,4 +39,5 @@ getterBody = `return Impl.${this.idl.name};`; | ||
} | ||
getterBody = reflector[this.idl.idlType.idlType].get(objName, this.idl.name); | ||
setterBody = reflector[this.idl.idlType.idlType].set(objName, this.idl.name); | ||
const attrName = shouldReflect.rhs && shouldReflect.rhs.value.replace(/_/g, '-') || this.idl.name; | ||
getterBody = reflector[this.idl.idlType.idlType].get(objName, attrName); | ||
setterBody = reflector[this.idl.idlType.idlType].set(objName, attrName); | ||
} | ||
@@ -43,0 +44,0 @@ |
@@ -85,20 +85,6 @@ "use strict"; | ||
for (let i = 0; i < this.mixins.length; ++i) { | ||
requireStr += `const ${this.mixins[i]} = require("./${this.mixins[i]}.js").interface;\n`; | ||
requireStr += `const ${this.mixins[i]} = require("./${this.mixins[i]}.js");\n`; | ||
} | ||
} | ||
if (this.idl.inheritance !== "Element" && this.mixins.indexOf("Element") === -1) { | ||
let needsElement = false; | ||
for (let i = 0; i < this.idl.members.length; ++i) { | ||
const memberIdl = this.idl.members[i]; | ||
if (memberIdl.type === "attribute" && utils.getExtAttr(memberIdl.extAttrs, "Reflect")) { | ||
needsElement = true; | ||
break; | ||
} | ||
} | ||
if (needsElement) { | ||
requireStr += `const Element = require("./Element.js").interface;\n`; | ||
} | ||
} | ||
for (let key in this.requires) { | ||
@@ -113,4 +99,6 @@ requireStr += `const ${key} = ${this.requires[key]};\n`; | ||
Interface.prototype.generateMixins = function () { | ||
this.str += `\n`; | ||
for (let i = 0; i < this.mixins.length; ++i) { | ||
this.str += `mixin(${this.name}.prototype, ${this.mixins[i]}.prototype);\n`; | ||
this.str += `mixin(${this.name}.prototype, ${this.mixins[i]}.interface.prototype); | ||
${this.mixins[i]}.mixedInto.push(${this.name});\n`; | ||
} | ||
@@ -150,5 +138,31 @@ }; | ||
this.str += `\nmodule.exports = { | ||
mixedInto: [], | ||
is(obj) { | ||
return !!obj && obj[impl] instanceof Impl.implementation; | ||
if (obj) { | ||
if (obj[impl] instanceof Impl.implementation) { | ||
return true; | ||
} | ||
for (let i = 0; i < module.exports.mixedInto.length; ++i) { | ||
if (obj instanceof module.exports.mixedInto[i]) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
}, | ||
isImpl(obj) { | ||
if (obj) { | ||
if (obj instanceof Impl.implementation) { | ||
return true; | ||
} | ||
const wrapper = utils.wrapperForImpl(obj); | ||
for (let i = 0; i < module.exports.mixedInto.length; ++i) { | ||
if (wrapper instanceof module.exports.mixedInto[i]) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
}, | ||
create(constructorArgs, privateData) { | ||
@@ -159,2 +173,7 @@ let obj = Object.create(${this.name}.prototype); | ||
}, | ||
createImpl(constructorArgs, privateData) { | ||
let obj = Object.create(${this.name}.prototype); | ||
this.setup(obj, constructorArgs, privateData); | ||
return utils.implForWrapper(obj); | ||
}, | ||
setup(obj, constructorArgs, privateData) { | ||
@@ -252,2 +271,40 @@ if (!privateData) privateData = {}; | ||
Interface.prototype.generateToString = function () { | ||
let hasToString = false; | ||
for (const member of this.idl.members) { | ||
if (member.stringifier || member.name === "toString") { | ||
hasToString = true; | ||
} | ||
} | ||
if (!hasToString) { | ||
this.str += `\n${this.name}.prototype.toString = function () { | ||
if (this === ${this.name}.prototype) { | ||
return "[object ${this.name}Prototype]"; | ||
}`; | ||
if (this.idl.inheritance) { | ||
this.str += ` | ||
return ${this.idl.inheritance}.interface.prototype.toString.call(this);`; | ||
} else { | ||
this.str += ` | ||
return this[impl].toString();`; | ||
} | ||
this.str += ` | ||
};\n`; | ||
} | ||
}; | ||
Interface.prototype.generateSymbols = function () { | ||
const unscopables = {}; | ||
for (const member of this.idl.members) { | ||
if (utils.getExtAttr(member.extAttrs, "Unscopeable")) { | ||
unscopables[member.name] = true; | ||
} | ||
} | ||
if (Object.keys(unscopables).length) { | ||
this.str += ` | ||
${this.name}.prototype[Symbol.unscopables] = ${JSON.stringify(unscopables, null, ' ')};\n`; | ||
} | ||
}; | ||
Interface.prototype.generate = function () { | ||
@@ -258,6 +315,9 @@ this.generateConstructor(); | ||
this.generateOperations(); | ||
this.generateToString(); | ||
this.generateAttributes(); | ||
this.generateAttributes(); | ||
this.generateRequires(); | ||
this.generateSymbols(); | ||
this.generateExport(); | ||
@@ -264,0 +324,0 @@ }; |
@@ -59,3 +59,3 @@ "use strict"; | ||
str += ` | ||
return ${callOn}.${name}.apply(${callOn}, args); | ||
return utils.tryWrapperForImpl(${callOn}.${name}.apply(${callOn}, args)); | ||
};\n`; | ||
@@ -62,0 +62,0 @@ if (this.idl.stringifier && name !== "toString") { |
@@ -6,2 +6,6 @@ "use strict"; | ||
for (let i = 0; i < keys.length; ++i) { | ||
if (keys[i] in target) { | ||
continue; | ||
} | ||
Object.defineProperty(target, keys[i], Object.getOwnPropertyDescriptor(source, keys[i])); | ||
@@ -15,8 +19,17 @@ } | ||
module.exports.wrapperForImpl = function (impl) { | ||
return impl[module.exports.wrapperSymbol]; | ||
return impl ? impl[module.exports.wrapperSymbol] : null; | ||
}; | ||
module.exports.implForWrapper = function (wrapper) { | ||
return wrapper[module.exports.implSymbol]; | ||
return wrapper ? wrapper[module.exports.implSymbol] : null; | ||
}; | ||
module.exports.tryWrapperForImpl = function (impl) { | ||
const wrapper = module.exports.wrapperForImpl(impl); | ||
return wrapper ? wrapper : impl; | ||
}; | ||
module.exports.tryImplForWrapper = function (wrapper) { | ||
const impl = module.exports.implForWrapper(wrapper); | ||
return impl ? impl : wrapper; | ||
}; |
@@ -78,2 +78,6 @@ "use strict"; | ||
for (let i = 0; i < maxArguments; ++i) { | ||
if (overloads[0].operation.arguments.length <= i) { | ||
break; | ||
} | ||
let maybeType = { type: overloads[0].operation.arguments[i].idlType, optional: overloads[0].optionalityList[i] !== "required" }; | ||
@@ -80,0 +84,0 @@ for (let j = 1; j < overloads.length; ++j) { |
@@ -27,2 +27,8 @@ "use strict"; | ||
if (idlType.nullable) { | ||
str += ` | ||
if (${name} === null || ${name} === undefined) { | ||
${name} = null; | ||
} else {`; | ||
} | ||
let conversionFn = null; | ||
@@ -52,2 +58,7 @@ if (conversions[idlType.idlType]) { | ||
} | ||
if (idlType.nullable) { | ||
str += ` | ||
}`; | ||
} | ||
} | ||
@@ -66,3 +77,3 @@ | ||
let maxArguments = overloads[0].nameList.length; | ||
let isVariadic = false; | ||
let isVariadic = overloads[0].optionalityList.indexOf("variadic") !== -1; | ||
@@ -90,3 +101,3 @@ for (let i = 1; i < overloads.length; ++i) { | ||
str += `; ++i) { | ||
args[i] = arguments[i]; | ||
args[i] = utils.tryImplForWrapper(arguments[i]); | ||
}`; | ||
@@ -93,0 +104,0 @@ for (let i = 0; i < typeConversions.length; ++i) { |
@@ -5,9 +5,9 @@ "use strict"; | ||
get(objName, attrName) { | ||
return `return Element.prototype.hasAttribute.call(${objName}, "${attrName}");`; | ||
return `return this.hasAttribute("${attrName}");`; | ||
}, | ||
set(objName, attrName) { | ||
return `if (V) { | ||
Element.prototype.setAttribute.call(${objName}, "${attrName}", ""); | ||
this.setAttribute("${attrName}", ""); | ||
} else { | ||
Element.prototype.removeAttribute.call(${objName}, "${attrName}"); | ||
this.removeAttribute("${attrName}"); | ||
}`; | ||
@@ -19,8 +19,20 @@ } | ||
get(objName, attrName) { | ||
return `const value = Element.prototype.getAttribute.call(${objName}, "${attrName}"); | ||
return `const value = this.getAttribute("${attrName}"); | ||
return value === null ? "" : value;`; | ||
}, | ||
set(objName, attrName) { | ||
return `Element.prototype.setAttribute.call(${objName}, "${attrName}", V);`; | ||
return `this.setAttribute("${attrName}", V);`; | ||
} | ||
} | ||
}; | ||
["long", "unsigned long"].forEach((key) => { | ||
module.exports[key] = { | ||
get(objName, attrName) { | ||
return `const value = this.getAttribute("${attrName}"); | ||
return value === null ? "" : String(value);`; | ||
}, | ||
set(objName, attrName) { | ||
return `this.setAttribute("${attrName}", String(V));`; | ||
} | ||
}; | ||
}); |
{ | ||
"name": "webidl2js", | ||
"version": "3.0.2", | ||
"version": "4.0.0", | ||
"description": "Auto-generates class structures for WebIDL specifications", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
31619
14
920