0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00

lib: implement interface converter in webidl

PR-URL: https://github.com/nodejs/node/pull/54965
Fixes: https://github.com/nodejs/node/issues/54962
Reviewed-By: Matthew Aitken <maitken033380023@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Jason Zhang 2024-09-17 20:41:02 +09:30 committed by Node.js GitHub Bot
parent 27dab9d916
commit a5a946d8a5

View File

@ -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,