Socket
Socket
Sign inDemoInstall

webidl2js

Package Overview
Dependencies
Maintainers
6
Versions
57
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

webidl2js - npm Package Compare versions

Comparing version 10.0.0 to 11.0.0

4

lib/constructs/attribute.js

@@ -14,3 +14,3 @@ "use strict";

this.idl = idl;
this.static = idl.static;
this.static = idl.special === "static";
}

@@ -113,3 +113,3 @@

if (!this.static && this.idl.stringifier) {
if (!this.static && this.idl.special === "stringifier") {
addMethod("toString", [], `

@@ -116,0 +116,0 @@ if (!this || !module.exports.is(this)) {

@@ -69,3 +69,2 @@ "use strict";

this.name = idl.name;
this.factory = Boolean(utils.getExtAttr(this.idl.extAttrs, "WebIDL2JSFactory"));
for (const member of this.idl.members) {

@@ -104,3 +103,3 @@ member.definingInterface = this.name;

this.isGlobal = Boolean(global);
if (global && (!global.rhs || global.arguments)) {
if (global && !global.rhs) {
throw new Error(`[Global] must take an identifier or an identifier list in interface ${this.name}`);

@@ -195,3 +194,3 @@ }

if (member.type === "operation") {
if (member.getter) {
if (member.special === "getter") {
let msg = `Invalid getter ${member.name ? `"${member.name}" ` : ""}on interface ${this.name}`;

@@ -219,3 +218,3 @@ if (member.definingInterface !== this.name) {

}
if (member.setter) {
if (member.special === "setter") {
let msg = `Invalid setter ${member.name ? `"${member.name}" ` : ""}on interface ${this.name}`;

@@ -244,3 +243,3 @@ if (member.definingInterface !== this.name) {

}
if (member.deleter) {
if (member.special === "deleter") {
let msg = `Invalid deleter ${member.name ? `"${member.name}" ` : ""}on interface ${this.name}`;

@@ -272,3 +271,3 @@ if (member.definingInterface !== this.name) {

case "operation":
key = member.static ? "staticOperations" : "operations";
key = member.special === "static" ? "staticOperations" : "operations";
if (member.name) {

@@ -283,3 +282,3 @@ if (!this[key].has(member.name)) {

case "attribute":
key = member.static ? "staticAttributes" : "attributes";
key = member.special === "static" ? "staticAttributes" : "attributes";
this[key].set(member.name, new Attribute(this.ctx, this, member));

@@ -304,3 +303,3 @@ break;

if (member.stringifier) {
if (member.special === "stringifier") {
let msg = `Invalid stringifier ${member.name ? `"${member.name}" ` : ""}on interface ${this.name}`;

@@ -331,3 +330,3 @@ if (member.definingInterface !== this.name) {

} else if (member.type === "attribute") {
if (member.static) {
if (member.special === "static") {
throw new Error(msg + "keyword cannot be placed on static attribute");

@@ -371,3 +370,3 @@ }

for (const member of this.allMembers()) {
if (forbiddenMembers.has(member.name) || (member.name && member.name[0] === "_")) {
if (forbiddenMembers.has(member.name)) {
let msg = `${member.name} is forbidden in interface ${this.name}`;

@@ -509,2 +508,3 @@ if (member.definingInterface !== this.name) {

this.requires.addRaw("impl", "utils.implSymbol");
this.requires.addRaw("ctorRegistry", "utils.ctorRegistrySymbol");

@@ -602,3 +602,3 @@ if (this.idl.inheritance !== null) {

if (unsupportedValue) {
const func = this.indexedGetter.name !== null ? `.${this.indexedGetter.name}` : "[utils.indexedGet]";
const func = this.indexedGetter.name ? `.${this.indexedGetter.name}` : "[utils.indexedGet]";
const value = indexedValue || `${O}[impl]${func}(${index})`;

@@ -616,3 +616,3 @@ return `${value} !== ${unsupportedValue}`;

if (unsupportedValue) {
const func = this.namedGetter.name !== null ? `.${this.namedGetter.name}` : "[utils.namedGet]";
const func = this.namedGetter.name ? `.${this.namedGetter.name}` : "[utils.namedGet]";
const value = namedValue || `${O}[impl]${func}(${P})`;

@@ -654,3 +654,3 @@ return `${value} !== ${unsupportedValue}`;

if (this.indexedSetter.name === null) {
if (!this.indexedSetter.name) {
str += `

@@ -686,3 +686,3 @@ const creating = !(${supportsPropertyIndex(O, "index")});

if (this.namedSetter.name === null) {
if (!this.namedSetter.name) {
str += `

@@ -797,3 +797,3 @@ const creating = !(${supportsPropertyName(O, P)});

const func = this.indexedGetter.name !== null ? `.${this.indexedGetter.name}` : "[utils.indexedGet]";
const func = this.indexedGetter.name ? `.${this.indexedGetter.name}` : "[utils.indexedGet]";
let preamble = "";

@@ -824,3 +824,3 @@ let condition;

if (this.supportsNamedProperties) {
const func = this.namedGetter.name !== null ? `.${this.namedGetter.name}` : "[utils.namedGet]";
const func = this.namedGetter.name ? `.${this.namedGetter.name}` : "[utils.namedGet]";
const enumerable = !utils.getExtAttr(this.idl.extAttrs, "LegacyUnenumerableNamedProperties");

@@ -909,3 +909,3 @@ let preamble = "";

const func = this.indexedGetter.name !== null ? `.${this.indexedGetter.name}` : "[utils.indexedGet]";
const func = this.indexedGetter.name ? `.${this.indexedGetter.name}` : "[utils.indexedGet]";
let preamble = "";

@@ -1000,3 +1000,3 @@ let condition;

for (const m of this.allMembers()) {
if ((m.type === "attribute" || m.type === "operation") && !m.static &&
if ((m.type === "attribute" || m.type === "operation") && m.special !== "static" &&
utils.getExtAttr(m.extAttrs, "Unforgeable")) {

@@ -1081,3 +1081,3 @@ unforgeable.add(m.name);

} else {
const func = this.namedDeleter.name !== null ? `.${this.namedDeleter.name}` : "[utils.namedDelete]";
const func = this.namedDeleter.name ? `.${this.namedDeleter.name}` : "[utils.namedDelete]";
if (this.namedDeleter.idlType.idlType === "bool") {

@@ -1116,37 +1116,19 @@ this.str += `

generateIface() {
const shouldExposeRoot = !utils.getExtAttr(this.idl.extAttrs, "NoInterfaceObject");
this.str += `
create(globalObject, constructorArgs, privateData) {
if (globalObject[ctorRegistry] === undefined) {
throw new Error('Internal error: invalid global object');
}
const exposedMap = {};
if (shouldExposeRoot) {
let exposedOn = ["Window"];
const exposedAttrs = this.idl.extAttrs
.filter(attr => attr.name === "Exposed");
if (exposedAttrs.length !== 0) {
if (typeof exposedAttrs[0].rhs.value === "string") {
exposedAttrs[0].rhs.value = [exposedAttrs[0].rhs.value];
const ctor = globalObject[ctorRegistry]["${this.name}"];
if (ctor === undefined) {
throw new Error('Internal error: constructor ${this.name} is not installed on the passed global object');
}
exposedOn = exposedAttrs[0].rhs.value;
}
for (let i = 0; i < exposedOn.length; ++i) {
if (!exposedMap[exposedOn[i]]) {
exposedMap[exposedOn[i]] = [];
}
exposedMap[exposedOn[i]].push(this.name);
}
}
const exposers = [];
for (let keys = Object.keys(exposedMap), i = 0; i < keys.length; ++i) {
exposers.push(keys[i] + ": { " + exposedMap[keys[i]].join(", ") + " }");
}
this.str += `
create(constructorArgs, privateData) {
let obj = Object.create(${this.name}.prototype);
obj = this.setup(obj, constructorArgs, privateData);
let obj = Object.create(ctor.prototype);
obj = iface.setup(obj, globalObject, constructorArgs, privateData);
return obj;
},
createImpl(constructorArgs, privateData) {
let obj = Object.create(${this.name}.prototype);
obj = this.setup(obj, constructorArgs, privateData);
createImpl(globalObject, constructorArgs, privateData) {
const obj = iface.create(globalObject, constructorArgs, privateData);
return utils.implForWrapper(obj);

@@ -1167,22 +1149,8 @@ },

},
setup(obj, constructorArgs, privateData) {
if (!privateData) privateData = {};
`;
if (this.factory) {
this.str += `
for (var prop in defaultPrivateData) {
if (!(prop in privateData)) {
privateData[prop] = defaultPrivateData[prop];
}
}
`;
}
this.str += `
setup(obj, globalObject, constructorArgs = [], privateData = {}) {
privateData.wrapper = obj;
this._internalSetup(obj);
iface._internalSetup(obj);
Object.defineProperty(obj, impl, {
value: new Impl.implementation(constructorArgs, privateData),
value: new Impl.implementation(globalObject, constructorArgs, privateData),
configurable: true

@@ -1203,6 +1171,2 @@ });

},
interface: ${this.name},
expose: {
${exposers.join(",\n ")}
}
`;

@@ -1230,6 +1194,11 @@ }

const passArgs = conversions.hasArgs ? ", args" : "";
const setupArgs = [
"Object.create(new.target.prototype)",
"globalObject",
conversions.hasArgs ? "args" : "undefined"
];
body = `
${conversions.body}
return iface.setup(Object.create(new.target.prototype)${passArgs});
return iface.setup(${formatArgs(setupArgs)});
`;

@@ -1307,3 +1276,3 @@ } else {

const propName = utils.stringifyPropertyName(name);
const propName = utils.stringifyPropertyKey(name);
if (type === "regular") {

@@ -1322,3 +1291,3 @@ addOne(propName, args, body);

for (const [name, { type, args, body }] of this._outputStaticMethods) {
const propName = utils.stringifyPropertyName(name);
const propName = utils.stringifyPropertyKey(name);
if (type === "regular") {

@@ -1360,3 +1329,3 @@ addOne(`static ${propName}`, args, body);

}
protoProps.set(utils.stringifyPropertyName(name), descriptorModifier);
protoProps.set(utils.stringifyPropertyKey(name), descriptorModifier);
}

@@ -1369,3 +1338,3 @@

}
classProps.set(utils.stringifyPropertyName(name), descriptorModifier);
classProps.set(utils.stringifyPropertyKey(name), descriptorModifier);
}

@@ -1380,3 +1349,3 @@

getPropertyDescriptorModifier(defaultDefinePropertyDescriptor, descriptor, "regular", body);
protoProps.set(utils.stringifyPropertyName(name), descriptorModifier);
protoProps.set(utils.stringifyPropertyKey(name), descriptorModifier);
}

@@ -1387,3 +1356,3 @@

getPropertyDescriptorModifier(defaultDefinePropertyDescriptor, descriptor, "regular", body);
classProps.set(utils.stringifyPropertyName(name), descriptorModifier);
classProps.set(utils.stringifyPropertyKey(name), descriptorModifier);
}

@@ -1417,3 +1386,3 @@

const propName = utils.stringifyPropertyName(name);
const propName = utils.stringifyPropertyKey(name);
if (type === "regular") {

@@ -1434,3 +1403,3 @@ addOne(propName, args, body);

}
props.set(utils.stringifyPropertyName(name), descriptorModifier);
props.set(utils.stringifyPropertyKey(name), descriptorModifier);
}

@@ -1443,3 +1412,3 @@

const propName = utils.stringifyPropertyName(name);
const propName = utils.stringifyPropertyKey(name);
methods.push(`${propName}: ${body}`);

@@ -1473,17 +1442,21 @@

generate() {
this.generateIterator();
generateInstall() {
const { idl, name } = this;
if (this.factory) {
this.str += `
install(globalObject) {
`;
if (idl.inheritance) {
this.str += `
module.exports = {
createInterface: function (defaultPrivateData = {}) {
if (globalObject.${idl.inheritance} === undefined) {
throw new Error('Internal error: attempting to evaluate ${name} before ${idl.inheritance}');
}
`;
}
const ext = this.idl.inheritance ? ` extends ${this.idl.inheritance}.interface` : "";
this.str += `class ${this.name}${ext} {`;
const ext = idl.inheritance ? ` extends globalObject.${idl.inheritance}` : "";
this.str += `class ${name}${ext} {`;
this.generateOffInstanceMethods();
this.str += "}";

@@ -1494,25 +1467,32 @@

this.str += `
if (globalObject[ctorRegistry] === undefined) {
globalObject[ctorRegistry] = Object.create(null);
}
globalObject[ctorRegistry]["${name}"] = ${name};
Object.defineProperty(globalObject, "${name}", {
configurable: true,
writable: true,
value: ${name}
});
},
`;
}
generate() {
this.generateIterator();
this.str += `
const iface = {
`;
if (this.factory) {
this.generateIface();
this.str += `
}; // iface
return iface;
}, // createInterface
`;
this.generateExport();
this.str += `
}; // module.exports
`;
} else {
this.generateExport();
this.generateIface();
this.str += `
}; // iface
module.exports = iface;
`;
}
this.generateExport();
this.generateIface();
this.generateInstall();
this.str += `
}; // iface
module.exports = iface;
`;
this.generateMixins();

@@ -1519,0 +1499,0 @@ this.generateRequires();

@@ -15,3 +15,3 @@ "use strict";

this.name = idl.name;
this.static = idl.static;
this.static = idl.special === "static";
}

@@ -18,0 +18,0 @@

@@ -15,2 +15,3 @@ "use strict";

const sameObjectCaches = Symbol("SameObject caches");
const ctorRegistrySymbol = Symbol.for("[webidl2js] constructor registry");

@@ -84,2 +85,3 @@ function getSameObject(wrapper, prop, creator) {

getSameObject,
ctorRegistrySymbol,
wrapperForImpl,

@@ -86,0 +88,0 @@ implForWrapper,

@@ -58,6 +58,3 @@ "use strict";

return idlType;
} else if (idlType.generic === "sequence" || idlType.generic === "FrozenArray" || idlType.generic === "Promise") {
idlType.idlType = resolveType(ctx, idlType.idlType, stack);
return idlType;
} else if (idlType.generic === "record") {
} else if (idlType.generic) {
idlType.idlType = idlType.idlType.map(t => resolveType(ctx, t, stack));

@@ -283,3 +280,3 @@ return idlType;

function generateSequence() {
const conv = generateTypeConversion(ctx, "nextItem", idlType.idlType, [], parentName,
const conv = generateTypeConversion(ctx, "nextItem", idlType.idlType[0], [], parentName,
`${errPrefix} + "'s element"`);

@@ -334,7 +331,7 @@ requires.merge(conv.requires);

let handler;
if (idlType.idlType.idlType === "void") {
if (idlType.idlType[0].idlType === "void") {
// Do nothing.
handler = "";
} else {
const conv = generateTypeConversion(ctx, "value", idlType.idlType, [], parentName,
const conv = generateTypeConversion(ctx, "value", idlType.idlType[0], [], parentName,
`${errPrefix} + " promise value"`);

@@ -341,0 +338,0 @@ requires.merge(conv.requires);

@@ -36,22 +36,30 @@ "use strict";

function isOnInstance(memberIDL, interfaceIDL) {
return !memberIDL.static && (getExtAttr(memberIDL.extAttrs, "Unforgeable") || isGlobal(interfaceIDL));
return memberIDL.special !== "static" && (getExtAttr(memberIDL.extAttrs, "Unforgeable") || isGlobal(interfaceIDL));
}
function stringifyPropertyName(propName) {
if (typeof propName === "symbol") {
const desc = String(propName).replace(/^Symbol\((.*)\)$/, "$1");
if (!desc.startsWith("Symbol.")) {
throw new Error(`Internal error: Unsupported property name ${String(propName)}`);
}
return `[${desc}]`;
function symbolName(symbol) {
const desc = String(symbol).replace(/^Symbol\((.*)\)$/, "$1");
if (!desc.startsWith("Symbol.")) {
throw new Error(`Internal error: Unsupported property name ${String(symbol)}`);
}
return desc;
}
function propertyName(name) {
// All Web IDL identifiers are valid JavaScript PropertyNames, other than those with '-'.
const isJSIdentifier = !propName.includes("-");
const isJSIdentifier = !name.includes("-");
if (isJSIdentifier) {
return propName;
return name;
}
return JSON.stringify(propName);
return JSON.stringify(name);
}
function stringifyPropertyKey(prop) {
return typeof prop === "symbol" ? `[${symbolName(prop)}]` : propertyName(prop);
}
function stringifyPropertyName(prop) {
return typeof prop === "symbol" ? symbolName(prop) : JSON.stringify(propertyName(prop));
}
class RequiresMap extends Map {

@@ -98,4 +106,5 @@ constructor(ctx) {

isOnInstance,
stringifyPropertyKey,
stringifyPropertyName,
RequiresMap
};
{
"name": "webidl2js",
"version": "10.0.0",
"version": "11.0.0",
"description": "Auto-generates class structures for WebIDL specifications",

@@ -9,8 +9,8 @@ "main": "lib/transformer.js",

"pn": "^1.1.0",
"prettier": "^1.18.2",
"prettier": "^1.19.1",
"webidl-conversions": "^4.0.0",
"webidl2": "^10.3.3"
"webidl2": "^23.10.1"
},
"devDependencies": {
"eslint": "^6.5.1",
"eslint": "^6.6.0",
"jest": "^24.9.0"

@@ -24,3 +24,6 @@ },

"jest": {
"testEnvironment": "node"
"testEnvironment": "node",
"watchPathIgnorePatterns": [
"<rootDir>/test/output"
]
},

@@ -27,0 +30,0 @@ "engines": {

@@ -30,2 +30,3 @@ # JavaScript bindings generator for Web IDL

const impl = require("./utils.js").implSymbol;
const ctorRegistry = require("./utils.js").ctorRegistrySymbol;

@@ -68,6 +69,5 @@ const Impl = require("./SomeInterface-impl.js").implementation;

exports.interface = SomeInterface;
exports.create = (constructorArgs = [], privateData = {}) => {
const obj = Object.create(SomeInterface.prototype);
exports.create = (globalObject, constructorArgs = [], privateData = {}) => {
const ctor = globalObject[ctorRegistry].SomeInterface;
const obj = Object.create(ctor.prototype);
obj[impl] = new Impl(constructorArgs, privateData);

@@ -156,42 +156,24 @@ return obj;

#### `create(constructorArgs, privateData)`
#### `install(globalObject)`
Creates a new instance of the wrapper class and corresponding implementation class, passing in the `constructorArgs` array and `privateData` object to the implementation class constructor. Then returns the wrapper class.
This method creates a brand new wrapper constructor and prototype and attach it to the passed `globalObject`. It also registers the created constructor with the `globalObject`'s global constructor registry, which makes `create()`, `createImpl()`, and `setup()` work. (Thus, it is important to invoke `install()` before invoking those methods, as otherwise they will throw.)
#### `create(globalObject, constructorArgs, privateData)`
Creates a new instance of the wrapper class and corresponding implementation class, passing in the `globalObject`, the `constructorArgs` array and `privateData` object to the implementation class constructor. Then returns the wrapper class.
This is useful in other parts of your program that are not implementation class files, but instead want to interface with them from the outside. It's also mostly useful when creating instances of classes that do not have a `[Constructor]`, i.e. are not constructible via their wrapper class constructor.
#### `createImpl(constructorArgs, privateData)`
#### `createImpl(globalObject, constructorArgs, privateData)`
Creates a new instance of the wrapper class and corresponding implementation class, passing in the `constructorArgs` array and `privateData` object to the implementation class constructor. Then returns the implementation class.
Creates a new instance of the wrapper class and corresponding implementation class, passing in the `globalObject`, the `constructorArgs` array and `privateData` object to the implementation class constructor. Then returns the implementation class.
This is useful inside implementation class files, where it is easiest to only deal with impls, not wrappers.
#### `setup(obj, constructorArgs, privateData)`
#### `setup(obj, globalObject, constructorArgs, privateData)`
This function is mostly used internally, and almost never should be called by your code. The one exception is if you need to inherit from a wrapper class corresponding to an interface without a `[Constructor]`, from a non-webidl2js-generated class. Then, you can call `SuperClass.setup(this, [], privateData)` as a substitute for doing `super()` (which would throw).
This function is mostly used internally, and almost never should be called by your code. The one exception is if you need to inherit from a wrapper class corresponding to an interface without a `[Constructor]`, from a non-webidl2js-generated class. Then, you can call `SuperClass.setup(this, globalObject, [], privateData)` as a substitute for doing `super()` (which would throw).
jsdom does this for `Window`, which is written in custom, non-webidl2js-generated code, but inherits from `EventTarget`, which is generated by webidl2js.
#### `interface`
This export is the wrapper class interface, suitable for example for putting on a global scope or exporting to module consumers who don't know anything about webidl2js.
#### `expose`
This export contains information about where an interface is supposed to be exposed as a property. It takes into account the Web IDL extended attributes `[Expose]` and `[NoInterfaceObject]` to generate a data structure of the form:
```js
{
nameOfGlobal1: {
nameOfInterface: InterfaceClass
},
nameOfGlobal2: {
nameOfInterface: InterfaceClass
},
// etc.
}
```
This format may seem a bit verbose, but eventually when we support `[NamedConstructor]`, there will be potentially more than one key/value pair per global, and it will show its worth.
### For dictionaries

@@ -213,5 +195,6 @@

A constructor for your implementation class, with signature `(constructorArgs, privateData)` can serve several purposes:
A constructor for your implementation class, with signature `(globalObject, constructorArgs, privateData)` can serve several purposes:
- Setting up initial state that will always be used, such as caches or default values
- Keep a reference to the relevant `globalObject` for later consumption.
- Processing constructor arguments `constructorArgs` passed to the wrapper class constructor, if the interface in question has a `[Constructor]` extended attribute.

@@ -254,3 +237,3 @@ - Processing any private data `privateData` which is provided when other parts of your program use the generated `create()` or `createImpl()` exports of the wrapper class file. This is useful for constructing instances with specific state that cannot be constructed via the wrapper class constructor.

Just like static operations, static attributes are defined as properties on the constructor of the implementation class. And just like other attributes, the attribute can either be implemented as an accessor attribute or (if it is readonly) a data attribute. Note that, unless the `[WebIDL2JSFactory]` extended attribute is specified on the interface, any mutations to writable static attributes of the class will reflect on other places that use the same interface.
Just like static operations, static attributes are defined as properties on the constructor of the implementation class. And just like other attributes, the attribute can either be implemented as an accessor attribute or (if it is readonly) a data attribute.

@@ -354,3 +337,3 @@ ### toString method implementing IDL stringifier

- `[EnforceRange]`
- `[Exposed]` and `[NoInterfaceObject]` (by exporting metadata on where/whether it is exposed)
- `[Exposed]` (by exporting metadata on where/whether it is exposed)
- `[LegacyArrayClass]`

@@ -395,8 +378,2 @@ - `[LegacyUnenumerableNamedProperties]`

### `[WebIDL2JSFactory]`
This extended attribute can be applied to interfaces to cause them to generate a factory that generates wrapper classes, instead of generating a single wrapper class.
It is currently used by [jsdom](https://github.com/tmpvar/jsdom) for classes which need to specialize their behavior per `Window` object; by default [jsdom shares classes across all `Window`s](https://github.com/tmpvar/jsdom#shared-constructors-and-prototypes), but with `[WebIDL2JSFactory]`, an exception can be made for certain classes that need it.
### `[WebIDL2JSValueAsUnsupported=value]`

@@ -403,0 +380,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc