webidl2js icon indicating copy to clipboard operation
webidl2js copied to clipboard

Monkey patch partial interface definitions

Open danyao opened this issue 7 years ago • 4 comments

When a spec defines a partial addition to an existing interface, it will be useful if webidl2js can generate the monkey patch required to implement the addition.

For example, from the Credential Management Level 1 spec:

partial interface Navigator {
  [SecureContext, SameObject] readonly attribute CredentialsContainer credentials;
};

Currently webild2js assumes that Navigator is defined somewhere else in the provided webidl and errors out in parsing as it's not found:

TypeError: Cannot read property 'idl' of undefined
    at Transformer._parse (/Users/danyao/sandbox/webidl2js/e2e/node_modules/webidl2js/lib/transformer.js:143:58)
    at Transformer.<anonymous> (/Users/danyao/sandbox/webidl2js/e2e/node_modules/webidl2js/lib/transformer.js:250:12)
    at Generator.next (<anonymous>)
    at onFulfilled (/Users/danyao/sandbox/webidl2js/e2e/node_modules/co/index.js:65:19)
    at <anonymous>

What we need is probably something like this:

const CredentialsContainer = require("./CredentialsContainer");
navigator.credentials = Object.create(CredentialsContainer.interface.prototype);

We might need to also account for the fact that CredentialsContainer may be provided natively by the UA, so may not have the interface attribute (see https://github.com/jsdom/webidl2js/issues/81).

danyao avatar Oct 11 '17 17:10 danyao

Can you work around this by adding all the necessary dependent idl interfaces and throwing away the output of the unnecessary files?

Sebmaster avatar Oct 12 '17 00:10 Sebmaster

Oh, if you're monkey patching anyways, do you even need the partial Navigator idl at all?

Sebmaster avatar Oct 12 '17 00:10 Sebmaster

It seems to me that between this and #81 there's room for a "assume existing globals" mode (or "polyfill mode") which makes this sort of thing seamlessly work.

domenic avatar Oct 12 '17 00:10 domenic

An polyfill mode would be great!

For my use case, I'm using webidl2js to generate JavaScript polyfills for use in a browser. Adding necessary dependent IDL interfaces and throwing away the output can work, but would be tricky to automate into the build process.

The partial Navigator IDL is convenient so I can use the same IDL source for both interface generating using webidl2js and running Web Platform Test idlharness test to cover the entire interface definition.

danyao avatar Oct 12 '17 04:10 danyao