Skip to content

Commit

Permalink
fix(webgpu): add webidl records and simple unions (denoland#9698)
Browse files Browse the repository at this point in the history
The only functional user facing difference is that this commit allows the
use SPIRV shaders, not just WGSL. This matches FF and Chrome Canary.
  • Loading branch information
lucacasonato committed Mar 8, 2021
1 parent 0bc488c commit c009dad
Show file tree
Hide file tree
Showing 3 changed files with 249 additions and 110 deletions.
29 changes: 29 additions & 0 deletions op_crates/web/00_webidl.js
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,13 @@

converters.VoidFunction = convertCallbackFunction;

converters["UVString?"] = createNullableConverter(
converters.USVString,
);
converters["sequence<double>"] = createSequenceConverter(
converters["double"],
);

function requiredArguments(length, required, opts = {}) {
if (length < required) {
const errMsg = `${
Expand Down Expand Up @@ -737,6 +744,26 @@
};
}

function createRecordConverter(keyConverter, valueConverter) {
return (V, opts) => {
if (typeof V !== "object") {
throw makeException(
TypeError,
"can not be converted to dictionary.",
opts,
);
}
const result = {};
for (const key of V) {
const typedKey = keyConverter(key, opts);
const value = V[key];
const typedValue = valueConverter(value, opts);
result[typedKey] = typedValue;
}
return result;
};
}

const brand = Symbol("[[webidl.brand]]");

function createInterfaceConverter(name, prototype) {
Expand Down Expand Up @@ -766,12 +793,14 @@

window.__bootstrap ??= {};
window.__bootstrap.webidl = {
makeException,
converters,
requiredArguments,
createDictionaryConverter,
createEnumConverter,
createNullableConverter,
createSequenceConverter,
createRecordConverter,
createInterfaceConverter,
brand,
createBranded,
Expand Down
18 changes: 18 additions & 0 deletions op_crates/web/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ declare namespace globalThis {
*/
context: string;
}
declare function makeException(
ErrorType: any,
message: string,
opts: ValueConverterOpts,
): any;
declare interface IntConverterOpts extends ValueConverterOpts {
/**
* Wether to throw if the number is outside of the acceptable values for
Expand Down Expand Up @@ -191,6 +196,8 @@ declare namespace globalThis {
* Convert a value into a `VoidFunction` (() => void).
*/
VoidFunction(v: any, opts?: ValueConverterOpts): () => void;
["UVString?"](v: any, opts?: ValueConverterOpts): string | null;
["sequence<double>"](v: any, opts?: ValueConverterOpts): number[];

[type: string]: (v: any, opts: ValueConverterOpts) => any;
};
Expand Down Expand Up @@ -268,6 +275,17 @@ declare namespace globalThis {
name: string,
prototype: any,
): (v: any, opts: ValueConverterOpts) => any;

declare function createRecordConverter<
K extends string | number | symbol,
V,
>(
keyConverter: (v: any, opts: ValueConverterOpts) => K,
valueConverter: (v: any, opts: ValueConverterOpts) => V,
): (
v: Record<K, V>,
opts: ValueConverterOpts,
) => any;
}

declare var eventTarget: {
Expand Down
Loading

0 comments on commit c009dad

Please sign in to comment.