Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
lib: implement interface converter in webidl
  • Loading branch information
jazelly committed Sep 19, 2024
commit ce65d653e50ad71a9398c713e23babeedee9bb3b
18 changes: 17 additions & 1 deletion lib/internal/webidl.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const {
NumberMAX_SAFE_INTEGER,
NumberMIN_SAFE_INTEGER,
ObjectAssign,
ObjectPrototypeIsPrototypeOf,
SafeSet,
String,
SymbolIterator,
Expand All @@ -20,6 +21,7 @@ const {

const {
codes: {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
},
} = require('internal/errors');
Expand Down Expand Up @@ -275,20 +277,34 @@ function createSequenceConverter(converter) {
const val = converter(res.value, {
__proto__: null,
...opts,
context: `${opts.context}, index ${array.length}`,
Comment thread
jazelly marked this conversation as resolved.
context: `${opts.context}[${array.length}]`,
});
ArrayPrototypePush(array, val);
};
return array;
};
}

// https://webidl.spec.whatwg.org/#js-interface
function createInterfaceConverter(name, I) {
return (V, opts = kEmptyObject) => {
Comment thread
KhafraDev marked this conversation as resolved.
// 1. If V implements I, then return the IDL interface type value that
Comment thread
jazelly marked this conversation as resolved.
// 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,
Expand Down