Skip to content

Commit 4b4f0b7

Browse files
authored
"Polyfill" AllowSharedBufferSource (#179)
We need to use our own definition for `AllowSharedBufferSource` (here called `GPUAllowSharedBufferSource`) because the built-in one in TypeScript is broken. Fixes #178
1 parent f8d7680 commit 4b4f0b7

16 files changed

Lines changed: 58 additions & 26 deletions

File tree

dist/index.d.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,24 @@
66
// Manually-written
77
// *********************************************************************************************
88

9+
// AllowSharedBufferSource wasn't introduced until TypeScript 5.2.
10+
// But it also didn't include SharedArrayBuffer in the union.
11+
// This broke in ES2024 when ArrayBuffer gained some properties that SharedArrayBuffer didn't.
12+
// So, we use our own definition for AllowSharedBufferSource.
13+
14+
type GPUAllowSharedBufferSource =
15+
16+
| BufferSource
17+
| SharedArrayBuffer;
18+
19+
// Stronger typing for getContext()
20+
921
interface HTMLCanvasElement {
1022
getContext(
1123
contextId:
1224
| "webgpu"
1325
): GPUCanvasContext | null;
1426
}
15-
1627
interface OffscreenCanvas {
1728
getContext(
1829
contextId:
@@ -21,33 +32,34 @@ interface OffscreenCanvas {
2132
}
2233

2334
// Defined as an empty interface here to prevent errors when using these types in a worker.
35+
2436
interface HTMLVideoElement {}
2537

38+
// Strict types defined to help developers catch a common class of errors.
39+
// This interface defines depth as an undefined, which will cause a type check failure if someone
40+
// attempts to set depth rather than depthOrArrayLayers on a GPUExtent3D (an easy mistake to make.)
41+
2642
type GPUOrigin2DStrict =
2743

2844
| Iterable<GPUIntegerCoordinate>
2945
| GPUOrigin2DDictStrict;
30-
3146
interface GPUOrigin2DDictStrict
3247
extends GPUOrigin2DDict {
3348
/** @deprecated Does not exist for GPUOrigin2D. */
3449
z?: undefined;
3550
}
36-
3751
type GPUExtent3DStrict =
3852

3953
| Iterable<GPUIntegerCoordinate>
4054
| GPUExtent3DDictStrict;
41-
42-
// GPUExtent3DDictStrict is defined to help developers catch a common class of errors.
43-
// This interface defines depth as an undefined, which will cause a type check failure if someone
44-
// attempts to set depth rather than depthOrArrayLayers on a GPUExtent3D (an easy mistake to make.)
4555
interface GPUExtent3DDictStrict
4656
extends GPUExtent3DDict {
4757
/** @deprecated The correct name is `depthOrArrayLayers`. */
4858
depth?: undefined;
4959
}
5060

61+
// Stronger typing for event listeners.
62+
5163
/** @internal */
5264
interface __GPUDeviceEventMap {
5365
uncapturederror: GPUUncapturedErrorEvent;
@@ -2865,7 +2877,7 @@ interface GPUQueue
28652877
writeBuffer(
28662878
buffer: GPUBuffer,
28672879
bufferOffset: GPUSize64,
2868-
data: AllowSharedBufferSource,
2880+
data: GPUAllowSharedBufferSource,
28692881
dataOffset?: GPUSize64,
28702882
size?: GPUSize64
28712883
): undefined;
@@ -2878,7 +2890,7 @@ interface GPUQueue
28782890
*/
28792891
writeTexture(
28802892
destination: GPUTexelCopyTextureInfo,
2881-
data: AllowSharedBufferSource,
2893+
data: GPUAllowSharedBufferSource,
28822894
dataLayout: GPUTexelCopyBufferLayout,
28832895
size: GPUExtent3DStrict
28842896
): undefined;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import fs from 'fs';
33
async function main() {
44
let ts = fs.readFileSync('generated/index.d.ts', { encoding: 'utf-8' });
55
ts = ts
6+
// Replace broken AllowSharedBufferSource with GPUAllowSharedBufferSource
7+
.replace(/AllowSharedBufferSource/g, 'GPUAllowSharedBufferSource')
8+
69
// convert [[#anchor]] to {@link spec_url}
710
// convert [[#anchor|text]] to {@link spec_url|text}
811
.replace(/([^#])\[\[([^\[].*?)\]\]/g, '$1{@link https://www.w3.org/TR/webgpu/$2}')

generated/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2494,7 +2494,7 @@ interface GPUQueue
24942494
writeBuffer(
24952495
buffer: GPUBuffer,
24962496
bufferOffset: GPUSize64,
2497-
data: AllowSharedBufferSource,
2497+
data: GPUAllowSharedBufferSource,
24982498
dataOffset?: GPUSize64,
24992499
size?: GPUSize64
25002500
): undefined;
@@ -2507,7 +2507,7 @@ interface GPUQueue
25072507
*/
25082508
writeTexture(
25092509
destination: GPUTexelCopyTextureInfo,
2510-
data: AllowSharedBufferSource,
2510+
data: GPUAllowSharedBufferSource,
25112511
dataLayout: GPUTexelCopyBufferLayout,
25122512
size: GPUExtent3D
25132513
): undefined;

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
"dist/**/*"
1212
],
1313
"scripts": {
14-
"test": "for t in tests/*/ ; do (cd \"$t\" && npm i && npm test); done",
14+
"test": "for t in tests/*/ ; do echo \"\\n======== $t ========\" ; (cd \"$t\" && npm i && npm test); done",
1515
"build-docs": "cd tsdoc-src && npm ci && npm run build",
16-
"generate": "bikeshed-to-ts --in ./gpuweb/spec/index.bs --out ./generated/index.d.ts --forceGlobal --nominal && node fix-generated-comments.mjs && prettier -w generated/index.d.ts",
16+
"generate": "bikeshed-to-ts --in ./gpuweb/spec/index.bs --out ./generated/index.d.ts --forceGlobal --nominal && node fix-generated.mjs && prettier -w generated/index.d.ts",
1717
"format": "prettier -w dist/index.d.ts"
1818
},
1919
"devDependencies": {

tests/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function test_writeBuffer(q: GPUQueue, b: GPUBuffer, sab: SharedArrayBuffer) {
2+
q.writeBuffer(b, 0, sab);
3+
}

tests/ts4.6/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/ts4.6/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"extends": "../tsconfig.json",
3-
"include": ["index.ts"]
3+
"include": ["../index.ts"]
44
}

tests/ts4.7/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/ts4.7/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"extends": "../tsconfig.json",
3-
"include": ["index.ts"]
3+
"include": ["../index.ts"]
44
}

tests/ts4.9/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)