From 67737937e21b530391aacf0974239cc4a8d1f085 Mon Sep 17 00:00:00 2001 From: Jason Zhang Date: Tue, 17 Sep 2024 20:41:02 +0930 Subject: [PATCH] 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 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Matteo Collina Reviewed-By: James M Snell --- lib/internal/webidl.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/internal/webidl.js b/lib/internal/webidl.js index 4b689cf5eb24b2..1278f4cac7fb81 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,