From cacfccadf3784f05123402f5b7e739c85ac26f35 Mon Sep 17 00:00:00 2001 From: Zirro Date: Sun, 20 Aug 2017 22:16:00 +0200 Subject: [PATCH] Add support for [LegacyArrayClass] --- lib/constructs/interface.js | 4 ++ test/__snapshots__/test.js.snap | 119 ++++++++++++++++++++++++++++++++ test/cases/LegacyArrayClass.idl | 4 ++ 3 files changed, 127 insertions(+) create mode 100644 test/cases/LegacyArrayClass.idl diff --git a/lib/constructs/interface.js b/lib/constructs/interface.js index 67f61626..b911b17f 100644 --- a/lib/constructs/interface.js +++ b/lib/constructs/interface.js @@ -311,6 +311,10 @@ class Interface { Object.setPrototypeOf(${this.name}.prototype, ${this.idl.inheritance}.interface.prototype); Object.setPrototypeOf(${this.name}, ${this.idl.inheritance}.interface); `; + } else if (utils.getExtAttr(this.idl.extAttrs, "LegacyArrayClass")) { + this.str += ` + Object.setPrototypeOf(${this.name}.prototype, Array.prototype); + `; } this.str += ` diff --git a/test/__snapshots__/test.js.snap b/test/__snapshots__/test.js.snap index 8c992727..de29d76a 100644 --- a/test/__snapshots__/test.js.snap +++ b/test/__snapshots__/test.js.snap @@ -236,6 +236,125 @@ const Impl = require(\\"../implementations/Factory.js\\"); " `; +exports[`LegacyArrayClass.idl 1`] = ` +"\\"use strict\\"; + +const conversions = require(\\"webidl-conversions\\"); +const utils = require(\\"./utils.js\\"); + +const impl = utils.implSymbol; + +function LegacyArrayClass() { + throw new TypeError(\\"Illegal constructor\\"); +} + +Object.setPrototypeOf(LegacyArrayClass.prototype, Array.prototype); + +Object.defineProperty(LegacyArrayClass, \\"prototype\\", { + value: LegacyArrayClass.prototype, + writable: false, + enumerable: false, + configurable: false +}); + +Object.defineProperty(LegacyArrayClass.prototype, \\"length\\", { + get() { + if (!this || !module.exports.is(this)) { + throw new TypeError(\\"Illegal invocation\\"); + } + + return this[impl][\\"length\\"]; + }, + + enumerable: true, + configurable: true +}); + +Object.defineProperty(LegacyArrayClass.prototype, Symbol.toStringTag, { + value: \\"LegacyArrayClass\\", + writable: false, + enumerable: false, + configurable: true +}); + +const iface = { + mixedInto: [], + is(obj) { + 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; + }, + convert(obj, { context = \\"The provided value\\" } = {}) { + if (module.exports.is(obj)) { + return utils.implForWrapper(obj); + } + throw new TypeError(\`\${context} is not of type 'LegacyArrayClass'.\`); + }, + + create(constructorArgs, privateData) { + let obj = Object.create(LegacyArrayClass.prototype); + obj = this.setup(obj, constructorArgs, privateData); + return obj; + }, + createImpl(constructorArgs, privateData) { + let obj = Object.create(LegacyArrayClass.prototype); + obj = this.setup(obj, constructorArgs, privateData); + return utils.implForWrapper(obj); + }, + _internalSetup(obj) {}, + setup(obj, constructorArgs, privateData) { + if (!privateData) privateData = {}; + + privateData.wrapper = obj; + + this._internalSetup(obj); + Object.defineProperty(obj, impl, { + value: new Impl.implementation(constructorArgs, privateData), + writable: false, + enumerable: false, + configurable: true + }); + + obj[impl][utils.wrapperSymbol] = obj; + if (Impl.init) { + Impl.init(obj[impl], privateData); + } + return obj; + }, + interface: LegacyArrayClass, + expose: { + Window: { LegacyArrayClass } + } +}; // iface +module.exports = iface; + +const Impl = require(\\"../implementations/LegacyArrayClass.js\\"); +" +`; + exports[`PromiseTypes.idl 1`] = ` "\\"use strict\\"; diff --git a/test/cases/LegacyArrayClass.idl b/test/cases/LegacyArrayClass.idl new file mode 100644 index 00000000..42189ad9 --- /dev/null +++ b/test/cases/LegacyArrayClass.idl @@ -0,0 +1,4 @@ +[LegacyArrayClass] +interface LegacyArrayClass { + readonly attribute unsigned long length; +};