Skip to content

Commit a5a946d

Browse files
jazellynodejs-github-bot
authored andcommitted
lib: implement interface converter in webidl
PR-URL: #54965 Fixes: #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>
1 parent 27dab9d commit a5a946d

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

lib/internal/webidl.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const {
1212
NumberMAX_SAFE_INTEGER,
1313
NumberMIN_SAFE_INTEGER,
1414
ObjectAssign,
15+
ObjectPrototypeIsPrototypeOf,
1516
SafeSet,
1617
String,
1718
SymbolIterator,
@@ -20,6 +21,7 @@ const {
2021

2122
const {
2223
codes: {
24+
ERR_INVALID_ARG_TYPE,
2325
ERR_INVALID_ARG_VALUE,
2426
},
2527
} = require('internal/errors');
@@ -275,20 +277,34 @@ function createSequenceConverter(converter) {
275277
const val = converter(res.value, {
276278
__proto__: null,
277279
...opts,
278-
context: `${opts.context}, index ${array.length}`,
280+
context: `${opts.context}[${array.length}]`,
279281
});
280282
ArrayPrototypePush(array, val);
281283
};
282284
return array;
283285
};
284286
}
285287

288+
// https://webidl.spec.whatwg.org/#js-interface
289+
function createInterfaceConverter(name, I) {
290+
return (V, opts = kEmptyObject) => {
291+
// 1. If V implements I, then return the IDL interface type value that
292+
// represents a reference to that platform object.
293+
if (ObjectPrototypeIsPrototypeOf(I, V)) return V;
294+
// 2. Throw a TypeError.
295+
throw new ERR_INVALID_ARG_TYPE(
296+
typeof opts.context === 'string' ? opts.context : 'value', name, V,
297+
);
298+
};
299+
}
300+
286301

287302
module.exports = {
288303
type,
289304
converters,
290305
convertToInt,
291306
createEnumConverter,
307+
createInterfaceConverter,
292308
createSequenceConverter,
293309
evenRound,
294310
makeException,

0 commit comments

Comments
 (0)