Skip to content

Support generic Uint8Array<ArrayBuffer> #1944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Apr 18, 2025
2 changes: 1 addition & 1 deletion src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ async function emitDom() {
compilerBehavior: {
useIteratorObject: true,
allowUnrelatedSetterType: true,
genericTypedArrays: true,
useGenericTypedArrays: true,
},
},
// ts5.6
Expand Down
28 changes: 8 additions & 20 deletions src/build/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
integerTypes,
baseTypeConversionMap,
assertUnique,
arrayBufferViewTypes,
} from "./helpers.js";
import { collectLegacyNamespaceTypes } from "./legacy-namespace.js";

Expand Down Expand Up @@ -135,7 +136,7 @@ function isEventHandler(p: Browser.Property) {
export interface CompilerBehavior {
useIteratorObject?: boolean;
allowUnrelatedSetterType?: boolean;
genericTypedArrays?: boolean;
useGenericTypedArrays?: boolean;
}

export function emitWebIdl(
Expand Down Expand Up @@ -390,7 +391,9 @@ export function emitWebIdl(

// propagate `any`
const converted = types.map(convertDomTypeToTsTypeWorker);
if (converted.includes("any")) return "any";
if (converted.includes("any")) {
return "any";
}

// convert `ArrayBuffer | SharedArrayBuffer` into `ArrayBufferLike` to be pre-ES2017 friendly.
const arrayBufferIndex = converted.indexOf("ArrayBuffer");
Expand All @@ -412,24 +415,9 @@ export function emitWebIdl(
.map(convertDomTypeToTsType)
.join(", ");

if (!subtypeString && compilerBehavior.genericTypedArrays) {
switch (type) {
case "ArrayBufferView":
case "DataView":
case "Int8Array":
case "Uint8Array":
case "Uint8ClampedArray":
case "Int16Array":
case "Uint16Array":
case "Int32Array":
case "Uint32Array":
case "Float32Array":
case "Float64Array":
case "BigInt64Array":
case "BigUint64Array":
case "Float16Array":
subtypeString = obj.allowShared ? "ArrayBufferLike" : "ArrayBuffer";
break;
if (!subtypeString && compilerBehavior.useGenericTypedArrays) {
if (arrayBufferViewTypes.has(type)) {
subtypeString = obj.allowShared ? "ArrayBufferLike" : "ArrayBuffer";
}
}

Expand Down
11 changes: 7 additions & 4 deletions src/build/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import * as Browser from "./types.js";

// Extended types used but not defined in the spec
export const bufferSourceTypes = new Set([
"ArrayBuffer",
"SharedArrayBuffer",
"ArrayBufferLike",
export const arrayBufferViewTypes = new Set([
"ArrayBufferView",
"DataView",
"Int8Array",
Expand All @@ -17,6 +14,11 @@ export const bufferSourceTypes = new Set([
"Float32Array",
"Float64Array",
]);
export const bufferSourceTypes = new Set([
"ArrayBuffer",
"SharedArrayBuffer",
...arrayBufferViewTypes,
]);
export const integerTypes = new Set([
"byte",
"octet",
Expand Down Expand Up @@ -68,6 +70,7 @@ export const baseTypeConversionMap = new Map<string, string>([
["record", "Record"],
["FrozenArray", "ReadonlyArray"],
["EventHandler", "EventHandler"],
["ArrayBufferLike", "ArrayBufferLike"],
]);

export function deepFilter<T>(
Expand Down
Loading