mirror of
https://github.com/denoland/deno.git
synced 2024-12-26 09:10:40 -05:00
chore: add internal webidl helpers for enums and nullables (#9504)
Co-authored-by: Luca Casonato <lucacasonato@yahoo.com>
This commit is contained in:
parent
0cf952e7fb
commit
a6beab8248
2 changed files with 52 additions and 3 deletions
|
@ -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);
|
||||
|
|
19
op_crates/web/internal.d.ts
vendored
19
op_crates/web/internal.d.ts
vendored
|
@ -209,13 +209,28 @@ declare namespace globalThis {
|
|||
required?: boolean;
|
||||
}
|
||||
|
||||
/**ie
|
||||
* Assert that the a function has at least a required amount of arguments.
|
||||
/**
|
||||
* Create a converter for dictionaries.
|
||||
*/
|
||||
declare function createDictionaryConverter<T>(
|
||||
name: string,
|
||||
...dictionaries: Dictionary[]
|
||||
): (v: any, opts: ValueConverterOpts) => T;
|
||||
|
||||
/**
|
||||
* Create a converter for enums.
|
||||
*/
|
||||
declare function createEnumConverter(
|
||||
name: string,
|
||||
values: string[],
|
||||
): (v: any, opts: ValueConverterOpts) => string;
|
||||
|
||||
/**
|
||||
* Create a converter that makes the contained type nullable.
|
||||
*/
|
||||
declare function createNullableConverter<T>(
|
||||
converter: (v: any, opts: ValueConverterOpts) => T,
|
||||
): (v: any, opts: ValueConverterOpts) => T | null;
|
||||
}
|
||||
|
||||
declare var url: {
|
||||
|
|
Loading…
Reference in a new issue