diff --git a/lib/internal/webidl.js b/lib/internal/webidl.js index 9e4b20435bf831..4f0fe6d8543b1e 100644 --- a/lib/internal/webidl.js +++ b/lib/internal/webidl.js @@ -12,6 +12,7 @@ const { NumberMAX_SAFE_INTEGER, NumberMIN_SAFE_INTEGER, ObjectAssign, + ObjectPrototypeIsPrototypeOf, SafeSet, String, SymbolIterator, @@ -20,6 +21,7 @@ const { const { codes: { + ERR_INVALID_ARG_TYPE, ERR_INVALID_ARG_VALUE, }, } = require('internal/errors'); @@ -275,7 +277,7 @@ function createSequenceConverter(converter) { const val = converter(res.value, { __proto__: null, ...opts, - context: `${opts.context}, index ${array.length}`, + context: `${opts.context}[${array.length}]`, }); ArrayPrototypePush(array, val); }; @@ -283,12 +285,26 @@ function createSequenceConverter(converter) { }; } +// https://webidl.spec.whatwg.org/#js-interface +function createInterfaceConverter(name, I) { + return (V, opts = kEmptyObject) => { + // 1. If V implements I, then return the IDL interface type value that + // represents a reference to that platform object. + if (ObjectPrototypeIsPrototypeOf(I, V)) return V; + // 2. Throw a TypeError. + throw new ERR_INVALID_ARG_TYPE( + typeof opts.context === 'string' ? opts.context : 'value', name, V, + ); + }; +} + module.exports = { type, converters, convertToInt, createEnumConverter, + createInterfaceConverter, createSequenceConverter, evenRound, makeException,