diff options
author | Anonymous <65428781+00ff0000red@users.noreply.github.com> | 2021-02-15 15:13:46 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-16 00:13:46 +0100 |
commit | a6beab824815cededf0ba9fc7904d3b00fde75e4 (patch) | |
tree | 431c5b47a186be5e7464194ed7e1fb839be4d286 /op_crates/web/00_webidl.js | |
parent | 0cf952e7fbcabb90b0b9999eabd12d22a458f933 (diff) |
chore: add internal webidl helpers for enums and nullables (#9504)
Co-authored-by: Luca Casonato <lucacasonato@yahoo.com>
Diffstat (limited to 'op_crates/web/00_webidl.js')
-rw-r--r-- | op_crates/web/00_webidl.js | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/op_crates/web/00_webidl.js b/op_crates/web/00_webidl.js index 72c58c377..8831cb71e 100644 --- a/op_crates/web/00_webidl.js +++ b/op_crates/web/00_webidl.js @@ -662,10 +662,44 @@ }; } - window.__bootstrap = window.__bootstrap || {}; + // https://heycam.github.io/webidl/#es-enumeration + function createEnumConverter(name, values) { + const E = new Set(values); + + return function (V, opts = {}) { + const S = String(V); + + if (!E.has(S)) { + throw makeException( + TypeError, + `The provided value '${V}' is not a valid enum value of type ${name}.`, + opts, + ); + } + + return S; + }; + } + + function createNullableConverter(converter) { + return (V, opts = {}) => { + // FIXME: If Type(V) is not Object, and the conversion to an IDL value is + // being performed due to V being assigned to an attribute whose type is a + // nullable callback function that is annotated with + // [LegacyTreatNonObjectAsNull], then return the IDL nullable type T? + // value null. + + if (V === null || V === undefined) return null; + return converter(V, opts); + }; + } + + window.__bootstrap ??= {}; window.__bootstrap.webidl = { converters, requiredArguments, createDictionaryConverter, + createEnumConverter, + createNullableConverter, }; })(this); |